Skip to content

Commit

Permalink
Merge pull request #4998 from NREL/4997-Python_workspace_cast
Browse files Browse the repository at this point in the history
Fix #4997 - Add Workspace casting methods in python bindings
  • Loading branch information
jmarrec committed Oct 16, 2023
2 parents 6d120d4 + c4768bc commit 186456d
Showing 1 changed file with 43 additions and 2 deletions.
45 changes: 43 additions & 2 deletions src/model/ModelCore.i
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
%init %{
rb_eval_string("OpenStudio::Workspace.class_eval { define_method(:to_Model) { OpenStudio::Model::toModel(self); } }");
rb_eval_string("OpenStudio::Workspace.class_eval { define_method(:to_OptionalModel) { OpenStudio::Model::toOptionalModel(self); } }");
rb_eval_string("OpenStudio::Workspace.class_eval { define_method(:to_Component) { OpenStudio::Component::toComponent(self); } }");
rb_eval_string("OpenStudio::Workspace.class_eval { define_method(:to_OptionalComponent) { OpenStudio::Component::toOptionalComponent(self); } }");
rb_eval_string("OpenStudio::Workspace.class_eval { define_method(:to_Component) { OpenStudio::Model::toComponent(self); } }");
rb_eval_string("OpenStudio::Workspace.class_eval { define_method(:to_OptionalComponent) { OpenStudio::Model::toOptionalComponent(self); } }");
%}

#elif defined SWIGCSHARP
Expand Down Expand Up @@ -144,6 +144,47 @@
// Model = openstudiomodelcore.Model
// %}

// At this point, 'Model' and co aren't declared yet, so forward declare type hints
%pythoncode %{
def _workspace_to_Model(self) -> "Model":
"""Cast the Workspace to a Model.
Throws if not an actual Model
:return: A Model.
"""
return toModel(self)
openstudioutilitiesidf.Workspace.to_Model = _workspace_to_Model

def _workspace_to_OptionalModel(self) -> "OptionalModel":
"""Try to cast the Workspace to a Model.
:return: An OptionalModel.
"""
return toOptionalModel(self)
openstudioutilitiesidf.Workspace.to_OptionalModel = _workspace_to_OptionalModel



def _workspace_to_Component(self) -> "Component":
"""Cast the Workspace to a Component.
Throws if not an actual Component
:return: A Component.
"""
return toComponent(self)
openstudioutilitiesidf.Workspace.to_Component = _workspace_to_Component

def _workspace_to_OptionalComponent(self) -> "OptionalComponent":
"""Try to cast the Workspace to a Component.
:return: An OptionalComponent.
"""
return toOptionalComponent(self)
openstudioutilitiesidf.Workspace.to_OptionalComponent = _workspace_to_OptionalComponent
%}

#else

#endif
Expand Down

0 comments on commit 186456d

Please sign in to comment.