'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