cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - You can change your system assigned username to something more personal in your community settings. X

Translate the entire conversation x

Delete component from assembly

jbryant74
12-Amethyst

Delete component from assembly

Help to see what I am missing. 
I want to find the first component in the assembly and delete it.
I can find the first component, but having trouble deleting.

Here is my run method

public void run() throws jxthrowable, IOException {
        Session session = pfcSession.GetCurrentSession();
        Model model = session.GetActiveModel();

        if (model == null) {
            return;
        }

        if (model.GetType() == ModelType.MDL_ASSEMBLY) {
            Assembly assembly = (Assembly) model;

            Features components = assembly.ListFeaturesByType(false, FeatureType.FEATTYPE_COMPONENT);

            if (components.getarraysize() > 0) {
                try {
                    // --- Find the first component in the model tree
                    ComponentFeat targetComponent = null;

                    // Get all features in order.
                    Features allFeatures = assembly.ListFeaturesByType(false, null);

                    // Find the first component feature in the feature tree
                    for (int i = 0; i < allFeatures.getarraysize(); i++) {
                        Feature feat = allFeatures.get(i);
                        if (feat instanceof ComponentFeat) {
                            targetComponent = (ComponentFeat) feat;
                            break; // Stop at the first component found
                        }
                    }

                    ModelDescriptor desc = targetComponent.GetModelDescr();
                    
                    FeatureOperations featOps = FeatureOperations.create();
                    featOps.append(targetComponent.CreateDeleteOp());
                    assembly.ExecuteFeatureOps(featOps, null);
                    assembly.Regenerate(null);

                    session.GetModelWindow(assembly).Repaint();

                } catch (jxthrowable e) {
                    System.out.println("Error during component deletion: " + e.getMessage());
                }
            } else {
                System.out.println("No components to delete.");
            }
        } else {
            System.out.println("The active model is not an assembly.");
        }
    }
ACCEPTED SOLUTION

Accepted Solutions
RandyJones
20-Turquoise
(To:jbryant74)
2 REPLIES 2
RandyJones
20-Turquoise
(To:jbryant74)

Thanks for this info.

Announcements

Top Tags