rmaddin
Mechanical
- Aug 4, 2016
- 7
Hi,
I'm trying to produce a journal that reads an NX attribute (SAP_CC_NAME) and then uses it as a filename for a STEP file export.
So far I've found and combined (probably not in the most efficient way - I'm new to this!) two journals, one for automated STEP export and another I've adapted to read and report my attribute.
The next part is using my value to create the filename. The path where the file is to be stored can be hard coded, such as C:\STEPFILES\ for example.
Here's what my journal looks like so far, many thanks in advance.
Russell.
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
Sub Main()
If IsNothing(theSession.Parts.BaseWork) Then
'active part required
Return
End If
Dim lw As ListingWindow = theSession.ListingWindow
lw.Open()
Const attributeName As String = "SAP_CC_NAME"
Dim attributeInfo As NXObject.AttributeInformation
If workPart.HasUserAttribute(attributeName, NXObject.AttributeType.String, -1) Then
attributeInfo = workPart.GetUserAttribute(attributeName, NXObject.AttributeType.String, -1)
lw.WriteLine("attribute value: " & attributeInfo.StringValue)
Else
lw.WriteLine("the work part does not have an attribute named: " & attributeName)
End If
Const undoMarkName As String = "export bodies [STEP]"
Dim markId1 As Session.UndoMarkId
markId1 = theSession.SetUndoMark(Session.MarkVisibility.Visible, undoMarkName)
Dim theBodies As New List(Of Body)
'gather the solid bodies from the display part
For Each tempBody As Body In displayPart.Bodies
If tempBody.IsSolidBody Then
theBodies.Add(tempBody)
End If
Next
If theBodies.Count > 0 Then
Dim exportFile As String = ""
exportFile = displayPart.FullPath.Substring(0, displayPart.FullPath.Length - 3) & "stp"
' exportFile = "C:\STEPFILES\ "
StepExport(exportFile, theBodies)
Else
MsgBox("No bodies to export")
End If
lw.Close()
End Sub
Sub StepExport(ByVal exportFileName As String, ByVal theBodies As List(Of Body))
Dim StepSettingsFile As String = ""
'Get UG install directory using NXOpen API
Dim STEP214UG_DIR As String = theSession.GetEnvironmentVariableValue("STEP214UG_DIR")
StepSettingsFile = IO.Path.Combine(STEP214UG_DIR, "ugstep214.def")
'lg.WriteLine("looking for STEP settings file: " & StepSettingsFile)
'check if the StepSettingsFile exists
If Not IO.File.Exists(StepSettingsFile) Then
'file does not exist in default directory or user specified directory
'lg.WriteLine("The STEP settings file was not found in the specified location: " & StepSettingsFile)
MsgBox("The STEP settings file (ugstep214.def) was not found." & vbCrLf & _
"STEP file export will be skipped.", vbOKOnly + vbExclamation, "Warning")
Return
End If
'does file exist? if so, try to delete it (overwrite)
Try
DeleteFile(exportFileName)
Catch ex As Exception
'file delete failed
MsgBox("STEP file overwrite failed")
'exit subroutine
Return
End Try
Dim step214Creator1 As Step214Creator
step214Creator1 = theSession.DexManager.CreateStep214Creator()
Try
With step214Creator1
.SettingsFile = StepSettingsFile
'.SettingsFile = "C:\Temp\data_management\test.def"
.BsplineTol = 0.001
.ExportSelectionBlock.SelectionScope = ObjectSelector.Scope.SelectedObjects
.ObjectTypes.Solids = True
.ObjectTypes.Surfaces = True
.ObjectTypes.Csys = True
.ObjectTypes.Curves = True
.InputFile = displayPart.FullPath
.OutputFile = exportFileName
.FileSaveFlag = False
.LayerMask = "1-256"
'.ProcessHoldFlag = True
Dim added1 As Boolean
added1 = .ExportSelectionBlock.SelectionComp.Add(theBodies.ToArray)
End With
Dim nXObject1 As NXObject
nXObject1 = step214Creator1.Commit()
Catch ex As NXException
MsgBox("Error: " & ex.Message, vbOKOnly + vbCritical, "Error")
Return
Finally
step214Creator1.Destroy()
End Try
End Sub
Sub DeleteFile(ByVal filePath As String)
'does file exist? if so, try to delete it (overwrite)
If IO.File.Exists(filePath) Then
IO.File.Delete(filePath)
End If
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'm trying to produce a journal that reads an NX attribute (SAP_CC_NAME) and then uses it as a filename for a STEP file export.
So far I've found and combined (probably not in the most efficient way - I'm new to this!) two journals, one for automated STEP export and another I've adapted to read and report my attribute.
The next part is using my value to create the filename. The path where the file is to be stored can be hard coded, such as C:\STEPFILES\ for example.
Here's what my journal looks like so far, many thanks in advance.
Russell.
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
Sub Main()
If IsNothing(theSession.Parts.BaseWork) Then
'active part required
Return
End If
Dim lw As ListingWindow = theSession.ListingWindow
lw.Open()
Const attributeName As String = "SAP_CC_NAME"
Dim attributeInfo As NXObject.AttributeInformation
If workPart.HasUserAttribute(attributeName, NXObject.AttributeType.String, -1) Then
attributeInfo = workPart.GetUserAttribute(attributeName, NXObject.AttributeType.String, -1)
lw.WriteLine("attribute value: " & attributeInfo.StringValue)
Else
lw.WriteLine("the work part does not have an attribute named: " & attributeName)
End If
Const undoMarkName As String = "export bodies [STEP]"
Dim markId1 As Session.UndoMarkId
markId1 = theSession.SetUndoMark(Session.MarkVisibility.Visible, undoMarkName)
Dim theBodies As New List(Of Body)
'gather the solid bodies from the display part
For Each tempBody As Body In displayPart.Bodies
If tempBody.IsSolidBody Then
theBodies.Add(tempBody)
End If
Next
If theBodies.Count > 0 Then
Dim exportFile As String = ""
exportFile = displayPart.FullPath.Substring(0, displayPart.FullPath.Length - 3) & "stp"
' exportFile = "C:\STEPFILES\ "
StepExport(exportFile, theBodies)
Else
MsgBox("No bodies to export")
End If
lw.Close()
End Sub
Sub StepExport(ByVal exportFileName As String, ByVal theBodies As List(Of Body))
Dim StepSettingsFile As String = ""
'Get UG install directory using NXOpen API
Dim STEP214UG_DIR As String = theSession.GetEnvironmentVariableValue("STEP214UG_DIR")
StepSettingsFile = IO.Path.Combine(STEP214UG_DIR, "ugstep214.def")
'lg.WriteLine("looking for STEP settings file: " & StepSettingsFile)
'check if the StepSettingsFile exists
If Not IO.File.Exists(StepSettingsFile) Then
'file does not exist in default directory or user specified directory
'lg.WriteLine("The STEP settings file was not found in the specified location: " & StepSettingsFile)
MsgBox("The STEP settings file (ugstep214.def) was not found." & vbCrLf & _
"STEP file export will be skipped.", vbOKOnly + vbExclamation, "Warning")
Return
End If
'does file exist? if so, try to delete it (overwrite)
Try
DeleteFile(exportFileName)
Catch ex As Exception
'file delete failed
MsgBox("STEP file overwrite failed")
'exit subroutine
Return
End Try
Dim step214Creator1 As Step214Creator
step214Creator1 = theSession.DexManager.CreateStep214Creator()
Try
With step214Creator1
.SettingsFile = StepSettingsFile
'.SettingsFile = "C:\Temp\data_management\test.def"
.BsplineTol = 0.001
.ExportSelectionBlock.SelectionScope = ObjectSelector.Scope.SelectedObjects
.ObjectTypes.Solids = True
.ObjectTypes.Surfaces = True
.ObjectTypes.Csys = True
.ObjectTypes.Curves = True
.InputFile = displayPart.FullPath
.OutputFile = exportFileName
.FileSaveFlag = False
.LayerMask = "1-256"
'.ProcessHoldFlag = True
Dim added1 As Boolean
added1 = .ExportSelectionBlock.SelectionComp.Add(theBodies.ToArray)
End With
Dim nXObject1 As NXObject
nXObject1 = step214Creator1.Commit()
Catch ex As NXException
MsgBox("Error: " & ex.Message, vbOKOnly + vbCritical, "Error")
Return
Finally
step214Creator1.Destroy()
End Try
End Sub
Sub DeleteFile(ByVal filePath As String)
'does file exist? if so, try to delete it (overwrite)
If IO.File.Exists(filePath) Then
IO.File.Delete(filePath)
End If
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