Skip to content

Commit

Permalink
moved parameter functions to ghcomponent_helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
obucklin committed Feb 16, 2024
1 parent 166c601 commit dc9c070
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,27 @@
from compas_timber.ghpython import manage_dynamic_params



class CategoryJointRule(component):
def RunScript(self, joint_options, *args):
names = [name + " category" for name in joint_options.beam_names]
if joint_options:
names = [name + " category" for name in joint_options.beam_names]
else:
names = None

manage_dynamic_params(names, ghenv)

manage_dynamic_params(names)
if not args or not names: # check that dynamic params generated
return

if len(args) != len(names): # check that dynamic params are correct
if len(args) != len(names): # check that dynamic params generated correctly
self.AddRuntimeMessage(Error, "Input parameter error.")
return

create_rule = True
for i in range(len(names)):
if not args[i]:
self.AddRuntimeMessage(
Warning,
"Input parameter {} {} failed to collect data.".format(names[i], "categories"),
Warning, "Input parameter {} {} failed to collect data.".format(names[i], "categories")
)
create_rule = False
if create_rule:
Expand Down
20 changes: 14 additions & 6 deletions src/compas_timber/ghpython/components/CT_Joint_Rule_Direct/code.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,35 @@

class DirectJointRule(component):
def RunScript(self, joint_options, *args):
names = None
if joint_options:
names = [name + " category" for name in joint_options.beam_names]

manage_dynamic_params(joint_options.beam_names)
manage_dynamic_params(names, ghenv)

if len(args) != len(joint_options.beam_names): # check that dynamic params are correct
if not args or not names: # check that dynamic params generated
return

if len(args) != len(names): # check that dynamic params generated correctly
self.AddRuntimeMessage(Error, "Input parameter error.")
return

beams = []
create_rule = True
for i in range(len(joint_options.beam_names)):
if not args[i]:
if not args[i]: # test that input recieved data
self.AddRuntimeMessage(
Warning, "Input parameter {} failed to collect data.".format(joint_options.beam_names[i])
)
create_rule = False
else:
beam_args = []
if not isinstance(args[i], list):
args[i] = [args[i]]
beams.append(args[i])
beam_args = [args[i]]
beams.append(beam_args)

if create_rule:
if len(beams[0]) != len(beams[1]):
if len(beams[0]) != len(beams[1]): # test that beam list lengths match
self.AddRuntimeMessage(
Error,
"Number of items in {} and {} must match!".format(
Expand Down
20 changes: 11 additions & 9 deletions src/compas_timber/ghpython/ghcomponent_helpers.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from Grasshopper.Kernel.GH_RuntimeMessageLevel import Remark
from Grasshopper.Kernel.GH_RuntimeMessageLevel import Warning
import Grasshopper


def list_input_valid(component, Param, name):
Expand All @@ -24,7 +25,7 @@ def item_input_valid(component, Param, name):
return False


def add_GH_param(name, io):
def add_GH_param(name, io, ghenv):
assert io in ("Output", "Input")
params = [param.NickName for param in getattr(ghenv.Component.Params, io)]
if name not in params:
Expand All @@ -40,17 +41,18 @@ def add_GH_param(name, io):
ghenv.Component.Params.OnParametersChanged()


def clear_GH_params(permanent_param_count = 1):
for param in ghenv.Component.Params.Input:
if param.Index >= permanent_param_count:
ghenv.Component.Params.UnregisterInputParameter(param)
def clear_GH_params(ghenv, permanent_param_count=1):
while len(ghenv.Component.Params.Input) > permanent_param_count:
ghenv.Component.Params.UnregisterInputParameter(
ghenv.Component.Params.Input[len(ghenv.Component.Params.Input) - 1]
)
ghenv.Component.Params.OnParametersChanged()
ghenv.Component.ExpireSolution(False)


def manage_dynamic_params(input_names, permanent_param_count = 1):
def manage_dynamic_params(input_names, ghenv, permanent_param_count=1):
if not input_names: # if no joint_options is input
clear_GH_params(permanent_param_count)
clear_GH_params(ghenv, permanent_param_count)
return
register_params = False
if len(ghenv.Component.Params.Input) == len(input_names) + permanent_param_count:
Expand All @@ -61,6 +63,6 @@ def manage_dynamic_params(input_names, permanent_param_count = 1):
else:
register_params = True
if register_params:
clear_GH_params(permanent_param_count)
clear_GH_params(ghenv, permanent_param_count)
for name in input_names:
add_GH_param(name, "Input")
add_GH_param(name, "Input", ghenv)
2 changes: 1 addition & 1 deletion src/compas_timber/ghpython/workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ class JointOptions(object):
"""

def __init__(self, type, beam_names = [], **kwargs):
def __init__(self, type, beam_names=[], **kwargs):
self.type = type
self.kwargs = kwargs
self.beam_names = beam_names
Expand Down

0 comments on commit dc9c070

Please sign in to comment.