1313from typing_extensions import Protocol , Self
1414from math import degrees , radians
1515
16+ from OCP .TCollection import TCollection_HAsciiString
1617from OCP .TDocStd import TDocStd_Document
1718from OCP .TCollection import TCollection_ExtendedString
1819from OCP .XCAFDoc import (
1920 XCAFDoc_DocumentTool ,
2021 XCAFDoc_ColorType ,
2122 XCAFDoc_ColorGen ,
23+ XCAFDoc_Material ,
24+ XCAFDoc_VisMaterial ,
2225)
2326from OCP .XCAFApp import XCAFApp_Application
2427from OCP .BinXCAFDrivers import BinXCAFDrivers
5861AssemblyObjects = Union [Shape , Workplane , None ]
5962
6063
64+ class Material (object ):
65+ """
66+ Wrapper for the OCCT material classes XCAFDoc_Material and XCAFDoc_VisMaterial.
67+ XCAFDoc_Material is focused on physical material properties and
68+ XCAFDoc_VisMaterial is for visual properties to be used when rendering.
69+ """
70+
71+ wrapped : XCAFDoc_Material
72+ wrapped_vis : XCAFDoc_VisMaterial
73+
74+ def __init__ (self , name : str | None = None , ** kwargs ):
75+ """
76+ Can be passed an arbitrary string name for the material along with keyword
77+ arguments defining some other characteristics of the material. If nothing is
78+ passed, arbitrary defaults are used.
79+ """
80+
81+ # Create the default material object and prepare to set a few defaults
82+ self .wrapped = XCAFDoc_Material ()
83+
84+ # Default values in case the user did not set any others
85+ aName = "Default"
86+ aDescription = "Default material with properties similar to low carbon steel"
87+ aDensity = 7.85
88+ aDensityName = "Mass density"
89+ aDensityTypeName = "g/cm^3"
90+
91+ # See if there are any non-defaults to be set
92+ if name :
93+ aName = name
94+ if "description" in kwargs .keys ():
95+ aDescription = kwargs ["description" ]
96+ if "density" in kwargs .keys ():
97+ aDensity = kwargs ["density" ]
98+ if "densityUnit" in kwargs .keys ():
99+ aDensityTypeName = kwargs ["densityUnit" ]
100+
101+ # Set the properties on the material object
102+ self .wrapped .Set (
103+ TCollection_HAsciiString (aName ),
104+ TCollection_HAsciiString (aDescription ),
105+ aDensity ,
106+ TCollection_HAsciiString (aDensityName ),
107+ TCollection_HAsciiString (aDensityTypeName ),
108+ )
109+
110+ # Create the default visual material object and allow it to be used just with
111+ # the OCC layer, for now. When this material class is expanded to include visual
112+ # attributes, the OCC docs say that XCAFDoc_VisMaterialTool should be used to
113+ # manage those attributes on the XCAFDoc_VisMaterial class.
114+ self .wrapped_vis = XCAFDoc_VisMaterial ()
115+
116+ @property
117+ def name (self ) -> str :
118+ """
119+ Get the string name of the material.
120+ """
121+ return self .wrapped .GetName ().ToCString ()
122+
123+ @property
124+ def description (self ) -> str :
125+ """
126+ Get the string description of the material.
127+ """
128+ return self .wrapped .GetDescription ().ToCString ()
129+
130+ @property
131+ def density (self ) -> float :
132+ """
133+ Get the density value of the material.
134+ """
135+ return self .wrapped .GetDensity ()
136+
137+ @property
138+ def densityUnit (self ) -> str :
139+ """
140+ Get the units that the material density is defined in.
141+ """
142+ return self .wrapped .GetDensValType ().ToCString ()
143+
144+ def toTuple (self ) -> Tuple [str , str , float , str ]:
145+ """
146+ Convert Material to a tuple.
147+ """
148+ name = self .name
149+ description = self .description
150+ density = self .density
151+ densityUnit = self .densityUnit
152+
153+ return (name , description , density , densityUnit )
154+
155+ def __hash__ (self ):
156+ """
157+ Create a unique hash for this material via its tuple.
158+ """
159+ return hash (self .toTuple ())
160+
161+ def __eq__ (self , other ):
162+ """
163+ Check equality of this material against another via its tuple.
164+ """
165+ return self .toTuple () == other .toTuple ()
166+
167+ def __getstate__ (self ) -> Tuple [str , str , float , str ]:
168+ """
169+ Allows pickling.
170+ """
171+ return self .toTuple ()
172+
173+ def __setstate__ (self , data : Tuple [str , str , float , str ]):
174+ """
175+ Allows pickling.
176+ """
177+ self .wrapped = XCAFDoc_Material ()
178+ self .wrapped .Set (
179+ TCollection_HAsciiString (data [0 ]),
180+ TCollection_HAsciiString (data [1 ]),
181+ data [2 ],
182+ TCollection_HAsciiString ("Mass density" ),
183+ TCollection_HAsciiString (data [3 ]),
184+ )
185+
186+
61187class Color (object ):
62188 """
63189 Wrapper for the OCCT color object Quantity_ColorRGBA.
@@ -239,6 +365,7 @@ def add(
239365 loc : Optional [Location ] = None ,
240366 name : Optional [str ] = None ,
241367 color : Optional [Color ] = None ,
368+ material : Optional [Union [Material , str ]] = None ,
242369 metadata : Optional [Dict [str , Any ]] = None ,
243370 ) -> Self :
244371 ...
@@ -249,6 +376,7 @@ def add(
249376 loc : Optional [Location ] = None ,
250377 name : Optional [str ] = None ,
251378 color : Optional [Color ] = None ,
379+ material : Optional [Union [Material , str ]] = None ,
252380 metadata : Optional [Dict [str , Any ]] = None ,
253381 ** kwargs : Any ,
254382 ) -> Self :
0 commit comments