List of parts, Add to assembly.

Hello,

I have a list of parts that I want to add to an assembly in NX 10 with team center.

I already have code to capture the list of part. Am really just looking for how to add the latest revision of the item.

When I journaled recorder a journal it looked like the explicit revision number was captured.

Dose any one have a small snippet that will add the latest revision of a part to the current work part?

This is the code I use to find the latest revision:-


Dim theUfSession As UFSession = UFSession.GetUFSession()
Dim ugmgr As UFUgmgr = theUfSession.Ugmgr
Dim revcount As Integer
Dim revstr() As Tag = Nothing
Dim parttag As Tag
Dim partname as String ' Use the TC Item ID. Do not prefix with @DB/
Dim revision as string

Try
ugmgr.AskPartTag(partname, parttag)
ugmgr.ListPartRevisions(parttag, revcount, revstr)
ugmgr.AskPartRevisionId(revstr(revcount - 1), revision)
MsgBox("Latest revision is " & revision)
Catch ex As Exception
MsgBox("Error occurred loading part number " & partname)
End Try

Mike

mmiscool, What code you are using to capture the part lists. Can you please share the codes for the whole operation? Thanks

Regards,
MFJ

This is used to update the assembly from list of parts from excel, also by pulling the latest revision form TcE. You can modify it to match your requirement.

Option Strict Off

Imports System
Imports System.IO
Imports System.Windows.Forms
Imports System.Drawing
Imports System.Collections
Imports System.Collections.Generic
Imports System.Drawing.Imaging
Imports System.Runtime.InteropServices
Imports NXOpen
Imports NXOpen.UF
Imports NXOpen.Assemblies
Imports NXOpenUI

Module AssemblyUpdateBasedOnExcelBOM

'NX Session Syntax
Public theSession As Session = Session.GetSession()
Public ufs As UFSession = UFSession.GetUFSession()
Public theUISession As UI = UI.GetUI
Public lw As ListingWindow = theSession.ListingWindow

'Excel Syntax
Const xlCenter As Long = -4108
Const xlDown As Long = -4121
Const xlFormulas As Long = -4123
Const xlLeft As Long = -4131
Const xlAbove As Long = 0
Const xlWhole As Long = 1
Const xlByRows As Long = 1
Const xlNext As Long = 1
Const msoTrue As Long = -1
Const msoFalse As Long = -1
Const msoFormControl As Long = 8
Const strPicFilesPath As String = "C:\Temp\"
Dim lngLevelStart(20) As Long
Dim colPartNo As Integer = 2
Dim colPartRev As Integer = 4
Dim colPartName As Integer = 10
Dim colPartCodepNo As Integer = 6
Dim colPartFiatNo As Integer = 8
Dim objExcel As Object = Nothing
Dim objWorkbook As Object = Nothing
Dim objWorksheet As Object = Nothing
Dim excelFileName As String = "C:\Users\jow\Desktop\WS-BOM MANAGER.xlsx"
Dim excelFileExists As Boolean = False
Dim row As Long = 1
Dim column As Long = 1

'NX Part Syntax

Dim displayPart As Part = theSession.Parts.Display
Dim workPart As Part = theSession.Parts.Work

Sub Main()

lw.Open()

Dim markId1 As Session.UndoMarkId
markId1 = theSession.SetUndoMark(Session.MarkVisibility.Visible, "Assembly Update")

'create Excel object
objExcel = CreateObject("Excel.Application")
If objExcel Is Nothing Then
theUISession.NXMessageBox.Show("Error", NXMessageBox.DialogType.Error, "Could not start Excel, journal exiting")
theSession.UndoToMark(markId1, "journal")
Exit Sub
End If

If File.Exists(excelFileName) Then
'Open the Excel file
excelFileExists = True
objWorkbook = objExcel.Workbooks.Open(excelFileName)
Else
MsgBox("Unable to Find the Excel File, Please Select the Correct Location")
Exit Sub
End If

For Each Mysheet As Object In objWorkbook.Worksheets
Mysheet.Activate()
Dim SheetName As String = Mysheet.Name
If SheetName.Contains("Master") Then
Mysheet.activate()
Dim Mycell As Object = Nothing
For Each Mycell In Mysheet.Range("B4:Z4") 'change the xl table as per your input
Dim AssemblyPartNo As String = Mycell.Value

If AssemblyPartNo.Contains("XXX") Then 'change xxx to some letters which all your part no have
'Open Assembly
Dim ToOpenPart As String = Nothing
ToOpenPart = "@DB/" & AssemblyPartNo
theSession.Parts.SetNonmasterSeedPartData(ToOpenPart)
Dim basePart1 As BasePart = Nothing
Try
Dim partLoadStatus1 As PartLoadStatus = Nothing
basePart1 = theSession.Parts.Open(ToOpenPart, partLoadStatus1)
partLoadStatus1.Dispose()
Dim partLoadStatus2 As PartLoadStatus = Nothing
Dim status1 As PartCollection.SdpsStatus
status1 = theSession.Parts.SetDisplay(basePart1, False, True, partLoadStatus2)
partLoadStatus2.Dispose()
Catch exc As NXException
lw.WriteLine(exc.Message)
End Try

displayPart = theSession.Parts.Display
workPart = theSession.Parts.Work
Dim PartsList As List(Of String) = New List(Of String)
Dim PartsListWORev As List(Of String) = New List(Of String)
Dim PartNoWORev As String = Nothing

For Each child As Component In displayPart.ComponentAssembly.RootComponent.GetChildren
PartsList.Add(child.DisplayName)
Next

For Each MyS As String In PartsList
PartNoWORev = Left(MyS, 12)
PartsListWORev.Add(PartNoWORev)
Next

If displayPart.IsReadOnly.ToString = False Then

Dim CompRange As Object = Nothing
CompRange = Mysheet.Range(Mycell.Offset(1, 0), Mycell.Offset(1, 0).End(xlDown))
Dim ChildPartCell As Object = Nothing
Dim ChildPartNo As String = Nothing
'Add component to the assembly
For Each ChildPartCell In CompRange
ChildPartNo = ChildPartCell.Value

Dim markId2 As Session.UndoMarkId
markId2 = theSession.SetUndoMark(Session.MarkVisibility.Visible, "Opening Part" & ChildPartNo)
theSession.SetUndoMarkName(markId2, "Opening Part" & ChildPartNo)
Dim Parttag As Tag = Nothing
ufs.Ugmgr.AskPartTag(ChildPartNo, Parttag)
Dim RevCount As Integer = Nothing
Dim RevisionsTag() As Tag = Nothing
Dim LatestRevision As String = Nothing
Dim PartName As String = Nothing
Dim PartDesc As String = Nothing

If Parttag <> NXOpen.Tag.Null Then
ufs.Ugmgr.ListPartRevisions(Parttag, RevCount, RevisionsTag)
ufs.Ugmgr.AskPartRevisionId(RevisionsTag(RevCount - 1), LatestRevision)
ufs.Ugmgr.AskPartNameDesc(Parttag, PartName, PartDesc)
Else
lw.WriteLine(ChildPartNo & ": Unable to Open the Correct Revision of the Part")
End If
Dim ToAddPart As String = Nothing

ToAddPart = "@DB/" & ChildPartNo & "/" & LatestRevision

If PartsList.Contains(ToAddPart) Then
'Skip This part
ElseIf PartsListWORev.Contains(ChildPartNo) Then
For Each child As Component In displayPart.ComponentAssembly.RootComponent.GetChildren
If child.DisplayName.Contains("ChildPartNo") Then
'Replace compponent If found earlier version
Dim replaceComponentBuilder1 As Assemblies.ReplaceComponentBuilder
replaceComponentBuilder1 = theSession.Parts.Work.AssemblyManager.CreateReplaceComponentBuilder()
replaceComponentBuilder1.ReplaceAllOccurrences = True
replaceComponentBuilder1.ComponentNameType = Assemblies.ReplaceComponentBuilder.ComponentNameOption.AsSpecified
Dim added1 As Boolean
added1 = replaceComponentBuilder1.ComponentsToReplace.Add(child)
'path to replacement part, edit as needed
replaceComponentBuilder1.ReplacementPart = ToAddPart
replaceComponentBuilder1.SetComponentReferenceSetType(Assemblies.ReplaceComponentBuilder.ComponentReferenceSet.Maintain, Nothing)
Dim partLoadStatus1 As PartLoadStatus
partLoadStatus1 = replaceComponentBuilder1.RegisterReplacePartLoadStatus()
Dim nXObject1 As NXObject
nXObject1 = replaceComponentBuilder1.Commit()
partLoadStatus1.Dispose()
replaceComponentBuilder1.Destroy()
End If
Next

Else

Dim component1 As Component = Nothing
Dim partLoadStatus1 As PartLoadStatus = Nothing

Dim basePoint1 As Point3d = New Point3d(0.0, 0.0, 0.0)
Dim orientation1 As Matrix3x3
orientation1.Xx = 1.0
orientation1.Xy = 0.0
orientation1.Xz = 0.0
orientation1.Yx = 0.0
orientation1.Yy = 1.0
orientation1.Yz = 0.0
orientation1.Zx = 0.0
orientation1.Zy = 0.0
orientation1.Zz = 1.0

Dim markId3 As Session.UndoMarkId = theSession.SetUndoMark(Session.MarkVisibility.Visible, "Add Component")

Try

theSession.Parts.SetNonmasterSeedPartData(ToAddPart)

component1 = workPart.ComponentAssembly.AddComponent(ToAddPart.ToString, "PART", PartName.ToString, basePoint1, orientation1, 1, partLoadStatus1, True)

Catch ex As Exception
MsgBox("An error occurred while adding the component part." & _
vbCrLf & "Please check that " & ToAddPart & " exists in Teamcenter." & _
vbCrLf & ex.GetType.ToString & " : " & ex.Message, vbCritical + vbOKOnly, "Error")

lw.WriteLine("An error occurred while adding the component part.")
lw.WriteLine("Please check that " & ToAddPart & " exists in Teamcenter.")
lw.WriteLine(ex.GetType.ToString & " : " & ex.Message)
'Exit Sub
Finally

End Try

ufs.Ui.SetPrompt("")
partLoadStatus1.Dispose()
theSession.UpdateManager.DoUpdate(markId3)
theSession.DeleteUndoMark(markId3, Nothing)

End If
Next

workPart.Save(BasePart.SaveComponents.False, BasePart.CloseAfterSave.True)
Else

MsgBox(AssemblyPartNo & "- Is read only- Journal will continue to the Next Assembly")

End If
End If
Next
End If
Next

MsgBox("Assembly Successfully Updated")

objWorkbook.Save()
objWorkbook.Close()
objExcel.Quit()
objWorksheet = Nothing
objWorkbook = Nothing
objExcel = Nothing

End Sub

Public Function GetUnloadOption(ByVal dummy As String) As Integer

'Unloads the image when the NX session terminates
GetUnloadOption = NXOpen.Session.LibraryUnloadOption.AtTermination

End Function

End Module

Regards,

Joe