Skip to content

Commit 38218df

Browse files
Updated exportStepMeta to handle materials
1 parent 0dbe26c commit 38218df

File tree

2 files changed

+50
-8
lines changed

2 files changed

+50
-8
lines changed

cadquery/occ_impl/exporters/assembly.py

+46-5
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,14 @@
3030
from OCP.Message import Message_ProgressRange
3131
from OCP.Interface import Interface_Static
3232

33-
from ..assembly import AssemblyProtocol, toCAF, toVTK, toFusedCAF
33+
from ..assembly import (
34+
AssemblyProtocol,
35+
toCAF,
36+
toVTK,
37+
toFusedCAF,
38+
color_to_occt,
39+
material_to_occt,
40+
)
3441
from ..geom import Location
3542
from ..shapes import Shape, Compound
3643
from ..assembly import Color
@@ -140,6 +147,8 @@ def exportStepMeta(
140147
shape_tool = XCAFDoc_DocumentTool.ShapeTool_s(doc.Main())
141148
color_tool = XCAFDoc_DocumentTool.ColorTool_s(doc.Main())
142149
layer_tool = XCAFDoc_DocumentTool.LayerTool_s(doc.Main())
150+
material_tool = XCAFDoc_DocumentTool.MaterialTool_s(doc.Main())
151+
vis_material_tool = XCAFDoc_DocumentTool.VisMaterialTool_s(doc.Main())
143152

144153
def _process_child(child: AssemblyProtocol, assy_label: TDF_Label):
145154
"""
@@ -167,16 +176,46 @@ def _process_child(child: AssemblyProtocol, assy_label: TDF_Label):
167176
child.name,
168177
child.loc,
169178
child.color,
179+
child.material,
170180
)
171181

172182
if child_items:
173-
shape, name, loc, color = child_items
183+
shape, name, loc, color, material = child_items
174184

175185
# Handle shape name, color and location
176186
part_label = shape_tool.AddShape(shape.wrapped, False)
177187
TDataStd_Name.Set_s(part_label, TCollection_ExtendedString(name))
178-
if color:
179-
color_tool.SetColor(part_label, color.wrapped, XCAFDoc_ColorGen)
188+
189+
# Handle color and material
190+
if material:
191+
# Set color from material if available
192+
if material.color:
193+
color_tool.SetColor(
194+
part_label, color_to_occt(material.color), XCAFDoc_ColorGen
195+
)
196+
197+
# Convert material to OCCT format and add to document
198+
mat, vis_mat = material_to_occt(material)
199+
200+
# Create material label
201+
mat_lab = material_tool.AddMaterial(
202+
mat.GetName(),
203+
mat.GetDescription(),
204+
mat.GetDensity(),
205+
mat.GetDensName(),
206+
mat.GetDensValType(),
207+
)
208+
material_tool.SetMaterial(part_label, mat_lab)
209+
210+
# Add visualization material to the document
211+
vis_mat_lab = vis_material_tool.AddMaterial(
212+
vis_mat, TCollection_AsciiString(material.name)
213+
)
214+
vis_material_tool.SetShapeMaterial(part_label, vis_mat_lab)
215+
elif color:
216+
# If no material but color exists, set the color directly
217+
color_tool.SetColor(part_label, color_to_occt(color), XCAFDoc_ColorGen)
218+
180219
shape_tool.AddComponent(assy_label, part_label, loc.wrapped)
181220

182221
# If this assembly has shape metadata, add it to the shape
@@ -207,7 +246,9 @@ def _process_child(child: AssemblyProtocol, assy_label: TDF_Label):
207246
# Set the individual face color
208247
if face in colors:
209248
color_tool.SetColor(
210-
face_label, colors[face].wrapped, XCAFDoc_ColorGen,
249+
face_label,
250+
color_to_occt(colors[face]),
251+
XCAFDoc_ColorGen,
211252
)
212253

213254
# Also add a layer to hold the face label data

examples/Ex027_Materials.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import cadquery as cq
2+
from cadquery.occ_impl.exporters.assembly import exportStepMeta
23

34
# Create a simple cube
45
cube = cq.Workplane().box(10, 10, 10)
@@ -69,9 +70,9 @@
6970
assy.add(cube.translate((45, 0, 0)), name="gold_cube", material=gold_material)
7071

7172
# Export as OBJ and GLTF to showcase materials
72-
assy.export("materials.step") # Step format
73-
assy.export("materials.gltf") # Step format
74-
assy.export("materials.glb") # GLTF format with PBR materials
73+
assy.export("materials.step") # STEP format
74+
exportStepMeta(assy, "materials_meta.step") # STEP format with metadata
75+
assy.export("materials.glb") # GLTF format (binary) with PBR materials
7576

7677
# Show the assembly in the UI
7778
show_object(assy)

0 commit comments

Comments
 (0)