-
Notifications
You must be signed in to change notification settings - Fork 295
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to access individual objects in a Compound object? #1715
Comments
You can access individual solid letters with iteration or unpacking.: letter_o = [*text3D.val()][4] or with selectors: letter_nearest_point = text3D.val().solids(
cq.selectors.NearestToPointSelector((0, 0, 0))
) There are several ways to access the solids. It depends on your criteria for selection. letters_by_vol = text3D.solids().sort(cq.Shape.Volume)
letter_largest_vol = letters_by_vol[-1] |
@lorenzncode Thank you for your help. These methods are able to access the individual solid in the text3D Compound; however, we want to manipulate the individual solid which then affect the original text3D Compound. For example, we want to translate the letter "O" 100 in Z direction, and then we could get the changed text3D object with "O" letter translated 100 in Z direction. Can we achieve this effect with CadQuery?
|
Currently a copy is made (I think in OCP, TBC). You'd need to recreate the compoud: letters = list(text3D)
letters[0].move(z=10)
text3D = compound(letters) |
That's exactly what we want. The code you provided could run as expected when the Compound is directly made by our own code; However, when the Compound object is imported from a STEP file (saved from our own code), then, we can not unpack the Compound object. The following is the MVP code. The STEP file "text_Hello_CadQuery_3D_asm.step" is saved from the commented code. We are working on an exploding function, and we need to access and manipulate both the Compound and the objects in the Compound.
|
Try flattening the compound, something like this: def flatten(obj):
if not isinstance(obj, Compound):
yield obj
else:
for child in obj:
yield from flatten(child)
print(len(list(flatten(text_Hello_CadQuery_3D)))) # 16 |
@lorenzncode Thank you for your help. The flatten function has partially solved the problem. However, the flatten function can not keep the feature tree's hierarchical structure. Right now, the flatten function could be my first stage solution. Thank you for both of you, @adam-urbanczyk and @lorenzncode. |
We want to access individual letter in the text3D Compound, and we've found there is a function called siblings(self, shape: "Shape", kind: Shapes, level: int = 1). We wonder how to call the siblings function properly??//
The text was updated successfully, but these errors were encountered: