Setting Workpart To Newly inserted component

As part of a program that will WaveLink some geometry from one part in an assembly into a newly inserted component, I am having issues setting the newly inserted component as the WorkPart. I have attached a complete set of code that asks the user to select a component and inserts an existing prt file into it. It then tries to make the new component the workpart. Any ideas why the new component doesn't highlight after attempting to set it to work part? The wavelinking done later doesn't behave like it is work either.

Note: I wasn't sure If I should have stayed in the previous discussion about getting the component of the inserted file. I broke it out to a new subject for those looking to see how to set an inserted component to the work part.

Public Shared Sub Main(ByVal args() As String)

Gvars.m_ufs.Ui.ExitListingWindow()
Gvars.lw.Open()

'---------------------------------------------------------
'// select host component
'---------------------------------------------------------
Dim compCupCrvUnit As Component
Dim strPrompt As String = "Pick Curve Unit"
If SelectComponent(strPrompt, m_compCupCrvUnit) = Selection.Response.Cancel Then
'// user cancelled the selection
Exit Sub
End If
m_strCurveUnitName = m_compCupCrvUnit.Name
Gvars.lw.WriteLine("after select component. name: " & m_strCurveUnitName)

'-------------------------------------
' insert component
'-------------------------------------
Dim newComp As Component
Dim strNewFnPath As String = "c:\test\TestPart5.prt"
InsertPartAtOrigin(m_compCupCrvUnit.Prototype, strNewFnPath, "test5", newComp)

'-------------------------------------
'// set work part
'-------------------------------------
Try
Dim partLoadStatus1 As NXOpen.PartLoadStatus = Nothing
Gvars.Session.Parts.SetWorkComponent(newComp, NXOpen.PartCollection.RefsetOption.Entire,
NXOpen.PartCollection.WorkComponentOption.Visible, partLoadStatus1)

Dim somepart As Part = Gvars.Session.Parts.Work

Catch ex As Exception
Gvars.lw.WriteLine("#### Error while setting work part to NewCurve start part " & ex.Message)
End Try

'------------------------------------------------
'// get the work part and find out who this is
'------------------------------------------------
Try
Dim tempWorkPart As Part
tempWorkPart = Gvars.Session.Parts.Work
Gvars.lw.WriteLine("workpart name: " & tempWorkPart.Name)
Catch ex As Exception
Gvars.lw.WriteLine("### error getting work part: " & ex.ToString)
End Try

Gvars.lw.WriteLine("I'm expecting the inserted component to highlight as if it is the work part")

End Sub

'***************************************************************************************
'// 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

Public Shared Function SelectComponent(ByVal prompt As String, ByRef oComp As Assemblies.Component) As NXOpen.Selection.Response

Dim mask() As Selection.MaskTriple =
{New Selection.MaskTriple(UFConstants.UF_component_type, 0, 0)}
Dim cursor As Point3d = Nothing
Dim obj As TaggedObject = Nothing

Dim resp As Selection.Response =
UI.GetUI().SelectionManager.SelectTaggedObject(prompt, prompt,
Selection.SelectionScope.AnyInAssembly,
Selection.SelectionAction.ClearAndEnableSpecific,
False, False, mask, obj, cursor)

If resp = Selection.Response.ObjectSelected Or
resp = Selection.Response.ObjectSelectedByName Then
oComp = CType(obj, Assemblies.Component)
Return NXOpen.Selection.Response.Ok
Else
Return NXOpen.Selection.Response.Cancel
End If
End Function

To prove out the method I'm using to set the work part, run the version of the Main below (using the same subs as the first set of code) on the component inserted by the first version of the program and you will see the component highlight. I'm wondering if perhaps the object returned by the selection sub is different from the one returned by the InsertPartAtOrigin sub.

Public Shared Sub Main(ByVal args() As String)

Gvars.m_ufs.Ui.ExitListingWindow()
Gvars.lw.Open()

'---------------------------------------------------------------------------------------
'// select component to prove code to set workpart works when selecting the component
'---------------------------------------------------------------------------------------
Dim newComp As Component
Dim strPrompt As String = "Pick component to make work"
If SelectComponent(strPrompt, newComp) = Selection.Response.Cancel Then
'// user cancelled the selection
Exit Sub
End If

'-------------------------------------
'// set work part
'-------------------------------------
Try
Dim partLoadStatus1 As NXOpen.PartLoadStatus = Nothing
Gvars.Session.Parts.SetWorkComponent(newComp, NXOpen.PartCollection.RefsetOption.Entire,
NXOpen.PartCollection.WorkComponentOption.Visible, partLoadStatus1)

Catch ex As Exception
Gvars.lw.WriteLine("#### Error while setting work part to NewCurve start part " & ex.Message)
End Try

'------------------------------------------------
'// get the work part and find out who this is
'------------------------------------------------
Try
Dim tempWorkPart As Part
tempWorkPart = Gvars.Session.Parts.Work
Gvars.lw.WriteLine("workpart name: " & tempWorkPart.Name)
Catch ex As Exception
Gvars.lw.WriteLine("### error getting work part: " & ex.ToString)
End Try

Gvars.lw.WriteLine("I'm expecting the inserted component to highlight as if it is the work part")

End Sub
>

From the code that you have posted, it appears that you are selecting a component in the current displayed assembly, adding a new component to the selected one (creating a subassembly), then you want to make the newly added component the work part.

So, if you start with the following
Top level assy
--compA

then select compA, the code will create a component under compA
Top level assy
--compA
----compB

The InsertPartAtOrigin function returns the component compB that is owned by compA. What you really want is the component compB that is owned by Top level assy. One way to get this is to map the component up from the subassembly to the top level assembly.

InsertPartAtOrigin(compCupCrvUnit.Prototype, strNewFnPath, "test5", newComp)

Dim topLevelComp As Component = Nothing
For Each aComp As Component In
theSession.Parts.Display.ComponentAssembly.MapComponentsFromSubassembly(newComp)
topLevelComp = aComp
Next

'-------------------------------------
'// set work part
'-------------------------------------
Try
Dim partLoadStatus1 As NXOpen.PartLoadStatus = Nothing
theSession.Parts.SetWorkComponent(topLevelComp, NXOpen.PartCollection.RefsetOption.Entire, NXOpen.PartCollection.WorkComponentOption.Visible, partLoadStatus1)

Your solution works and allows me to set the newly inserted component to be the work part. Sorry it took so long to reply, got pulled onto another project for a bit. Thanks again!