Skip to content

Commit 3cf8742

Browse files
authored
fix: Deduplicate objects with same color on step export (#1690)
* fix: Deduplicate objects with same color on step export * Define Color __hash__, __eq__ methods; Black fix * Remove unused variable in test
1 parent dfba42f commit 3cf8742

File tree

2 files changed

+33
-6
lines changed

2 files changed

+33
-6
lines changed

cadquery/occ_impl/assembly.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,14 @@ def __init__(self, *args, **kwargs):
100100
else:
101101
raise ValueError(f"Unsupported arguments: {args}, {kwargs}")
102102

103+
def __hash__(self):
104+
105+
return hash(self.toTuple())
106+
107+
def __eq__(self, other):
108+
109+
return self.toTuple() == other.toTuple()
110+
103111
def toTuple(self) -> Tuple[float, float, float, float]:
104112
"""
105113
Convert Color to RGB tuple.

tests/test_assembly.py

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
)
1717
from cadquery.occ_impl.assembly import toJSON, toCAF, toFusedCAF
1818
from cadquery.occ_impl.shapes import Face, box
19-
from cadquery.occ_impl.geom import Location
2019

2120
from OCP.gp import gp_XYZ
2221
from OCP.TDocStd import TDocStd_Document
@@ -399,7 +398,6 @@ def get_doc_nodes(doc, leaf=False):
399398

400399
name_att = TDataStd_Name()
401400
label.FindAttribute(TDataStd_Name.GetID_s(), name_att)
402-
name = TCollection_ExtendedString(name_att.Get()).ToExtString()
403401

404402
color = style.GetColorSurfRGBA()
405403
shape = expl.FindShapeFromPathId_s(doc, node.Id)
@@ -412,7 +410,6 @@ def get_doc_nodes(doc, leaf=False):
412410
faces = []
413411
if not node.IsAssembly:
414412
it = TDF_ChildIterator(label)
415-
i = 0
416413
while it.More():
417414
child = it.Value()
418415
child_shape = tool.GetShape_s(child)
@@ -916,7 +913,7 @@ def check_nodes(doc, expected):
916913
"boxes0_assy",
917914
[
918915
(["box0", "box0_part"], {"color_shape": (1.0, 0.0, 0.0, 1.0)}),
919-
(["box1", "box1_part"], {"color_shape": (1.0, 0.0, 0.0, 1.0)}),
916+
(["box1", "box0_part"], {"color_shape": (1.0, 0.0, 0.0, 1.0)}),
920917
],
921918
),
922919
(
@@ -983,15 +980,15 @@ def check_nodes(doc, expected):
983980
{"color_shape": (1.0, 0.0, 0.0, 1.0)},
984981
),
985982
(
986-
["chassis", "wheel-axle-front", "wheel:right", "wheel:right_part"],
983+
["chassis", "wheel-axle-front", "wheel:right", "wheel:left_part"],
987984
{"color_shape": (1.0, 0.0, 0.0, 1.0)},
988985
),
989986
(
990987
["chassis", "wheel-axle-rear", "wheel:left", "wheel:left_part"],
991988
{"color_shape": (1.0, 0.0, 0.0, 1.0)},
992989
),
993990
(
994-
["chassis", "wheel-axle-rear", "wheel:right", "wheel:right_part"],
991+
["chassis", "wheel-axle-rear", "wheel:right", "wheel:left_part"],
995992
{"color_shape": (1.0, 0.0, 0.0, 1.0)},
996993
),
997994
(
@@ -1685,3 +1682,25 @@ def test_order_of_transform():
16851682
m1, m2 = assy2.toCompound().Solids()
16861683

16871684
assert (m1.Center() - m2.Center()).Length == approx(0)
1685+
1686+
1687+
def test_step_export_filesize(tmpdir):
1688+
"""A sanity check of STEP file size.
1689+
Multiple instances of a shape with same color is not expected to result
1690+
in significant file size increase.
1691+
"""
1692+
part = box(1, 1, 1)
1693+
N = 10
1694+
filesize = {}
1695+
1696+
for i, color in enumerate((None, cq.Color("red"))):
1697+
assy = cq.Assembly()
1698+
for j in range(1, N + 1):
1699+
assy.add(
1700+
part, name=f"part{j}", loc=cq.Location(x=j * 1), color=copy.copy(color)
1701+
)
1702+
stepfile = Path(tmpdir, f"assy_step_filesize{i}.step")
1703+
assy.export(str(stepfile))
1704+
filesize[i] = stepfile.stat().st_size
1705+
1706+
assert filesize[1] < 1.2 * filesize[0]

0 commit comments

Comments
 (0)