我们在之前已经介绍过了如何创建自己的第一个电池,其中里面提到了我们制作的大部分电池都需要至少完成下面三个部分的代码

  • RegisterInputParams
  • RegisterOutputParams
  • SolveInstance

其中 RegisterInputParamsRegisterOutputParams 是用来声明电池的输入和输出的两个部分,重要程度不言而喻,本文我们就来看看他们俩到底是什么。


进一步认识RegisterInputParam和RegisterOutputParams

Params是Parameters的简写,即“参数”,这两个函数的名字也十分的直观 “Register Input Parameters” 和 “Register Output Parameters”,就是我们需要把电池需要处理的输入和输出参数进行注册。

不过凡是都有个为什么,我们首先关注的问题是“为什么需要多出来这两个函数?”,直接用 SolveInstance 接收传递参数不是更直观吗?要回答这个问题,我们需要来看看Grasshopper电池是背后的架构是什么样的。前面已经提到过,我们所有的自定义电池都是继承自 GH_Component ,而下图展示了一个它的类图。

#mermaid-svg-wH3Kjsluagbj2fp2 .label{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);fill:#333;color:#333}#mermaid-svg-wH3Kjsluagbj2fp2 .label text{fill:#333}#mermaid-svg-wH3Kjsluagbj2fp2 .node rect,#mermaid-svg-wH3Kjsluagbj2fp2 .node circle,#mermaid-svg-wH3Kjsluagbj2fp2 .node ellipse,#mermaid-svg-wH3Kjsluagbj2fp2 .node polygon,#mermaid-svg-wH3Kjsluagbj2fp2 .node path{fill:#ECECFF;stroke:#9370db;stroke-width:1px}#mermaid-svg-wH3Kjsluagbj2fp2 .node .label{text-align:center;fill:#333}#mermaid-svg-wH3Kjsluagbj2fp2 .node.clickable{cursor:pointer}#mermaid-svg-wH3Kjsluagbj2fp2 .arrowheadPath{fill:#333}#mermaid-svg-wH3Kjsluagbj2fp2 .edgePath .path{stroke:#333;stroke-width:1.5px}#mermaid-svg-wH3Kjsluagbj2fp2 .flowchart-link{stroke:#333;fill:none}#mermaid-svg-wH3Kjsluagbj2fp2 .edgeLabel{background-color:#e8e8e8;text-align:center}#mermaid-svg-wH3Kjsluagbj2fp2 .edgeLabel rect{opacity:0.9}#mermaid-svg-wH3Kjsluagbj2fp2 .edgeLabel span{color:#333}#mermaid-svg-wH3Kjsluagbj2fp2 .cluster rect{fill:#ffffde;stroke:#aa3;stroke-width:1px}#mermaid-svg-wH3Kjsluagbj2fp2 .cluster text{fill:#333}#mermaid-svg-wH3Kjsluagbj2fp2 div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);font-size:12px;background:#ffffde;border:1px solid #aa3;border-radius:2px;pointer-events:none;z-index:100}#mermaid-svg-wH3Kjsluagbj2fp2 .actor{stroke:#ccf;fill:#ECECFF}#mermaid-svg-wH3Kjsluagbj2fp2 text.actor>tspan{fill:#000;stroke:none}#mermaid-svg-wH3Kjsluagbj2fp2 .actor-line{stroke:grey}#mermaid-svg-wH3Kjsluagbj2fp2 .messageLine0{stroke-width:1.5;stroke-dasharray:none;stroke:#333}#mermaid-svg-wH3Kjsluagbj2fp2 .messageLine1{stroke-width:1.5;stroke-dasharray:2, 2;stroke:#333}#mermaid-svg-wH3Kjsluagbj2fp2 #arrowhead path{fill:#333;stroke:#333}#mermaid-svg-wH3Kjsluagbj2fp2 .sequenceNumber{fill:#fff}#mermaid-svg-wH3Kjsluagbj2fp2 #sequencenumber{fill:#333}#mermaid-svg-wH3Kjsluagbj2fp2 #crosshead path{fill:#333;stroke:#333}#mermaid-svg-wH3Kjsluagbj2fp2 .messageText{fill:#333;stroke:#333}#mermaid-svg-wH3Kjsluagbj2fp2 .labelBox{stroke:#ccf;fill:#ECECFF}#mermaid-svg-wH3Kjsluagbj2fp2 .labelText,#mermaid-svg-wH3Kjsluagbj2fp2 .labelText>tspan{fill:#000;stroke:none}#mermaid-svg-wH3Kjsluagbj2fp2 .loopText,#mermaid-svg-wH3Kjsluagbj2fp2 .loopText>tspan{fill:#000;stroke:none}#mermaid-svg-wH3Kjsluagbj2fp2 .loopLine{stroke-width:2px;stroke-dasharray:2, 2;stroke:#ccf;fill:#ccf}#mermaid-svg-wH3Kjsluagbj2fp2 .note{stroke:#aa3;fill:#fff5ad}#mermaid-svg-wH3Kjsluagbj2fp2 .noteText,#mermaid-svg-wH3Kjsluagbj2fp2 .noteText>tspan{fill:#000;stroke:none}#mermaid-svg-wH3Kjsluagbj2fp2 .activation0{fill:#f4f4f4;stroke:#666}#mermaid-svg-wH3Kjsluagbj2fp2 .activation1{fill:#f4f4f4;stroke:#666}#mermaid-svg-wH3Kjsluagbj2fp2 .activation2{fill:#f4f4f4;stroke:#666}#mermaid-svg-wH3Kjsluagbj2fp2 .mermaid-main-font{font-family:"trebuchet ms", verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-wH3Kjsluagbj2fp2 .section{stroke:none;opacity:0.2}#mermaid-svg-wH3Kjsluagbj2fp2 .section0{fill:rgba(102,102,255,0.49)}#mermaid-svg-wH3Kjsluagbj2fp2 .section2{fill:#fff400}#mermaid-svg-wH3Kjsluagbj2fp2 .section1,#mermaid-svg-wH3Kjsluagbj2fp2 .section3{fill:#fff;opacity:0.2}#mermaid-svg-wH3Kjsluagbj2fp2 .sectionTitle0{fill:#333}#mermaid-svg-wH3Kjsluagbj2fp2 .sectionTitle1{fill:#333}#mermaid-svg-wH3Kjsluagbj2fp2 .sectionTitle2{fill:#333}#mermaid-svg-wH3Kjsluagbj2fp2 .sectionTitle3{fill:#333}#mermaid-svg-wH3Kjsluagbj2fp2 .sectionTitle{text-anchor:start;font-size:11px;text-height:14px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-wH3Kjsluagbj2fp2 .grid .tick{stroke:#d3d3d3;opacity:0.8;shape-rendering:crispEdges}#mermaid-svg-wH3Kjsluagbj2fp2 .grid .tick text{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-wH3Kjsluagbj2fp2 .grid path{stroke-width:0}#mermaid-svg-wH3Kjsluagbj2fp2 .today{fill:none;stroke:red;stroke-width:2px}#mermaid-svg-wH3Kjsluagbj2fp2 .task{stroke-width:2}#mermaid-svg-wH3Kjsluagbj2fp2 .taskText{text-anchor:middle;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-wH3Kjsluagbj2fp2 .taskText:not([font-size]){font-size:11px}#mermaid-svg-wH3Kjsluagbj2fp2 .taskTextOutsideRight{fill:#000;text-anchor:start;font-size:11px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-wH3Kjsluagbj2fp2 .taskTextOutsideLeft{fill:#000;text-anchor:end;font-size:11px}#mermaid-svg-wH3Kjsluagbj2fp2 .task.clickable{cursor:pointer}#mermaid-svg-wH3Kjsluagbj2fp2 .taskText.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-wH3Kjsluagbj2fp2 .taskTextOutsideLeft.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-wH3Kjsluagbj2fp2 .taskTextOutsideRight.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-wH3Kjsluagbj2fp2 .taskText0,#mermaid-svg-wH3Kjsluagbj2fp2 .taskText1,#mermaid-svg-wH3Kjsluagbj2fp2 .taskText2,#mermaid-svg-wH3Kjsluagbj2fp2 .taskText3{fill:#fff}#mermaid-svg-wH3Kjsluagbj2fp2 .task0,#mermaid-svg-wH3Kjsluagbj2fp2 .task1,#mermaid-svg-wH3Kjsluagbj2fp2 .task2,#mermaid-svg-wH3Kjsluagbj2fp2 .task3{fill:#8a90dd;stroke:#534fbc}#mermaid-svg-wH3Kjsluagbj2fp2 .taskTextOutside0,#mermaid-svg-wH3Kjsluagbj2fp2 .taskTextOutside2{fill:#000}#mermaid-svg-wH3Kjsluagbj2fp2 .taskTextOutside1,#mermaid-svg-wH3Kjsluagbj2fp2 .taskTextOutside3{fill:#000}#mermaid-svg-wH3Kjsluagbj2fp2 .active0,#mermaid-svg-wH3Kjsluagbj2fp2 .active1,#mermaid-svg-wH3Kjsluagbj2fp2 .active2,#mermaid-svg-wH3Kjsluagbj2fp2 .active3{fill:#bfc7ff;stroke:#534fbc}#mermaid-svg-wH3Kjsluagbj2fp2 .activeText0,#mermaid-svg-wH3Kjsluagbj2fp2 .activeText1,#mermaid-svg-wH3Kjsluagbj2fp2 .activeText2,#mermaid-svg-wH3Kjsluagbj2fp2 .activeText3{fill:#000 !important}#mermaid-svg-wH3Kjsluagbj2fp2 .done0,#mermaid-svg-wH3Kjsluagbj2fp2 .done1,#mermaid-svg-wH3Kjsluagbj2fp2 .done2,#mermaid-svg-wH3Kjsluagbj2fp2 .done3{stroke:grey;fill:#d3d3d3;stroke-width:2}#mermaid-svg-wH3Kjsluagbj2fp2 .doneText0,#mermaid-svg-wH3Kjsluagbj2fp2 .doneText1,#mermaid-svg-wH3Kjsluagbj2fp2 .doneText2,#mermaid-svg-wH3Kjsluagbj2fp2 .doneText3{fill:#000 !important}#mermaid-svg-wH3Kjsluagbj2fp2 .crit0,#mermaid-svg-wH3Kjsluagbj2fp2 .crit1,#mermaid-svg-wH3Kjsluagbj2fp2 .crit2,#mermaid-svg-wH3Kjsluagbj2fp2 .crit3{stroke:#f88;fill:red;stroke-width:2}#mermaid-svg-wH3Kjsluagbj2fp2 .activeCrit0,#mermaid-svg-wH3Kjsluagbj2fp2 .activeCrit1,#mermaid-svg-wH3Kjsluagbj2fp2 .activeCrit2,#mermaid-svg-wH3Kjsluagbj2fp2 .activeCrit3{stroke:#f88;fill:#bfc7ff;stroke-width:2}#mermaid-svg-wH3Kjsluagbj2fp2 .doneCrit0,#mermaid-svg-wH3Kjsluagbj2fp2 .doneCrit1,#mermaid-svg-wH3Kjsluagbj2fp2 .doneCrit2,#mermaid-svg-wH3Kjsluagbj2fp2 .doneCrit3{stroke:#f88;fill:#d3d3d3;stroke-width:2;cursor:pointer;shape-rendering:crispEdges}#mermaid-svg-wH3Kjsluagbj2fp2 .milestone{transform:rotate(45deg) scale(0.8, 0.8)}#mermaid-svg-wH3Kjsluagbj2fp2 .milestoneText{font-style:italic}#mermaid-svg-wH3Kjsluagbj2fp2 .doneCritText0,#mermaid-svg-wH3Kjsluagbj2fp2 .doneCritText1,#mermaid-svg-wH3Kjsluagbj2fp2 .doneCritText2,#mermaid-svg-wH3Kjsluagbj2fp2 .doneCritText3{fill:#000 !important}#mermaid-svg-wH3Kjsluagbj2fp2 .activeCritText0,#mermaid-svg-wH3Kjsluagbj2fp2 .activeCritText1,#mermaid-svg-wH3Kjsluagbj2fp2 .activeCritText2,#mermaid-svg-wH3Kjsluagbj2fp2 .activeCritText3{fill:#000 !important}#mermaid-svg-wH3Kjsluagbj2fp2 .titleText{text-anchor:middle;font-size:18px;fill:#000;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-wH3Kjsluagbj2fp2 g.classGroup text{fill:#9370db;stroke:none;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);font-size:10px}#mermaid-svg-wH3Kjsluagbj2fp2 g.classGroup text .title{font-weight:bolder}#mermaid-svg-wH3Kjsluagbj2fp2 g.clickable{cursor:pointer}#mermaid-svg-wH3Kjsluagbj2fp2 g.classGroup rect{fill:#ECECFF;stroke:#9370db}#mermaid-svg-wH3Kjsluagbj2fp2 g.classGroup line{stroke:#9370db;stroke-width:1}#mermaid-svg-wH3Kjsluagbj2fp2 .classLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.5}#mermaid-svg-wH3Kjsluagbj2fp2 .classLabel .label{fill:#9370db;font-size:10px}#mermaid-svg-wH3Kjsluagbj2fp2 .relation{stroke:#9370db;stroke-width:1;fill:none}#mermaid-svg-wH3Kjsluagbj2fp2 .dashed-line{stroke-dasharray:3}#mermaid-svg-wH3Kjsluagbj2fp2 #compositionStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-wH3Kjsluagbj2fp2 #compositionEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-wH3Kjsluagbj2fp2 #aggregationStart{fill:#ECECFF;stroke:#9370db;stroke-width:1}#mermaid-svg-wH3Kjsluagbj2fp2 #aggregationEnd{fill:#ECECFF;stroke:#9370db;stroke-width:1}#mermaid-svg-wH3Kjsluagbj2fp2 #dependencyStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-wH3Kjsluagbj2fp2 #dependencyEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-wH3Kjsluagbj2fp2 #extensionStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-wH3Kjsluagbj2fp2 #extensionEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-wH3Kjsluagbj2fp2 .commit-id,#mermaid-svg-wH3Kjsluagbj2fp2 .commit-msg,#mermaid-svg-wH3Kjsluagbj2fp2 .branch-label{fill:lightgrey;color:lightgrey;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-wH3Kjsluagbj2fp2 .pieTitleText{text-anchor:middle;font-size:25px;fill:#000;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-wH3Kjsluagbj2fp2 .slice{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-wH3Kjsluagbj2fp2 g.stateGroup text{fill:#9370db;stroke:none;font-size:10px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-wH3Kjsluagbj2fp2 g.stateGroup text{fill:#9370db;fill:#333;stroke:none;font-size:10px}#mermaid-svg-wH3Kjsluagbj2fp2 g.statediagram-cluster .cluster-label text{fill:#333}#mermaid-svg-wH3Kjsluagbj2fp2 g.stateGroup .state-title{font-weight:bolder;fill:#000}#mermaid-svg-wH3Kjsluagbj2fp2 g.stateGroup rect{fill:#ECECFF;stroke:#9370db}#mermaid-svg-wH3Kjsluagbj2fp2 g.stateGroup line{stroke:#9370db;stroke-width:1}#mermaid-svg-wH3Kjsluagbj2fp2 .transition{stroke:#9370db;stroke-width:1;fill:none}#mermaid-svg-wH3Kjsluagbj2fp2 .stateGroup .composit{fill:white;border-bottom:1px}#mermaid-svg-wH3Kjsluagbj2fp2 .stateGroup .alt-composit{fill:#e0e0e0;border-bottom:1px}#mermaid-svg-wH3Kjsluagbj2fp2 .state-note{stroke:#aa3;fill:#fff5ad}#mermaid-svg-wH3Kjsluagbj2fp2 .state-note text{fill:black;stroke:none;font-size:10px}#mermaid-svg-wH3Kjsluagbj2fp2 .stateLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.7}#mermaid-svg-wH3Kjsluagbj2fp2 .edgeLabel text{fill:#333}#mermaid-svg-wH3Kjsluagbj2fp2 .stateLabel text{fill:#000;font-size:10px;font-weight:bold;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-wH3Kjsluagbj2fp2 .node circle.state-start{fill:black;stroke:black}#mermaid-svg-wH3Kjsluagbj2fp2 .node circle.state-end{fill:black;stroke:white;stroke-width:1.5}#mermaid-svg-wH3Kjsluagbj2fp2 #statediagram-barbEnd{fill:#9370db}#mermaid-svg-wH3Kjsluagbj2fp2 .statediagram-cluster rect{fill:#ECECFF;stroke:#9370db;stroke-width:1px}#mermaid-svg-wH3Kjsluagbj2fp2 .statediagram-cluster rect.outer{rx:5px;ry:5px}#mermaid-svg-wH3Kjsluagbj2fp2 .statediagram-state .divider{stroke:#9370db}#mermaid-svg-wH3Kjsluagbj2fp2 .statediagram-state .title-state{rx:5px;ry:5px}#mermaid-svg-wH3Kjsluagbj2fp2 .statediagram-cluster.statediagram-cluster .inner{fill:white}#mermaid-svg-wH3Kjsluagbj2fp2 .statediagram-cluster.statediagram-cluster-alt .inner{fill:#e0e0e0}#mermaid-svg-wH3Kjsluagbj2fp2 .statediagram-cluster .inner{rx:0;ry:0}#mermaid-svg-wH3Kjsluagbj2fp2 .statediagram-state rect.basic{rx:5px;ry:5px}#mermaid-svg-wH3Kjsluagbj2fp2 .statediagram-state rect.divider{stroke-dasharray:10,10;fill:#efefef}#mermaid-svg-wH3Kjsluagbj2fp2 .note-edge{stroke-dasharray:5}#mermaid-svg-wH3Kjsluagbj2fp2 .statediagram-note rect{fill:#fff5ad;stroke:#aa3;stroke-width:1px;rx:0;ry:0}:root{--mermaid-font-family: '"trebuchet ms", verdana, arial';--mermaid-font-family: "Comic Sans MS", "Comic Sans", cursive}#mermaid-svg-wH3Kjsluagbj2fp2 .error-icon{fill:#522}#mermaid-svg-wH3Kjsluagbj2fp2 .error-text{fill:#522;stroke:#522}#mermaid-svg-wH3Kjsluagbj2fp2 .edge-thickness-normal{stroke-width:2px}#mermaid-svg-wH3Kjsluagbj2fp2 .edge-thickness-thick{stroke-width:3.5px}#mermaid-svg-wH3Kjsluagbj2fp2 .edge-pattern-solid{stroke-dasharray:0}#mermaid-svg-wH3Kjsluagbj2fp2 .edge-pattern-dashed{stroke-dasharray:3}#mermaid-svg-wH3Kjsluagbj2fp2 .edge-pattern-dotted{stroke-dasharray:2}#mermaid-svg-wH3Kjsluagbj2fp2 .marker{fill:#333}#mermaid-svg-wH3Kjsluagbj2fp2 .marker.cross{stroke:#333}:root { --mermaid-font-family: "trebuchet ms", verdana, arial;}#mermaid-svg-wH3Kjsluagbj2fp2 {color: rgba(0, 0, 0, 0.75);font: ;}GH_ActiveObjectGH_ComponentGH_ComponentParamServer Paramsint RunCountRegisterInputParams()RegisterOutputParams()SolveInstance()

类图的每一个框代表着一个类,框之间的箭头代表着继承和派生关系,框里面被分割成三个部分,最上面为类名,中间为该类包含的属性,最下面为该类包含的方法。从上面的类图可以看出,GH_Component 是直接继承自 GH_ActiveObject

由于GH所特有的数据树结构(GH_Structure 以及 DataTree)存在,每个GH电池内部对数据的匹配和处理逻辑会变得十分复杂,其中包括一系列问题,例如数据列表与数据树的分支要如何进行数据匹配、数据树与数据树之间要如何匹配等。举个例子,例如下图中,通过两个点构造直线的电池,在输入端分别对应数据列表和数据树的情况下,就会出现不同的结果。

我们还可以通过把鼠标悬停在电池图标的中间来知道这个电池被运行了几次(SolveInstance被Call了多少次),图中可以看出,靠上方的输入端均为数据列表的电池运行了3次,而靠下方的输入端一个为数据列表另一个为数据树的电池运行了9次。

GH电池需要一个统一的管理数据匹配的逻辑才能保证所有电池以同一种方式运行,那如果将这种对数据逻辑的管理完全交给我们电池的开发者,那必然会导致两个后果:1)开发者的学习成本变高,我们不但要学习专注于数据处理的逻辑,还要为GH额外学习一部分的数据匹配逻辑;2)GH的用户体验变差,电池的质量参差不齐,每个电池对于数据匹配的逻辑很可能都是不一样的,对于用户来说,这就是一个灾难,在使用电池之前还需要“猜”一下。

因此GH在每个电池内部均内置了一套管理数据的类,即 GH_ComponentParamServer。这样,作为开发者,无需纠结数据树、数据列表的匹配问题,可以专心设计电池的核心部分——SolveInstance,仅需将电池的输入、输出端口在这个数据管理类中注册即可。

此外,GH_ComponentParamServer也会包含一些UI相关的处理,例如输入端和输出端的数量增加会导致电池在画布上显得更大,电池没有输出端则会以锯齿状显示电池的右端(参考 Custom Preview 电池,下图右侧电池)等等情况。

每个电池实例中均包含一个 GH_ComponentParamServer 实例,而每个 GH_ComponentParamServer 包含一个 GH_InputParamManager 实例和一个 GH_OutputParamManager 实例,分别对应管理输入端的参数和输出端的参数。

#mermaid-svg-Fw0lHBVTjx3SQMZJ .label{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);fill:#333;color:#333}#mermaid-svg-Fw0lHBVTjx3SQMZJ .label text{fill:#333}#mermaid-svg-Fw0lHBVTjx3SQMZJ .node rect,#mermaid-svg-Fw0lHBVTjx3SQMZJ .node circle,#mermaid-svg-Fw0lHBVTjx3SQMZJ .node ellipse,#mermaid-svg-Fw0lHBVTjx3SQMZJ .node polygon,#mermaid-svg-Fw0lHBVTjx3SQMZJ .node path{fill:#ECECFF;stroke:#9370db;stroke-width:1px}#mermaid-svg-Fw0lHBVTjx3SQMZJ .node .label{text-align:center;fill:#333}#mermaid-svg-Fw0lHBVTjx3SQMZJ .node.clickable{cursor:pointer}#mermaid-svg-Fw0lHBVTjx3SQMZJ .arrowheadPath{fill:#333}#mermaid-svg-Fw0lHBVTjx3SQMZJ .edgePath .path{stroke:#333;stroke-width:1.5px}#mermaid-svg-Fw0lHBVTjx3SQMZJ .flowchart-link{stroke:#333;fill:none}#mermaid-svg-Fw0lHBVTjx3SQMZJ .edgeLabel{background-color:#e8e8e8;text-align:center}#mermaid-svg-Fw0lHBVTjx3SQMZJ .edgeLabel rect{opacity:0.9}#mermaid-svg-Fw0lHBVTjx3SQMZJ .edgeLabel span{color:#333}#mermaid-svg-Fw0lHBVTjx3SQMZJ .cluster rect{fill:#ffffde;stroke:#aa3;stroke-width:1px}#mermaid-svg-Fw0lHBVTjx3SQMZJ .cluster text{fill:#333}#mermaid-svg-Fw0lHBVTjx3SQMZJ div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);font-size:12px;background:#ffffde;border:1px solid #aa3;border-radius:2px;pointer-events:none;z-index:100}#mermaid-svg-Fw0lHBVTjx3SQMZJ .actor{stroke:#ccf;fill:#ECECFF}#mermaid-svg-Fw0lHBVTjx3SQMZJ text.actor>tspan{fill:#000;stroke:none}#mermaid-svg-Fw0lHBVTjx3SQMZJ .actor-line{stroke:grey}#mermaid-svg-Fw0lHBVTjx3SQMZJ .messageLine0{stroke-width:1.5;stroke-dasharray:none;stroke:#333}#mermaid-svg-Fw0lHBVTjx3SQMZJ .messageLine1{stroke-width:1.5;stroke-dasharray:2, 2;stroke:#333}#mermaid-svg-Fw0lHBVTjx3SQMZJ #arrowhead path{fill:#333;stroke:#333}#mermaid-svg-Fw0lHBVTjx3SQMZJ .sequenceNumber{fill:#fff}#mermaid-svg-Fw0lHBVTjx3SQMZJ #sequencenumber{fill:#333}#mermaid-svg-Fw0lHBVTjx3SQMZJ #crosshead path{fill:#333;stroke:#333}#mermaid-svg-Fw0lHBVTjx3SQMZJ .messageText{fill:#333;stroke:#333}#mermaid-svg-Fw0lHBVTjx3SQMZJ .labelBox{stroke:#ccf;fill:#ECECFF}#mermaid-svg-Fw0lHBVTjx3SQMZJ .labelText,#mermaid-svg-Fw0lHBVTjx3SQMZJ .labelText>tspan{fill:#000;stroke:none}#mermaid-svg-Fw0lHBVTjx3SQMZJ .loopText,#mermaid-svg-Fw0lHBVTjx3SQMZJ .loopText>tspan{fill:#000;stroke:none}#mermaid-svg-Fw0lHBVTjx3SQMZJ .loopLine{stroke-width:2px;stroke-dasharray:2, 2;stroke:#ccf;fill:#ccf}#mermaid-svg-Fw0lHBVTjx3SQMZJ .note{stroke:#aa3;fill:#fff5ad}#mermaid-svg-Fw0lHBVTjx3SQMZJ .noteText,#mermaid-svg-Fw0lHBVTjx3SQMZJ .noteText>tspan{fill:#000;stroke:none}#mermaid-svg-Fw0lHBVTjx3SQMZJ .activation0{fill:#f4f4f4;stroke:#666}#mermaid-svg-Fw0lHBVTjx3SQMZJ .activation1{fill:#f4f4f4;stroke:#666}#mermaid-svg-Fw0lHBVTjx3SQMZJ .activation2{fill:#f4f4f4;stroke:#666}#mermaid-svg-Fw0lHBVTjx3SQMZJ .mermaid-main-font{font-family:"trebuchet ms", verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-Fw0lHBVTjx3SQMZJ .section{stroke:none;opacity:0.2}#mermaid-svg-Fw0lHBVTjx3SQMZJ .section0{fill:rgba(102,102,255,0.49)}#mermaid-svg-Fw0lHBVTjx3SQMZJ .section2{fill:#fff400}#mermaid-svg-Fw0lHBVTjx3SQMZJ .section1,#mermaid-svg-Fw0lHBVTjx3SQMZJ .section3{fill:#fff;opacity:0.2}#mermaid-svg-Fw0lHBVTjx3SQMZJ .sectionTitle0{fill:#333}#mermaid-svg-Fw0lHBVTjx3SQMZJ .sectionTitle1{fill:#333}#mermaid-svg-Fw0lHBVTjx3SQMZJ .sectionTitle2{fill:#333}#mermaid-svg-Fw0lHBVTjx3SQMZJ .sectionTitle3{fill:#333}#mermaid-svg-Fw0lHBVTjx3SQMZJ .sectionTitle{text-anchor:start;font-size:11px;text-height:14px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-Fw0lHBVTjx3SQMZJ .grid .tick{stroke:#d3d3d3;opacity:0.8;shape-rendering:crispEdges}#mermaid-svg-Fw0lHBVTjx3SQMZJ .grid .tick text{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-Fw0lHBVTjx3SQMZJ .grid path{stroke-width:0}#mermaid-svg-Fw0lHBVTjx3SQMZJ .today{fill:none;stroke:red;stroke-width:2px}#mermaid-svg-Fw0lHBVTjx3SQMZJ .task{stroke-width:2}#mermaid-svg-Fw0lHBVTjx3SQMZJ .taskText{text-anchor:middle;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-Fw0lHBVTjx3SQMZJ .taskText:not([font-size]){font-size:11px}#mermaid-svg-Fw0lHBVTjx3SQMZJ .taskTextOutsideRight{fill:#000;text-anchor:start;font-size:11px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-Fw0lHBVTjx3SQMZJ .taskTextOutsideLeft{fill:#000;text-anchor:end;font-size:11px}#mermaid-svg-Fw0lHBVTjx3SQMZJ .task.clickable{cursor:pointer}#mermaid-svg-Fw0lHBVTjx3SQMZJ .taskText.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-Fw0lHBVTjx3SQMZJ .taskTextOutsideLeft.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-Fw0lHBVTjx3SQMZJ .taskTextOutsideRight.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-Fw0lHBVTjx3SQMZJ .taskText0,#mermaid-svg-Fw0lHBVTjx3SQMZJ .taskText1,#mermaid-svg-Fw0lHBVTjx3SQMZJ .taskText2,#mermaid-svg-Fw0lHBVTjx3SQMZJ .taskText3{fill:#fff}#mermaid-svg-Fw0lHBVTjx3SQMZJ .task0,#mermaid-svg-Fw0lHBVTjx3SQMZJ .task1,#mermaid-svg-Fw0lHBVTjx3SQMZJ .task2,#mermaid-svg-Fw0lHBVTjx3SQMZJ .task3{fill:#8a90dd;stroke:#534fbc}#mermaid-svg-Fw0lHBVTjx3SQMZJ .taskTextOutside0,#mermaid-svg-Fw0lHBVTjx3SQMZJ .taskTextOutside2{fill:#000}#mermaid-svg-Fw0lHBVTjx3SQMZJ .taskTextOutside1,#mermaid-svg-Fw0lHBVTjx3SQMZJ .taskTextOutside3{fill:#000}#mermaid-svg-Fw0lHBVTjx3SQMZJ .active0,#mermaid-svg-Fw0lHBVTjx3SQMZJ .active1,#mermaid-svg-Fw0lHBVTjx3SQMZJ .active2,#mermaid-svg-Fw0lHBVTjx3SQMZJ .active3{fill:#bfc7ff;stroke:#534fbc}#mermaid-svg-Fw0lHBVTjx3SQMZJ .activeText0,#mermaid-svg-Fw0lHBVTjx3SQMZJ .activeText1,#mermaid-svg-Fw0lHBVTjx3SQMZJ .activeText2,#mermaid-svg-Fw0lHBVTjx3SQMZJ .activeText3{fill:#000 !important}#mermaid-svg-Fw0lHBVTjx3SQMZJ .done0,#mermaid-svg-Fw0lHBVTjx3SQMZJ .done1,#mermaid-svg-Fw0lHBVTjx3SQMZJ .done2,#mermaid-svg-Fw0lHBVTjx3SQMZJ .done3{stroke:grey;fill:#d3d3d3;stroke-width:2}#mermaid-svg-Fw0lHBVTjx3SQMZJ .doneText0,#mermaid-svg-Fw0lHBVTjx3SQMZJ .doneText1,#mermaid-svg-Fw0lHBVTjx3SQMZJ .doneText2,#mermaid-svg-Fw0lHBVTjx3SQMZJ .doneText3{fill:#000 !important}#mermaid-svg-Fw0lHBVTjx3SQMZJ .crit0,#mermaid-svg-Fw0lHBVTjx3SQMZJ .crit1,#mermaid-svg-Fw0lHBVTjx3SQMZJ .crit2,#mermaid-svg-Fw0lHBVTjx3SQMZJ .crit3{stroke:#f88;fill:red;stroke-width:2}#mermaid-svg-Fw0lHBVTjx3SQMZJ .activeCrit0,#mermaid-svg-Fw0lHBVTjx3SQMZJ .activeCrit1,#mermaid-svg-Fw0lHBVTjx3SQMZJ .activeCrit2,#mermaid-svg-Fw0lHBVTjx3SQMZJ .activeCrit3{stroke:#f88;fill:#bfc7ff;stroke-width:2}#mermaid-svg-Fw0lHBVTjx3SQMZJ .doneCrit0,#mermaid-svg-Fw0lHBVTjx3SQMZJ .doneCrit1,#mermaid-svg-Fw0lHBVTjx3SQMZJ .doneCrit2,#mermaid-svg-Fw0lHBVTjx3SQMZJ .doneCrit3{stroke:#f88;fill:#d3d3d3;stroke-width:2;cursor:pointer;shape-rendering:crispEdges}#mermaid-svg-Fw0lHBVTjx3SQMZJ .milestone{transform:rotate(45deg) scale(0.8, 0.8)}#mermaid-svg-Fw0lHBVTjx3SQMZJ .milestoneText{font-style:italic}#mermaid-svg-Fw0lHBVTjx3SQMZJ .doneCritText0,#mermaid-svg-Fw0lHBVTjx3SQMZJ .doneCritText1,#mermaid-svg-Fw0lHBVTjx3SQMZJ .doneCritText2,#mermaid-svg-Fw0lHBVTjx3SQMZJ .doneCritText3{fill:#000 !important}#mermaid-svg-Fw0lHBVTjx3SQMZJ .activeCritText0,#mermaid-svg-Fw0lHBVTjx3SQMZJ .activeCritText1,#mermaid-svg-Fw0lHBVTjx3SQMZJ .activeCritText2,#mermaid-svg-Fw0lHBVTjx3SQMZJ .activeCritText3{fill:#000 !important}#mermaid-svg-Fw0lHBVTjx3SQMZJ .titleText{text-anchor:middle;font-size:18px;fill:#000;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-Fw0lHBVTjx3SQMZJ g.classGroup text{fill:#9370db;stroke:none;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);font-size:10px}#mermaid-svg-Fw0lHBVTjx3SQMZJ g.classGroup text .title{font-weight:bolder}#mermaid-svg-Fw0lHBVTjx3SQMZJ g.clickable{cursor:pointer}#mermaid-svg-Fw0lHBVTjx3SQMZJ g.classGroup rect{fill:#ECECFF;stroke:#9370db}#mermaid-svg-Fw0lHBVTjx3SQMZJ g.classGroup line{stroke:#9370db;stroke-width:1}#mermaid-svg-Fw0lHBVTjx3SQMZJ .classLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.5}#mermaid-svg-Fw0lHBVTjx3SQMZJ .classLabel .label{fill:#9370db;font-size:10px}#mermaid-svg-Fw0lHBVTjx3SQMZJ .relation{stroke:#9370db;stroke-width:1;fill:none}#mermaid-svg-Fw0lHBVTjx3SQMZJ .dashed-line{stroke-dasharray:3}#mermaid-svg-Fw0lHBVTjx3SQMZJ #compositionStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-Fw0lHBVTjx3SQMZJ #compositionEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-Fw0lHBVTjx3SQMZJ #aggregationStart{fill:#ECECFF;stroke:#9370db;stroke-width:1}#mermaid-svg-Fw0lHBVTjx3SQMZJ #aggregationEnd{fill:#ECECFF;stroke:#9370db;stroke-width:1}#mermaid-svg-Fw0lHBVTjx3SQMZJ #dependencyStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-Fw0lHBVTjx3SQMZJ #dependencyEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-Fw0lHBVTjx3SQMZJ #extensionStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-Fw0lHBVTjx3SQMZJ #extensionEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-Fw0lHBVTjx3SQMZJ .commit-id,#mermaid-svg-Fw0lHBVTjx3SQMZJ .commit-msg,#mermaid-svg-Fw0lHBVTjx3SQMZJ .branch-label{fill:lightgrey;color:lightgrey;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-Fw0lHBVTjx3SQMZJ .pieTitleText{text-anchor:middle;font-size:25px;fill:#000;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-Fw0lHBVTjx3SQMZJ .slice{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-Fw0lHBVTjx3SQMZJ g.stateGroup text{fill:#9370db;stroke:none;font-size:10px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-Fw0lHBVTjx3SQMZJ g.stateGroup text{fill:#9370db;fill:#333;stroke:none;font-size:10px}#mermaid-svg-Fw0lHBVTjx3SQMZJ g.statediagram-cluster .cluster-label text{fill:#333}#mermaid-svg-Fw0lHBVTjx3SQMZJ g.stateGroup .state-title{font-weight:bolder;fill:#000}#mermaid-svg-Fw0lHBVTjx3SQMZJ g.stateGroup rect{fill:#ECECFF;stroke:#9370db}#mermaid-svg-Fw0lHBVTjx3SQMZJ g.stateGroup line{stroke:#9370db;stroke-width:1}#mermaid-svg-Fw0lHBVTjx3SQMZJ .transition{stroke:#9370db;stroke-width:1;fill:none}#mermaid-svg-Fw0lHBVTjx3SQMZJ .stateGroup .composit{fill:white;border-bottom:1px}#mermaid-svg-Fw0lHBVTjx3SQMZJ .stateGroup .alt-composit{fill:#e0e0e0;border-bottom:1px}#mermaid-svg-Fw0lHBVTjx3SQMZJ .state-note{stroke:#aa3;fill:#fff5ad}#mermaid-svg-Fw0lHBVTjx3SQMZJ .state-note text{fill:black;stroke:none;font-size:10px}#mermaid-svg-Fw0lHBVTjx3SQMZJ .stateLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.7}#mermaid-svg-Fw0lHBVTjx3SQMZJ .edgeLabel text{fill:#333}#mermaid-svg-Fw0lHBVTjx3SQMZJ .stateLabel text{fill:#000;font-size:10px;font-weight:bold;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-Fw0lHBVTjx3SQMZJ .node circle.state-start{fill:black;stroke:black}#mermaid-svg-Fw0lHBVTjx3SQMZJ .node circle.state-end{fill:black;stroke:white;stroke-width:1.5}#mermaid-svg-Fw0lHBVTjx3SQMZJ #statediagram-barbEnd{fill:#9370db}#mermaid-svg-Fw0lHBVTjx3SQMZJ .statediagram-cluster rect{fill:#ECECFF;stroke:#9370db;stroke-width:1px}#mermaid-svg-Fw0lHBVTjx3SQMZJ .statediagram-cluster rect.outer{rx:5px;ry:5px}#mermaid-svg-Fw0lHBVTjx3SQMZJ .statediagram-state .divider{stroke:#9370db}#mermaid-svg-Fw0lHBVTjx3SQMZJ .statediagram-state .title-state{rx:5px;ry:5px}#mermaid-svg-Fw0lHBVTjx3SQMZJ .statediagram-cluster.statediagram-cluster .inner{fill:white}#mermaid-svg-Fw0lHBVTjx3SQMZJ .statediagram-cluster.statediagram-cluster-alt .inner{fill:#e0e0e0}#mermaid-svg-Fw0lHBVTjx3SQMZJ .statediagram-cluster .inner{rx:0;ry:0}#mermaid-svg-Fw0lHBVTjx3SQMZJ .statediagram-state rect.basic{rx:5px;ry:5px}#mermaid-svg-Fw0lHBVTjx3SQMZJ .statediagram-state rect.divider{stroke-dasharray:10,10;fill:#efefef}#mermaid-svg-Fw0lHBVTjx3SQMZJ .note-edge{stroke-dasharray:5}#mermaid-svg-Fw0lHBVTjx3SQMZJ .statediagram-note rect{fill:#fff5ad;stroke:#aa3;stroke-width:1px;rx:0;ry:0}:root{--mermaid-font-family: '"trebuchet ms", verdana, arial';--mermaid-font-family: "Comic Sans MS", "Comic Sans", cursive}#mermaid-svg-Fw0lHBVTjx3SQMZJ .error-icon{fill:#522}#mermaid-svg-Fw0lHBVTjx3SQMZJ .error-text{fill:#522;stroke:#522}#mermaid-svg-Fw0lHBVTjx3SQMZJ .edge-thickness-normal{stroke-width:2px}#mermaid-svg-Fw0lHBVTjx3SQMZJ .edge-thickness-thick{stroke-width:3.5px}#mermaid-svg-Fw0lHBVTjx3SQMZJ .edge-pattern-solid{stroke-dasharray:0}#mermaid-svg-Fw0lHBVTjx3SQMZJ .edge-pattern-dashed{stroke-dasharray:3}#mermaid-svg-Fw0lHBVTjx3SQMZJ .edge-pattern-dotted{stroke-dasharray:2}#mermaid-svg-Fw0lHBVTjx3SQMZJ .marker{fill:#333}#mermaid-svg-Fw0lHBVTjx3SQMZJ .marker.cross{stroke:#333}:root { --mermaid-font-family: "trebuchet ms", verdana, arial;}#mermaid-svg-Fw0lHBVTjx3SQMZJ {color: rgba(0, 0, 0, 0.75);font: ;}

GH_Component
GH_ComponentParamServer
GH_InputParamManager
GH_OutputParamManager

我们在 RegisterInputParamsRegisterOutputParams 函数中可以看到一个传参为对应的 [Input|Output]ParamManager 。GH主线程在构造我们电池时,会调用这两个函数,并传入对应的实例。我们通过这个实例的一些方法,就可以实现在GH电池上添加输入/输出端了。常用的一些方法包含

  • AddBooleanParameter
  • AddCurveParameter
  • AddIntegerParameter
  • AddLineParameter
  • AddNumberParameter
  • AddTextParameter

大部分的方法都可以通过我们需要添加的输入/输出量的C#类名来确定,例如我们需要处理 Curve(曲线)类的参数,则使用 AddCurveParameter,处理 Line(直线)类的参数则使用 AddLineParameter

有几个特殊的例子值得注意:

  • doublefloat所代表的浮点数,在GH中会统一使用 AddNumberParameter来添加,且此时底层会使用double来保存该值
  • string 字符串类型在GH会使用AddTextParameter来添加,此时底层对数据的存储仍然是string类型,似乎这里只是一个名字的改变

下面就是一个例子,我们在输入端添加了3个输入值,分别是布尔值、直线以及浮点数,而输出端则是一个整数值。而且当我们试图将一个曲线接入电池的直线输入端时,电池会直接报错,并提示错误信息“无法将曲线转换为直线”。

protected override void RegisterInputParams(GH_Component.GH_InputParamManager pManager)
{pManager.AddBooleanParameter("布尔", "BOOL", "一个布尔值", GH_ParamAccess.item);pManager.AddLineParameter("直线", "LINE", "一个直线", GH_ParamAccess.item);pManager.AddNumberParameter("浮点数", "NUM", "一个浮点数值", GH_ParamAccess.item);
}
protected override void RegisterOutputParams(GH_Component.GH_OutputParamManager pManager)
{pManager.AddIntegerParameter("整数", "INT", "一个整数值", GH_ParamAccess.item);
}


设置某输入参数为可选

在我们不接入任何数据的时候,电池也会提示我们数据输入口没有数据接入,且电池的 SolveInstance 也不会运行,从而提供某种程度上的数据验证功能,如下图所示。

我们当然可以选择不使用这种数据验证,将一个或某个电池的输入端数据为“可选”参数 —— 即该数据端口可不接入数据,电池仍可正常运作。

Grasshopper电池的数据管理类的底层是通过一个列表来存储我们添加的各个参数的,每个参数都是最终继承于 GH_Param 父类。我们可以通过使用列表获取到每个被添加的参数,然后将参数的 Optional 属性设置成 true 就可以实现该参数在不被连入数据时电池也可正常运作。具体看下列代码及结果演示,电池在 直线浮点数 两个输入参数端口即便没有数据连入仍可正常工作:

protected override void RegisterInputParams(GH_Component.GH_InputParamManager pManager)
{pManager.AddBooleanParameter("布尔", "BOOL", "一个布尔值", GH_ParamAccess.item);pManager.AddLineParameter("直线", "LINE", "一个直线", GH_ParamAccess.item);pManager.AddNumberParameter("浮点数", "NUM", "一个浮点数值", GH_ParamAccess.item);pManager[1].Optional = true;pManager[2].Optional = true;
}


为输入参数设置默认值

除了将参数设为“可选”之外,另一种可以让电池的某些输入端口不需要数据接入就可以工作的方法就是为参数指定默认值。电池在没有数据接入时就会自动采用默认值,Grasshopper原生电池的 Series 电池(生成等距数列)就使用了默认值。

值得注意的是,设置参数默认值与设置参数“可选”不同的是,即便电池的参数设置了默认值,用户仍然可以通过右键单击该参数,将默认值删除,此时电池将不会工作。

默认值可以在注册参数时设置,比如下列代码就可以将前文例中的电池的 布尔 参数设置一个默认值为 false

pManager.AddBooleanParameter("布尔", "BOOL", "一个布尔值", GH_ParamAccess.item, false);

或者是添加一个默认值列表,这样可以将默认值设置成一个 list 输入。下例中的代码将默认值设置为一个列表。

IEnumerable<bool> boolList = new List<bool>{ true, false, true };
pManager.AddBooleanParameter("布尔", "BOOL", "一个布尔值", GH_ParamAccess.item, boolList);

其他参数属性

Grasshopper电池中的输入/输出参数对应的基类 GH_Param 除了上一节当中说提到的 Optional 属性之外,还有下列属性是常用到的

  • Name
  • Nickname
  • Description
  • Access
  • DataMapping
  • VolatileData

其中,NameNicknameDescription顾名思义,就是这个参数的一些描述性信息,在注册参数的时候也可以指定,也可以通过直接修改这些属性值进行再定义和修改。

Access属性则决定了电池的SolveInstance被调用时,会以什么样的方式来获取到连入的数据或者传出数据。Access属性也是一个在注册参数时的一个必填项,其值为一个枚举类 GH_ParamAccess,共有3个值,分别是 itemlisttree。它们的具体的区别会在本文最后介绍。

DataMapping属性可以改变参数的数据结构,它提供FlattenGraftNone这三个枚举类值可以赋予,分别对应将参数内的数据进行对应的数据结构改变。这里的改变与我们在正常使用Grasshopper时,在电池的输入/输出端通过右键点击每个参数,改变数据结构时完全相同。

VolatileData属性则是可以直接以数据树形式获取该输入/输出参数的数值,这在某些特定场景十分有用,尤其是当我们不希望使用Grasshopper默认的数据管理模式的时候,通过VolatileData属性可以直接获取到该参数对应的输入值或者输出值。


ParamAccess与数据结构

前文提到,Grasshopper电池中的每个输入/输出都对应有一个Access属性,该属性是一个枚举类型的值,一共有三个值(itemlisttree)可选择,每个值对应的作用如下:

  • item: 每次电池内SolveInstance函数被执行时,从该参数输入/输出端操作数据时,以每个参数的实例来进行操作
  • list: 每次电池内SolveInstance函数被执行时,从该参数输入/输出端操作数据时,以封装参数实例的列表来进行操作
  • tree: 每次电池内SolveInstance函数被执行时,从该参数输入/输出端操作数据时,以封装参数实例的数据树来进行操作

上面的文字描述读起来不是特别直观,下面就来用一个图来说明他们的区别

图中构造了一个简单的数据树,该数据树一共有三个树枝,第一个树枝中有三个实例,第二个树枝中有两个实例,第三个树枝中有四个实例,共计九个实例存储于该数据树中。此时,如果将该数据树连入某电池的输入端,当电池的输入端的参数具备不同的Access属性时,该电池将由如下不同的执行逻辑:

  • item:电池的SolveInstance将会被执行 9 次,且每次执行时,从该输入端获取数据时,将按实例来获取:第一次执行时,SolveInstance将获得object1,第二次执行时,SolveInstance将获得object2,以此类推
  • list:电池的SolveInstance将会被执行 3 次,因为该数据树一共有3个树枝。每次执行时,将按树枝提取这个树枝上所有的实例,并按照列表方式读入:第一次执行时,SolveInstance将获取到 List<object>{ object0, object1, object2 },第二次执行时,SolveInstance将获取到 List<object>{ object3, object4 },以此类推,直至所有树枝被处理完毕
  • tree:电池的SolveInstance将仅被执行 1 次,且数据树将会被完整地传入。此时,获取的数据与直接使用VolatileData获取得到的数据树相同。

下图就是分别设置输入端为不同的 GH_ParamAccess 值时,电池所对应的不同的表现。


【Grasshopper基础2】Grasshopper电池的输入和输出不是那么详的解/进一步了解Params相关推荐

  1. html input不能输入小数_【Python基础(八)】输入和输出

    本节将会介绍python中的输入和输出操作,基础部分主要就是介绍input()和print()的用法,print()我们在之前的学习中已经多次使用了,并不陌生,这一节再详细的梳理一下print()中可 ...

  2. 【Grasshopper基础8】电池的序列化与反序列化 Serilization of Grasshopper Component

    这篇文章的内容是介绍GH_Component中另外一对可以被 override 的函数: Read Write 当我们在自己的电池中直接 override 时,Visual Studio会帮我们添加基 ...

  3. c++中整形输入逗号_C语言输入和输出方法amp;技巧详解

    引言 今天刷题,写了好久写完了,一提交0分,改了好久没改对1分,看了一下别人的代码,又简洁又直观,代码量又少,还是满分,心理不平衡了,仔细看了一下,对输入输出的处理处理的好好.吃了这方面的亏,干脆借着 ...

  4. 【Grasshopper基础6】输入/输出参数可变的电池 / 如何让电池支持参数增加和减少

    相信大家一定在Grasshopper中见过输入或者输出参数可以自由变化的电池,例如,笔者常用的电池"Entwine"就可以在电池的输入端添加一个参数或者减少参数,用来支持更多的电池 ...

  5. 【Grasshopper基础12】打造自定义可复用的有按钮电池外观模版

    作者:"咕咕咕?下一篇马上就写好了" 上一篇[基础11]向大家介绍了怎么在Grasshopper里制作自己的带有按钮的电池外观.从反馈来看,挺多读者对这个例子十分感兴趣,同时也私信 ...

  6. 【Grasshopper基础7】如何重写Grasshopper电池的预览 DrawViewportWires / DrawViewportMeshes

    经过[基础1~6]的介绍,相信大家对Grasshopper电池的开发的框架已经又有更深的了解了, 甚至已经在实现各种奇奇怪怪的 逻辑了.无论是在电池上修改原有功能还是增加新的功能,都是通过继承自GH_ ...

  7. 【Grasshopper基础5】在GH里看基金? —— 简单电池项目实战

    经过前面[Grasshopper基础1~4]的介绍,相信读者已经了解了如何在Visual Studio里创建电池.如何获取数据.如何传出数据. 那么在了解这些原理之后,就让我们来一起实现一个小的项目, ...

  8. 【Grasshopper基础10】GH_Attribute简介 —— 自定义电池样式的终极后台

    距离上次的[基础9]已经过去了又有差不多1个月了,工作上的事情越来越多,能抽出空来(主要是要找到"有空并且有舒适的心情来写"的时间挺难的-)的时间也变少了.笔者最近在做的工作跟Gr ...

  9. 【Grasshopper基础14】创建可在画布上自由传递的自定义类型数据(下)—— 电池与自定义IGH_Goo的交互

    2022年3月至今,这段日子真是一段令人印象深刻的时光. 总之,居家这么久了,忙了一阵终于可以来写点东西了.首先要对上一期[基础13]内的两个小错误进行一个勘误: 在 class Pudge 中对 I ...

最新文章

  1. php 伪协议 lfi,php://伪协议(I/O)总能给你惊喜——Bugku CTF-welcome to bugkuctf
  2. mac 部署python环境
  3. shell中字符串基本用法
  4. [转]TortoiseSVN客户端重新设置用户名和密码
  5. 开发优秀产品的六大秘诀
  6. java获取时间,本周,本月,本季度的起始
  7. 矩阵加法(信息学奥赛一本通-T1124)
  8. Linux Shell脚本入门教程系列之(三)Shell变量
  9. LeetCode 69. Sqrt(x)
  10. 参考的datalist分页helper
  11. C++开源矩阵计算工具——Eigen的简单用法(三)
  12. 海思3559AV100上运行自己训练的yolov3
  13. nlogn求最长不上升子序列
  14. 华为证实 P40 将没有 Google 服务;李世石退役;Python 3.8.1 发布 | 极客头条
  15. 服务器鉴别信息清楚,对服务器远程管理时鉴别信息未加密
  16. Host '***' is blocked because of many connection errors...
  17. 网络嵌入之STNE model
  18. 西门子S7-200的PLC,CPU224XP的模拟量接线怎样接
  19. 什么U盘启动盘制作工具是纯净版的,没有捆绑软件和广告的?
  20. 京东客小程序功能模块源码V6.0.2

热门文章

  1. skb_clone 与skb_copy
  2. 职场经典专用PPT模板
  3. (二)爬取新房销售信息——数据分析+可视化篇
  4. 机房监控系统的六大感官!
  5. Redis学习笔记(转尚硅谷周阳)
  6. 打砖块教学(初学:5)
  7. jq 改变其他兄弟元素的样式
  8. [CF1598G]The Sum of Good Numbers
  9. Canvas画电路图
  10. 51单片机IIC_EEPROM