Assign building unit to spaces? [closed]    
   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

 
 
 

For starters, you never define the
spacesobject. Do you have more code that you could include?Hi Luis, please see my updated question above. Thanks
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?Hi shorowit, you are right. There was a problem of the indentation in the code. Thanks a lot.
Could you provide your working code as an answer? It will help the community in case someone has a similar question.