Question-and-Answer Resource for the Building Energy Modeling Community
Get started with the Help page
Ask Your Question

Revision history [back]

As requested by @Luis Lara: I have a residential multifamily building whose spaces are named as A01_Living, A01_Bed1, B01_Bed2, B01_TechLoc, C01_Living, C01_WC etc. The building has more than 350 spaces and I tried to gather all spaces of an appartment into an unit such as A01 (which contents living,bedroom, bathroom,etc), B01, C01, etc. Below is the openstudio measure for this task (I found that the openstudio measure work faster than user_script for OpenStudio-Sketchup plugin)

class AssignBuildingUnitToSpaces < OpenStudio::Measure::ModelMeasure 

 def name
  return 'Assign building unit to spaces based on spaces name' 
 end

 def description
  return 'description for user'
 end

 def modeler_description
  return 'description for modeler'
 end

 def arguments(model)
  args = OpenStudio::Measure::OSArgumentVector.new
  return args
 end

 def run(model, runner, user_arguments)
  super(model, runner, user_arguments)
   if !runner.validateUserArguments(arguments(model), user_arguments)
    return false
   end       
   spaces = model.getSpaces    
   spaces.each do |space|
    space_name = [] 
    if space.name.to_s.include?('_') 
     space_name = space.name.to_s.split('_') 
    else
     space_name[0] = space.name.to_s
    end        
    buildingunits = model.getBuildingUnits
    buildingunit_names = []
    buildingunits.each do |buildingunit|
     buildingunit_names << buildingunit.name.to_s
    end
    if ["A","B","C","D","M"].any? {|prefix| space_name[0].include?(prefix)}
     if (space.buildingUnit.empty?) || (!space.buildingUnit.get.name.to_s.include?(space_name[0]))
      unless buildingunit_names.include?(space_name[0]) 
        unit = OpenStudio::Model::BuildingUnit.new(model) 
        unit.setBuildingUnitType("Residential")
        unit.setName(space_name[0])
        space.setBuildingUnit(unit)
        runner.registerInfo("#{unit.name.to_s} has been assigned to space #{space.name.to_s}")
      else
       buildingunits.each do |buildingunit|
        if buildingunit.name.to_s.include?(space_name[0])
         space.setBuildingUnit(buildingunit)
         # space.setBuildingUnit(unit)
         runner.registerInfo("#{buildingunit.name.to_s} has been assigned to space #{space.name.to_s}")
        end
       end
      end          
    end
  end
end
return true
end
end
AssignBuildingUnitToSpaces.new.registerWithApplication