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

OpenStudio Store plant loops in an array-like object

asked 2015-04-08 06:20:27 -0500

updated 2017-08-05 13:21:32 -0500

I want to loop on all my plant loops, and if the plant loop meets a certain criteria, I want to add it to an array-like object.

Later, I want to be able to create another loop to retrieve each plant loop objects.

I've tried the following:

plant_loops = model.getPlantLoops
plant_handles = OpenStudio::StringVector.new

#Initial loop to store air loops
plant_loops.each do |plant_loop|
  #Criteria...
  if ....
    plant_handles << plant_loop.handle.to_s
  end #end if
end #end do

# Loop to retrieve air loop objects
plant_handles do |plant_handle|
   #Following line is where it throws an error
   if not plant_handle.get.to_PlantLoop.empty?
     plant_loop = plant_handle.get.to_PlantLoop.get
   end #end if
end #end do

I'm getting an error for plant_handle.get. If I do plant_handle.to_PlantLoop directly I'll get the same error anyways.

undefined method 'get' for "{979a795...95f}":String (NoMethodError).

I completely understand that a string doesn't have a 'get' method, but I don't know how to return an object from its handle.

So, how can I store plant loop objects during a loop in order to reuse them in another loop?

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
4

answered 2015-04-14 11:54:38 -0500

@Julien Marrec "I'm still interested in knowing how one can return the object from its handle"

If I am interpreting your question correctly here is what you need to know. Basically you need the Model::getObject(Handle) method. Here are the details.

m = OpenStudio::Model::Model.new
plant = OpenStudio::Model::PlantLoop.new m
h = plant.handle
optional_model_object = m.getObject h
# optional_model_object will be a generic (optional) ModelObject
# if you want to know what specific type it is consider optional_model_object.get.iddObject.name
# You can cast back to plant like this
plant = optional_model_object.get.to_PlantLoop.get
# Watch out, either of the previous calls to "get" could crash the program if your call to getObject failed or the cast to PlantLoop failed.
edit flag offensive delete link more

Comments

Thanks, that is what I meant. This is a ruby question more than anything, but can we use the begin... rescue statement in ruby like Python's try: ... except:... statement in order to avoid crashes then?

Julien Marrec's avatar Julien Marrec  ( 2015-04-14 17:18:42 -0500 )edit
2

answered 2015-04-08 06:25:51 -0500

updated 2015-04-08 08:21:47 -0500

This is my bad. I had tried with an array doing plant_loops_tokeep = [] and then plant_loops_tokeep += plant_loop but it didn't work. This is a very basic Ruby mistake.

I'm still interested in knowing how one can return the object from its handle (especially without knowing the type of object it is).

Here's code that works:

plant_loops = model.getPlantLoops
plant_loops_tokeep =[]

#Initial loop to store air loops
plant_loops.each do |plant_loop|
  #Criteria...
  if ....
    # Correct way to assign to an array
    plant_loops_tokeep << plant_loop
  end #end if
end #end do

# Loop to retrieve air loop objects
plant_loops_tokeep.each do |plant_loop|
   #Do stuff with the plant_loop, such as returning its name
   runner.registerInfo("Plant loop name: #{plant_loop.name}")
end #end do
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Careers

Question Tools

1 follower

Stats

Asked: 2015-04-08 06:20:27 -0500

Seen: 121 times

Last updated: Apr 14 '15