Skip to content

Commit 4967786

Browse files
Updated exportStepMeta to handle materials
1 parent e8f77f5 commit 4967786

File tree

3 files changed

+47
-9
lines changed

3 files changed

+47
-9
lines changed

cadquery/occ_impl/assembly.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -403,9 +403,7 @@ def toVTKAssy(
403403
if element.material.pbr:
404404
actor.GetProperty().SetMetallic(element.material.pbr.metallic)
405405
actor.GetProperty().SetRoughness(element.material.pbr.roughness)
406-
actor.GetProperty().SetRefractionIndex(
407-
element.material.pbr.refraction_index
408-
)
406+
actor.GetProperty().SetBaseIOR(element.material.pbr.refraction_index)
409407
if element.material.pbr.emissive_factor:
410408
actor.GetProperty().SetEmissiveFactor(
411409
*element.material.pbr.emissive_factor.rgb()

cadquery/occ_impl/exporters/assembly.py

Lines changed: 42 additions & 3 deletions
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, color_to_occt, toCAF, toVTK, toFusedCAF
33+
from ..assembly import (
34+
AssemblyProtocol,
35+
color_to_occt,
36+
toCAF,
37+
toVTK,
38+
toFusedCAF,
39+
material_to_occt,
40+
)
3441
from ..geom import Location
3542
from ..shapes import Shape, Compound
3643

@@ -139,6 +146,8 @@ def exportStepMeta(
139146
shape_tool = XCAFDoc_DocumentTool.ShapeTool_s(doc.Main())
140147
color_tool = XCAFDoc_DocumentTool.ColorTool_s(doc.Main())
141148
layer_tool = XCAFDoc_DocumentTool.LayerTool_s(doc.Main())
149+
material_tool = XCAFDoc_DocumentTool.MaterialTool_s(doc.Main())
150+
vis_material_tool = XCAFDoc_DocumentTool.VisMaterialTool_s(doc.Main())
142151

143152
def _process_child(child: AssemblyProtocol, assy_label: TDF_Label):
144153
"""
@@ -166,16 +175,46 @@ def _process_child(child: AssemblyProtocol, assy_label: TDF_Label):
166175
child.name,
167176
child.loc,
168177
child.color,
178+
child.material,
169179
)
170180

171181
if child_items:
172-
shape, name, loc, color = child_items
182+
shape, name, loc, color, material = child_items
173183

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

181220
# If this assembly has shape metadata, add it to the shape

examples/Ex027_Materials.py

Lines changed: 4 additions & 3 deletions
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)