Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
Imports System
Imports System.Collections.Generic
Imports System.IO
Imports NXOpen
Imports NXOpen.UF
Module rotate_step_files
Dim theSession As Session = Session.GetSession()
Dim lw As ListingWindow = theSession.ListingWindow
Sub Main()
lw.Open()
Dim stepFolderPath As String = "C:\temp\Rotate"
Dim stepFiles As String() = Directory.GetFiles(stepFolderPath, "*.stp")
Dim tempPrtFiles As New List(Of String)
For Each stepFilePath As String In stepFiles
'open file
'lw.WriteLine("opening file: " & stepFilePath)
Dim basePart1 As NXOpen.BasePart = Nothing
Dim partLoadStatus1 As NXOpen.PartLoadStatus = Nothing
basePart1 = theSession.Parts.OpenBaseDisplay(stepFilePath, partLoadStatus1)
partLoadStatus1.Dispose()
'rotate bodies
'lw.WriteLine("rotating bodies")
'origin point and vector for rotation in absolute coordinates
Dim origin1 As NXOpen.Point3d = New NXOpen.Point3d(0.0, 0.0, 0.0)
Dim vector1 As NXOpen.Vector3d = New NXOpen.Vector3d(0.0, 0.0, 1.0)
RotateBodies(vector1, origin1, 45.0)
'save file, must be saved for STEP export to work on rotated geometry
Dim partSaveStatus1 As NXOpen.PartSaveStatus = Nothing
partSaveStatus1 = basePart1.Save(NXOpen.BasePart.SaveComponents.True, NXOpen.BasePart.CloseAfterSave.False)
partSaveStatus1.Dispose()
'export new step file
Dim exportName As String = IO.Path.Combine(IO.Path.GetDirectoryName(stepFilePath), IO.Path.GetFileNameWithoutExtension(stepFilePath) & "_rotated.stp")
'lw.WriteLine("exportName: " & exportName)
If My.Computer.FileSystem.FileExists(exportName) Then
lw.WriteLine("file: '" & exportName & "' already exists, skipping export")
Else
'lw.WriteLine("exporting STEP file")
ExportStep(basePart1, exportName)
End If
'close temp part file without saving
tempPrtFiles.Add(basePart1.FullPath)
'lw.WriteLine("closing file")
basePart1.Close(BasePart.CloseWholeTree.True, BasePart.CloseModified.UseResponses, Nothing)
Next
'delete temp files
For Each tempFile As String In tempPrtFiles
Try
My.Computer.FileSystem.DeleteFile(tempFile)
Catch ex As Exception
lw.WriteLine("error deleting file: " & ex.Message)
End Try
Next
lw.Close()
End Sub
Sub RotateBodies(ByVal rotationAxis As Vector3d, ByVal rotationAxisPoint As Point3d, ByVal degrees As Double)
Dim nullNXOpen_Features_MoveObject As NXOpen.Features.MoveObject = Nothing
Dim moveObjectBuilder1 As NXOpen.Features.MoveObjectBuilder = Nothing
moveObjectBuilder1 = theSession.Parts.Work.BaseFeatures.CreateMoveObjectBuilder(nullNXOpen_Features_MoveObject)
Dim added1 As Boolean = Nothing
added1 = moveObjectBuilder1.ObjectToMoveObject.Add(theSession.Parts.Work.Bodies.ToArray)
moveObjectBuilder1.TransformMotion.Option = NXOpen.GeometricUtilities.ModlMotion.Options.Angle
'origin point and vector for rotation
'Dim origin1 As NXOpen.Point3d = New NXOpen.Point3d(0.0, 0.0, 0.0)
'Dim vector1 As NXOpen.Vector3d = New NXOpen.Vector3d(0.0, 0.0, 1.0)
Dim axis1 As NXOpen.Axis = Nothing
axis1 = theSession.Parts.Work.Axes.CreateAxis(rotationAxisPoint, rotationAxis, NXOpen.SmartObject.UpdateOption.WithinModeling)
moveObjectBuilder1.TransformMotion.AngularAxis = axis1
'amount of rotation (degrees)
moveObjectBuilder1.TransformMotion.Angle.SetFormula(degrees.ToString)
Dim nXObject1 As NXOpen.NXObject = Nothing
nXObject1 = moveObjectBuilder1.Commit()
Dim objects1() As NXOpen.NXObject
objects1 = moveObjectBuilder1.GetCommittedObjects()
moveObjectBuilder1.Destroy()
End Sub
Sub ExportStep(ByVal partToExport As Part, ByVal exportFileName As String)
Dim STEP214UG_DIR As String = theSession.GetEnvironmentVariableValue("STEP214UG_DIR")
Dim step214File As String
step214File = IO.Path.Combine(STEP214UG_DIR, "ugstep214.def")
If Not IO.File.Exists(step214File) Then
'Throw New Exception("ugstep214.def file not found")
lw.WriteLine("ugstep214.def file not found, skipping export")
Return
End If
Dim stepCreator1 As StepCreator = Nothing
stepCreator1 = theSession.DexManager.CreateStepCreator()
With stepCreator1
.ExportAs = StepCreator.ExportAsOption.Ap214
.ExportFrom = StepCreator.ExportFromOption.ExistingPart
.SettingsFile = step214File
.ObjectTypes.Surfaces = True
.ObjectTypes.Solids = True
.InputFile = partToExport.FullPath
.OutputFile = exportFileName
.FileSaveFlag = False
.LayerMask = "1-256"
.ProcessHoldFlag = True
End With
Dim nXObject1 As NXObject
nXObject1 = stepCreator1.Commit()
stepCreator1.Destroy()
End Sub
End Module
I had not checked this in a while. I had gotten so far as to opening every step file in a loop. This works! Thank you!Here is some code that will open step files, rotate the geometry 45° about the absolute +Z axis and export the geometry out to a new step file (with "_rotated" appended to the name). It works for STEP files that do not have any assembly structure. I did not test on STEP assembly files, but I assume that it would give some undesired results.
The journal is written in VB.net and was tested on NX2206.
Code:Imports System Imports System.Collections.Generic Imports System.IO Imports NXOpen Imports NXOpen.UF Module rotate_step_files Dim theSession As Session = Session.GetSession() Dim lw As ListingWindow = theSession.ListingWindow Sub Main() lw.Open() Dim stepFolderPath As String = "C:\temp\Rotate" Dim stepFiles As String() = Directory.GetFiles(stepFolderPath, "*.stp") Dim tempPrtFiles As New List(Of String) For Each stepFilePath As String In stepFiles 'open file 'lw.WriteLine("opening file: " & stepFilePath) Dim basePart1 As NXOpen.BasePart = Nothing Dim partLoadStatus1 As NXOpen.PartLoadStatus = Nothing basePart1 = theSession.Parts.OpenBaseDisplay(stepFilePath, partLoadStatus1) partLoadStatus1.Dispose() 'rotate bodies 'lw.WriteLine("rotating bodies") 'origin point and vector for rotation in absolute coordinates Dim origin1 As NXOpen.Point3d = New NXOpen.Point3d(0.0, 0.0, 0.0) Dim vector1 As NXOpen.Vector3d = New NXOpen.Vector3d(0.0, 0.0, 1.0) RotateBodies(vector1, origin1, 45.0) 'save file, must be saved for STEP export to work on rotated geometry Dim partSaveStatus1 As NXOpen.PartSaveStatus = Nothing partSaveStatus1 = basePart1.Save(NXOpen.BasePart.SaveComponents.True, NXOpen.BasePart.CloseAfterSave.False) partSaveStatus1.Dispose() 'export new step file Dim exportName As String = IO.Path.Combine(IO.Path.GetDirectoryName(stepFilePath), IO.Path.GetFileNameWithoutExtension(stepFilePath) & "_rotated.stp") 'lw.WriteLine("exportName: " & exportName) If My.Computer.FileSystem.FileExists(exportName) Then lw.WriteLine("file: '" & exportName & "' already exists, skipping export") Else 'lw.WriteLine("exporting STEP file") ExportStep(basePart1, exportName) End If 'close temp part file without saving tempPrtFiles.Add(basePart1.FullPath) 'lw.WriteLine("closing file") basePart1.Close(BasePart.CloseWholeTree.True, BasePart.CloseModified.UseResponses, Nothing) Next 'delete temp files For Each tempFile As String In tempPrtFiles Try My.Computer.FileSystem.DeleteFile(tempFile) Catch ex As Exception lw.WriteLine("error deleting file: " & ex.Message) End Try Next lw.Close() End Sub Sub RotateBodies(ByVal rotationAxis As Vector3d, ByVal rotationAxisPoint As Point3d, ByVal degrees As Double) Dim nullNXOpen_Features_MoveObject As NXOpen.Features.MoveObject = Nothing Dim moveObjectBuilder1 As NXOpen.Features.MoveObjectBuilder = Nothing moveObjectBuilder1 = theSession.Parts.Work.BaseFeatures.CreateMoveObjectBuilder(nullNXOpen_Features_MoveObject) Dim added1 As Boolean = Nothing added1 = moveObjectBuilder1.ObjectToMoveObject.Add(theSession.Parts.Work.Bodies.ToArray) moveObjectBuilder1.TransformMotion.Option = NXOpen.GeometricUtilities.ModlMotion.Options.Angle 'origin point and vector for rotation 'Dim origin1 As NXOpen.Point3d = New NXOpen.Point3d(0.0, 0.0, 0.0) 'Dim vector1 As NXOpen.Vector3d = New NXOpen.Vector3d(0.0, 0.0, 1.0) Dim axis1 As NXOpen.Axis = Nothing axis1 = theSession.Parts.Work.Axes.CreateAxis(rotationAxisPoint, rotationAxis, NXOpen.SmartObject.UpdateOption.WithinModeling) moveObjectBuilder1.TransformMotion.AngularAxis = axis1 'amount of rotation (degrees) moveObjectBuilder1.TransformMotion.Angle.SetFormula(degrees.ToString) Dim nXObject1 As NXOpen.NXObject = Nothing nXObject1 = moveObjectBuilder1.Commit() Dim objects1() As NXOpen.NXObject objects1 = moveObjectBuilder1.GetCommittedObjects() moveObjectBuilder1.Destroy() End Sub Sub ExportStep(ByVal partToExport As Part, ByVal exportFileName As String) Dim STEP214UG_DIR As String = theSession.GetEnvironmentVariableValue("STEP214UG_DIR") Dim step214File As String step214File = IO.Path.Combine(STEP214UG_DIR, "ugstep214.def") If Not IO.File.Exists(step214File) Then 'Throw New Exception("ugstep214.def file not found") lw.WriteLine("ugstep214.def file not found, skipping export") Return End If Dim stepCreator1 As StepCreator = Nothing stepCreator1 = theSession.DexManager.CreateStepCreator() With stepCreator1 .ExportAs = StepCreator.ExportAsOption.Ap214 .ExportFrom = StepCreator.ExportFromOption.ExistingPart .SettingsFile = step214File .ObjectTypes.Surfaces = True .ObjectTypes.Solids = True .InputFile = partToExport.FullPath .OutputFile = exportFileName .FileSaveFlag = False .LayerMask = "1-256" .ProcessHoldFlag = True End With Dim nXObject1 As NXObject nXObject1 = stepCreator1.Commit() stepCreator1.Destroy() End Sub End Module