CadQuery / cadquery

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

metadata Is Not Preserved When Adding a Sub-Assembly #1321

Closed jmwright closed 1 year ago

jmwright commented 1 year ago

In this section of code, metadata is not copied to a sub-assembly when it is added to a main assembly, is this intentional?

Sample code to reproduce:

import cadquery as cq

# Sub-assembly
sub_assy = cq.Assembly()
sub_assy.add(cq.Workplane().box(10, 10, 10), color=cq.Color(1, 0, 0, 1))
sub_assy.add(cq.Workplane().cylinder(10, 20), loc=cq.Location((0, 25, 0)), color=cq.Color(0, 1, 0, 1))

# Main assembly
assy = cq.Assembly()
assy.add(cq.Workplane().box(5, 5, 5), loc=cq.Location((0, 47.5, 0)), color=cq.Color(0, 0, 1, 1), metadata={"mykey": 10})
assy.add(sub_assy, metadata={"mykey_2": 20})

# Output the metadata from all the assembly children
for child in assy.children:
    log(child.metadata)

show_object(assy)

Output (CQ-editor console):

[2023-04-24 15:27:43.048934] INFO: Generic: {'mykey': 10}
[2023-04-24 15:27:43.049059] INFO: Generic: {}

Expected Output:

[2023-04-24 15:27:43.048934] INFO: Generic: {'mykey': 10}
[2023-04-24 15:27:43.049059] INFO: Generic: {'mykey_2': 20}
adam-urbanczyk commented 1 year ago

AFAICT your code should not pass static analysis - there is no add overload with a metadata argument.

So far metadata is a placeholder for future use. Do you have a nice use case?

jmwright commented 1 year ago

So far metadata is a placeholder for future use. Do you have a nice use case?

I'm working on exploded assembly views, and I am storing a relative translation for each part in the metadata. I don't want automatic explode translations, I want to be able to control the resulting view.

lorenzncode commented 1 year ago

workaround:

# Sub-assembly
sub_assy = cq.Assembly(metadata={"mykey_2": 20})  # add metadata here

+1 for support in add method too.