Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations IDS on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

NXOPEN Replace Component Automation

Status
Not open for further replies.

TROV-103

New member
Jan 12, 2021
1
Hi everyone,

I am trying to automate a script using Visual Basic to read a component name from an excel file (column 1), then in NX, replace that component with another component reading the name in column 2 of the excel file. The reason for this automation is to replace labels rapidly whiteout having to do one by one because there can be up to 100 labels in an assembly. No constraints to be maintained, just position. I am working in NX12. I started with a journal but it gets tricky as I have never used VB before. I can attach my code thus far. Any help is much appreciated.

Also would like to add an If statement that if the condition where column 2 is blank, the part in column 1 is deleted and no replaced.

Please le me know if this is even possible. Thanks in advance
 
Replies continue below

Recommended for you

Hi
Are you using Native NX or Teamcenter. If it is Native NX are all the part files in same directory?
 
Hello,

I try help, but mayby ​​my help is not very usefull.
"Please le me know if this is even possible" - YES it's possible.
I wrote journal for change selected components in assembly.
It is specyfic for my company and if I will share all code, I think will be not usefull for You.
But I can share functions for replace components.
For native: (Replacing_object is full path for replacing file)
Code:
Public Sub Replace (ByVal Replace_Object As NXOpen.Assemblies.Component, Replacing_object As String)

        Dim replaceComponentBuilder1 As NXOpen.Assemblies.ReplaceComponentBuilder
        replaceComponentBuilder1 = workPart.AssemblyManager.CreateReplaceComponentBuilder ()
        replaceComponentBuilder1.ComponentNameType = NXOpen.Assemblies.ReplaceComponentBuilder.ComponentNameOption.AsSpecified

        Dim component As NXOpen.Assemblies.Component = CType (Replace_Object, NXOpen.Assemblies.Component)
        Dim added1 As Boolean
        added1 = replaceComponentBuilder1.ComponentsToReplace.Add (component)
        replaceComponentBuilder1.ReplacementPart = Replacing_object 'the item it replaces
        replaceComponentBuilder1.SetComponentReferenceSetType (NXOpen.Assemblies.ReplaceComponentBuilder.ComponentReferenceSet.Maintain, Nothing)

        Dim nXObject As NXOpen.NXObject
        nXObject = replaceComponentBuilder1.Commit ()

        replaceComponentBuilder1.Destroy ()

    End Sub

For TC (name is name of Replacing file, but this file have always revision "A"):
Code:
Sub ReplaceTC (component1 As NXOpen.Assemblies.Component, name As String)

        Dim replaceComponentBuilder1 As NXOpen.Assemblies.ReplaceComponentBuilder = Nothing
        replaceComponentBuilder1 = workPart.AssemblyManager.CreateReplaceComponentBuilder ()
        replaceComponentBuilder1.ComponentNameType = NXOpen.Assemblies.ReplaceComponentBuilder.ComponentNameOption.AsSpecified
        Dim added1 As Boolean = Nothing
        added1 = replaceComponentBuilder1.ComponentsToReplace.Add (component1)
        replaceComponentBuilder1.ComponentName = name

        replaceComponentBuilder1.ReplacementPart = "@ DB /" & name & "/ A"
        replaceComponentBuilder1.SetComponentReferenceSetType (NXOpen.Assemblies.ReplaceComponentBuilder.ComponentReferenceSet.Maintain, Nothing)
        Dim partLoadStatus1 As NXOpen.PartLoadStatus = Nothing
        partLoadStatus1 = replaceComponentBuilder1.RegisterReplacePartLoadStatus ()
        Dim nXObject1 As NXOpen.NXObject = Nothing
        nXObject1 = replaceComponentBuilder1.Commit ()
        partLoadStatus1.Dispose ()
        replaceComponentBuilder1.Destroy ()
End Sub

If You want select component from assembly to replace You can use:
Code:
For Each child As Component In c.RootComponent.GetChildren ()
            If (child.Name) = "your_name" Then
                mySelection.Add (child)
            End If
And after use:
Code:
For Each myComponent as Assembly.Component in mySelection
Replace (myComponent, full_path_of_replacing_file)
Next


I was wrote this journal some time ago and I don't remember all dependencies, but if You need help - ask - I try help You.

Best regards
MANok
NX12
TC10
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor