gumyr / cq_warehouse

A cadquery parametric part collection
Apache License 2.0
107 stars 23 forks source link

Importing cq_warehouse.extensions breaks Assembly.add() for multiple instances with color argument #83

Closed anderson-pa closed 10 months ago

anderson-pa commented 10 months ago

This code (based on an official CadQuery example) works as expected with cq v2.3.1 and viewing in the lastest cq-editor (v0.3.0dev):

import cadquery as cq
#import cq_warehouse.extensions

cone = cq.Solid.makeCone(1, 0, 2)

assy = cq.Assembly()
assy.add(cone, loc=cq.Location((0, 0, 1)), name='test',
         color=cq.Color('blue'), 
         )
assy.add(cone,
         color=cq.Color('green'), 
         )

show_object(assy)

If the cq_warehouse.extensions import is uncommented, the example breaks with the following traceback:

File "\~/.local/lib/python3.10/site-packages/cq_editor/widgets/object_tree.py", line 256, in addObjects ais,shape_display = make_AIS(obj.shape,obj.options) File "\~/.local/lib/python3.10/site-packages/cq_editor/cq_utils.py", line 63, in make_AIS label, shape = toCAF(obj) File "\~/.local/lib/python3.10/site-packages/cadquery/occ_impl/assembly.py", line 221, in toCAF top = _toCAF(assy, None, None) File "\~/.local/lib/python3.10/site-packages/cadquery/occ_impl/assembly.py", line 212, in _toCAF _toCAF(child, subassy, current_color) File "\~/.local/lib/python3.10/site-packages/cadquery/occ_impl/assembly.py", line 188, in _toCAF compound = compounds[key1].copy(mesh) TypeError: shape_copy() takes 1 positional argument but 2 were given

Commenting out the color arguments for both calls to Assembly.add() seems to work still. Not seeing the same issue with the name argument. Also, since the export is persistent, cq-editor must be restarted to get back to a clean state. This is using cq_warehouse v0.8.0. Have not been able to verify if the latest dev version has the same issue as it is not installing for me correctly (still investigating if this is a me issue or not). Running Ubuntu 22.04.3 LTS and Python 3.10.

anderson-pa commented 10 months ago

Was able to fix by modifying shape_copy() in extensions.py to match the signature I found in cadquery's occ_impl/shapes.py. Namely, added mesh: bool = False as an argument, and then passed in the additional arguments True, mesh to the BRepBuilderAPI_Copy call.

def shape_copy(self: "Shape", mesh: bool = False) -> "Shape":
    """
    Creates a new object that is a copy of this object.
    """
    # The wrapped object is a OCCT TopoDS_Shape which can't be pickled or copied
    # with the standard python copy/deepcopy, so create a deepcopy 'memo' with this
    # value already copied which causes deepcopy to skip it.
    memo = {id(self.wrapped): downcast(BRepBuilderAPI_Copy(self.wrapped, True, mesh).Shape())}
    copy_of_shape = copy.deepcopy(self, memo)
    return copy_of_shape

Can fork and submit a PR if desired.

gumyr commented 10 months ago

I would appreciate the PR - thanks in advance.

anderson-pa commented 10 months ago

Just submitted, let me know if anything else I can do.

Thanks for this awesome package, really helpful to have this set of additions to cadquery. Also excited to discover build123d.

gumyr commented 10 months ago

Thanks for identifying an fixing this problem. I spend all my time on build123d these days so cq_warehouse doesn't get much attention.