Skip to content

Commit bda8995

Browse files
committed
STEP export
* exporters.export: expose STEP options * Use default OCCT write.precision.mode value 0 * Doc update Merge branch 'master' into step
2 parents 638d338 + cee66ff commit bda8995

File tree

11 files changed

+425
-185
lines changed

11 files changed

+425
-185
lines changed

cadquery/assembly.py

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -357,13 +357,16 @@ def solve(self) -> "Assembly":
357357
ents = {}
358358

359359
i = 0
360-
locked = []
360+
locked: List[int] = []
361+
361362
for c in self.constraints:
362363
for name in c.objects:
363364
if name not in ents:
364365
ents[name] = i
365366
i += 1
366-
if c.kind == "Fixed" or name == self.name:
367+
if (c.kind == "Fixed" or name == self.name) and ents[
368+
name
369+
] not in locked:
367370
locked.append(ents[name])
368371

369372
# Lock the first occuring entity if needed.
@@ -402,15 +405,32 @@ def solve(self) -> "Assembly":
402405
if not constraints:
403406
raise ValueError("At least one constraint required")
404407

408+
# check if at least two entities are present
409+
if len(ents) < 2:
410+
raise ValueError("At least two entities need to be constrained")
411+
405412
# instantiate the solver
406-
solver = ConstraintSolver(locs, constraints, locked=locked)
413+
scale = self.toCompound().BoundingBox().DiagonalLength
414+
solver = ConstraintSolver(locs, constraints, locked=locked, scale=scale)
407415

408416
# solve
409417
locs_new, self._solve_result = solver.solve()
410418

411419
# update positions
420+
421+
# find the inverse root loc
422+
loc_root_inv = Location()
423+
424+
if self.obj:
425+
for loc_new, n in zip(locs_new, ents):
426+
if n == self.name:
427+
loc_root_inv = loc_new.inverse
428+
break
429+
430+
# update the positions
412431
for loc_new, n in zip(locs_new, ents):
413-
self.objects[n].loc = loc_new
432+
if n != self.name:
433+
self.objects[n].loc = loc_root_inv * loc_new
414434

415435
return self
416436

@@ -423,12 +443,14 @@ def save(
423443
**kwargs,
424444
) -> "Assembly":
425445
"""
426-
save as STEP or OCCT native XML file
446+
Save assembly to a file.
427447
428-
:param path: filepath
448+
:param path: Path and filename for writing.
429449
:param exportType: export format (default: None, results in format being inferred form the path)
430450
:param tolerance: the deflection tolerance, in model units. Only used for GLTF, VRML. Default 0.1.
431451
:param angularTolerance: the angular tolerance, in radians. Only used for GLTF, VRML. Default 0.1.
452+
:param **kwargs: Additional keyword arguments. Only used for STEP.
453+
See :meth:`~cadquery.occ_impl.exporters.assembly.exportAssembly`.
432454
"""
433455

434456
if exportType is None:

cadquery/occ_impl/exporters/__init__.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,10 @@ def export(
100100
raise ValueError("Only Workplanes can be exported as DXF")
101101

102102
elif exportType == ExportTypes.STEP:
103-
shape.exportStep(fname)
103+
if opt:
104+
shape.exportStep(fname, **opt)
105+
else:
106+
shape.exportStep(fname)
104107

105108
elif exportType == ExportTypes.STL:
106109
shape.exportStl(fname, tolerance, angularTolerance)

cadquery/occ_impl/exporters/assembly.py

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,31 +28,26 @@
2828

2929
def exportAssembly(assy: AssemblyProtocol, path: str, **kwargs) -> bool:
3030
"""
31-
Export an assembly to a step a file.
31+
Export an assembly to a STEP file.
3232
3333
kwargs is used to provide optional keyword arguments to configure the exporter.
34-
The current parameters are as follows:
3534
36-
:param write_pcurves: Enables or disables P-Curve entities written to STEP file
35+
:param assy: assembly
36+
:param path: Path and filename for writing
37+
:param write_pcurves: Enable or disable writing parametric curves to the STEP file. Default True.
38+
39+
If False, writes STEP file without pcurves. This decreases the size of the resulting STEP file.
3740
:type write_pcurves: boolean
38-
:param precision_mode: Sets the precision mode of the OCCT STEP exporter
41+
:param precision_mode: Controls the uncertainty value for STEP entities. Specify -1, 0, or 1. Default 0.
42+
See OCCT documentation.
3943
: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.
4944
"""
5045

5146
# Handle the extra settings for the STEP export
5247
pcurves = 1
5348
if "write_pcurves" in kwargs and not kwargs["write_pcurves"]:
5449
pcurves = 0
55-
precision_mode = kwargs["precision_mode"] if "precision_mode" in kwargs else 1
50+
precision_mode = kwargs["precision_mode"] if "precision_mode" in kwargs else 0
5651

5752
_, doc = toCAF(assy, True)
5853

cadquery/occ_impl/geom.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -790,6 +790,9 @@ class BoundBox(object):
790790
zmax: float
791791
zlen: float
792792

793+
center: Vector
794+
DiagonalLength: float
795+
793796
def __init__(self, bb: Bnd_Box) -> None:
794797
self.wrapped = bb
795798
XMin, YMin, ZMin, XMax, YMax, ZMax = bb.Get()

cadquery/occ_impl/shapes.py

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -453,31 +453,25 @@ def exportStl(
453453

454454
def exportStep(self, fileName: str, **kwargs) -> IFSelect_ReturnStatus:
455455
"""
456-
Export this shape to a STEP file
456+
Export this shape to a STEP file.
457457
458458
kwargs is used to provide optional keyword arguments to configure the exporter.
459-
The current parameters are as follows:
460459
461-
:param write_pcurves: Enables or disables P-Curve entities written to STEP file
460+
:param fileName: Path and filename for writing.
461+
:param write_pcurves: Enable or disable writing parametric curves to the STEP file. Default True.
462+
463+
If False, writes STEP file without pcurves. This decreases the size of the resulting STEP file.
462464
:type write_pcurves: boolean
463-
:param precision_mode: Sets the precision mode of the OCCT STEP exporter
465+
:param precision_mode: Controls the uncertainty value for STEP entities. Specify -1, 0, or 1. Default 0.
466+
See OCCT documentation.
464467
: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.
474468
"""
475469

476470
# Handle the extra settings for the STEP export
477471
pcurves = 1
478472
if "write_pcurves" in kwargs and not kwargs["write_pcurves"]:
479473
pcurves = 0
480-
precision_mode = kwargs["precision_mode"] if "precision_mode" in kwargs else 1
474+
precision_mode = kwargs["precision_mode"] if "precision_mode" in kwargs else 0
481475

482476
writer = STEPControl_Writer()
483477
Interface_Static.SetIVal_s("write.surfacecurve.mode", pcurves)

0 commit comments

Comments
 (0)