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

How can I load an OpenStudio component (osc) into my current model?

asked 2016-02-24 09:04:55 -0500

The title pretty much says it all: I want to load an OpenStudio component (.osc) into my current model, whether it's using the App or the API.

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
3

answered 2016-02-24 09:47:08 -0500

updated 2016-02-24 12:25:03 -0500

The OpenStudio GUI supports only supports adding specific component types (Materials, Constructions, and Ductless Heat Pumps). This is done via the "Components and Measures / Find Components" menu item.

The API can access any kind of component on the BCL. This is demonstrated in the Get BCL Weather File user script in the sketchup plugin.

I have a few unpublished measures that exercise the api (One to get site components, another to get construction). I'll see if I can get one of these posted on BCL as en example The trick to using a measure that accesses BCL components is planning it in a way so that the search results that populate the choice list are not too long. The user script can iterate (e.g. first pick city, then pick from sub-set of weather files for that city). A measure has to do it in one pass.

Here is a short snippet if you already know the UID of the component you want.

# download from BCL
remote = OpenStudio::RemoteBCL.new
remote.downloadComponent(uid)
component = remote.waitForComponentDownload()
if component.empty?
  runner.registerError("Cannot find local component")
  return false
end
component = component.get

# get osc file
files = component.files("osc")
if files.empty?
  runner.registerError("No file found")
  return false
end
construction_path = component.files("osc")[0]
construction_file = OpenStudio::IdfFile::load(construction_path)
vt = OpenStudio::OSVersion::VersionTranslator.new
constructionComponent = vt.loadComponent(OpenStudio::Path.new(construction_path))

# get model object
if constructionComponent.empty?
  runner.registerError("translateSurfaceConstruction: Cannot load construction component '#{construction_file}'")
  return false
else
  object = constructionComponent.get.primaryObject
  if object.to_Construction.empty?
    runner.registerError("translateSurfaceConstruction: Construction component '#{construction_file}' does not include a construction object")
    return false
  else
    componentData = model.insertComponent(constructionComponent.get)
    if componentData.empty?
      runner.registerError("translateSurfaceConstruction: Failed to insert construction component '#{construction_file}' into model")
      return false
    else
      new_construction = componentData.get.primaryComponentObject.to_Construction.get
    end
  end
end

Here is the entire run section of a measure that creates model objects from all the OSC files from a "resources" directory within the measure.

# define what happens when the measure is run
def run(model, runner, user_arguments)
super(model, runner, user_arguments)

# use the built-in error checking
if !runner.validateUserArguments(arguments(model), user_arguments)
  return false
end

# report initial condition of model
runner.registerInitialCondition("The building started with #{model.numObjects} objects.")

# get all objects in directory
files = Dir.entries("#{File.dirname(__FILE__)}/resources/")
files.each do |new_object|
  next if not new_object.include?(".osc")
  runner.registerInfo("importing component from #{new_object}")

  #load the osc file
  new_object_path = OpenStudio::Path.new("#{File.dirname(__FILE__)}/resources/#{new_object}")
  new_object_file = OpenStudio::IdfFile::load(new_object_path)

  if new_object_file.empty?
    runner.registerError("Unable to find the file #{new_object}.osc")
    return false
  else
    new_object_file = new_object_file.get
  end

  vt = OpenStudio::OSVersion::VersionTranslator.new
  new_objectComponent = vt.loadComponent(OpenStudio::Path.new(new_object_path))
  if new_objectComponent.empty?
    runner.registerError("Cannot load new_object component '#{new_object_file}'")
    return false
  else
    object = new_objectComponent.get.primaryObject
    componentData = model.insertComponent(new_objectComponent.get)
    if componentData.empty?
      runner.registerError("Failed to insert new_object component '#{new_object_file}' into model")
      return false
    else
      new_new_object = componentData.get.primaryComponentObject
    end
  end

end

# report final condition of model ...
(more)
edit flag offensive delete link more

Comments

my bad for not being clear. I have my own .OSC (a chiller with all its curves). So far what's I've been doing is to do versiontranslator.loadComponent, find the model object that is the chiller and clone that to the model. It works, but I was wondering if there's a better way

Julien Marrec's avatar Julien Marrec  ( 2016-02-24 12:06:46 -0500 )edit

Ah, I have a measure for that too. One to make local components, and another to add them. See updated answer.

David Goldwasser's avatar David Goldwasser  ( 2016-02-24 12:23:13 -0500 )edit

Nice, I didn't know about model.insertComponent(component) :)

Julien Marrec's avatar Julien Marrec  ( 2016-02-25 02:32:18 -0500 )edit

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: 2016-02-24 09:04:55 -0500

Seen: 731 times

Last updated: Feb 24 '16