×
INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

Log In

Come Join Us!

Are you an
Engineering professional?
Join Eng-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

NX Journal for STEP export

NX Journal for STEP export

NX Journal for STEP export

(OP)
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

RE: NX Journal for STEP export

Get the attribute value with attributeInfo.StringValue. You can then build a file path with the value; something like:

CODE

dim outputFile as string = "C:\StepFiles\" & attributeInfo.StringValue & ".stp" 

That's the basic idea, you will want to add some checking and error handling as needed. For instance, the attribute value cannot contain any characters that would be illegal in a file name. Also, you will want to check that the output folder exists before trying to export a file to that location; stuff like that.

www.nxjournaling.com

RE: NX Journal for STEP export

(OP)
Hi Cowski,

That works perfectly. Only other thing that I had to do was set an environment variable for STEP214UG_DIR as it was returning an error.
Is it possible to expand the scope to export parts within an assembly, so a single step file with the top level and components underneath?

Regards,

Russell.

RE: NX Journal for STEP export

It sounds like you want to export component bodies individually and keep the assembly structure. If so, this can be done in interactive NX by exporting components rather than bodies (or by using the "entire part" export option). I've not tried it, but I imagine that the same could be accomplished through a journal by using similar options.

www.nxjournaling.com

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Eng-Tips forums is a member-only feature.

Click Here to join Eng-Tips and talk with other members!


Resources