Skip to content

Commit 2a9b50a

Browse files
committed
Added quality controls to STEP export for shape and assembly
1 parent 803a05e commit 2a9b50a

File tree

3 files changed

+57
-3
lines changed

3 files changed

+57
-3
lines changed

cadquery/assembly.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,7 @@ def save(
420420
exportType: Optional[ExportLiterals] = None,
421421
tolerance: float = 0.1,
422422
angularTolerance: float = 0.1,
423+
**kwargs,
423424
) -> "Assembly":
424425
"""
425426
save as STEP or OCCT native XML file
@@ -438,7 +439,7 @@ def save(
438439
raise ValueError("Unknown extension, specify export type explicitly")
439440

440441
if exportType == "STEP":
441-
exportAssembly(self, path)
442+
exportAssembly(self, path, **kwargs)
442443
elif exportType == "XML":
443444
exportCAF(self, path)
444445
elif exportType == "VRML":

cadquery/occ_impl/exporters/assembly.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,22 +21,48 @@
2121
from OCP.RWGltf import RWGltf_CafWriter
2222
from OCP.TColStd import TColStd_IndexedDataMapOfStringString
2323
from OCP.Message import Message_ProgressRange
24+
from OCP.Interface import Interface_Static
2425

2526
from ..assembly import AssemblyProtocol, toCAF, toVTK
2627

2728

28-
def exportAssembly(assy: AssemblyProtocol, path: str) -> bool:
29+
def exportAssembly(assy: AssemblyProtocol, path: str, **kwargs) -> bool:
2930
"""
3031
Export an assembly to a step a file.
32+
33+
kwargs is used to provide optional keyword arguments to configure the exporter.
34+
The current parameters are as follows:
35+
36+
:param write_pcurves: Enables or disables P-Curve entities written to STEP file
37+
:type write_pcurves: boolean
38+
:param precision_mode: Sets the precision mode of the OCCT STEP exporter
39+
:type precision_mode: int
40+
41+
The default behaviour of the OCCT STEP file writer is to add redundant
42+
P-Curve entities to the STEP file. This can often double the size of the
43+
resulting STEP file. Turning off P-Curves can save file size and almost
44+
never impacts the quality of the STEP file.
45+
46+
The precision mode parameter coresponds to the OCCT STEP file precision
47+
for writing geometric data. The default value of 1 for maximum precision
48+
is used by can be changed if desired.
3149
"""
3250

51+
# Handle the extra settings for the STEP export
52+
pcurves = 1
53+
if "write_pcurves" in kwargs and not kwargs["write_pcurves"]:
54+
pcurves = 0
55+
precision_mode = kwargs["precision_mode"] if "precision_mode" in kwargs else 1
56+
3357
_, doc = toCAF(assy, True)
3458

3559
session = XSControl_WorkSession()
3660
writer = STEPCAFControl_Writer(session, False)
3761
writer.SetColorMode(True)
3862
writer.SetLayerMode(True)
3963
writer.SetNameMode(True)
64+
Interface_Static.SetIVal_s("write.surfacecurve.mode", pcurves)
65+
Interface_Static.SetIVal_s("write.precision.mode", precision_mode)
4066
writer.Transfer(doc, STEPControl_StepModelType.STEPControl_AsIs)
4167

4268
status = writer.Write(path)

cadquery/occ_impl/shapes.py

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,8 @@
230230
# for catching exceptions
231231
from OCP.Standard import Standard_NoSuchObject, Standard_Failure
232232

233+
from OCP.Interface import Interface_Static
234+
233235
from math import pi, sqrt
234236
import warnings
235237

@@ -449,12 +451,37 @@ def exportStl(
449451

450452
return writer.Write(self.wrapped, fileName)
451453

452-
def exportStep(self, fileName: str) -> IFSelect_ReturnStatus:
454+
def exportStep(self, fileName: str, **kwargs) -> IFSelect_ReturnStatus:
453455
"""
454456
Export this shape to a STEP file
457+
458+
kwargs is used to provide optional keyword arguments to configure the exporter.
459+
The current parameters are as follows:
460+
461+
:param write_pcurves: Enables or disables P-Curve entities written to STEP file
462+
:type write_pcurves: boolean
463+
:param precision_mode: Sets the precision mode of the OCCT STEP exporter
464+
:type precision_mode: int
465+
466+
The default behaviour of the OCCT STEP file writer is to add redundant
467+
P-Curve entities to the STEP file. This can often double the size of the
468+
resulting STEP file. Turning off P-Curves can save file size and almost
469+
never impacts the quality of the STEP file.
470+
471+
The precision mode parameter coresponds to the OCCT STEP file precision
472+
for writing geometric data. The default value of 1 for maximum precision
473+
is used by can be changed if desired.
455474
"""
456475

476+
# Handle the extra settings for the STEP export
477+
pcurves = 1
478+
if "write_pcurves" in kwargs and not kwargs["write_pcurves"]:
479+
pcurves = 0
480+
precision_mode = kwargs["precision_mode"] if "precision_mode" in kwargs else 1
481+
457482
writer = STEPControl_Writer()
483+
Interface_Static.SetIVal_s("write.surfacecurve.mode", pcurves)
484+
Interface_Static.SetIVal_s("write.precision.mode", precision_mode)
458485
writer.Transfer(self.wrapped, STEPControl_AsIs)
459486

460487
return writer.Write(fileName)

0 commit comments

Comments
 (0)