Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

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

Journal to set cell settings and export Parts List

jtozer

Aerospace
Aug 8, 2018
15
We have an internal system that we upload the comma separated text file Parts List Export to. This system reads each line to help automate component ordering for tooling packages. We need all the text in the Parts List cells to be on one line for the exported text file to be formated correctly for this internal system.

I am trying to create a journal to set the Parts List cell preference fit methods to force all the text in the cell to one line (AutoSizeRow and AutoSizeText on, Wrap and all other off) then export the Parts List to a comma separated text file. I started with the example Parts List export journal and figured out how to get the cell preferences set, but it takes over 1 second per cell and that speed is just not acceptable for what I want to accomplish.
When setting the cell preferences manually it only takes a few seconds total, so I don't know why it takes so long this way, aside from the fact that I'm using a different method.

I tried using the TableEditSettingsBuilder from a recorded journal, but I couldn't figure out how to send the correct object to that function.

Can someone tell me how to speed this process up or suggest a different method of automatically changing the Parts List cell settings?

Code that works, but is slow:
Code:
Option Strict Off

Imports System
Imports NXOpen
Imports NXOpen.Annotations
Imports NXOpen.Assemblies
Imports NXOpen.BlockStyler
Imports NXOpen.Diagramming
Imports NXOpen.UF
Imports NXOpen.UI
Imports NXOpen.Utilities

Module NXJournal

    Dim s As Session = Session.GetSession()
    Dim ufs As UFSession = UFSession.GetUFSession()
    Dim lw As ListingWindow = s.ListingWindow
    Dim wPart As NXOpen.Part = s.Parts.Work

    Sub Main()
        lw.Open()
        Dim my_plist As NXOpen.Tag = NXOpen.Tag.Null
        Dim dispPart As NXOpen.Tag = s.Parts.Display.Tag
        Dim markId1 As NXOpen.Session.UndoMarkId = Nothing
        markId1 = s.SetUndoMark(NXOpen.Session.MarkVisibility.Visible, "Do update")

        my_plist = GetPlistTagVal(dispPart)

        Dim r As Integer
        Dim c As Integer
        Dim i As Integer
        If my_plist <> NXOpen.Tag.Null Then
            Try
                Dim numCol As Integer = 0
                Dim numRow As Integer = 0
                Dim rowTag As NXOpen.Tag = NXOpen.Tag.Null
                Dim colTag As NXOpen.Tag = NXOpen.Tag.Null
                Dim cellTag As NXOpen.Tag = NXOpen.Tag.Null
                Dim cellDispOb() As NXOpen.DisplayableObject
                ReDim cellDispOb(1)
                Dim cPrefs As UFTabnot.CellPrefs = Nothing

                ufs.Tabnot.AskNmColumns(my_plist, numCol)
                ufs.Tabnot.AskNmRows(my_plist, numRow)

                If numCol > 0 And numRow > 0 Then
                    ufs.Disp().SetDisplay(UFConstants.UF_DISP_SUPPRESS_DISPLAY)
                    For r = 0 To numRow - 1
                        ufs.Tabnot.AskNthRow(my_plist, r, rowTag)
                        For c = 0 To numCol - 1
                            ufs.Tabnot.AskNthColumn(my_plist, c, colTag)
                            ufs.Tabnot.AskCellAtRowCol(rowTag, colTag, cellTag)
                            'MsgBox(cellTag.ToString())
                            ufs.Tabnot.AskCellPrefs(cellTag, cPrefs)
                            For i = 0 To cPrefs.fit_methods.Length - 1
                                'MsgBox(cPrefs.fit_methods(i).ToString())
                                cPrefs.fit_methods(1) = UFTabnot.FitMethod.FitMethodNone
                            Next
                            'cPrefs.fit_methods.
                            cPrefs.fit_methods(0) = UFTabnot.FitMethod.FitMethodAutoSizeRow
                            cPrefs.fit_methods(1) = UFTabnot.FitMethod.FitMethodAutoSizeText

                            ufs.Tabnot.SetCellPrefs(cellTag, cPrefs)

                        Next
                    Next
                    ufs.Disp().SetDisplay(UFConstants.UF_DISP_UNSUPPRESS_DISPLAY)
                    ufs.Disp.RegenerateDisplay()
                End If
                MsgBox("Table Formatted")
                's.UpdateManager.DoUpdate(markId1)
                Dim filename As String = "c:\Temp\plist_out.txt"

                'master_model
                'top_level_only
                'leaves_only
                ufs.Plist.ListToFile(my_plist, filename, 1, "")
            Catch ex As Exception
                MsgBox("Row: " & r & " Col: " & c & vbCr & ex.ToString())
            End Try

            ufs.Disp().SetDisplay(UFConstants.UF_DISP_UNSUPPRESS_DISPLAY)
            ufs.Disp.RegenerateDisplay()

        End If

    End Sub

    Public Function GetPlistTagVal(ByRef partTag As NXOpen.Tag) As NXOpen.Tag

        Dim tempTag As NXOpen.Tag = NXOpen.Tag.Null
        Dim type As Integer
        Dim subtype As Integer

        Do
            ufs.Obj.CycleObjsInPart(partTag,
            UFConstants.UF_tabular_note_type, tempTag)

            ufs.Obj.AskTypeAndSubtype(tempTag, type, subtype)

            If subtype = UFConstants.UF_parts_list_subtype Then
                Return tempTag
            End If

        Loop Until tempTag = NXOpen.Tag.Null
        Return NXOpen.Tag.Null
    End Function

    Public Function GetUnloadOption(ByVal dummy As String) As Integer

        Return Session.LibraryUnloadOption.Immediately

    End Function

End Module


Recorded journal of setting the preferences of a single cell:
C#:
// NX 2206
// Journal created by 212331576 on Thu Oct 31 10:47:16 2024 Eastern Daylight Time
//
using System;
using NXOpen;

public class NXJournal
{
  public static void Main(string[] args)
  {
    NXOpen.Session theSession = NXOpen.Session.GetSession();
    NXOpen.Part workPart = theSession.Parts.Work;
    NXOpen.Part displayPart = theSession.Parts.Display;
    // ----------------------------------------------
    //   Menu: Edit->Table->Settings...
    // ----------------------------------------------
    NXOpen.Session.UndoMarkId markId1;
    markId1 = theSession.SetUndoMark(NXOpen.Session.MarkVisibility.Visible, "Start");
   
    NXOpen.DisplayableObject[] objects1 = new NXOpen.DisplayableObject[1];
    NXOpen.DisplayableObject displayableObject1 = ((NXOpen.DisplayableObject)workPart.FindObject("HANDLE R-171268"));
    objects1[0] = displayableObject1;
    NXOpen.Annotations.TableEditSettingsBuilder tableEditSettingsBuilder1;
    tableEditSettingsBuilder1 = workPart.SettingsManager.CreateTableEditSettingsBuilder(objects1);
   
    theSession.SetUndoMarkName(markId1, "Settings Dialog");
   
    NXOpen.Drafting.BaseEditSettingsBuilder[] editsettingsbuilders1 = new NXOpen.Drafting.BaseEditSettingsBuilder[1];
    editsettingsbuilders1[0] = tableEditSettingsBuilder1;
    workPart.SettingsManager.ProcessForMultipleObjectsSettings(editsettingsbuilders1);
   
    NXOpen.Session.UndoMarkId markId2;
    markId2 = theSession.SetUndoMark(NXOpen.Session.MarkVisibility.Invisible, "Settings");
   
    NXOpen.Annotations.TableCellStyleBuilder.FitMethodType[] fitmethods1 = new NXOpen.Annotations.TableCellStyleBuilder.FitMethodType[3];
    fitmethods1[0] = NXOpen.Annotations.TableCellStyleBuilder.FitMethodType.Wrap;
    fitmethods1[1] = NXOpen.Annotations.TableCellStyleBuilder.FitMethodType.AutoSizeRow;
    fitmethods1[2] = NXOpen.Annotations.TableCellStyleBuilder.FitMethodType.AutoSizeText;
    tableEditSettingsBuilder1.TableCell.SetFitMethods(fitmethods1);
   
    theSession.SetUndoMarkName(markId2, "Settings - FIt Methods");
   
    theSession.SetUndoMarkVisibility(markId2, null, NXOpen.Session.MarkVisibility.Visible);
   
    theSession.SetUndoMarkVisibility(markId1, null, NXOpen.Session.MarkVisibility.Invisible);
   
    NXOpen.Session.UndoMarkId markId3;
    markId3 = theSession.SetUndoMark(NXOpen.Session.MarkVisibility.Invisible, "Settings");
   
    NXOpen.Annotations.TableCellStyleBuilder.FitMethodType[] fitmethods2 = new NXOpen.Annotations.TableCellStyleBuilder.FitMethodType[2];
    fitmethods2[0] = NXOpen.Annotations.TableCellStyleBuilder.FitMethodType.AutoSizeRow;
    fitmethods2[1] = NXOpen.Annotations.TableCellStyleBuilder.FitMethodType.AutoSizeText;
    tableEditSettingsBuilder1.TableCell.SetFitMethods(fitmethods2);
   
    theSession.SetUndoMarkName(markId3, "Settings - FIt Methods");
   
    theSession.SetUndoMarkVisibility(markId3, null, NXOpen.Session.MarkVisibility.Visible);
   
    theSession.SetUndoMarkVisibility(markId1, null, NXOpen.Session.MarkVisibility.Invisible);
   
    NXOpen.Session.UndoMarkId markId4;
    markId4 = theSession.SetUndoMark(NXOpen.Session.MarkVisibility.Invisible, "Settings");
   
    NXOpen.Session.UndoMarkId markId5;
    markId5 = theSession.SetUndoMark(NXOpen.Session.MarkVisibility.Invisible, "Settings");
   
    NXOpen.NXObject nXObject1;
    nXObject1 = tableEditSettingsBuilder1.Commit();
   
    theSession.DeleteUndoMark(markId5, null);
   
    theSession.SetUndoMarkName(markId1, "Settings");
   
    tableEditSettingsBuilder1.Destroy();
   
    theSession.DeleteUndoMark(markId4, null);
   
    theSession.SetUndoMarkVisibility(markId1, null, NXOpen.Session.MarkVisibility.Visible);
   
    theSession.DeleteUndoMark(markId3, null);
   
    theSession.DeleteUndoMark(markId2, null);
   
    // ----------------------------------------------
    //   Menu: Tools->Journal->Stop Recording
    // ----------------------------------------------
   
  }
  public static int GetUnloadOption(string dummy) { return (int)NXOpen.Session.LibraryUnloadOption.Immediately; }
}
 
Replies continue below

Recommended for you

Have you tried the "theUfSession.Plist.ListToFile" method? You'd probably have to post process the file to get CSV output, but it may get you close.
 
Have you tried the "theUfSession.Plist.ListToFile" method? You'd probably have to post process the file to get CSV output, but it may get you close.
I am using the Plist.ListToFile method to export the list, however I didn't realize it only exported in tab-delimited, I'll have to look into that.

The issue with wrapped text that I am trying to solve is still an issue with that method. Each line of text in the parts list gets a line in the output file, so if an attribute is large enough to wrap the text to a new line inside the cell, the output file will have a mostly blank line, except for the wrapped text and that messes with the system we are importing the list into.
An example is below. The last column has "AUTOMATION" and "DIRECT" on 2 separate lines even though they are in the same cell in the parts list as "AUTOMATION DIRECT"
Code:
        17      1       STK              -               -                         TERMINAL WIRING BLOCK       DN-T10-A        AUTOMATION             
                                                                                                                                DIRECT

If I'm already post-processing the file programmatically to convert it to csv, this problem may no longer be an NX function problem though.
 
Sorry, I should have read your code more closely. I saw that you were iterating through each cell and assumed you were writing out the text yourself.... Which may be the way to go here. You could grab the displayed text from each cell and not have to worry about cell style preferences.
 
Sorry, I should have read your code more closely. I saw that you were iterating through each cell and assumed you were writing out the text yourself.... Which may be the way to go here. You could grab the displayed text from each cell and not have to worry about cell style preferences.
Oh, duh. If I'm making a function to export the parts list, I don't need the cells formatted correctly because we will just use the button that can handle it. Thanks!

Here's what I came up with. I couldn't get the Microsoft.Win32.SaveFileDialog working for some reason, so I just dump it to the user's Documents folder. SaveFileDialog was giving me an error "Type 'SaveFileDialog' is not defined."

Code:
Option Strict Off

Imports System
Imports System.IO
Imports Microsoft.Win32
Imports NXOpen
Imports NXOpen.Annotations
Imports NXOpen.Assemblies
Imports NXOpen.BlockStyler
Imports NXOpen.Diagramming
Imports NXOpen.UF

'Imports NXOpen.UI
Imports NXOpen.Utilities

Module NXJournal

    Dim s As Session = Session.GetSession()
    Dim ufs As UFSession = UFSession.GetUFSession()
    Dim uis As UI = UI.GetUI
    Dim lw As ListingWindow = s.ListingWindow
    Dim wPart As NXOpen.Part = s.Parts.Work

    Sub Main()
        lw.Open()
        Dim my_plist As NXOpen.Tag = NXOpen.Tag.Null
        Dim dispPart As NXOpen.Tag = s.Parts.Display.Tag
        Dim markId1 As NXOpen.Session.UndoMarkId = Nothing
        markId1 = s.SetUndoMark(NXOpen.Session.MarkVisibility.Visible, "Do update")

        '// Get name of work part
        Dim workPartName = wPart.Name
        'uis.NXMessageBox.Show("", NXMessageBox.DialogType.Information, workPartName)
        my_plist = GetPlistTagVal(dispPart)

        Dim r As Integer
        Dim c As Integer
        Dim i As Integer
        Dim dDir As String
        If my_plist <> NXOpen.Tag.Null Then
            dDir = System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)

            Dim filename As String = Path.Combine(dDir, workPartName.Substring(0, workPartName.IndexOf(" ")) & " BOM Export" & ".txt")
            'uis.NXMessageBox.Show("", NXMessageBox.DialogType.Information, filename)

            Dim out As StreamWriter = New StreamWriter(filename)

            Try

                Dim numCol As Integer = 0
                Dim numRow As Integer = 0
                Dim rowTag As NXOpen.Tag = NXOpen.Tag.Null
                Dim colTag As NXOpen.Tag = NXOpen.Tag.Null
                Dim cellTag As NXOpen.Tag = NXOpen.Tag.Null
                Dim cTxt As String = ""
                Dim cr(0) As Byte
                cr(0) = 10

                '// Get number of rows and columns in parts list
                ufs.Tabnot.AskNmColumns(my_plist, numCol)
                ufs.Tabnot.AskNmRows(my_plist, numRow)

                '// Prompt user if correct number of columns is not present
                '// 9 is preferred, 8 is acceptable. REV column is not necesary
                If numCol < 8 Then
                    uis.NXMessageBox.Show("Parts List Column Count Error", NXMessageBox.DialogType.Error, "Not enough columns in Parts List")
                    Exit Sub
                ElseIf numCol > 9 Then
                    uis.NXMessageBox.Show("Parts List Column Count Error", NXMessageBox.DialogType.Error, "Too many columns in Parts List")
                    Exit Sub
                End If


                '// Write header line
                out.WriteLine("REV, DET , QTY,  MATERIAL  , TREATMENT    ,ATERIAL SIZE  ,     DESCRIPTION    ,PART NUMBER ,CATALOG")

                '// Iterate all cells in Parts List and write line data to a file
                If numCol > 0 And numRow > 0 Then
                    'ufs.Disp().SetDisplay(UFConstants.UF_DISP_SUPPRESS_DISPLAY)

                    For r = 0 To numRow - 1
                        '// Get Tag for current row
                        ufs.Tabnot.AskNthRow(my_plist, r, rowTag)
                        Dim outStr As String = ""
                        For c = 0 To numCol - 1
                            '// Get Tag for current column
                            ufs.Tabnot.AskNthColumn(my_plist, c, colTag)
                            '// Get Tag for current cell
                            ufs.Tabnot.AskCellAtRowCol(rowTag, colTag, cellTag)
                            '// Get text for current cell
                            ufs.Tabnot.AskEvaluatedCellText(cellTag, cTxt)

                            '// Concat string to output for this line
                            '// If column count is 8, include leading ',' for missing REV column
                            If c > 0 Or numCol = 8 Then
                                '// Concat all cells in row to comma separated line, using the Replace command to remove carriage return characters
                                outStr += "," + cTxt.Replace(System.Text.Encoding.ASCII.GetString(cr), "")
                            Else
                                outStr = cTxt
                            End If

                        Next
                        out.WriteLine(outStr)

                        '// Get Ascii code for specific character. Used to find code to use to replace carriage return characters
                        'Dim asciis() As Byte = System.Text.Encoding.ASCII.GetBytes(cTxt)
                        'If r = 16 Then
                        ' MsgBox(asciis(10))
                        'End If
                    Next
                    ufs.Disp().SetDisplay(UFConstants.UF_DISP_UNSUPPRESS_DISPLAY)
                    ufs.Disp.RegenerateDisplay()
                End If

                'master_model
                'top_level_only
                'leaves_only

                uis.NXMessageBox.Show("", NXMessageBox.DialogType.Information, "BOM exported to " & filename)
            Catch ex As Exception
                MsgBox(vbCr & ex.ToString())
            End Try
            ufs.Disp().SetDisplay(UFConstants.UF_DISP_UNSUPPRESS_DISPLAY)
            ufs.Disp.RegenerateDisplay()
            out.Close()

        End If

    End Sub

    Public Function GetPlistTagVal(ByRef partTag As NXOpen.Tag) As NXOpen.Tag

        Dim tempTag As NXOpen.Tag = NXOpen.Tag.Null
        Dim type As Integer
        Dim subtype As Integer

        Do
            ufs.Obj.CycleObjsInPart(partTag,
            UFConstants.UF_tabular_note_type, tempTag)

            ufs.Obj.AskTypeAndSubtype(tempTag, type, subtype)

            If subtype = UFConstants.UF_parts_list_subtype Then
                Return tempTag
            End If

        Loop Until tempTag = NXOpen.Tag.Null
        Return NXOpen.Tag.Null
    End Function

    Public Function GetUnloadOption(ByVal dummy As String) As Integer

        Return Session.LibraryUnloadOption.Immediately

    End Function

End Module
 

Part and Inventory Search

Sponsor