How to import points from MS Excel in NX?

Missing small

Use these scripts to import 3D coordinates from MS Excel file into NX model (unfortunately sample script and files attachments not allowed).

  1. Step 1:

    Press ALT+F8 keys, select the file which contains the following code (extension is .vb):

    Option Strict Off
    Imports System
    Imports NXOpen
    Module NXJournal

    Sub Main(ByVal args() As String)
    Dim theSession As Session = Session.GetSession()
    Dim workPart As Part = theSession.Parts.Work()
    Dim pnt As Point3d
    Dim newPoint As Point
    Dim X, Y, Z As Double
    Dim EXCEL = CreateObject("Excel.Application")
    EXCEL.Visible = False
    Dim Doc = EXCEL.Workbooks.Open("C:\Siemens NX\KBE\Points.xlsx", ReadOnly:=True)
    Dim Sheets = EXCEL.Sheets
    For i As Integer = 1 To Doc.Sheets.Count
    Dim Sheet = Doc.Sheets.Item(i)
    Dim pointCounter As Integer
    For pointCounter = 0 To Sheet.UsedRange.Rows.Count - 1
    X = Sheet.Cells((pointCounter + 1), 1).Value
    Y = Sheet.Cells((pointCounter + 1), 2).Value
    Z = Sheet.Cells((pointCounter + 1), 3).Value
    pnt = New Point3d(X, Y, Z)
    newPoint = workPart.Points.CreatePoint(pnt)
    newPoint.SetVisibility(SmartObject.VisibilityOption.Visible)
    Next
    workPart.ModelingViews.WorkView.Fit()
    Next
    Doc.Close()
    EXCEL.Quit()
    End Sub
    End Module

  2. Step 2:

    The Excel file location is in this folder:
    "C:\Siemens NX\KBE\Points.xlsx"
    Change the folder or location as you desired.

    The result can be seen on this video:
    https://www.youtube.com/watch?v=4V-NsKdTUmY

  3. Step 3:

    Faster method with openfile dialog, press ALT+F8 keys, select the file which contains the following code (extension is .vb):

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

    Module NXJournal
    Sub Main(ByVal args() As String)

    Dim openFileDialog1 As New OpenFileDialog()
    openFileDialog1.Filter = "Excel files (*.xls, *.xlsx)|*.xls;*.xlsx|All files (*.*)|*.*"
    openFileDialog1.FilterIndex = 1
    If openFileDialog1.ShowDialog() = DialogResult.OK Then
    Dim theSession As Session = Session.GetSession()
    Dim workPart As Part = theSession.Parts.Work
    Dim pointsFromFile As NXOpen.GeometricUtilities.PointsFromFileBuilder
    pointsFromFile = workPart.CreatePointsFromFileBuilder()
    Dim EXCEL = CreateObject("Excel.Application")
    EXCEL.Visible = False
    EXCEL.DisplayAlerts = False
    Dim Doc = EXCEL.Workbooks.Open(openFileDialog1.FileName, ReadOnly:=True)
    Dim fileFolder As String = IO.Path.GetDirectoryName(openFileDialog1.FileName)
    Dim Sheets = EXCEL.Sheets
    For i As Integer = 1 To Doc.Sheets.Count
    Dim Sheet = Doc.Sheets.Item(i)
    Dim newCSV As String = fileFolder & "\" & Sheet.Name & ".csv"
    Sheet.SaveAs(newCSV, 20, , , , , , 2)
    pointsFromFile.FileName = newCSV
    pointsFromFile.CoordinateOption = NXOpen.GeometricUtilities.PointsFromFileBuilder.Options.Absolute
    pointsFromFile.Commit()
    workPart.ModelingViews.WorkView.Fit()
    Next
    pointsFromFile.Destroy()
    Doc.Close()
    EXCEL.Quit()
    For Each FileDelete As String In IO.Directory.GetFiles(fileFolder & "\", "*.csv")
    IO.File.Delete(FileDelete)
    Next
    End If
    End Sub
    End Module

    The result can be seen on this video:
    https://www.youtube.com/watch?v=zpZsMIH21iU

Comments

Please log in to add comments
  • Feed
    Hegedűs György

    Hi Neys Schreiner,

    how many splines do you have? Did you use my excel sample? If you did you should see 10 splines in your window. I test the script above just now, it work, but seems to slow.
    The spline points are stored in the excel sheet, the format like this one:
    93.28529 -58.29111 -65.3806
    94.15698 -56.87234 -66.38992
    94.6904 -55.9797 -66.47845
    95.15935 -55.17878 -66.41687
    95.59131 -54.42702 -66.27813
    95.99736 -53.70761 -66.09041
    96.38336 -53.01178 -65.86814
    96.75292 -52.33423 -65.61984
    97.10848 -51.67149 -65.35108
    97.45177 -51.0211 -65.0657
    97.78409 -50.38125 -64.7665
    98.10648 -49.75056 -64.45556
    98.41973 -49.12795 -64.13453
    The columns are the x, y, z coordinates of the points. If you have more than one sheet in your excel file, you will have more than one spline.

    If you have question, do not hesitate!

    Hope this helps!

    July 10th, 2018 22:27
  • Missing feed
    Neys Schreiner

    Hi there. I downloaded your .vb file and ran it, however, it doesn't seem to complete. The work in progress pop up doesn't disappear and hangs when I try to stop operation. I can see the spine created in the vis window. Can you help?

    July 10th, 2018 16:20
  • Missing feed
    Alfonso Cerdeño

    Hello, do you know where these files and scripts can be found? Thanks!

    June 9th, 2017 11:30