How to get component after inserting part

Hi all,

I'm trying to create a subroutine that inserts a part into an assembly using the "AddPartToAssembly" ufunc. The sub will insert the part just fine. However, I need to return the Component that is created upon insertion. As I understand it, the 2nd from the last parameter should be the tag of the component. Either I'm misunderstanding what that really is or am having trouble converting from Tag to Component. The line where the sub crashes is:
NewComponent = CType(taggedObj1, Component)

Here is my subroutine:

'***************************************************************************************
'// partDest: part where you want to insert the new component
'// strPartFnPath: the file name and path to the part being inserted
'// strComponentName: the name assigned to the new component
'// the component created by the insertion.
'***************************************************************************************
Public Shared Sub InsertPartAtOrigin(ByVal partDest As Part,
ByVal strPartFnPath As String,
ByVal strComponentName As String,
ByRef NewComponent As Component)

Dim dblCsysMatrix(8) As Double
Dim dblXVec(2) As Double
Dim dblYVec(2) As Double

Dim tagCsysMatrix As NXOpen.Tag
Dim tagCsys As NXOpen.Tag

Dim tagNewComp As NXOpen.Tag
Dim layer As Integer = -1 ' Original Layer
Dim LoadStatus As NXOpen.UF.UFPart.LoadStatus
Dim tagParentPart As NXOpen.Tag

Dim strNewRefSetName As String = ""

'-------------------------------------------------------
'-- Define position matrix
'-------------------------------------------------------
Dim dblInputMatrix(5) As Double
dblInputMatrix(0) = 1
dblInputMatrix(1) = 0
dblInputMatrix(2) = 0
dblInputMatrix(3) = 0
dblInputMatrix(4) = 1
dblInputMatrix(5) = 0

'// Note: the instance name will show up as "Component Name" in the assembly browser
Dim strInstanceName As String = strComponentName
Dim dblCompOrigin(2) As Double
dblCompOrigin(0) = 0
dblCompOrigin(1) = 0
dblCompOrigin(2) = 0

Try
Gvars.m_ufs.Assem.AddPartToAssembly(partDest.Tag,
strPartFnPath,
strNewRefSetName,
strInstanceName,
dblCompOrigin,
dblInputMatrix,
layer,
tagNewComp,
LoadStatus)

'---------------------------------------------------
'// get the part and return it
'---------------------------------------------------
Dim taggedObj1 As NXOpen.TaggedObject = NXOpen.Utilities.NXObjectManager.Get(tagNewComp)
NewComponent = CType(taggedObj1, Component)

Catch ex As Exception
MsgBox("insert failed")
End Try

End Sub

I'm curious why you are using the UF functions as opposed to the newer "add component builder"?

If you must use the UF functions, try the code below:

'***************************************************************************************
'// partDest: part where you want to insert the new component
'// strPartFnPath: the file name and path to the part being inserted
'// strComponentName: the name assigned to the new component
'// the component created by the insertion.
'***************************************************************************************
Sub InsertPartAtOrigin(ByVal partDest As Part,
ByVal strPartFnPath As String,
ByVal strComponentName As String,
ByRef NewComponent As Assemblies.Component)

Dim dblCsysMatrix(8) As Double
Dim dblXVec(2) As Double
Dim dblYVec(2) As Double

Dim tagCsysMatrix As NXOpen.Tag
Dim tagCsys As NXOpen.Tag

Dim tagNewInst As NXOpen.Tag
Dim layer As Integer = -1 ' Original Layer
Dim LoadStatus As NXOpen.UF.UFPart.LoadStatus
Dim tagParentPart As NXOpen.Tag

Dim strNewRefSetName As String = ""

'-------------------------------------------------------
'-- Define position matrix
'-------------------------------------------------------
Dim dblInputMatrix(5) As Double
dblInputMatrix(0) = 1
dblInputMatrix(1) = 0
dblInputMatrix(2) = 0
dblInputMatrix(3) = 0
dblInputMatrix(4) = 1
dblInputMatrix(5) = 0

'// Note: the instance name will show up as "Component Name" in the assembly browser
Dim strInstanceName As String = strComponentName
Dim dblCompOrigin(2) As Double
dblCompOrigin(0) = 0
dblCompOrigin(1) = 0
dblCompOrigin(2) = 0

Try
Gvars.m_ufs.Assem.AddPartToAssembly(partDest.Tag,
strPartFnPath,
strNewRefSetName,
strInstanceName,
dblCompOrigin,
dblInputMatrix,
layer,
tagNewInst,
LoadStatus)

'---------------------------------------------------
'// get the part and return it
'---------------------------------------------------
Dim newCompTag As Tag = Tag.Null
newCompTag = Gvars.m_ufs.Assem.AskPartOccOfInst(Gvars.m_ufs.Assem.AskRootPartOcc(partDest.Tag), tagNewInst)
NewComponent = NXOpen.Utilities.NXObjectManager.Get(newCompTag)

Catch ex As Exception
MsgBox("error: " & ex.Message)
End Try

End Sub

Thank you! that did the trick. I have posted the completed sub below for anyone else that may need it. As for why I'm using the UF Function, I already had it from an older program I had written. I did look into using the newer Add component builder via a recorded journal but it looked very messy (as most journals do) and I was in a hurry. I do plan to develop a subroutine that uses the newer Add Component method I just need to move forward with my current project at this time.

'***************************************************************************************
'// partDest: part where you want to insert the new component
'// strPartFnPath: the file name and path to the part being inserted
'// strComponentName: the name assigned to the new component
'// the component created by the insertion.
'***************************************************************************************
Public Shared Sub InsertPartAtOrigin(ByVal partDest As Part,
ByVal strPartFnPath As String,
ByVal strComponentName As String,
ByRef NewComponent As Component)

Dim tagNewInstance As NXOpen.Tag
Dim layer As Integer = -1 ' Original Layer
Dim LoadStatus As NXOpen.UF.UFPart.LoadStatus
Dim strNewRefSetName As String = ""

'-------------------------------------------------------
'-- insert the component
'-------------------------------------------------------
Dim dblInputMatrix(5) As Double
dblInputMatrix(0) = 1
dblInputMatrix(1) = 0
dblInputMatrix(2) = 0
dblInputMatrix(3) = 0
dblInputMatrix(4) = 1
dblInputMatrix(5) = 0

'-------------------------------------------------------
'add the component to the assembly
'-------------------------------------------------------
Dim strInstanceName As String = strComponentName
Dim dblCompOrigin(2) As Double
dblCompOrigin(0) = 0
dblCompOrigin(1) = 0
dblCompOrigin(2) = 0

Try
Gvars.m_ufs.Assem.AddPartToAssembly(partDest.Tag,
strPartFnPath,
strNewRefSetName,
strInstanceName,
dblCompOrigin,
dblInputMatrix,
layer,
tagNewInstance,
LoadStatus)

Catch ex As Exception
MsgBox("Insert failed: " & ex.ToString)
Gvars.lw.WriteLine("AddPartToAssembly failed: " & ex.ToString)
End Try

Try
'-----------------------------------------------------
'// get and return the componet
'-----------------------------------------------------
Dim newCompTag As Tag = NXOpen.Tag.Null
newCompTag = Gvars.m_ufs.Assem.AskPartOccOfInst(Gvars.m_ufs.Assem.AskRootPartOcc(partDest.Tag), tagNewInstance)
NewComponent = NXOpen.Utilities.NXObjectManager.Get(newCompTag)

'-------------------------------------------------------
'// test to see if the NewComponent is nothing
'-------------------------------------------------------
If NewComponent Is Nothing Then
Gvars.lw.WriteLine("## Error the NewComponent is nothing:")
Else
Gvars.lw.WriteLine("It worked! We have the new component: " & NewComponent.Name)
End If

Catch ex As Exception
Gvars.lw.WriteLine("Getting componnent failed: " & ex.ToString)
End Try

End Sub