Sorry, this content is no longer available
3

OpenStudio Store plant loops in an array-like object

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?

Julien Marrec's avatar
29.7k
Julien Marrec
asked 2015-04-08 06:20:27 -0500
__AmirRoth__'s avatar
4.4k
__AmirRoth__
updated 2017-08-05 13:21:32 -0500
edit flag offensive 0 remove flag close merge delete

Comments

add a comment see more comments

2 Answers

4

@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.
Kyle Benne's avatar
6k
Kyle Benne
answered 2015-04-14 11:54:38 -0500
edit flag offensive 0 remove flag delete link

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
add a comment see more comments
2

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
Julien Marrec's avatar
29.7k
Julien Marrec
answered 2015-04-08 06:25:51 -0500, updated 2015-04-08 08:21:47 -0500
edit flag offensive 0 remove flag delete link

Comments

add a comment see more comments