Hello,
I'm trying to create a journal to import All .Stp files from a Folder and process the imported bodies.
I managed to do the import all from a selected directory, but I'm a bit stuck at the moment.
This is what I have so far:
Sub Main()
lw.Open()
'''' Get Folder Path '''''
Dim Path As String = GetPath()
If Path Is Nothing Then
lw.WriteLine("No Folder Selected")
Exit Sub
Else
lw.WriteLine("Importing Files from Folder: " & Path)
End If
Import_Files(Path)
End Sub
Function GetPath()
' Select Directory to import Files'
Dim FolderBrowserDialog1 As New FolderBrowserDialog ' Change the .SelectedPath property to the default location
With FolderBrowserDialog1 ' Desktop is the root folder in the dialog.
.RootFolder = Environment.SpecialFolder.Desktop ' Change the following line to default to a given path
.SelectedPath = "C:\" ' Prompt the user with a custom message.
.Description = "Select the directory to scan"
If .ShowDialog = DialogResult.OK Then ' Display the selected folder if the user clicked on the OK button.
strIntputFolder = .SelectedPath
Return strIntputFolder
Else
'user pressed "cancel", exit the journal
Return Nothing
Exit Function
End If
End With
GetUnloadOption("dummy")
End Function
Function Import_Files(directoryPath)
Dim File_Number As Integer = 0
Dim files() As String
files = Directory.GetFiles(directoryPath, "*.stp", SearchOption.TopDirectoryOnly)
lw.WriteLine("")
'' Create Stp Importer and Setting ''
Dim Stp_Importer As Step214Importer
Stp_Importer = theSession.DexManager.CreateStep214Importer()
Stp_Importer.SimplifyGeometry = True
Stp_Importer.ObjectTypes.Solids = True
Stp_Importer.SettingsFile = "C:\Program Files\Siemens\NX 9.0\step214ug\step214ug.def"
For Each fileName As String In files
lw.Open()
File_Number += 1
Dim Name As String = Path.GetFileNameWithoutExtension(fileName)
lw.WriteLine(Name & " To Layer " & File_Number)
'''' Import Stp Files '''''
'Stp_Importer.LayerDefault = File_Number
Stp_Importer.InputFile = fileName
Stp_Importer.OutputFile = workPart.FullPath
Stp_Importer.FileOpenFlag = False
'' create Object ''
Dim nXObject1 As NXObject
nXObject1 = Stp_Importer.Commit()
Dim ObjectTag As String = nXObject1.JournalIdentifier
'' Move to Layer''
Dim objectArray1(0) As DisplayableObject
Dim body1 As Body = CType(workPart.Bodies.FindObject("UNPARAMETERIZED_FEATURE(1)"), Body)
objectArray1(0) = body1
workPart.Layers.MoveDisplayableObjects(File_Number, objectArray1)
Next
'' Clear importer from memory ''
Stp_Importer.Destroy()
lw.WriteLine("")
lw.WriteLine("Total Number of Imported Files = " & File_Number)
Return Nothing
End Function
I though nXObject1 = Stp_Importer.Commit() would put all object in a list of objects, but it is crashing there.
How can I get a list of all the solid bodies names, journalling tags and see how many solids there are for each imports?
I'm trying to import all stp files, put all solids in a different layer, and delete some solid bodies depending on their name.
Any help would be very appreciated
Thanks
re: step import from folder
I used your code as a starting point; the code below will import the files from the chosen directory. The bodies from each file will be placed into a group named after the step file. There is probably a more elegant way to identify the newly imported bodies, but it is the first approach that came to mind and it works.
The journal doesn't do much error checking...
Option Strict Off
Imports System
Imports System.Collections.Generic
Imports System.Windows.Forms
Imports NXOpen
Module Module1
Dim theSession As Session = Session.GetSession
Dim workPart As Part = theSession.Parts.Work
Dim lw As ListingWindow = theSession.ListingWindow
Sub Main()
lw.Open()
Dim Path As String = GetPath()
If Path Is Nothing Then
lw.WriteLine("No Folder Selected")
Exit Sub
Else
lw.WriteLine("Importing Files from Folder: " & Path)
End If
Import_Files(Path)
End Sub
Function GetPath() As String
' Select Directory to import Files'
Dim FolderBrowserDialog1 As New FolderBrowserDialog ' Change the .SelectedPath property to the default location
With FolderBrowserDialog1 ' Desktop is the root folder in the dialog.
.RootFolder = Environment.SpecialFolder.Desktop ' Change the following line to default to a given path
.SelectedPath = "C:\" ' Prompt the user with a custom message.
.Description = "Select the directory to scan"
If .ShowDialog = DialogResult.OK Then ' Display the selected folder if the user clicked on the OK button.
Return .SelectedPath
Else
'user pressed "cancel", exit the journal
Return Nothing
Exit Function
End If
End With
End Function
Sub Import_Files(directoryPath)
Dim Step214_DIR As String = theSession.GetEnvironmentVariableValue("STEP214UG_DIR")
Dim stepSettingsFile As String
stepSettingsFile = IO.Path.Combine(Step214_DIR, "step214ug.def")
Dim File_Number As Integer = 0
Dim files() As String
files = IO.Directory.GetFiles(directoryPath, "*.stp", IO.SearchOption.TopDirectoryOnly)
lw.WriteLine("")
'' Create Stp Importer and Setting ''
Dim Stp_Importer As Step214Importer
Stp_Importer = theSession.DexManager.CreateStep214Importer()
Stp_Importer.SimplifyGeometry = True
Stp_Importer.ObjectTypes.Solids = True
'Stp_Importer.SettingsFile = "C:\Program Files\Siemens\NX 9.0\step214ug\step214ug.def"
Stp_Importer.SettingsFile = stepSettingsFile
Dim theBodies As New List(Of Body)
Dim oldBodyCount As Integer = 0
For Each fileName As String In files
lw.Open()
File_Number += 1
Dim Name As String = IO.Path.GetFileNameWithoutExtension(fileName)
lw.WriteLine(Name & " To Group: " & Name)
'''' Import Stp Files '''''
'Stp_Importer.LayerDefault = File_Number
Stp_Importer.InputFile = fileName
Stp_Importer.OutputFile = workPart.FullPath
Stp_Importer.FileOpenFlag = False
'' create Object ''
Dim nXObject1 As NXObject
nXObject1 = Stp_Importer.Commit()
Dim newBodies As New List(Of Body)
oldBodyCount = theBodies.Count
For Each tempBody As Body In workPart.Bodies
If Not theBodies.Contains(tempBody) Then
newBodies.Add(tempBody)
End If
Next
'add new bodies to group
Dim groupBuilder1 As GroupBuilder
groupBuilder1 = workPart.CreateGatewayGroupBuilder(Nothing)
groupBuilder1.ActivegroupOption = True
'new group: action 0
groupBuilder1.ActionType = 0
groupBuilder1.GroupDisplayProperties = False
groupBuilder1.GroupName = Name
Dim added1 As Boolean
added1 = groupBuilder1.ObjectsInGroup.Add(newBodies.ToArray)
Dim group1 As NXObject
group1 = groupBuilder1.Commit()
groupBuilder1.Destroy()
lw.WriteLine(" " & newBodies.Count & " body(ies) imported")
For Each tempbody As Body In newBodies
theBodies.Add(tempbody)
lw.WriteLine(" tag: " & tempbody.Tag)
Next
newBodies.Clear()
lw.WriteLine("")
Next
'' Clear importer from memory ''
Stp_Importer.Destroy()
lw.WriteLine("")
lw.WriteLine("Total Number of Imported Files = " & File_Number)
End Sub
End Module