CadQuery / cadquery

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

How to create an array of Workplane objects #1542

Closed GaN-T closed 3 months ago

GaN-T commented 3 months ago

I am trying to create an array from an imported model (.step). How do I do this?

base = cq.importers.importStep("myModel.step")
rarray = cq.Workplane("front").rarray(19,19,3,3,False)   # points on which I would like the copies of the model to be centered on
GaN-T commented 3 months ago

my current solution is as follows. But is there a more direct way?

merged = base
for loc in rarray.vals():
    merged += base.translate(loc)
michaelgale commented 3 months ago

This alternative also works:

merged = (
    cq.Workplane("front")
    .rarray(19, 19, 3, 3, False)
    .eachpoint(lambda loc: base.val().located(loc), combine=True)
)

Your solution is also valid and it is likely the underlying geometric representation is the same, i.e. the union of multiple solids into a composite compound.

GaN-T commented 3 months ago

Your method looks better. Will look into eachpoint() and located(). I wasn't aware of those functions. Thank you for the suggestion :)