STEP-Export with step214Creator

Hello,

I tried writing a Journal that allows me to save several part files in an assembly as step files. This Journal works fine as
long as the processed file is a root component. But there are part files with children components as well so I checked if there are any children components and if so, suppress them so I would only get the solid body. This seems to work and the output file is just like I need it but when the journal terminates after processing such a file, NX won't let me do anything. When I try to open a file after running the journal, it won't even show anything. Does anyone have an idea what the reason for this could be and how to fix this problem?

' STEP erstellen
Dim step214Creator(n) As Step214Creator
step214Creator(n) = theSession.DexManager.CreateStep214Creator()

step214Creator(n).SettingsFile = step214File
step214Creator(n).ObjectTypes.Solids = True
step214Creator(n).ObjectTypes.Structures = False
step214Creator(n).LayerMask = "1-256"
step214Creator(n).Author = Environment.UserName
step214Creator(n).InputFile = tmpPart.FullPath
step214Creator(n).OutputFile = outputFile
step214Creator(n).FileSaveFlag = False
step214Creator(n).ExportSelectionBlock.SelectionScope = ObjectSelector.Scope.EntirePart

Dim nXObject(n) As NXObject
nXObject(n) = step214Creator(n).Commit()

step214Creator(n).Destroy()

Why are you defining step214Creator as an array? That seems a bit odd, but I doubt it is the cause of your problem. Without seeing more of the code it would be difficult to say what the issue is.

well, this was something I tried while figuring out what was wrong. I didn't change it back. I hope the code for the main loop can help you help me :)


' Teile durchgehen
Dim n As Integer

For n = 0 To pList.Count - 1

Dim partName As String = pList.Item(n).Substring(0, pList.Item(n).IndexOf(",", 0, pList.Item(n).Length - 1))
Dim tmpPart As Part = CType(theSession.Parts.FindObject(partName), Part)
theSession.Parts.SetWork(tmpPart)

' Name und Pfad der STEP definieren
Dim x As Integer = pList.Item(n).IndexOf(",", 0, pList.Item(n).Length) + 1
Dim stepFile As String = pList.Item(n).Substring(x, pList.Item(n).Length - x)
Dim outputFile As String = IO.Path.Combine(outputPath, stepFile & ".stp")

' Abfrage ob Arbeitsteil Unterbaugruppe darstellt
Dim workComponent As ComponentAssembly = tmpPart.ComponentAssembly

If Not IsNothing(workComponent.RootComponent) Then

For Each child As Component In workComponent.RootComponent.GetChildren()

child.Suppress()

Next

End If

' STEP erstellen
Dim step214Creator(n) As Step214Creator
step214Creator(n) = theSession.DexManager.CreateStep214Creator()

step214Creator(n).SettingsFile = step214File
step214Creator(n).ObjectTypes.Solids = True
step214Creator(n).ObjectTypes.Structures = False
step214Creator(n).LayerMask = "1-256"
step214Creator(n).Author = Environment.UserName
step214Creator(n).InputFile = tmpPart.FullPath
step214Creator(n).OutputFile = outputFile
step214Creator(n).FileSaveFlag = False
step214Creator(n).ExportSelectionBlock.SelectionScope = ObjectSelector.Scope.EntirePart

Dim nXObject1(n) As NXObject
nXObject1(n) = step214Creator(n).Commit()

step214Creator(n).Destroy()

' Einzelteile Unterdrückung aufheben
If Not IsNothing(workComponent.RootComponent) Then

For Each child As Component In workComponent.RootComponent.GetChildren()

child.UnSuppress()

Next

End If

Next

I'd suggest starting by making the step214Creator a single variable rather than an array variable.

But I think the root cause of your problem is that the STEP files are being created within a For loop. The For loop executes as quickly as it can, which means that it is loading up new files to export before the first one has finished (the STEP translator runs as a separate process from the rest of the journal code). This can cause some issues with NX. If you are using a fairly recent version of NX (NX 8 or higher), try using step214Creator.ProcessHoldFlag = True in your code. This will cause the journal code to pause and wait for the STEP translator to finish its work before continuing.

Unfortunately we are using NX 7.5 so I can't find this property of the stepcreator-class. I tried making that class a single variable but it didn't change anything. I also tried to pause the current thread by means of Sleep() after launching the step translator (which will continue working) but the result is always the same. After (successfully) creating the step file, I can't control NX anymore and it has to be restarted.

Any other ideas? I could try do to the step exporting in a do-Loop but I don't think this is the cause.
Thanks in advance

An alternative to the .ProcessHoldFlag is to wait until the desired STEP file is created. The "export dxf" code on this site does something similar:

lg.WriteLine("Waiting on creation of DXF file...")
While Not File.Exists(dxfFile)
Application.DoEvents()
End While
lg.WriteLine("DXF file created: " & dxfFile)

You could follow a similar strategy to wait for the STEP file creation.

A few other questions:
What is pList and how is it defined?

You specify the "entire part" to be exported, but you suppress all the components. Do you want to export only the solid bodies in each file? If so, why not use the "Selected Objects" option and pass in the bodies of the part from the .Bodies collection?

Thanks for the advice. Waiting for the file to be created slows down the process and makes sure one file after the other is processed.

pList is a list object I created and filled with components of an assembly to be processed (not every component has to be).

I still have a problem with rootcomponents and suppressing their children components. This might actually be because of EntirePart property as you said. Is there a nice and easy way to get the object left after suppressing the children components as an NXObject type?

The rootcomponent is not only a part file filled with part geometry but also with components like rivets. These need to be suppressed.

The code below will export only the solid bodies in the current display part; it will not export any of the components.

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 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"

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

Thank you very much, this works perfectly!

Hello,

Is there any way to export a Single body to a Step File as well?

I try something alike this, but within a for-loop. The selected bodies count up in the loop, because unfortunately there is no way to deselect the items in the nxSelectedObjects-List. I would need just single bodies (Only one at each selection). It compiles, but after i select a body, it stops working. What am I doing wrong? How do i manage to pass the selection array properly?

Thank you for your help

Best Regards

My approach is:


For j As Integer = 1 To i

If SelectObjects("choose body " & j , _
mySelectedObject) = Selection.Response.Ok Then

Try
STP_Export("C:\test\" & j & ".stp", currentFile, mySelectedObject)
End Try
End If
Next

Sub STP_Export_AV(ByVal outputFile As String, ByVal inputFile As String,_
stpSelectedObject As TaggedObject())

Dim stepCreator1 As NXOpen.StepCreator = Nothing
stepCreator1 = theSession.DexManager.CreateStepCreator()
stepCreator1.ExportAs = NXOpen.StepCreator.ExportAsOption.Ap214
stepCreator1.ObjectTypes.Solids = True
stepCreator1.SettingsFile = "C:\nx11\step214ug\ugstep214.def"
stepCreator1.InputFile = inputFile
stepCreator1.OutputFile = outputFile
stepCreator1.ExportSelectionBlock.SelectionScope = NXOpen.ObjectSelector.Scope.SelectedObjects

Dim added1 As Boolean = Nothing
added1=stepCreator1.ExportSelectionBlock.SelectionComp.Add(stpSelectedObject(1))

stepCreator1.FileSaveFlag = False
stepCreator1.LayerMask = "1-256"
Dim nXObject1 As NXOpen.NXObject = Nothing
nXObject1 = stepCreator1.Commit()
stepCreator1.Destroy()

End Sub

I tried it too complicated. For anybody who also tries to export Single objects in a loop:

You can use the Export-Sub from NX-Journaling and just remove the parts during the loop of the list with List.Clear Method ().

Best regards

Is anybody have the code to export multiple part files from folder to step files. The code will scan one partfile open the partfile and export to step file and close the partfile. this to be done with all partfiles in a folder and sent to specific output folder

sourabh gupta

NX ships with a bulk translator that will do exactly this, no coding required. Look for the "Step AP203" (or 214 or 242) in the program group.