@@ -83,7 +83,7 @@ def __init__(self, name: str | None = None, **kwargs):
8383
8484 # Default values in case the user did not set any others
8585 aName = "Default"
86- aDescription = "Default material with properties similar to low carbon steel "
86+ aDescription = ""
8787 aDensity = 7.85
8888 aDensityName = "Mass density"
8989 aDensityTypeName = "g/cm^3"
@@ -285,6 +285,7 @@ def __init__(
285285 loc : Optional [Location ] = None ,
286286 name : Optional [str ] = None ,
287287 color : Optional [Color ] = None ,
288+ material : Optional [Material ] = None ,
288289 ):
289290 ...
290291
@@ -316,6 +317,14 @@ def color(self) -> Optional[Color]:
316317 def color (self , value : Optional [Color ]) -> None :
317318 ...
318319
320+ @property
321+ def material (self ) -> Optional [Material ]:
322+ ...
323+
324+ @material .setter
325+ def material (self , value : Optional [Material ]) -> None :
326+ ...
327+
319328 @property
320329 def obj (self ) -> AssemblyObjects :
321330 ...
@@ -355,6 +364,7 @@ def add(
355364 loc : Optional [Location ] = None ,
356365 name : Optional [str ] = None ,
357366 color : Optional [Color ] = None ,
367+ material : Optional [Union [Material , str ]] = None ,
358368 ) -> Self :
359369 ...
360370
@@ -422,6 +432,18 @@ def setColor(l: TDF_Label, color: Color, tool):
422432 tool .SetColor (l , color .wrapped , XCAFDoc_ColorType .XCAFDoc_ColorSurf )
423433
424434
435+ def setMaterial (l : TDF_Label , material : Material , tool ):
436+
437+ tool .SetMaterial (
438+ l ,
439+ TCollection_HAsciiString (material .name ),
440+ TCollection_HAsciiString (material .description ),
441+ material .density ,
442+ TCollection_HAsciiString ("MassDensity" ),
443+ TCollection_HAsciiString (material .densityUnit ),
444+ )
445+
446+
425447def toCAF (
426448 assy : AssemblyProtocol ,
427449 coloredSTEP : bool = False ,
@@ -447,6 +469,7 @@ def toCAF(
447469 tool .SetAutoNaming_s (False )
448470 ctool = XCAFDoc_DocumentTool .ColorTool_s (doc .Main ())
449471 ltool = XCAFDoc_DocumentTool .LayerTool_s (doc .Main ())
472+ mtool = XCAFDoc_DocumentTool .MaterialTool_s (doc .Main ())
450473
451474 # used to store labels with unique part-color combinations
452475 unique_objs : Dict [Tuple [Color | None , AssemblyObjects ], TDF_Label ] = {}
@@ -463,6 +486,9 @@ def _toCAF(el: AssemblyProtocol, ancestor: TDF_Label | None) -> TDF_Label:
463486 # define the current color
464487 current_color = el .color if el .color else None
465488
489+ # define the current material
490+ current_material = el .material if el .material else None
491+
466492 # add a leaf with the actual part if needed
467493 if el .obj :
468494 # get/register unique parts referenced in the assy
@@ -490,6 +516,10 @@ def _toCAF(el: AssemblyProtocol, ancestor: TDF_Label | None) -> TDF_Label:
490516 if coloredSTEP and current_color :
491517 setColor (lab , current_color , ctool )
492518
519+ # Handle materials when exporting to STEP
520+ if current_material :
521+ setMaterial (lab , current_material , mtool )
522+
493523 # handle subshape names/colors/layers
494524 subshape_colors = el ._subshape_colors
495525 subshape_names = el ._subshape_names
0 commit comments