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

Assign building unit to spaces? [closed]

asked 2019-07-05 05:31:16 -0600

updated 2019-07-08 09:32:16 -0600

Hello, Could some one can tell me how can I assign the building unit to the spaces using ruby scripts. I tried to do something as follows, but it doesn't work

class AssignBuildingUnitToSpaces < OpenStudio::Ruleset::ModelUserScript

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

  def arguments(model)
    args = OpenStudio::Ruleset::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

    unit = OpenStudio::Model::BuildingUnit.new(model)
    unit.setBuildingUnitType("Residential")
    spaces.each do |space|
      space_name  = [] # space name as a list
      if space.name.to_s.include? '_'
          space_name = space.name.to_s.split('_') # split space name
      else
          space_name[0] = space.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])
            unit.setName(space_name[0])
            space.setBuildingUnit(unit)            
        end          
      end      
    end

    return true
  end
end

AssignBuildingUnitToSpaces.new.registerWithApplication

I am a very newbie in using OpenStudio and Openstudio SDK.

Thanks

Long

edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by vlle
close date 2019-10-04 03:23:36.112699

Comments

2

For starters, you never define the spaces object. Do you have more code that you could include?

Luis Lara's avatar Luis Lara  ( 2019-07-05 13:42:03 -0600 )edit

Hi Luis, please see my updated question above. Thanks

vlle's avatar vlle  ( 2019-07-06 03:09:10 -0600 )edit
1

What do you mean when you say "it doesn't work"? None of the spaces are assigned to the unit? Are you sure the line space.setBuildingUnit(unit) is ever reached?

shorowit's avatar shorowit  ( 2019-07-08 09:38:35 -0600 )edit

Hi shorowit, you are right. There was a problem of the indentation in the code. Thanks a lot.

vlle's avatar vlle  ( 2019-07-08 23:40:37 -0600 )edit

Could you provide your working code as an answer? It will help the community in case someone has a similar question.

Luis Lara's avatar Luis Lara  ( 2019-07-12 09:24:13 -0600 )edit

1 Answer

Sort by ยป oldest newest most voted
1

answered 2019-07-15 02:14:29 -0600

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
edit flag offensive delete link more

Training Workshops

Careers

Question Tools

2 followers

Stats

Asked: 2019-07-05 05:13:52 -0600

Seen: 352 times

Last updated: Jul 15 '19