CadQuery / cadquery

A python parametric CAD scripting framework based on OCCT
https://cadquery.readthedocs.io
Other
3.15k stars 289 forks source link

Workplane.clone() #1643

Open wirekang opened 1 month ago

wirekang commented 1 month ago

I know I can define a function or use w1.translate((0,0,0)) to create a new reference, but it would be great if there is a simple clone method:

w1 = cq.Workplane().box(1,2,3)
w2 = w1.clone()

w2.add(...)
    def clone(self: T) -> T:
        return self.newObject([*self.objects])
adam-urbanczyk commented 1 month ago

What is the use case? I.e. are you after a deep or shallow copy in the end?

wirekang commented 1 month ago

I thought CadQuery is based on immutable objects, since there are chaining methods with description Returns a copy of.... So the use case I thought was like:

left_pin = cq.Workplane()...
right_pin = left_pin.translate((0,0,0)) // Oh this is so hacky
cq.Assembly().add(left_pin).add(right_pin)...

You know this is wrong. They shares many things like CQObject and CQContext. If I understand correctly, newObject has nothing to do with immutable but for parent stack.

So if there is a clone method, it should deep copy everything, but I can't sure this kind of approche is valid in CadQuery.