Option Strict Off
Imports System
Imports NXOpen
Imports NXOpen.UF
Module NXJournal
Sub Main()
Dim theSession As Session = Session.GetSession()
Dim ufs As UFSession = UFSession.GetUFSession()
Dim workPart As Part = theSession.Parts.Work
Dim displayPart As Part = theSession.Parts.Display
Dim lw As ListingWindow = theSession.ListingWindow
Dim mySelectedObjects() As NXObject
Dim myResponse As Selection.Response
Dim tagList() As NXOpen.Tag
Dim exportFileName As String = Nothing
Dim i As Integer = 0
lw.Open()
myResponse = SelectObjects(mySelectedObjects)
If (myResponse = Selection.Response.Cancel) OrElse (myResponse = Selection.Response.Back) Then
'user canceled selection, exit journal
Exit Sub
End If
ReDim tagList(mySelectedObjects.GetUpperBound(0))
For i = 0 To mySelectedObjects.GetUpperBound(0)
tagList(i) = mySelectedObjects(i).Tag
Next
'return the full path of the work part
exportFileName = workPart.FullPath
'trim off ".prt" and add ".x_t"
exportFileName = exportFileName.Remove(exportFileName.Length - 4, 4) + ".x_t"
'if this file already exists, delete it
If My.Computer.FileSystem.FileExists(exportFileName) Then
My.Computer.FileSystem.DeleteFile(exportFileName)
End If
Try
ufs.Ps.ExportData(tagList, exportFileName)
Catch ex As NXException
lw.WriteLine("*** ERROR ***")
lw.WriteLine(ex.ErrorCode.ToString & " : " & ex.Message)
End Try
lw.Close()
End Sub
Function SelectObjects(ByRef selobj() As NXObject) As Selection.Response
Dim theUI As UI = UI.GetUI
Dim prompt as String = "Select Solid Bodies"
Dim title As String = "Selection"
Dim includeFeatures As Boolean = False
Dim keepHighlighted As Boolean = False
Dim selAction As Selection.SelectionAction = _
Selection.SelectionAction.ClearAndEnableSpecific
Dim scope As Selection.SelectionScope = _
Selection.SelectionScope.AnyInAssembly
Dim selectionMask_array(0) As Selection.MaskTriple
With selectionMask_array(0)
.Type = UFConstants.UF_solid_type
.Subtype = 0
.SolidBodySubtype = UFConstants.UF_UI_SEL_FEATURE_SOLID_BODY
End With
Dim resp As Selection.Response = _
theUI.SelectionManager.SelectObjects( _
prompt, title, scope, selAction, _
includeFeatures, keepHighlighted, _
selectionMask_array, selobj)
Return resp
End Function
End Module
Regard - How to change below code for Step file export .
I changed the code as ".stp" below instead ".x_t"
exportFileName = exportFileName.Remove(exportFileName.Length - 4, 4) + ".x_t"
But nothing is worked out.
what the change in the code will sort the issue
Gopal Parthasarathy
CERT/FEA Engineer
B/E Aerospace
re: export step
The file extension doesn't control the export format. The ufs.Ps.ExportData function will only output parasolid files. To export a step file, you will need to use a Step2xxCreator object. There is an example journal here that outputs a step 214 file:
http://www.eng-tips.com/viewthread.cfm?qid=321704
Regard - How to change below code for Step file export .
I have done as per suggestion
But I couldn't could you help me on these error
'**********************************
Option Strict Off
Imports System
Imports System.Collections.Generic
Imports NXOpen
Module Module1
Dim theSession As Session = Session.GetSession()
Dim workPart As Part = theSession.Parts.Work
Dim displayPart As Part = theSession.Parts.Display
Dim lw As ListingWindow = theSession.ListingWindow
Sub Main()
lw.Open()
Dim mySelectedObjects As New List(Of NXObject)
Dim tempSelectedObjects() As NXObject
Dim step214File As String
Dim outputPath As String = IO.Path.GetDirectoryName(workPart.FullPath)
outputFile = IO.Path.Combine(outputPath, outputFile & ".stp")
Dim STEP214UG_DIR As String = theSession.GetEnvironmentVariableValue("STEP214UG_DIR")
step214File = IO.Path.Combine(STEP214UG_DIR, "ugstep214.def")
If Not IO.File.Exists(step214File) Then
MsgBox("The step214 settings file (ugstep214.def) was not found." & vbCrLf & _ "This journal will now exit.", vbOKOnly + vbCritical) Exit Sub
Else 'lw.WriteLine("STEP214 definition file found at: " & step214File)
End If
AddSolidsFromLayer(1, mySelectedObjects)
AddSolidsFromLayer(4, mySelectedObjects)
AddSolidsFromLayer(5, mySelectedObjects)
AddSolidsFromLayer(63, mySelectedObjects)
tempSelectedObjects = workPart.Layers.GetAllObjectsOnLayer(41)
For Each obj As NXObject In tempSelectedObjects
If TypeOf obj Is Line Then
mySelectedObjects.Add(obj)
End If
Next
Dim step214Creator1 As Step214Creator
step214Creator1 = theSession.DexManager.CreateStep214Creator()
step214Creator1.SettingsFile = step214File
step214Creator1.ObjectTypes.Solids = True
step214Creator1.LayerMask = "1-256"
step214Creator1.InputFile = workPart.FullPath
step214Creator1.OutputFile = outputFile
step214Creator1.FileSaveFlag = False
step214Creator1.ExportSelectionBlock.SelectionScope = ObjectSelector.Scope.SelectedObjects
Dim added1 As Boolean added1 = step214Creator1.ExportSelectionBlock.SelectionComp.Add(mySelectedObjects.ToArray)
Dim nXObject1 As NXObject nXObject1 = step214Creator1.Commit()
step214Creator1.Destroy() lw.Close()
End Sub
Sub AddSolidsFromLayer(ByVal layer As Integer, ByRef objList As List(Of NXObject))
Dim tempselectedobjects() As NXObject Dim tempSolidObj As Body
tempselectedobjects = workPart.Layers.GetAllObjectsOnLayer(layer)
For Each obj As NXObject In tempselectedobjects If TypeOf obj Is Body Then
tempSolidObj = CType(obj, Body) If tempSolidObj.IsSolidBody Then
objList.Add(tempSolidObj) End If
End If
Next
End Sub
Function SelectObjects(ByVal prompt As String, _ ByRef selObj As NXObject()) As Selection.Response
Dim theUI As UI = UI.GetUI
Dim typeArray() As Selection.SelectionType = _ {Selection.SelectionType.All}
Dim resp As Selection.Response = theUI.SelectionManager.SelectObjects( _ prompt, "Select Objects for STEP Export", _ Selection.SelectionScope.WorkPart, _ False, typeArray, selObj)
If resp = Selection.Response.ObjectSelected Or _ resp = Selection.Response.ObjectSelectedByName Or _ resp = Selection.Response.Ok Then
Return Selection.Response.Ok Else
Return Selection.Response.Cancel
End If
End Function
Public Function GetUnloadOption(ByVal dummy As String) As Integer
'Unloads the image when the NX session terminates GetUnloadOption = NXOpen.Session.LibraryUnloadOption.AtTermination
End Function
End Module
'**********
List of Errors
'************
Name ' Outputfile ' is not declared
End of statement expected
'***********
regards
Gopal Parthasarathy
Gopal Parthasarathy
CERT/FEA Engineer
B/E Aerospace
re: export step
What version of NX?
And, what do you want to export out of the file? All the solid bodies?
re: export step code
Below is code that will export all the solid bodies from the current work part to a STEP file.
Option Strict Off
Imports System
Imports System.Collections.Generic
Imports NXOpen
Module Module2
Dim theSession As Session = Session.GetSession()
Dim workPart As Part = theSession.Parts.Work
Dim displayPart As Part = theSession.Parts.Display
Dim lw As ListingWindow = theSession.ListingWindow
Sub Main()
lw.Open()
Dim mySelectedObjects As New List(Of NXObject)
Dim step214File As String
Dim outputPath As String = IO.Path.GetDirectoryName(workPart.FullPath)
Dim outputFile As String = IO.Path.GetFileNameWithoutExtension(workPart.FullPath)
outputFile = IO.Path.Combine(outputPath, outputFile & ".stp")
Dim STEP214UG_DIR As String = theSession.GetEnvironmentVariableValue("STEP214UG_DIR")
step214File = IO.Path.Combine(STEP214UG_DIR, "ugstep214.def")
If Not IO.File.Exists(step214File) Then
MsgBox("The step214 settings file (ugstep214.def) was not found." & vbCrLf & _
"This journal will now exit.", vbOKOnly + vbCritical)
Exit Sub
Else
'lw.WriteLine("STEP214 definition file found at: " & step214File)
End If
'output all solids in the file to STEP
For Each temp As Body In workPart.Bodies
If temp.IsSolidBody Then
mySelectedObjects.Add(temp)
End If
Next
'export to STEP214 file
Dim step214Creator1 As Step214Creator
step214Creator1 = theSession.DexManager.CreateStep214Creator()
step214Creator1.SettingsFile = step214File
step214Creator1.ObjectTypes.Solids = True
step214Creator1.LayerMask = "1-256"
step214Creator1.InputFile = workPart.FullPath
step214Creator1.OutputFile = outputFile
step214Creator1.FileSaveFlag = False
step214Creator1.ExportSelectionBlock.SelectionScope = ObjectSelector.Scope.SelectedObjects
Dim added1 As Boolean
added1 = step214Creator1.ExportSelectionBlock.SelectionComp.Add(mySelectedObjects.ToArray)
Dim nXObject1 As NXObject
nXObject1 = step214Creator1.Commit()
step214Creator1.Destroy()
lw.Close()
End Sub
Sub AddSolidsFromLayer(ByVal layer As Integer, ByRef objList As List(Of NXObject))
Dim tempselectedobjects() As NXObject
Dim tempSolidObj As Body
tempselectedobjects = workPart.Layers.GetAllObjectsOnLayer(layer)
'filter out the solid bodies on the layer and add them to the export list
For Each obj As NXObject In tempselectedobjects
If TypeOf obj Is Body Then
tempSolidObj = CType(obj, Body)
If tempSolidObj.IsSolidBody Then
objList.Add(tempSolidObj)
End If
End If
Next
End Sub
Function SelectObjects(ByVal prompt As String, _
ByRef selObj As NXObject()) As Selection.Response
Dim theUI As UI = UI.GetUI
Dim typeArray() As Selection.SelectionType = _
{Selection.SelectionType.All}
Dim resp As Selection.Response = theUI.SelectionManager.SelectObjects( _
prompt, "Select Objects for STEP Export", _
Selection.SelectionScope.WorkPart, _
False, typeArray, selObj)
If resp = Selection.Response.ObjectSelected Or _
resp = Selection.Response.ObjectSelectedByName Or _
resp = Selection.Response.Ok Then
Return Selection.Response.Ok
Else
Return Selection.Response.Cancel
End If
End Function
Public Function GetUnloadOption(ByVal dummy As String) As Integer
'Unloads the image when the NX session terminates
GetUnloadOption = NXOpen.Session.LibraryUnloadOption.AtTermination
End Function
End Module