Skip to content

Constructions Materials

muh182 edited this page Nov 14, 2014 · 3 revisions

Clone a model or subset of a model objects

Clone is feature that enables user to clone part of a model from an existing model into a new model or make a duplicate for the same model. This is a very useful method especially for the constructions/materials. For example, if you would like to clone constructions from an input model to an output model, you need to follow this instruction

# initialize the input model
inputModel = OpenStudio::Model::Model.new

# import the model and replace it the initialized model
translator = OpenStudio::OSVersion::VersionTranslator.new
	
# define the path 
# You need to have "test.osm" in your current directory
path = OpenStudio::Path.new(File.dirname(__FILE__) + "/test.osm")
		
inputModel = translator.loadModel(path)
inputModel = inputModel.get

# initialize the output model
outputModel=OpenStudio::Model::Model.new

# get the construction for the model 
constructions=inputModel.getConstructions

# clone the constructions from the old model to the new one 
# enable cloning the materials 
newConstruction=constructions.clone(outputModel)

Notes

1. Clone inside of a loop

If you need to clone within a loop, ruby does not recognize the clone outside of the loop scope. For example, the last puts in this code does not recognize 'newConstruction'. Therefore, you will get an error message

constructions.each do |construction|
  newConstruction=construction.clone(outputModel)
end
puts "#{newConstruction}"  # when you call it outside of the loop, it does not work

One solution is to define the variable outside of the loop; then, model will recognize it:

newConstruction=nil
modifiedConstructions=[]
constructions.each do |construction|
   newConstruction=construction.clone(outputModel)
end

puts "#{newConstruction}"  # right now, it works