99from OCP .TopoDS import TopoDS_Shape
1010from OCP .AIS import AIS_InteractiveObject , AIS_Shape
1111from OCP .Quantity import \
12- Quantity_TOC_RGB as TOC_RGB , Quantity_Color
13-
12+ Quantity_TOC_RGB as TOC_RGB , Quantity_Color , Quantity_NOC_GOLD as GOLD
13+ from OCP .Graphic3d import Graphic3d_NOM_JADE , Graphic3d_MaterialAspect
14+
1415from PyQt5 .QtGui import QColor
1516
17+ DEFAULT_FACE_COLOR = Quantity_Color (GOLD )
18+ DEFAULT_MATERIAL = Graphic3d_MaterialAspect (Graphic3d_NOM_JADE )
19+
1620def find_cq_objects (results : dict ):
1721
1822 return {k :SimpleNamespace (shape = v ,options = {}) for k ,v in results .items () if isinstance (v ,cq .Workplane )}
@@ -30,7 +34,7 @@ def to_compound(obj : Union[cq.Workplane, List[cq.Workplane], cq.Shape, List[cq.
3034 elif isinstance (obj ,list ) and isinstance (obj [0 ],cq .Shape ):
3135 vals .extend (obj )
3236 elif isinstance (obj , TopoDS_Shape ):
33- vals .append (cq .Shape .cast (obj ))
37+ vals .append (cq .Shape .cast (obj ))
3438 elif isinstance (obj ,list ) and isinstance (obj [0 ],TopoDS_Shape ):
3539 vals .extend (cq .Shape .cast (o ) for o in obj )
3640 elif isinstance (obj , cq .Sketch ):
@@ -63,15 +67,18 @@ def make_AIS(obj : Union[cq.Workplane, List[cq.Workplane], cq.Shape, List[cq.Sha
6367 else :
6468 shape = to_compound (obj )
6569 ais = AIS_Shape (shape .wrapped )
66-
70+
71+ set_material (ais , DEFAULT_MATERIAL )
72+ set_color (ais , DEFAULT_FACE_COLOR )
73+
6774 if 'alpha' in options :
68- ais . SetTransparency ( options ['alpha' ])
75+ set_transparency ( ais , options ['alpha' ])
6976 if 'color' in options :
70- ais . SetColor ( to_occ_color (options ['color' ]))
77+ set_color ( ais , to_occ_color (options ['color' ]))
7178 if 'rgba' in options :
7279 r ,g ,b ,a = options ['rgba' ]
73- ais . SetColor ( to_occ_color ((r ,g ,b )))
74- ais . SetTransparency ( a )
80+ set_color ( ais , to_occ_color ((r ,g ,b )))
81+ set_transparency ( ais , a )
7582
7683 return ais ,shape
7784
@@ -88,7 +95,7 @@ def export(obj : Union[cq.Workplane, List[cq.Workplane]], type : str,
8895 comp .exportBrep (file )
8996
9097def to_occ_color (color ) -> Quantity_Color :
91-
98+
9299 if not isinstance (color , QColor ):
93100 if isinstance (color , tuple ):
94101 if isinstance (color [0 ], int ):
@@ -118,12 +125,29 @@ def get_occ_color(obj : Union[AIS_InteractiveObject, Quantity_Color]) -> QColor:
118125def set_color (ais : AIS_Shape , color : Quantity_Color ) -> AIS_Shape :
119126
120127 drawer = ais .Attributes ()
128+ drawer .SetupOwnShadingAspect ()
121129 drawer .ShadingAspect ().SetColor (color )
122130
123131 return ais
124132
133+ def set_material (ais : AIS_Shape , material : Graphic3d_MaterialAspect ) -> AIS_Shape :
134+
135+ drawer = ais .Attributes ()
136+ drawer .SetupOwnShadingAspect ()
137+ drawer .ShadingAspect ().SetMaterial (material )
138+
139+ return ais
140+
141+ def set_transparency (ais : AIS_Shape , alpha : float ) -> AIS_Shape :
142+
143+ drawer = ais .Attributes ()
144+ drawer .SetupOwnShadingAspect ()
145+ drawer .ShadingAspect ().SetTransparency (alpha )
146+
147+ return ais
148+
125149def reload_cq ():
126-
150+
127151 # NB: order of reloads is important
128152 reload (cq .types )
129153 reload (cq .occ_impl .geom )
@@ -147,13 +171,13 @@ def reload_cq():
147171 reload (cq .occ_impl .exporters )
148172 reload (cq .assembly )
149173 reload (cq )
150-
151-
174+
175+
152176def is_obj_empty (obj : Union [cq .Workplane ,cq .Shape ]) -> bool :
153-
177+
154178 rv = False
155-
179+
156180 if isinstance (obj , cq .Workplane ):
157181 rv = True if isinstance (obj .val (), cq .Vector ) else False
158-
182+
159183 return rv
0 commit comments