Surface from points help

Forums: 

I am importing points from an Excel file, processing it row by row. Each row contains 3 points. I was going to attempt to surface these points similar to the SNAP sample code below:

Dim p00, p01, p02, p03, p10, p11, p12, p13, p20, p21, p22, p23 As Position

p00 = { 0, 0, 0 } : p01 = { 0, 3, 1 } : p02 = { 0, 6, 1 } : p03 = { 0, 9, 0 }
p10 = { 3, 0, 1 } : p11 = { 3, 3, 2 } : p12 = { 3, 6, 2 } : p13 = { 3, 9, 1 }
p20 = { 6, 0, 0 } : p21 = { 6, 3, 1 } : p22 = { 6, 6, 2 } : p23 = { 6, 9, 0 }

Dim thruPoints As Position(,) = {{p00, p01, p02, p03}, {p10, p11, p12, p13}, {p20, p21, p22, p23} }

' Create Bezier patch using default nodes
BezierPatchThroughPoints(thruPoints)

I am getting stuck on the thruPoints variable though. How do I properly create thruPoints from:
For i As Integer = intStartingRow To intLastRow

Dim ptStart As New Position With {
.X = xlsWorkSheet.Cells(i, "A").Value,
.Y = xlsWorkSheet.Cells(i, "B").Value,
.Z = xlsWorkSheet.Cells(i, "C").Value
}
Dim ptMid As New Position With {
.X = xlsWorkSheet.Cells(i, "D").Value,
.Y = xlsWorkSheet.Cells(i, "E").Value,
.Z = xlsWorkSheet.Cells(i, "F").Value
}
Dim ptEnd As New Position With {
.X = xlsWorkSheet.Cells(i, "D").Value,
.Y = xlsWorkSheet.Cells(i, "E").Value,
.Z = xlsWorkSheet.Cells(i, "F").Value
}
Next

I don't currently have access to a SNAP license; from the documentation, the SNAP position appears to be very similar to a Point3d structure. I'm interpreting the question as "how do I get the point information from excel to a 2D array?" (if I have misinterpreted, please clarify). The code below illustrates how to do this. For my simple test file, I had 3 rows with 3 points per row. The journal is hard coded to start in cell A1 and look for 3 points in 3 rows. To make the journal more robust, I'd suggest querying excel for the number of rows and columns used rather than hard-coding the range; change the "numRows" and "ptsPerRow" variables to the range values returned from Excel.

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

Module Module90
Dim theSession As Session = Session.GetSession()
Dim theUfSession As UFSession = UFSession.GetUFSession
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.BaseWork) Then
'active part required
Return
End If

OpenExcelFile()

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

Dim numRows As Integer = 3
Dim ptsPerRow As Integer = 3
Dim allPositions(numRows - 1, ptsPerRow - 1) As Point3d

For i As Integer = 1 To numRows
Dim tempCoordinates As New List(Of Double)
Dim curPt As Integer = 0

For j As Integer = 1 To ptsPerRow * 3
Dim value As Double
value = objWorksheet.Cells(i, j).Value
'lw.WriteLine("value = " & value.ToString)
tempCoordinates.Add(value)
'lw.WriteLine("tempCoordinates.Count = " & tempCoordinates.Count.ToString)
If tempCoordinates.Count = 3 Then
Dim tempPt As New Point3d(tempCoordinates.Item(0), tempCoordinates.Item(1), tempCoordinates.Item(2))
allPositions(i - 1, curPt) = tempPt
curPt += 1
tempCoordinates.Clear()
'lw.WriteLine(tempPt.ToString)
End If
Next
Next

'double check coordinates
For i As Integer = 0 To numRows - 1
Dim outputLine As String = ""
For j As Integer = 0 To ptsPerRow - 1
outputLine &= allPositions(i, j).ToString
Next
lw.WriteLine(outputLine)
Next

lw.Close()

objWorkbook.Close()
objExcel.Quit()
Cleanup(objWorksheet, objWorkbook, objExcel)

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

Private Sub Cleanup(ParamArray objs As Object())
GC.Collect()
GC.WaitForPendingFinalizers()
For Each obj As Object In objs
System.Runtime.InteropServices.Marshal.ReleaseComObject(obj)
obj = Nothing
Next
End Sub

Public Function GetUnloadOption(ByVal dummy As String) As Integer

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

End Function

End Module

> I don't currently have access to a SNAP license; from the documentation,
> the SNAP position appears to be very similar to a Point3d structure

Yes, they're quite similar. The difference is that Snap.Position has some useful functions and built-in operators. The same goes for Snap.Vector versus NXOpen.Vector3d.

Even if you don't have a SNAP license, you can still use Position and Vector objects, because they are included in the freely available MiniSnap subset.

ciao,
Thanks for the reminder about mini SNAP, I had forgotten about it. I could have made use of it in this example.