cancel
Showing results for 
Search instead for 
Did you mean: 

How to load assemblies from Team Center

Valued Contributor
Valued Contributor

I want to be able to load assemblies from Teamcenter programmtically. How do I do that? I have a large list in Excel. If possible I'd rather not have to go into Teamcenter and load them manully.

14 REPLIES

Re: How to load assemblies from Team Center

Valued Contributor
Valued Contributor

This reads a CSV file with Item ID in the first column and revision in the second column. If revision is 0 then last revision is loaded. All Parts or assemblies are loaded into the session.

 

Option Strict Off

Imports System
Imports NXOpen
Imports NXOpen.UF
Imports System.IO
Imports System.Windows.Forms

Module NXJournal

    Public theSession As Session = Session.GetSession()
    Dim workPart As Part = theSession.Parts.Work
    Dim dispPart As Part = theSession.Parts.Display
    Public ufs As UFSession = UFSession.GetUFSession()
    Public lw As ListingWindow = theSession.ListingWindow
    Public lg As LogFile = theSession.LogFile
    Public strOutputFolder As String

    Dim partname As String
    Dim revision As String
    Dim theUfSession As UFSession = UFSession.GetUFSession()
    Dim ugmgr As UFUgmgr = theUfSession.Ugmgr

    Sub Main()

        Dim strLastPath As String = Nothing
     
        Dim tmp As Integer = 0
        Dim i As Integer = 0
        Dim j As Integer = 0

        lg.WriteLine(" ")
        lg.WriteLine("~~~~  Start of Load Parts from List journal. Version " & " ~~~~ " & DateTime.Now & " ~~~~")

        'lw.Open()

        'Use same folder for input text file as well as output files

        Dim openFileDialog1 As New OpenFileDialog()

        'Key will show up in HKEY_CURRENT_USER\Software\VB and VBA Program Settings
        Try
            'Get the last path used from the registry
            strLastPath = GetSetting("NX journal", "Load Parts from List", "LibraryPath")
        Catch e As ArgumentException
        Catch e As Exception
            MsgBox("error getting the last path used from the registry " & vbCrLf & e.GetType.ToString)
        Finally
        End Try

        openFileDialog1.Title = "Load Parts from List"
        'openFileDialog1.Filter = "CSV files (*.csv)|*.csv|All files (*.*)|*.*"
        openFileDialog1.Filter = "CSV files (*.csv)|*.csv"
        openFileDialog1.FilterIndex = 1
        openFileDialog1.RestoreDirectory = True
        If Directory.Exists(strLastPath) Then
            openFileDialog1.InitialDirectory = strLastPath
        Else
            openFileDialog1.InitialDirectory = "C:\"
        End If

        If openFileDialog1.ShowDialog() = DialogResult.OK Then
            strOutputFolder = openFileDialog1.FileName
            tmp = InStrRev(strOutputFolder, "\")
            strOutputFolder = Left(strOutputFolder, tmp)

            ' save the output folder path in the registry for use on next run
            SaveSetting("NX journal", "Load Parts from List", "LibraryPath", strOutputFolder)

        Else
            'user pressed 'cancel', exit the subroutine
            strOutputFolder = 0
            Exit Sub

        End If

        lw.WriteLine("Source file: " & openFileDialog1.FileName)

        'Get contents of csv file

        Dim sr As StreamReader = New StreamReader(openFileDialog1.FileName)
        Dim line As String
        Dim partslist(0) As String

        Try
            line = sr.ReadLine()
            While Not line Is Nothing
                Dim delim As Char() = {","c}
                Dim strings As String() = line.Split(delim)
                partname = (strings(0))
                revision = (strings(1))
                If revision = 0 Then getRev()
                ReDim Preserve partslist(i)
                partslist(i) = "@DB/" & partname & "/" & revision
                i = i + 1
                line = sr.ReadLine()
            End While
        Catch e As Exception
            MsgBox("error reading csv file " & vbCrLf & vbCrLf & e.GetBaseException.ToString)
            Exit Sub
        Finally
            sr.Close()
        End Try

        'Open part
        Dim partLoadStatus1 As PartLoadStatus = Nothing

        For j = 0 To i - 1

            Try
                theSession.Parts.SetNonmasterSeedPartData(partslist(j))

                Dim basePart1 As BasePart
                basePart1 = theSession.Parts.OpenBaseDisplay(partslist(j), partLoadStatus1)
                workPart = theSession.Parts.Work
                dispPart = theSession.Parts.Display

            Catch ex As Exception
                MsgBox("An error occurred while opening the  import part" & _
                vbCrLf & "Please check that " & partslist(j) & "exists in Teamcenter." & _
                vbCrLf & ex.GetType.ToString & " : " & ex.Message, vbCritical + vbOKOnly, "Error")
                Exit Sub
            Finally

            End Try
        Next

        lg.WriteLine("~~~~ exiting Load Parts from List journal - sub Main ~~~~ " & DateTime.Now & " ~~~~")
        lg.WriteLine("")

    End Sub

    Sub getRev()
        Dim revcount As Integer
        Dim revstr() As Tag = Nothing
        Dim parttag As Tag

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

    End Sub

    Public Function GetUnloadOption(ByVal dummy As String) As Integer

        'Unloads the image immediately after execution within NX
        GetUnloadOption = NXOpen.Session.LibraryUnloadOption.Immediately

    End Function

End Module

Code written for NX8.5 / TC9.1

 

Mike

NX 10 TC11.2
www.astonmartin.com

Re: How to load assemblies from Team Center

Valued Contributor
Valued Contributor
Is this all of the code? Where in the code does it search Teamcenter for the latest revision and load it?

Re: How to load assemblies from Team Center

Esteemed Contributor
Esteemed Contributor

junfan wrote:
Is this all of the code? Where in the code does it search Teamcenter for the latest revision and load it?

Is the last line you see

 Dim delim As Char() = {","c

If so, there are issues with IE truncating posts at "close braces"

 

They've been reported, I hve no idea if anything is being done.

Try Chrome or Firefox?

Ken Akerboom Sr CAx Systems Engr, Moog, Inc.
Production: NX10.0.3.5 MP5 + patch/TC11.2
I'd rather be e-steemed than e-diseaseled


Re: How to load assemblies from Team Center

Valued Contributor
Valued Contributor

Oh okay. I just got it. Thanks!

Re: How to load assemblies from Team Center

Valued Contributor
Valued Contributor

Awesome that worked great! Thank you. Can I ask, is there a way to edit this so it will load several models in the context of an assembly, instead of just loading each model in it's own window?

 

Re: How to load assemblies from Team Center

Valued Contributor
Valued Contributor

I have another question. Exactly how and where is this code accessing Teamcenter? I can see where it is getting the revision status, but where does it actually pass string into Teamcenter?

 

Is it with: theSession.Parts.SetNonmasterSeedPartData(partlist(j))?  Also what does the "@DB" characters do?

 

 

 

 

Re: How to load assemblies from Team Center

Esteemed Contributor
Esteemed Contributor

The "@DB" is the flag to NX that the part is in Teamcenter

Ken Akerboom Sr CAx Systems Engr, Moog, Inc.
Production: NX10.0.3.5 MP5 + patch/TC11.2
I'd rather be e-steemed than e-diseaseled


Re: How to load assemblies from Team Center

Valued Contributor
Valued Contributor

Oh okay that makes sense. So, if I wanted to use this code to not only open parts but return certain model attributes how could I do that?

 

Specifically 2 things:

• BOM Line

• Item Rev Status (not the revision)

• Also if possible Date of creation

 

Re: How to load assemblies from Team Center

Valued Contributor
Valued Contributor

Try replacing the Open Part portion of the code with this to load parts into an assembly:-

        'Open part
        Dim component1 As Assemblies.Component
        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 markId1 As Session.UndoMarkId = theSession.SetUndoMark(Session.MarkVisibility.Visible, "Add Component")



        For j = 0 To i - 1

            Try

                theSession.Parts.SetNonmasterSeedPartData(partslist(j))
ufs.Ui.SetPrompt("Loading component number " & j + 1 & " of " & i)
                component1 = workPart.ComponentAssembly.AddComponent(partslist(j), "MODEL", nameofpart(j), basePoint1, orientation1, -1, partLoadStatus1, True)
                lg.WriteLine("Loading " & partslist(j))
            Catch ex As Exception
                MsgBox("An error occurred while adding the component part." & _
                vbCrLf & "Please check that " & partslist(j) & " exists in Teamcenter." & _
                vbCrLf & ex.GetType.ToString & " : " & ex.Message, vbCritical + vbOKOnly, "Error")

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

            End Try
        Next

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

You will need to add Imports NXOpen.Assemblies with the other Imports statements.

Mike

NX 10 TC11.2
www.astonmartin.com