Showing results for 
Search instead for 
Do you mean 
Reply

Collect all the PMIs and associated objects from them

Hi,

 

I want to collect all the PMIs present in an inspection part and then get all the details (like length, height, radius, etc.) of the associated objects to those PMIs. This i want to achieve using C#. I am new to this and documentation doesn't have much info regarding methods used or any example. How i can do so? and parellaly an alternative approach to be followed to learn these things?

 

Thanks,

Divyanshu 

4 REPLIES

Re: Collect all the PMIs and associated objects from them

I posted some code in an eng-tips thread a while back that created a tabular note with all the PMI dimension values in the work part. It's not quite what you are asking for, but it may provide a starting point (finding PMI dimensions and reporting their values). The code is written in VB, but the C# version would be very similar.

Re: Collect all the PMIs and associated objects from them

I'm looking for how to refer to a PMI Collection using Python, and I would like to look at your example script to get some clues, but the link to dropbox fails. Could you please post the code directly to this forum somewhere? Or post a link to another location, maybe on your own website? Thanks!

Re: Collect all the PMIs and associated objects from them

This is probably OBE for the OG poster, but this post came up while I searched the forum for "PMI Collection".

 

I think I've figured out how to get the objects in a PMI collection using Python:

theSession  = NXOpen.Session.GetSession()
theParts = theSession.Parts
theWorkPart = theParts.Work
allPMIObjects = theWorkPart.PmiManager.Pmis

Unfortunately my approach to learn this was to collect syntax that might be useful (from journals and forum posts) and try a bunch of different combinations until one worked.  Does anyone have a good reference or decoder ring to the magic words to get the objects in a particular collection?  Someone referred to the SNAP Getting Started guide chapter 16, and that helped a lot, but more examples would be better.

 

Re: Collect all the PMIs and associated objects from them

Dropbox updated their policies; one of the changes resulted in breaking many of my "public" links. The PMI table journal is below:

 

'NXJournaling.com
'March 11, 2014
'
'create tabular note of PMI dimensions in model
'written & tested on NX 7.5
'ref: http://www.eng-tips.com/viewthread.cfm?qid=361232
'
'March 13, 2014
'  update: delete all tabular notes in file before creating new note

Option Strict Off
Imports System
Imports System.Collections.Generic
Imports NXOpen
Imports NXOpen.UF

Module Module1

    Dim theSession As Session = Session.GetSession()
    Dim theUfSession As UFSession = UFSession.GetUFSession()
    Dim workPart As Part = theSession.Parts.Work

    Sub Main()

        Dim lw As ListingWindow = theSession.ListingWindow
        lw.Open()

        Dim markId1 As Session.UndoMarkId
        markId1 = theSession.SetUndoMark(Session.MarkVisibility.Visible, "PMI Dimension Table")

        DeletePmiTable()

        Dim PmiDims As New List(Of Annotations.Dimension)
        For Each tempDim As Annotations.Dimension In workPart.Dimensions
            If tempDim.GetType.ToString.ToUpper.Contains("PMI") Then
                PmiDims.Add(tempDim)
            End If
        Next

        PmiDims.Sort(AddressOf CompareDimNames)

        'create tabular note
        Dim myTabularNoteOrigin As New Point3d(0, 0, 0)
        Dim myTabularNoteTag As Tag = CreateTabnoteWithSize(PmiDims.Count + 1, 5, myTabularNoteOrigin)

        'fill tabular note with PMI dimension info
        FillTable(myTabularNoteTag, PmiDims, workPart.PartUnits)

    End Sub

    Public Function CreateTabnoteWithSize( _
    ByVal nRows As Integer, ByVal nColumns As Integer, ByVal loc As Point3d) As NXOpen.Tag

        'Code taken from GTAC sample: nx_api4032
        'create tabnote with specified size and content
        'by Frank Berger

        ' Create the tabular note
        Dim secPrefs As UFTabnot.SectionPrefs
        theUfSession.Tabnot.AskDefaultSectionPrefs(secPrefs)
        Dim origin(2) As Double
        origin(0) = loc.X
        origin(1) = loc.Y
        origin(2) = loc.Z
        Dim tabnote As NXOpen.Tag = NXOpen.Tag.Null
        theUfSession.Tabnot.Create(secPrefs, origin, tabnote)

        ' Delete all existing columns and rows (we create them as needed)
        Dim nmRows As Integer = 0
        theUfSession.Tabnot.AskNmRows(tabnote, nmRows)
        For ii As Integer = 0 To nmRows - 1
            Dim row As NXOpen.Tag
            theUfSession.Tabnot.AskNthRow(tabnote, 0, row)
            theUfSession.Tabnot.RemoveRow(row)
            theUfSession.Obj.DeleteObject(row)
        Next
        Dim nmColumns As Integer = 0
        theUfSession.Tabnot.AskNmColumns(tabnote, nmColumns)
        For ii As Integer = 0 To nmColumns - 1
            Dim column As NXOpen.Tag
            theUfSession.Tabnot.AskNthColumn(tabnote, 0, column)
            theUfSession.Tabnot.RemoveColumn(column)
            theUfSession.Obj.DeleteObject(column)
        Next

        ' Now add our columns as needed
        Dim columns(nColumns - 1) As NXOpen.Tag
        For ii As Integer = 0 To nColumns - 1
            theUfSession.Tabnot.CreateColumn(30, columns(ii))
            theUfSession.Tabnot.AddColumn(tabnote, columns(ii), UFConstants.UF_TABNOT_APPEND)
        Next

        ' Now add our rows as needed
        Dim rows(nRows - 1) As NXOpen.Tag
        For ii As Integer = 0 To nRows - 1
            theUfSession.Tabnot.CreateRow(30, rows(ii))
            theUfSession.Tabnot.AddRow(tabnote, rows(ii), UFConstants.UF_TABNOT_APPEND)
        Next

        Return tabnote

    End Function

    Sub FillTable(ByVal tableTag As Tag, ByVal thePmiDims As List(Of Annotations.Dimension), ByVal partUnits As NXOpen.BasePart.Units)

        Dim cell_prefs As UFTabnot.CellPrefs
        theUfSession.Tabnot.AskDefaultCellPrefs(cell_prefs)

        Dim fit_methods(1) As UFTabnot.FitMethod
        cell_prefs.nm_fit_methods = 2
        cell_prefs.fit_methods(0) = UFTabnot.FitMethod.FitMethodAutoSizeRow
        cell_prefs.fit_methods(1) = UFTabnot.FitMethod.FitMethodAutoSizeCol

        'create header row
        Dim row As NXOpen.Tag
        Dim column As NXOpen.Tag
        Dim cell As Tag

        theUfSession.Tabnot.AskNthRow(tableTag, 0, row)
        For col As Integer = 0 To 4
            theUfSession.Tabnot.AskNthColumn(tableTag, col, column)

            theUfSession.Tabnot.AskCellAtRowCol(row, column, cell)

            Dim cell_text As String
            Select Case col
                Case Is = 0
                    cell_text = "PMI Name"
                Case Is = 1
                    cell_text = "Appended Text, Before"
                Case Is = 2
                    cell_text = "Measured Size"
                Case Is = 3
                    cell_text = "Tolerance, Lower"
                Case Is = 4
                    cell_text = "Tolerance, Upper"
                Case Else
                    cell_text = ""
            End Select
            theUfSession.Tabnot.SetCellPrefs(cell, cell_prefs)
            theUfSession.Tabnot.SetCellText(cell, cell_text)

        Next


        'fill in dimension information
        For rr As Integer = 1 To thePmiDims.Count
            theUfSession.Tabnot.AskNthRow(tableTag, rr, row)
            Dim myTolVal As New DimToleraceVal(thePmiDims.Item(rr - 1))
            For cc As Integer = 0 To 4

                theUfSession.Tabnot.AskNthColumn(tableTag, cc, column)

                theUfSession.Tabnot.AskCellAtRowCol(row, column, cell)

                Dim cell_text As String
                Select Case cc
                    Case Is = 0     'PMI name
                        cell_text = thePmiDims.Item(rr - 1).Name
                    Case Is = 1     'appended text before
                        Dim aTextBefore As String()
                        aTextBefore = thePmiDims.Item(rr - 1).GetAppendedText.GetBeforeText
                        If aTextBefore.Length > 0 Then
                            cell_text = aTextBefore(0)
                        Else
                            cell_text = ""
                        End If
                    Case Is = 2     'measured size
                        cell_text = thePmiDims.Item(rr - 1).ComputedSize.ToString
                    Case Is = 3     'tolerance lower
                        If partUnits = BasePart.Units.Inches Then
                            cell_text = myTolVal.ToleranceLower
                        Else
                            cell_text = myTolVal.ToleranceLowerMetric
                        End If
                    Case Is = 4     'tolerance upper
                        If partUnits = BasePart.Units.Inches Then
                            cell_text = myTolVal.ToleranceUpperMetric
                        Else
                            cell_text = myTolVal.ToleranceUpperMetric
                        End If
                    Case Else
                        cell_text = ""
                End Select
                theUfSession.Tabnot.SetCellPrefs(cell, cell_prefs)
                theUfSession.Tabnot.SetCellText(cell, cell_text)

            Next

        Next


    End Sub

    Private Function CompareDimNames(ByVal x As Annotations.Dimension, ByVal y As Annotations.Dimension) As Integer

        'case-insensitive sort
        Dim myStringComp As StringComparer = StringComparer.CurrentCultureIgnoreCase

        'for a case-sensitive sort (A-Z then a-z), change the above option to:
        'Dim myStringComp As StringComparer = StringComparer.CurrentCulture

        Return myStringComp.Compare(x.Name, y.Name)

    End Function

    Private Sub DeletePmiTable()

        Dim myTabNotes As New List(Of DisplayableObject)
        Dim tmpTabNote As NXOpen.Tag = NXOpen.Tag.Null
        Dim myTabNote As DisplayableObject
        Dim type As Integer
        Dim subtype As Integer

        Do
            theUfSession.Obj.CycleObjsInPart(workPart.Tag, UFConstants.UF_tabular_note_type, tmpTabNote)
            If tmpTabNote = NXOpen.Tag.Null Then
                Continue Do
            End If
            If tmpTabNote <> NXOpen.Tag.Null Then
                theUfSession.Obj.AskTypeAndSubtype(tmpTabNote, type, subtype)
                If subtype = UFConstants.UF_tabular_note_section_subtype Then
                    myTabNote = (Utilities.NXObjectManager.Get(tmpTabNote))
                    myTabNotes.Add(myTabNote)

                End If
            End If
        Loop Until tmpTabNote = NXOpen.Tag.Null

        Dim markId1 As Session.UndoMarkId
        markId1 = theSession.SetUndoMark(Session.MarkVisibility.Invisible, "Delete PMI Table")

        Dim nErrs1 As Integer
        nErrs1 = theSession.UpdateManager.AddToDeleteList(myTabNotes.ToArray)

        Dim nErrs2 As Integer
        nErrs2 = theSession.UpdateManager.DoUpdate(markId1)

    End Sub

    Public Function GetUnloadOption(ByVal dummy As String) As Integer

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

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

        'Unloads the image explicitly, via an unload dialog
        'GetUnloadOption = NXOpen.Session.LibraryUnloadOption.Explicitly
        '-------------------------------

    End Function

End Module

Public Class DimToleraceVal

    Private _theDimension As Annotations.Dimension
    Public Property Dimension() As Annotations.Dimension
        Get
            Return _theDimension
        End Get
        Set(ByVal value As Annotations.Dimension)
            _theDimension = value
            Me.GetToleranceValues()
        End Set
    End Property

    Private _toleranceUpper As String = ""
    Public ReadOnly Property ToleranceUpper() As String
        Get
            Return _toleranceUpper
        End Get
    End Property

    Private _toleranceLower As String = ""
    Public ReadOnly Property ToleranceLower() As String
        Get
            Return _toleranceLower
        End Get
    End Property

    Private _toleranceUpperMetric As String = ""
    Public ReadOnly Property ToleranceUpperMetric() As String
        Get
            Return _toleranceUpperMetric
        End Get
    End Property

    Private _toleranceLowerMetric As String = ""
    Public ReadOnly Property ToleranceLowerMetric() As String
        Get
            Return _toleranceLowerMetric
        End Get
    End Property


    Public Sub New()

    End Sub

    Public Sub New(ByVal theDim As Annotations.Dimension)
        Me.Dimension = theDim
    End Sub

    Private Sub GetToleranceValues()

        If _theDimension.ToleranceType = Annotations.ToleranceType.None Then
            _toleranceUpper = ""
            _toleranceUpperMetric = ""
            _toleranceLower = ""
            _toleranceLowerMetric = ""
        End If

        If _theDimension.ToleranceType = Annotations.ToleranceType.BilateralOneLine Then
            _toleranceUpper = _theDimension.UpperToleranceValue.ToString
            _toleranceUpperMetric = _theDimension.UpperMetricToleranceValue.ToString
            _toleranceLower = "-" & _toleranceUpper
            _toleranceLowerMetric = "-" & _toleranceUpperMetric
        End If

        If _theDimension.ToleranceType = Annotations.ToleranceType.BilateralTwoLines Then
            _toleranceUpper = _theDimension.UpperToleranceValue.ToString
            _toleranceUpperMetric = _theDimension.UpperMetricToleranceValue.ToString
            _toleranceLower = _theDimension.LowerToleranceValue.ToString
            _toleranceLowerMetric = _theDimension.LowerMetricToleranceValue.ToString
        End If

        If _theDimension.ToleranceType = Annotations.ToleranceType.UnilateralAbove Then
            _toleranceUpper = _theDimension.UpperToleranceValue.ToString
            _toleranceUpperMetric = _theDimension.UpperMetricToleranceValue.ToString
            _toleranceLower = "0"
            _toleranceLowerMetric = "0"
        End If

        If _theDimension.ToleranceType = Annotations.ToleranceType.UnilateralBelow Then
            _toleranceUpper = "0"
            _toleranceUpperMetric = "0"
            _toleranceLower = _theDimension.LowerToleranceValue.ToString
            _toleranceLowerMetric = _theDimension.LowerMetricToleranceValue.ToString
        End If

    End Sub

End Class

I have not updated the code since 2014; there are probably a few things that I'd do differently now (due to experience and updates to the API). But, here it is, I hope it helps.