Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

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

Importing multiple named points 1

Status
Not open for further replies.

Rich1121

Automotive
Sep 22, 2023
2
Hey Everyone,

I have a spreadsheet of about 500 named points, and I can't figure out how to import them with their name into NX. I've used the import points from file command and gotten the points in without their name but haven't been able to figure out how to rename them without manually renaming each point. I'm working with NX 2027. I also attached a screen grab of an example of what I'm trying to import ( point name, x,y,z). Any help would be appreciated!


Rich
 
 https://files.engineering.com/getfile.aspx?folder=e5d04cd6-2c09-429b-a0bb-263837614e81&file=Capture.PNG
Replies continue below

Recommended for you

Below is a journal that I wrote back in 2014. I have not tested it recently, but looking over the code, I think it will still work. Note that it only creates unassociative points (not point features).

Code:
'NXJournaling.com
'December 15, 2014
'import points in Excel file to the current work part
'Journal will prompt user to choose an existing Excel file.
'The current worksheet will be read for point information.
'Header information is assumed to be in row 1: col A - "name", col B - "X", col C - "Y", col D - "Z"
'The journal will read successive rows until a blank cell (in column A) is found.
'If any XYZ information cannot be parsed, that row will be skipped.
'A named point object (unassociative) will be created for each row successfully parsed.

Option Strict Off
Imports System
Imports System.Windows.Forms
Imports NXOpen
Imports NXOpenUI

Module import_points

    Dim theSession As Session = Session.GetSession()
    Dim theUISession As UI = UI.GetUI

    Dim excelFileName As String

    Dim objExcel As Object
    Dim objWorkbook As Object
    Dim objWorksheet As Object

    Sub Main()

        If IsNothing(theSession.Parts.Work) Then
            'active part required
            Return
        End If

        Dim workPart As Part = theSession.Parts.Work
        Dim lw As ListingWindow = theSession.ListingWindow
        lw.Open()

        OpenExcelFile()

        'row 1 headers: column A - name, column B - X, column C - Y, column D - Z
        Dim row As Integer = 2
        Dim column As Integer = 1

        Dim ptName As String
        Dim ptPos As Point3d

        Do Until objWorksheet.Cells(row, column).Value = ""
            'get point name from column 1
            ptName = objWorksheet.cells(row, column).Value
            column += 1

            Dim tmpDouble As Double
            'get X coordinate from column 2
            If Double.TryParse(objWorksheet.cells(row, column).value, tmpDouble) Then
                ptPos.X = tmpDouble
            Else
                'could not parse cell value, skip this line
                row += 1
                column = 1
                Continue Do
            End If

            column += 1
            If Double.TryParse(objWorksheet.cells(row, column).value, tmpDouble) Then
                ptPos.Y = tmpDouble
            Else
                'could not parse cell value, skip this line
                row += 1
                column = 1
                Continue Do
            End If

            column += 1
            If Double.TryParse(objWorksheet.cells(row, column).value, tmpDouble) Then
                ptPos.Z = tmpDouble
            Else
                'could not parse cell value, skip this line
                row += 1
                column = 1
                Continue Do
            End If

            'create point
            Dim newPt As Point
            newPt = workPart.Points.CreatePoint(ptPos)
            newPt.SetVisibility(SmartObject.VisibilityOption.Visible)
            newPt.SetName(ptName)

            'increment row value
            row += 1
            'reset column value
            column = 1

        Loop

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

        lw.Close()

    End Sub

    Sub OpenExcelFile()

        Dim markId1 As Session.UndoMarkId
        markId1 = theSession.SetUndoMark(Session.MarkVisibility.Visible, "Excel import points")

        Dim excelFileExists As Boolean = False
        Dim row As Long = 1
        Dim column As Long = 1

        'Allow the user to select an Excel file.
        Dim SaveFileDialog1 As New SaveFileDialog
        With SaveFileDialog1
            .Title = "Import points from Excel"
            .InitialDirectory = Environment.SpecialFolder.Desktop
            .Filter = "Excel files (*.xlsx)|*.xlsx|Macro enabled Excel files (*.xlsm)|*.xlsm|All files (*.*)|*.*"
            .FilterIndex = 1
            .RestoreDirectory = True
            .OverwritePrompt = False
            .FileName = theSession.Parts.Work.Leaf
            If .ShowDialog() = DialogResult.OK Then
                excelFileName = .FileName
            Else
                Exit Sub
            End If
        End With

        'create Excel object
        Try
            objExcel = CreateObject("Excel.Application")
        Catch ex As Exception
            'handle error, warn user
            Return
        End Try

        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 IO.File.Exists(excelFileName) Then
            'Open the Excel file
            excelFileExists = True
            objWorkbook = objExcel.Workbooks.Open(excelFileName)
        Else
            theUISession.NXMessageBox.Show("Error", NXMessageBox.DialogType.Error, "Excel file does not exist")
            theSession.UndoToMark(markId1, "journal")
            Exit Sub
        End If

        If objWorkbook Is Nothing Then
            theUISession.NXMessageBox.Show("Error", NXMessageBox.DialogType.Error, "Could not open Excel file: " & excelFileName & ControlChars.NewLine & "journal exiting.")
            theSession.UndoToMark(markId1, "journal")
            Exit Sub
        End If

        objWorksheet = objWorkbook.ActiveSheet

    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

www.nxjournaling.com
 
Hey Cowski,
This worked like a dream. Thanks for your help!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor