hermhart
Automotive
- Sep 28, 2004
- 23
This script takes the work part (assembly) and creates a new sub-directory under it called NewDirectory, and saves all the parts and assemblies to this one file.
Would anyone be able to modify this so instead of it creating a sub-directory called NewDirectory, it would instead create a new directory on your desktop of the work leaf part and put all the files in there?
Would anyone be able to modify this so instead of it creating a sub-directory called NewDirectory, it would instead create a new directory on your desktop of the work leaf part and put all the files in there?
Code:
' This inserts "\NewDirectory" into the filespec of each part,
' after the last folder and just before the part name.
'
' It saves the piece parts first, then the sub-assemblies,
' and finally the top-level assembly.
'
' You should then be able to open the assembly from
' the new location using the 'as saved' option if desired
'
' Only the paths are changed - the filenames remain intact.
'
' Use cloning if you need to rename the files.
Option Strict Off
Imports System
Imports System.Collections 'This line is required to run this as a Journal
Imports NXOpen
Imports NXOpen.UF
Imports NXOpen.UI
Imports NXOpen.Utilities
Module save_assembly_in_different_directory
Dim s As Session = Session.GetSession()
Dim ufs As UFSession = UFSession.GetUFSession()
Dim lw As ListingWindow = s.ListingWindow
Sub Main()
Dim allParts() As Part = s.Parts.ToArray()
Dim pieceParts As New ArrayList
Dim assyParts As New ArrayList
For Each thisPart As Part In allParts
Dim thisTag As NXOpen.Tag = ufs.Assem.AskRootPartOcc(thisPart.Tag)
If thisTag = NXOpen.Tag.Null Then
pieceParts.Add(thisPart)
Else
assyParts.Add(thisPart)
End If
Next
lw.Open()
lw.WriteLine("Piece parts found: " & pieceParts.Count.ToString())
For inx As Integer = 0 To pieceParts.Count - 1
Dim aPart As NXOpen.Part = pieceParts(inx)
lw.WriteLine(inx.ToString() & ". Original: " & aPart.FullPath())
Dim newPath As String = _
aPart.FullPath.Insert(aPart.FullPath.LastIndexOf("\"), _
"\NewDirectory")
lw.WriteLine(" Saving As: " & newPath)
Dim pathOnly As String = Microsoft.VisualBasic.Left(newPath, _
newPath.LastIndexOf("\"))
If My.Computer.FileSystem.DirectoryExists(pathOnly) = False Then
My.Computer.FileSystem.CreateDirectory(pathOnly)
End If
Dim saveStatus As PartSaveStatus = Nothing
saveStatus = aPart.SaveAs(newPath)
Next
lw.WriteLine("======================================================")
lw.WriteLine(" ")
lw.WriteLine("Assembly parts found:" & assyParts.Count.ToString())
For inx As Integer = 0 To assyParts.Count - 1
Dim aPart As NXOpen.Part = assyParts(inx)
lw.WriteLine(inx.ToString() & ". Original: " & aPart.FullPath())
Dim newPath As String = _
aPart.FullPath.Insert(aPart.FullPath.LastIndexOf("\"), _
"\NewDirectory")
lw.WriteLine(" Saving As: " & newPath)
Dim pathOnly As String = Microsoft.VisualBasic.Left(newPath, _
newPath.LastIndexOf("\"))
If My.Computer.FileSystem.DirectoryExists(pathOnly) = False Then
My.Computer.FileSystem.CreateDirectory(pathOnly)
End If
Dim saveStatus As PartSaveStatus = Nothing
saveStatus = aPart.SaveAs(newPath)
Next
End Sub
Public Function GetUnloadOption(ByVal dummy As String) As Integer
Return Session.LibraryUnloadOption.Immediately
End Function
End Module