Clone a part

I'm trying to create a simple program to clone a part and append some text to the file name. From what I've been able to put together from the chm files and what I have found on google, this is what I have so far.

I keep getting an error on the line that sets the name rule. The error is "Cannot perform runtime binding on a null reference..."

This is what I have so far:

Option Strict Off
Imports System
Imports NXOpen
Imports NXOpenUI
Imports NXOpen.UF

Module Clone
Public Sub Main()
Dim ufs As UFSession = UFSession.GetUFSession()
Dim theSession As Session = Session.GetSession()
Dim workPart As Part = theSession.Parts.Work

Dim displayPart As Part = theSession.Parts.Display
Dim lw As ListingWindow = theSession.ListingWindow
Dim myAsm As String = displayPart.FullPath
Dim myClone As NXOpen.UF.UFClone = ufs.Clone

lw.Open()
lw.WriteLine("Assembly to Clone: " & myAsm)
myClone.Terminate()
myClone.Initialise(NXOpen.UF.UFClone.OperationClass.CloneOperation)
myClone.SetDefNaming(UFClone.NamingTechnique.NamingRule)
myClone.SetDefDirectory("C:\NewFolder")
myClone.AddAssembly(myAsm, Nothing)

Dim myNameRule As NXOpen.UF.UFClone.NameRuleDef
myNameRule.type = UFClone.NameRuleType.AppendString
myNameRule.base_string = ""
myNameRule.new_string = "_AppendedText"

myClone.SetNameRule(myNameRule, Nothing) 'This is the line that gives me the error
myClone.SetDryrun(False)
myClone.PerformClone(Nothing)
myClone.Terminate()
End Sub
End Module

I'm not really sure why I am getting the error that I am getting. Any thoughts would be much appreciated! Thanks.

Try

Dim myNamingFailures as UFClone.NamingFailures
'initialize a NamingFailure structure.
'It should be called before passing the structure to ...
myClone.InitNamingFailures(myNamingFailures)
myClone.SetNameRule(myNameRule, myNamingFailures)

Thanks kam. Works perfectly now. :)

crashkey

Hi,
I have Initialized myNamingFailures in below code and able to clone in local but not in TeamCenter,
can you please help on how to clone from team center to local drive, thanks in advance

Imports NXOpen
Imports NXOpen.UF
Imports NXOpen.Assemblies
Imports NXOpen.Features
Imports NXOpen.Annotations

Module Module1

Public myFeature1 As Feature

Sub Main()

Dim theSession As NXOpen.Session = NXOpen.Session.GetSession()
Dim workPart As NXOpen.Part = theSession.Parts.Work
Dim displayPart As NXOpen.Part = theSession.Parts.Display
Dim lw As ListingWindow = theSession.ListingWindow

Dim ufs As UFSession = UFSession.GetUFSession()
Dim myAsm As String = displayPart.FullPath
Dim myClone As NXOpen.UF.UFClone = ufs.Clone

lw.Open()
lw.WriteLine("Assembly to Clone: " & myAsm)
myClone.Terminate()
myClone.Initialise(NXOpen.UF.UFClone.OperationClass.CloneOperation)
myClone.SetDefNaming(UFClone.NamingTechnique.NamingRule)
myClone.SetDefDirectory("C:\Users\218156\Desktop\Chesklist_update\")
myClone.AddAssembly(myAsm, Nothing)

Dim myNameRule As NXOpen.UF.UFClone.NameRuleDef
myNameRule.type = UFClone.NameRuleType.AppendString
myNameRule.base_string = ""
myNameRule.new_string = "_AppendedText"

Dim myNamingFailures As UFClone.NamingFailures
'initialize a NamingFailure structure.

Dim i As Integer = myNamingFailures.n_failures
Dim j As Integer() = myNamingFailures.statuses
Dim a As String() = myNamingFailures.input_names
Dim b As String() = myNamingFailures.output_names

'It should be called before passing the structure to ...
myClone.InitNamingFailures(myNamingFailures)
myClone.SetNameRule(myNameRule, myNamingFailures)

'myClone.SetNameRule(myNameRule, Nothing) 'This is the line that gives me the error
myClone.SetDryrun(False)
myClone.PerformClone(Nothing)
myClone.Terminate()

End Sub

End Module