tlemonds
Aerospace
- Feb 1, 2014
- 20
Hey guys,
So a few months ago I posted some questions about how to get started with VB in NX. You guys gave me some great tips and even provided me with example code! I'm now familiar enough with VB where I've written a few simple scripts to run in NX and a few scripts to run in Excel as well. However, I'm curious about beginning/if it is possible to write scripts that communicate between Excel and VB. For instance, I'm creating a parametric wing creation tool. The way I currently have it set up is that my NX sketch provides me the "outline" of the wing given user-defined expression inputs, I run a VB code in NX to export these coordinates to a .txt file. I then open up the embedded spreadsheet where these .txt files automatically refresh and are manipulated to provide me eight sets of airfoil coordinates in global coordinates and a macro exports them to .dat files. I then close the spreadsheet, give control back to NX, and run a VB script to populate the points and create splines among the points. This is beginning to seem like a very inefficient process. I was wondering if there was some way to write a VB script that can basically directly take the global coordinates of these "control" points and stick them in my Excel spreadsheet. Furthermore, I was hoping the VB script could simply import the manipulated point sets by calling them from a range of cells within the spreadsheet. I have no idea if this is remotely possible, not worth my time, or really easy. I think the actual coding would not be very difficult, but I'm not sure if you can run a VB script in NX that can call upon cells in an embedded spreadsheet, and vice versa. I've posted the script that I run within NX to export the "control" point coordinates to a .txt file. I'm thinking the trick lies in the first few lines of code where maybe you specify that you want to "Import" the embedded spreadsheet data or whatnot but I really have no idea where to start....
Option Strict Off
Imports System
Imports System.IO
Imports NXOpen
Imports NXOpen.Utilities
Imports NXOpen.Features
Imports NXOpenUI
Imports NXOpen.UF
Module PointsAlongCurveToTextFile
Dim theSession As Session = Session.GetSession()
Dim workPart As Part = theSession.Parts.Work
Dim displayPart As Part = theSession.Parts.Display
Sub Main()
Dim obj1() As NXObject
Dim pointset1 As PointSet = Nothing
Dim feats As FeatureCollection = workPart.Features
Dim name1 As String = Nothing
Dim objStreamWriter As StreamWriter
objStreamWriter = New StreamWriter("C:\Users\tlemonds\Documents\Applied Project\journals\coords1.txt")
Dim cnt1 as Integer = 1
For Each ft1 As Feature In feats
name1 = ft1.GetFeatureName
If name1.Contains("Point") Then
obj1 = ft1.GetEntities()
' write out to file
WriteCoords(obj1, cnt1, objStreamWriter, ft1)
cnt1 += 1
End If
Next
'Close the file.
objStreamWriter.Close()
End Sub
Public Sub SetUpCoordsFile()
Dim file As System.IO.StreamWriter
file = My.Computer.FileSystem.OpenTextFileWriter("C:\Users\tlemonds\Documents\Applied Project\journals\coords1.txt", True)
file.Close()
End Sub
Public Sub WriteCoords(ByVal obj1() As NXObject, ByVal cnt1 as Integer, ByVal objstreamwriter As StreamWriter, ByVal feat1 As Feature)
Dim xstr As String = Nothing
Dim ystr As String = Nothing
Dim zstr As String = Nothing
Dim name1 As String = feat1.GetFeatureName
For i As Integer = 0 To obj1.Length - 1
Dim pt As Point = DirectCast(obj1(i), Point)
xstr = FormatNumber(pt.Coordinates.X, 3, , , TriState.True)
ystr = FormatNumber(pt.Coordinates.Y, 3, , , TriState.True).PadLeft(15)
zstr = FormatNumber(pt.Coordinates.Z, 3, , , TriState.True).PadLeft(15)
objstreamwriter.WriteLine(xstr & ystr & zstr)
Next
End Sub
Public Function GetUnloadOption(ByVal dummy As String) As Integer
'----Other unload options-------
'Unloads the image immediately after execution within NX
GetUnloadOption = NXOpen.Session.LibraryUnloadOption.Immediately
End Function
End Module
So a few months ago I posted some questions about how to get started with VB in NX. You guys gave me some great tips and even provided me with example code! I'm now familiar enough with VB where I've written a few simple scripts to run in NX and a few scripts to run in Excel as well. However, I'm curious about beginning/if it is possible to write scripts that communicate between Excel and VB. For instance, I'm creating a parametric wing creation tool. The way I currently have it set up is that my NX sketch provides me the "outline" of the wing given user-defined expression inputs, I run a VB code in NX to export these coordinates to a .txt file. I then open up the embedded spreadsheet where these .txt files automatically refresh and are manipulated to provide me eight sets of airfoil coordinates in global coordinates and a macro exports them to .dat files. I then close the spreadsheet, give control back to NX, and run a VB script to populate the points and create splines among the points. This is beginning to seem like a very inefficient process. I was wondering if there was some way to write a VB script that can basically directly take the global coordinates of these "control" points and stick them in my Excel spreadsheet. Furthermore, I was hoping the VB script could simply import the manipulated point sets by calling them from a range of cells within the spreadsheet. I have no idea if this is remotely possible, not worth my time, or really easy. I think the actual coding would not be very difficult, but I'm not sure if you can run a VB script in NX that can call upon cells in an embedded spreadsheet, and vice versa. I've posted the script that I run within NX to export the "control" point coordinates to a .txt file. I'm thinking the trick lies in the first few lines of code where maybe you specify that you want to "Import" the embedded spreadsheet data or whatnot but I really have no idea where to start....
Option Strict Off
Imports System
Imports System.IO
Imports NXOpen
Imports NXOpen.Utilities
Imports NXOpen.Features
Imports NXOpenUI
Imports NXOpen.UF
Module PointsAlongCurveToTextFile
Dim theSession As Session = Session.GetSession()
Dim workPart As Part = theSession.Parts.Work
Dim displayPart As Part = theSession.Parts.Display
Sub Main()
Dim obj1() As NXObject
Dim pointset1 As PointSet = Nothing
Dim feats As FeatureCollection = workPart.Features
Dim name1 As String = Nothing
Dim objStreamWriter As StreamWriter
objStreamWriter = New StreamWriter("C:\Users\tlemonds\Documents\Applied Project\journals\coords1.txt")
Dim cnt1 as Integer = 1
For Each ft1 As Feature In feats
name1 = ft1.GetFeatureName
If name1.Contains("Point") Then
obj1 = ft1.GetEntities()
' write out to file
WriteCoords(obj1, cnt1, objStreamWriter, ft1)
cnt1 += 1
End If
Next
'Close the file.
objStreamWriter.Close()
End Sub
Public Sub SetUpCoordsFile()
Dim file As System.IO.StreamWriter
file = My.Computer.FileSystem.OpenTextFileWriter("C:\Users\tlemonds\Documents\Applied Project\journals\coords1.txt", True)
file.Close()
End Sub
Public Sub WriteCoords(ByVal obj1() As NXObject, ByVal cnt1 as Integer, ByVal objstreamwriter As StreamWriter, ByVal feat1 As Feature)
Dim xstr As String = Nothing
Dim ystr As String = Nothing
Dim zstr As String = Nothing
Dim name1 As String = feat1.GetFeatureName
For i As Integer = 0 To obj1.Length - 1
Dim pt As Point = DirectCast(obj1(i), Point)
xstr = FormatNumber(pt.Coordinates.X, 3, , , TriState.True)
ystr = FormatNumber(pt.Coordinates.Y, 3, , , TriState.True).PadLeft(15)
zstr = FormatNumber(pt.Coordinates.Z, 3, , , TriState.True).PadLeft(15)
objstreamwriter.WriteLine(xstr & ystr & zstr)
Next
End Sub
Public Function GetUnloadOption(ByVal dummy As String) As Integer
'----Other unload options-------
'Unloads the image immediately after execution within NX
GetUnloadOption = NXOpen.Session.LibraryUnloadOption.Immediately
End Function
End Module