paramak icon indicating copy to clipboard operation
paramak copied to clipboard

Removing part from assembly by name

Open shimwell opened this issue 1 year ago • 0 comments

cadquery.Assemby objects get returned by the functions and these include the plasma

Some use cases don't require the plasma (e.g. meshing for neutronics model) so it would be useful to be able to remove parts from the reactor

In the longer term cadquery assembies might have a .remove method but for now we can make a new assembly and filter by part name

This example makes an assembly with a box and sphere, then makes a new assembly without the box

import warnings
class Assembly(cq.Assembly):
    def remove(self, name:str):
        new_assembly = Assembly()
        part_found=False
        for part in self:
            if part[1].endswith(f'/{name}'):
                part_found = True
                print('removing' , part)
            else:
                print('adding' , part)
                new_assembly.add(part[0], name=part[1], color=part[3], loc=part[2])
        if part_found == False:
            warnings.warn(f'Part with name {name} not found')
        print()
        return new_assembly

sphere1 = cq.Workplane().moveTo(2, 2).sphere(1)
box1 = cq.Workplane().box(1, 1, 1)
assembly = Assembly()
assembly.add(box1, name="box1", color=cq.Color(0.5, 0.5, 0.5))
assembly.add(sphere1, name="sphere")


assembly2 = assembly.remove('sphere')
assembly3 = assembly.remove('box1')
assembly4 = assembly.remove('bosdfsdf')

Alternatively we could add an argument include_plasma which would avoid monkey patching the class.

shimwell avatar Dec 13 '24 17:12 shimwell