Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations IDS on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

Journal to read Teamcenter attributes for a list of part numbers

Status
Not open for further replies.

P_Towbar

Mechanical
May 6, 2021
3
Hi all,

At my work we're using NX 12 with Teamcenter 12. I'm still new to VB coding and I'm having trouble getting a journal to work that I cobbled together from different posts I've seen on the web. The main purpose is to:
[ul]
[li]read a list of a few thousand part numbers from a text file, e.g. "C:\part_list.txt",[/li]
[li]find parts from the list on Teamcenter using AskPartTag,[/li]
[li]for parts found on Teamcenter, get the latest revision number, part description and part type,[/li]
[li]write all attributes found to a text delimited log file, e.g. "TC_Part_Attributes.log", in the main Documents folder[/li]
[/ul]

The aim is to run this script from NX with no parts loaded, and it should just query Teamcenter directly and get all attributes. I've got thousands of part numbers to interrogate and using Teamcenter Structure Manager is too cumbersome and time consuming. The script works when I'm just looking up the part number, revision and description, e.g.:

Input text file:
PART030284
PART029802
PART093677
COMP058302
COMP500461

Output log:
Part Number;Revision;Teamcenter Description
PART030284;2;BEARING HOUSING
PART029802;4;OUTER DOOR HINGE SUPPORT
COMP058302;1;HEXAGON HEAD BOLT DIN 931 - M30 X 100 - 8.8 -A
COMP500461;3;THREADED ROD DIN 975 - M10 - 5.6

These all have specific commands for each of these attributes, e.g. ListPartRevisions. But it falls over when I'm trying to search other and custom attributes that we use in our company, e.g. "DB_PART_TYPE", "MANUFACTURING_SITE", etc. See below and attached code I'm trying to get to work with "DB_PART_TYPE", using GetStringAttribute.

Code:
'Journal to read attributes from Teamcenter without loading parts in NX
'Read list of part numbers from a text file, for each part number in list search Teamcenter for specified attribute(s). Output results to another text file.
Option Strict Off
Imports System
Imports System.Collections.Generic
Imports NXOpen
Imports NXOpen.UF
Imports NXOpenUI

Module QuickAdd

    Dim theSession As Session = Session.GetSession()
    Dim workPart As Part = theSession.Parts.Work
    Dim ufs As UFSession = UFSession.GetUFSession()
    Dim displayPart As Part = theSession.Parts.Display
    Dim theUI As UI = UI.GetUI()
    Dim lw As ListingWindow = theSession.ListingWindow

	
    Sub Main(ByVal args() As String)

	Dim myDocs As String = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
	Dim outputFile As String = IO.Path.Combine(myDocs, "TC_Part_Attributes.log")
		
        '***** comment out this section to append to existing file *****
        'does file already exist? if so, delete it
        If IO.File.Exists(outputFile) Then
            Try
                IO.File.Delete(outputFile)
            Catch ex As Exception
                MsgBox(ex.Message, MsgBoxStyle.OkOnly, "Error deleting file:")
            End Try
        End If
        '***** end of section *****
 
        Dim number_list As New List(Of String)
        Dim input_file_name As String
	input_file_name = "C:\part_list.txt"

            If System.IO.File.Exists(input_file_name) = True Then
                Dim objReader As New System.IO.StreamReader(input_file_name)
                Do While objReader.Peek() <> -1
                    number_list.Add(objReader.ReadLine())
                Loop
            Else
                MsgBox("no file")
            End If

            number_list.Add(input_file_name)

        Dim markId1 As Session.UndoMarkId
        markId1 = theSession.SetUndoMark(Session.MarkVisibility.Visible, "Get part description")

        'use listing window to write to file and window
        lw.SelectDevice(ListingWindow.DeviceType.FileAndWindow, outputFile)
        lw.Open()
	lw.WriteLine("Task Start: " & Now())
	lw.WriteLine("Part Number;Revision;Teamcenter Description;Item Type")
	'flush file buffer by changing listing window device
	lw.SelectDevice(ListingWindow.DeviceType.Window, "")
	lw.Close()
		
        For Each Part As String In number_list
            Try
		'use listing window to write to file and window
		lw.SelectDevice(ListingWindow.DeviceType.FileAndWindow, outputFile)
		lw.Open()
		lw.WriteLine(get_component_attr(Part))
		lw.Close()
		'flush file buffer by changing listing window device
		lw.SelectDevice(ListingWindow.DeviceType.Window, "")
            Catch ex As Exception
                'MsgBox(Part & " not importing correctly")
            End Try
        Next

        'use listing window to write to file and window
        lw.SelectDevice(ListingWindow.DeviceType.FileAndWindow, outputFile)
        lw.Open()
	lw.WriteLine("Task End: " & Now())
	'flush file buffer by changing listing window device
	lw.SelectDevice(ListingWindow.DeviceType.Window, "")
	lw.Close()
		
    End Sub
	
    Function get_component_attr(ByVal part_number As String) As String

        Dim mySearch As PDM.PdmSearch = theSession.PdmSearchManager.NewPdmSearch()
	Dim mySearchResult As PDM.SearchResult = mySearch.simple(part_number)
	Dim results() As String = mySearchResult.GetResultObjectNames()
	Dim mytag As NXOpen.Tag = NXOpen.Tag.Null
	Dim part_name As String
	Dim part_desc As String
	Dim part_type As String
        Dim RevisionCount As Integer 
        Dim RevisionTag() As NXOpen.Tag
        Dim cnt As Integer = 0
        Dim TagRevID As String = ""
	Dim type As Int32 = UFConstants.UF_ATTR_any
	Dim realtype As Int32
	Dim title As String = "DB_PART_TYPE"
		
        'Cycle through results to get attributes
        For Each resultName As String In results 'resultName = Part number found
            'Get Part Tag
            ufs.Ugmgr.AskPartTag(resultName, mytag)
			
            'Get latest revision
            ufs.Ugmgr.ListPartRevisions(mytag, RevisionCount, RevisionTag)
            For cnt = RevisionTag.GetLowerBound(0) To RevisionTag.GetUpperBound(0)
                ufs.Ugmgr.AskPartRevisionId(RevisionTag(cnt), TagRevID)
            Next
			
		'Get Part Description
		ufs.Ugmgr.AskPartNameDesc(mytag, part_name, part_desc)
			
		'============================================
		'PROBLEM AREA:
			
		ufs.Attr.AskPartAttribute(mytag)
			
		ufs.Attr.FindAttribute(mytag, type, title, realtype)
		If realtype <> 0 And realtype = UFConstants.UF_ATTR_string Then
			'part_type = workPart.GetStringAttribute(title) '<--- This gives "DB_PART_TYPE" attribute of currently loaded work part, and not part(s) in user list
			part_type = resultName.GetStringAttribute(title) '<--- This gives error 'GetStringAttribute' is not a member of 'NXOpen.Tag'.
		End If
		'============================================

		get_component_attr = resultName & ";" & TagRevID & ";" & part_desc & ";" & part_type
			
        Next
    End Function
End Module

The result should be something like this:

Output log:
Part Number;Revision;Teamcenter Description;Type
PART030284;2;BEARING HOUSING;PART
PART029802;4;OUTER DOOR HINGE SUPPORT;PART
COMP058302;1;HEXAGON HEAD BOLT DIN 931 - M30 X 100 - 8.8 -A;FASTENER
COMP500461;3;THREADED ROD DIN 975 - M10 - 5.6;FASTENER

All the code examples that I've seen so far only appear to work with finding the attributes (system default and custom) of loaded work parts, and not unloaded parts on Teamcenter. Can anybody please help? Thank you.
 
 https://files.engineering.com/getfile.aspx?folder=d10ee918-f1d4-421a-b81b-9d49cae2231f&file=get_attributes_from_Teamcenter_for_parts_list.vb
Replies continue below

Recommended for you

Yeah - I’m used to deal with NX TC stuff, but not quite sure if NX open toolkit provide a class for searching so deap in tc ...Will give it a try ..
What I’m sure on: Is that when using the Invoke PDM server from NX /then almost everything is possible;
the call from NX to tc, triggers an action on tc side / then it can return a string or a integer..,
 
Thanks for the suggestion, lklo. I'll have a look at the NXOpen.PDM package in more detail. NXOpen.PDM.DatabaseAttributeManager.GetAttribute sounds promising.

For the above code I'm only interested in reading attributes that could also be seen in NX by going into the Component or Displayed Part Properties. But it would be very useful to be able to read any other Teamcenter attribute as well, i.e. those not visible in NX but visible in Teamcenter Structure Manager, e.g. BOMLine.bl_rev_fnd0rollupMass (BOM rollup mass), BOMLine.awb0RevisionLasModifiedDate (Last Modified Date), etc. If that's possible..
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor