Removing a part from an assembly

Hello,

I am writing a program that is supposed to read a Step file of an assembly, remove a part from that assembly and then write separate Step files for the removed part and the original assembly but with that part removed.

I use the following code snippet to remove the part from the original assembly:

if(!ShapeTool->RemoveShape(removeL, false)){
  ShapeTool->RemoveComponent(removeL);
}
ShapeTool->UpdateAssemblies();

removeL is the label of the location of the part in the assembly.
When I write the document to a Step file however, it is still the entire assembly without the part being removed.

I have verified that ShapeTool->GetShape(removeL) is the correct shape that I want.

I would greatly appreciate any help. Thank you!

Best regards, Kaiwen

Dmitrii Pasukhin's picture

Hello,

Removing or replacing parts are popular CAD operations. For a part (a label with just a location and reference to a simple shape), you cannot call "RemoveShape". You should only call "RemoveComponent". By doing this, you will remove each attribute in the label and unlink the reference. The original shape will continue to exist. Additionally, when all references are removed, the original shape will become a free shape. You will need to remove the original shape as soon as you remove each reference to it.

If you need more information, please let me know.

Best regards, Dmitrii.

Kaiwen He's picture

Thank you for your reply,

I tried only calling "RemoveComponent" on the part label, then removing the shape afterwards as such:

ShapeTool->RemoveComponent(removeL);
TDF_Label shapeLabel = ShapeTool->FindShape(ShapeTool->GetShape(removeL));
ShapeTool->RemoveShape(shapeLabel);
ShapeTool->UpdateAssemblies();

Then I write the document to Step as such:

STEPCAFControl_Writer Writer;
Writer.Transfer(doc);
Writer.Write("restAssembly.stp");

But the Step file still shows the entire original Assembly, not the updated one.
Am I using the wrong label or is something wrong with the above code?

Kaiwen He's picture

Hello,

I have come to a solution that seemingly works:

removeL.ForgetAllAttributes();
ShapeTool->UpdateAssemblies();

Is this a valid solution? Do I need to do some cleanup to the document so it does not lead to some undefined behavior?