diff --git a/cadquery/occ_impl/shapes.py b/cadquery/occ_impl/shapes.py index 40dd9dccf..14309d2bf 100644 --- a/cadquery/occ_impl/shapes.py +++ b/cadquery/occ_impl/shapes.py @@ -1895,7 +1895,8 @@ def __setstate__(self, data: tuple[BytesIO, bool]) -> None: BinTools.Read_s(wrapped, data[0]) - self.wrapped = wrapped + # NB downcast raises on null shapes + self.wrapped = wrapped if wrapped.IsNull() else downcast(wrapped) self.forConstruction = data[1] def replace(self, old: Shape, *new: Shape) -> Self: diff --git a/tests/test_pickle.py b/tests/test_pickle.py index c0f25c167..3c85a16c8 100644 --- a/tests/test_pickle.py +++ b/tests/test_pickle.py @@ -1,3 +1,4 @@ +from copy import copy, deepcopy from pickle import loads, dumps from cadquery import ( @@ -41,6 +42,34 @@ def test_shape(): assert isinstance(loads(dumps(s)), Shape) +@mark.parametrize( + "copier", + [copy, deepcopy, lambda obj: loads(dumps(obj))], + ids=["copy", "deepcopy", "pickle"], +) +def test_shape_type(copier): + """ + Copying or unpickling a shape keeps its specialized TopoDS type + (e.g. TopoDS_Solid), as constructing it does. + """ + + s = box(1, 1, 1) + + for obj in (s, s.Faces()[0], s.Edges()[0], s.Vertices()[0]): + assert type(copier(obj).wrapped) is type(obj.wrapped) + + +def test_null_shape(): + """ + Unpickling a null shape keeps it null rather than raising. + """ + + s = box(1, 1, 1) + s.wrapped.Nullify() + + assert loads(dumps(s)).wrapped.IsNull() + + def test_assy(): mat_1 = Material(