cancel
Showing results for 
Search instead for 
Did you mean: 

Editing component name

Builder
Builder

If you a big assembly with a particular component throughout the assembly at al levels. Is there a way to set the Component name so that it reads the same throught the entire assembly?

1 REPLY

Re: Editing component name

Legend
Legend

Here is the code in C#.

 

using System;
using NXOpen;
using NXOpen.Assemblies;
using NXOpen.UF;



class renameSelectedComponentAllLevels
{
    static Session s = Session.GetSession();
    static UFSession ufs = UFSession.GetUFSession();
    static ListingWindow lw = s.ListingWindow;

    public static void Main()
    {

        lw.Open();
        Component root = s.Parts.Display.ComponentAssembly.RootComponent;

        Component theComponent = null;
        string newName;

        theComponent = SelectAComponent("Select Component to Rename");
        if (theComponent != null)
        {
            newName = theComponent.Name;

            if (getStringInput("New Name", ref newName))
            {
                renameComponentChildren(root, theComponent, newName);
            }
        }

    }

    public static void renameComponentChildren(Component comp, Component theComponent, string newName)
    {

        foreach (Component child in comp.GetChildren())
        {

            if (child.Prototype == theComponent.Prototype)
                child.SetName(newName);

            renameComponentChildren(child, theComponent, newName );
        }
    }

    public static NXOpen.Assemblies.Component SelectAComponent(string prompt)
    {
        TaggedObject selobj = null;
        Point3d cursor = default(Point3d);
        Selection.MaskTriple[] comps = { new Selection.MaskTriple(UFConstants.UF_component_type, 0, 0) };

        Selection.Response resp = UI.GetUI().SelectionManager.SelectTaggedObject(prompt, "Select a component",
            Selection.SelectionScope.AnyInAssembly, Selection.SelectionAction.ClearAndEnableSpecific, false, false, comps, out selobj, out cursor);

        return (NXOpen.Assemblies.Component)selobj;
    }

    static bool getStringInput(string prompt, ref string inputString)
    {
        int n = 0;
        int resp;

        NXOpen.UF.UFSession ufs = UFSession.GetUFSession();

        ufs.Ui.LockUgAccess(UFConstants.UF_UI_FROM_CUSTOM);

        resp = ufs.Ui.AskStringInput(prompt, ref inputString, out n);

        ufs.Ui.UnlockUgAccess(UFConstants.UF_UI_FROM_CUSTOM);

        if (resp == 3 || resp == 5)
            return true;
        else
            return false;

    }

    public static int GetUnloadOption(string dummy)
    {
        return (int)Session.LibraryUnloadOption.Immediately;
    }

}