Select part by name for replacement

Hello everybody,

I currently working on a configurator and have to replace some parts depending on some expressions. My code so far reads the expressions and chooses the right replacement parts. But at the moment I can only select the part to replace by its filename, which is useless because the file name changes as soon as I replace the part. The name of the part in the assembly stays always the same, so I searching for a possibility to replace a specific part by this constant name.

For example a simple replacement code:

Option Strict Off
Imports System
Imports NXOpen

Module NXJournal
Sub Main (ByVal args() As String)

Dim theSession As NXOpen.Session = NXOpen.Session.GetSession()
Dim workPart As NXOpen.Part = theSession.Parts.Work

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

replaceComponentBuilder1.ComponentNameType = NXOpen.Assemblies.ReplaceComponentBuilder.ComponentNameOption.AsSpecified

Dim component1 As NXOpen.Assemblies.Component = CType(workPart.ComponentAssembly.RootComponent.FindObject("COMPONENT 204670292 1"), NXOpen.Assemblies.Component)

Dim added1 As Boolean
added1 = replaceComponentBuilder1.ComponentsToReplace.Add(component1)

replaceComponentBuilder1.ReplacementPart = "G:.....\....\example.prt"

replaceComponentBuilder1.SetComponentReferenceSetType(NXOpen.Assemblies.ReplaceComponentBuilder.ComponentReferenceSet.Maintain, Nothing)

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

replaceComponentBuilder1.Destroy()

End Sub
End Module

In the line where the component1 gets specified is written: ...FindObject("COMPONENT 204670292 1")...
So the component1 is defined by the filename, how do I have to modify the code that the component1 gets defined by its name in the assembly?
I tried a lot of codes and now I'm really running out of ideas, does someone know to solve my problem?

thanks in advance
Lorenz

"searching for a possibility to replace a specific part by this constant name"

It sounds like you are looking for a component with a specific "component name". If so, you can iterate through the assembly looking for the component with the desired name. However, component names do not have to be unique, so you may need a way to verify that you have the correct one.

The following article shows how to iterate through the assembly components; you can check the .Name property of each to find the one you want.

http://nxjournaling.com/content/creating-subroutine-process-all-componen...

thank you for the quick reply!

I tried to modify the code for my application. I tried to modify the the if-else-code to find only the component with the Name in the assembly. So insted of:

...
if child.GetChildren.Length <> 0 then
...

i wrote:

...
if child.GetChildren.Name = "SHAFT" then
...

but when I want to run the code NX Returns: "Name" is not a Member of "System.Array"

I cant find the mistake in it.

Thanks in advance

Check the component's .Name property. The .GetChildren method returns an array of components contained by the given component. An array of components does not have a .Name property. You can check the component name with code similar to this:

For Each child As Component In comp.GetChildren()
If child.Name = "some name" Then
'do something with this component
End If
.
.
.
Next