22
33import System
44
5+ import typing
6+
57import Rhino .Geometry as rg
68
79from ghpythonlib .componentbase import executingcomponent as component
10+ import Grasshopper as gh
11+ from Grasshopper .Kernel import GH_RuntimeMessageLevel as RML
812
913import diffCheck
1014from diffCheck .df_geometries import DFAssembly
1418import numpy as np
1519
1620
21+ def add_bool_toggle (self ,
22+ nickname : str ,
23+ indx : int ,
24+ X_param_coord : float ,
25+ Y_param_coord : float ,
26+ X_offset : int = 87
27+ ) -> None :
28+ """
29+ Adds a boolean toggle to the component input
30+
31+ :param nickname: the nickname of the value list
32+ :param indx: the index of the input parameter
33+ :param X_param_coord: the x coordinate of the input parameter
34+ :param Y_param_coord: the y coordinate of the input parameter
35+ :param X_offset: the offset of the value list from the input parameter
36+ """
37+ param = ghenv .Component .Params .Input [indx ] # noqa: F821
38+ if param .SourceCount == 0 :
39+ toggle = gh .Kernel .Special .GH_BooleanToggle ()
40+ toggle .NickName = nickname
41+ toggle .Description = "Toggle the value to use with DFVizSettings"
42+ toggle .CreateAttributes ()
43+ toggle .Attributes .Pivot = System .Drawing .PointF (
44+ X_param_coord - (toggle .Attributes .Bounds .Width ) - X_offset ,
45+ Y_param_coord - (toggle .Attributes .Bounds .Height / 2 + 0.1 )
46+ )
47+ toggle .Attributes .ExpireLayout ()
48+ gh .Instances .ActiveCanvas .Document .AddObject (toggle , False )
49+ ghenv .Component .Params .Input [indx ].AddSource (toggle ) # noqa: F821
50+
1751class DFPreviewAssembly (component ):
1852 def __init__ (self ):
1953 super (DFPreviewAssembly , self ).__init__ ()
2054 self ._dfassembly = None
55+ self ._joint_rnd_clr = None
2156
22- def RunScript (self , i_assembly : DFAssembly = None ):
57+ ghenv .Component .ExpireSolution (True ) # noqa: F821
58+ ghenv .Component .Attributes .PerformLayout () # noqa: F821
59+ params = getattr (ghenv .Component .Params , "Input" ) # noqa: F821
60+ for j in range (len (params )):
61+ Y_cord = params [j ].Attributes .InputGrip .Y + 1
62+ X_cord = params [j ].Attributes .Pivot .X + 20
63+ input_indx = j
64+ if "i_are_joints_visible" == params [j ].NickName :
65+ add_bool_toggle (
66+ ghenv .Component , # noqa: F821
67+ "show_joints" ,
68+ input_indx , X_cord , Y_cord )
69+
70+
71+ def RunScript (self ,
72+ i_assembly : DFAssembly = None ,
73+ i_are_joints_visible : bool = None
74+ ):
2375 if i_assembly is None :
2476 return None
77+ if i_are_joints_visible is None :
78+ i_are_joints_visible = False
2579
2680 self ._dfassembly = i_assembly
2781
82+ self ._joint_rnd_clr = [System .Drawing .Color .FromArgb (
83+ System .Convert .ToInt32 (255 * np .random .rand ()),
84+ System .Convert .ToInt32 (255 * np .random .rand ()),
85+ System .Convert .ToInt32 (255 * np .random .rand ())) for _ in range (len (self ._dfassembly .beams ))]
86+
87+ self ._are_joints_visible = i_are_joints_visible
88+
89+ return None
90+
2891 # Preview overrides
2992 def DrawViewportWires (self , args ):
3093 for beam in self ._dfassembly .beams :
94+ #######################################
95+ ## DFBeams
96+ #######################################
3197 # beams' obb
3298 df_cloud = diffCheck .diffcheck_bindings .dfb_geometry .DFPointCloud ()
33- df_cloud .points = [np .array ([vertex .Location .X , vertex .Location .Y , vertex .Location .Z ]).reshape (3 , 1 ) for vertex in beam .to_brep ().Vertices ]
99+ vertices_pt3d_rh : typing .List [rg .Point3d ] = [vertex .to_rg_point3d () for vertex in beam .vertices ]
100+ df_cloud .points = [np .array ([vertex .X , vertex .Y , vertex .Z ]).reshape (3 , 1 ) for vertex in vertices_pt3d_rh ]
34101 obb : rg .Brep = diffCheck .df_cvt_bindings .cvt_dfOBB_2_rhbrep (df_cloud .get_tight_bounding_box ())
102+ # args.Display.DrawBrepWires(obb, System.Drawing.Color.Red) ## keep for debugging
35103
36104 # axis arrow
37105 obb_faces = obb .Faces
@@ -49,3 +117,28 @@ def DrawViewportWires(self, args):
49117 System .Drawing .Color .Violet ,
50118 anchor_pt ,
51119 True , 18 )
120+
121+ #######################################
122+ ## DFJoints
123+ #######################################
124+ if self ._are_joints_visible :
125+ for idx_joint , joint in enumerate (beam .joints ):
126+ joint_faces = joint .faces
127+ for idx_face , face in enumerate (joint_faces ):
128+ face_center = face .to_brep_face ().GetBoundingBox (False ).Center
129+ args .Display .DrawPoint (face_center , self ._joint_rnd_clr [idx_joint ])
130+
131+ vector_face_center_2_beam_center = face_center - beam .center
132+ vector_face_center_2_beam_center .Unitize ()
133+ vector_face_center_2_beam_center *= 0.4 * extension_length
134+
135+ ln = rg .Line (face_center , face_center + vector_face_center_2_beam_center )
136+ args .Display .DrawDottedLine (ln , self ._joint_rnd_clr [idx_joint ])
137+
138+ ghenv .Component .AddRuntimeMessage (RML .Remark , "legend joint naming: the beam index - the joint index - the face index by list order" ) # noqa: F821
139+ name_face_joint : str = f"{ beam .index_assembly } -{ joint .id } -{ idx_face } "
140+ args .Display .Draw2dText (
141+ name_face_joint ,
142+ self ._joint_rnd_clr [idx_joint ],
143+ ln .To ,
144+ True , 18 )
0 commit comments