Some journals to backup your files

Forum user isilter submitted the following journals; the following journals will create a "backup" folder in your current directory, create a backup file in this directory (with timestamp), and restore a backup file.

Thank you, isilter, for submitting your code!

Export part backup


'this journal
'is using Export Part command to export the open file
'to a "..\backup" folder with date and time stamp placed in its name

Option Strict Off
Imports System
Imports System.Collections.Generic
Imports NXOpen
Imports NXOpen.UF
Imports NXOpen.UI

Module NXJournal
Sub Main()

Dim theSession As Session = Session.GetSession()
Dim theUFSession As UFSession = UFSession.GetUFSession()

'********************************************************************************

Dim workPart As Part = theSession.Parts.Work
Dim lw As ListingWindow = theSession.ListingWindow

'find file name and folder path
Dim fileName As String = IO.Path.GetFileName(workPart.FullPath)
Dim fileNameNoExt As String = IO.Path.GetFileNameWithoutExtension(workPart.FullPath)
Dim parentFolder As String = IO.Path.GetDirectoryName(workPart.FullPath)

'prepare a clean dd.mm.yy hh.mm.ss type string
Dim TimeNote As DateTime
TimeNote = System.DateTime.Now()
Dim full_date_time As String = TimeNote
Dim hour As String = Mid(full_date_time, Len(full_date_time) - 7, 2)
Dim minute As String = Mid(full_date_time, Len(full_date_time) - 4, 2)
Dim second As String = Mid(full_date_time, Len(full_date_time) - 1, 2)
Dim ddmmyy As String = Left(full_date_time, Len(full_date_time) - 9)
Dim year_short As String = Right(ddmmyy, 2)
ddmmyy = Left(ddmmyy, Len(ddmmyy) - 4) & year_short
ddmmyy = ddmmyy.PadLeft(8, "0")

'prepare new backup folder if doesn't exist
Dim yeni_klasor As String = parentFolder & "\backup"
My.Computer.FileSystem.CreateDirectory(yeni_klasor)

'result of new file path with new file name
Dim sonuc As String = yeni_klasor & "\" & fileNameNoExt & " " & ddmmyy & " " & hour & "." & minute & "." & second & ".prt"

'********************************************************************************

Dim abody() As TaggedObject
If SelectBodies("select bodies", abody) = Selection.Response.Cancel Then
Return
End If

Dim bodyTags As New List(Of Tag)

For Each temp As TaggedObject In abody
bodyTags.Add(temp.Tag)
Next

Dim options As UFPart.ExportOptions
With options
.expression_mode = UFPart.ExportExpMode.CopyExpDeeply
.new_part = True
.params_mode = UFPart.ExportParamsMode.MaintainParams
End With

theUFSession.Part.ExportWithOptions(sonuc, bodyTags.Count, bodyTags.ToArray, options)

End Sub

Function SelectBodies(ByVal prompt As String, ByRef selObj() As TaggedObject) As Selection.Response

Dim theUI As UI = UI.GetUI
Dim title As String = "Select one or more bodies"
Dim includeFeatures As Boolean = False
Dim keepHighlighted As Boolean = False
Dim selAction As Selection.SelectionAction = Selection.SelectionAction.ClearAndEnableSpecific
Dim scope As Selection.SelectionScope = Selection.SelectionScope.WorkPart
Dim selectionMask_array(0) As Selection.MaskTriple

With selectionMask_array(0)
.Type = UFConstants.UF_solid_type
.SolidBodySubtype = UFConstants.UF_UI_SEL_FEATURE_BODY
End With

Dim resp As Selection.Response = theUI.SelectionManager.SelectTaggedObjects(prompt, _
title, scope, selAction, _
includeFeatures, keepHighlighted, selectionMask_array, _
selobj)
If resp = Selection.Response.Ok Then
Return Selection.Response.Ok
Else
Return Selection.Response.Cancel
End If

End Function
End Module

Export save as


'this journal:
'take a copy of existing part in the file system
'and paste inside a folder (where the part folder is) named "\arsiv"
'it doesn't save your file, or save as your file. just takes the
'part file which is open on your screen from your windows directory
'and copies it.

Option Strict Off
Imports System
Imports System.Collections.Generic
Imports NXOpen
Imports NXOpen.UF
Imports NXOpen.UI

Module NXJournal
Sub Main()

Dim theSession As Session = Session.GetSession()
Dim theUFSession As UFSession = UFSession.GetUFSession()

'********************************************************************************

Dim workPart As Part = theSession.Parts.Work
Dim lw As ListingWindow = theSession.ListingWindow

'find file name and folder path
Dim fileName As String = IO.Path.GetFileName(workPart.FullPath)
Dim fileNameNoExt As String = IO.Path.GetFileNameWithoutExtension(workPart.FullPath)
Dim parentFolder As String = IO.Path.GetDirectoryName(workPart.FullPath)

'prepare new backup folder if doesn't exist
Dim new_folder As String = parentFolder & "\arsiv"
My.Computer.FileSystem.CreateDirectory(new_folder)

Dim existingFile As String = parentFolder & "\" & fileName
Dim archiveFile As String = new_folder & "\" & fileName

My.Computer.FileSystem.CopyFile(existingFile, archiveFile, Microsoft.VisualBasic.FileIO.UIOption.AllDialogs, Microsoft.VisualBasic.FileIO.UICancelOption.DoNothing)

End Sub
End Module

Export solid


'this journal
'is using Export Part command to export the open file
'to a "..\backup" folder with date and time stamp placed in its name

Option Strict Off
Imports System
Imports System.Collections.Generic
Imports NXOpen
Imports NXOpen.UF
Imports NXOpen.UI

Module NXJournal
Sub Main()

Dim theSession As Session = Session.GetSession()
Dim theUFSession As UFSession = UFSession.GetUFSession()

'********************************************************************************

Dim workPart As Part = theSession.Parts.Work
Dim lw As ListingWindow = theSession.ListingWindow

'find file name and folder path
Dim fileName As String = IO.Path.GetFileName(workPart.FullPath)
Dim fileNameNoExt As String = IO.Path.GetFileNameWithoutExtension(workPart.FullPath)
Dim parentFolder As String = IO.Path.GetDirectoryName(workPart.FullPath)

'prepare a clean dd.mm.yy hh.mm.ss type string
Dim TimeNote As DateTime
TimeNote = System.DateTime.Now()
Dim full_date_time As String = TimeNote
Dim hour As String = Mid(full_date_time, Len(full_date_time) - 7, 2)
Dim minute As String = Mid(full_date_time, Len(full_date_time) - 4, 2)
Dim second As String = Mid(full_date_time, Len(full_date_time) - 1, 2)
Dim ddmmyy As String = Left(full_date_time, Len(full_date_time) - 9)
Dim year_short As String = Right(ddmmyy, 2)
ddmmyy = Left(ddmmyy, Len(ddmmyy) - 4) & year_short
ddmmyy = ddmmyy.PadLeft(8, "0")

'prepare new backup folder if doesn't exist
Dim yeni_klasor As String = parentFolder & "\backup"
My.Computer.FileSystem.CreateDirectory(yeni_klasor)

'result of new file path with new file name
Dim sonuc As String = yeni_klasor & "\" & fileNameNoExt & " " & ddmmyy & " " & hour & "." & minute & "." & second & ".prt"

'********************************************************************************

Dim abody() As TaggedObject
If SelectBodies("select bodies", abody) = Selection.Response.Cancel Then
Return
End If

Dim bodyTags As New List(Of Tag)

For Each temp As TaggedObject In abody
bodyTags.Add(temp.Tag)
Next

Dim options As UFPart.ExportOptions
With options
.expression_mode = UFPart.ExportExpMode.CopyExpDeeply
.new_part = True
.params_mode = UFPart.ExportParamsMode.RemoveParams
End With

theUFSession.Part.ExportWithOptions(sonuc, bodyTags.Count, bodyTags.ToArray, options)

End Sub

Function SelectBodies(ByVal prompt As String, ByRef selObj() As TaggedObject) As Selection.Response

Dim theUI As UI = UI.GetUI
Dim title As String = "Select one or more bodies"
Dim includeFeatures As Boolean = False
Dim keepHighlighted As Boolean = False
Dim selAction As Selection.SelectionAction = Selection.SelectionAction.ClearAndEnableSpecific
Dim scope As Selection.SelectionScope = Selection.SelectionScope.WorkPart
Dim selectionMask_array(0) As Selection.MaskTriple

With selectionMask_array(0)
.Type = UFConstants.UF_solid_type
.SolidBodySubtype = UFConstants.UF_UI_SEL_FEATURE_BODY
End With

Dim resp As Selection.Response = theUI.SelectionManager.SelectTaggedObjects(prompt, _
title, scope, selAction, _
includeFeatures, keepHighlighted, selectionMask_array, _
selobj)
If resp = Selection.Response.Ok Then
Return Selection.Response.Ok
Else
Return Selection.Response.Cancel
End If

End Function
End Module

Save and backup


'this journal
'is using Export Part command to export the open file
'to a "..\backup" folder with date and time stamp placed in its name

Option Strict Off
Imports System
Imports System.Collections.Generic
Imports NXOpen
Imports NXOpen.UF
Imports NXOpen.UI

Module NXJournal
Sub Main()

Dim theSession As Session = Session.GetSession()
Dim theUFSession As UFSession = UFSession.GetUFSession()

'********************************************************************************
' preparation
'********************************************************************************

Dim workPart As Part = theSession.Parts.Work
Dim lw As ListingWindow = theSession.ListingWindow

'find file name and folder path
Dim fileName As String = IO.Path.GetFileName(workPart.FullPath)
Dim fileNameNoExt As String = IO.Path.GetFileNameWithoutExtension(workPart.FullPath)
Dim parentFolder As String = IO.Path.GetDirectoryName(workPart.FullPath)

'prepare a clean dd.mm.yy hh.mm.ss type string
Dim TimeNote As DateTime
TimeNote = System.DateTime.Now()
Dim full_date_time As String = TimeNote
Dim hour As String = Mid(full_date_time, Len(full_date_time) - 7, 2)
Dim minute As String = Mid(full_date_time, Len(full_date_time) - 4, 2)
Dim second As String = Mid(full_date_time, Len(full_date_time) - 1, 2)
Dim ddmmyy As String = Left(full_date_time, Len(full_date_time) - 9)
Dim year_short As String = Right(ddmmyy, 2)
ddmmyy = Left(ddmmyy, Len(ddmmyy) - 4) & year_short
ddmmyy = ddmmyy.PadLeft(8, "0")
full_date_time = ddmmyy & " " & hour & "." & minute & "." & second

'prepare new backup folder if doesn't exist
Dim new_folder As String = parentFolder & "\backup"
My.Computer.FileSystem.CreateDirectory(new_folder)

'result of new file path with new file name
Dim sonuc As String = new_folder & "\" & fileNameNoExt & " " & full_date_time & ".prt"

'prepare file name strings
Dim existingFile As String = parentFolder & "\" & fileName
Dim backupFile As String = sonuc

'********************************************************************************

'save
Dim partSaveStatus1 As NXOpen.PartSaveStatus = Nothing
partSaveStatus1 = workPart.Save(NXOpen.BasePart.SaveComponents.True, NXOpen.BasePart.CloseAfterSave.False)

partSaveStatus1.Dispose()

'backup and rename
My.Computer.FileSystem.CopyFile(existingFile, backupFile, Microsoft.VisualBasic.FileIO.UIOption.AllDialogs, Microsoft.VisualBasic.FileIO.UICancelOption.DoNothing)

End Sub

End Module

Save, close, rename, open


'this journal:
' 1. saves the open part
' 2. close the open part
' 3. take a copy of the part to another folder named "..\arsiv"
' 4. rename the part. take the last 9 digits of part name and replace it with system date (format: dd.MM.yy)
' 5. open the renamed part

Option Strict Off
Imports System
Imports NXOpen
Imports System.Collections.Generic
Imports NXOpen.UF
Imports NXOpen.UI

Module NXJournal
Sub Main (ByVal args() As String)

Dim theSession As NXOpen.Session = NXOpen.Session.GetSession()
Dim workPart As NXOpen.Part = theSession.Parts.Work

Dim displayPart As NXOpen.Part = theSession.Parts.Display

'find file name and folder path
Dim fileName As String = IO.Path.GetFileName(workPart.FullPath)
Dim fileNameNoExt As String = IO.Path.GetFileNameWithoutExtension(workPart.FullPath)
Dim parentFolder As String = IO.Path.GetDirectoryName(workPart.FullPath)

' ----------------------------------------------
' Menu: File->Close->Save and Close
' ----------------------------------------------

Dim partSaveStatus1 As NXOpen.PartSaveStatus = Nothing
partSaveStatus1 = workPart.Save(NXOpen.BasePart.SaveComponents.True, NXOpen.BasePart.CloseAfterSave.True)

workPart = Nothing
displayPart = Nothing
partSaveStatus1.Dispose()
theSession.ApplicationSwitchImmediate("UG_APP_NOPART")

' ----------------------------------------------
' Menu: Tools->Journal->Stop Recording
' ----------------------------------------------

'prepare a clean dd.mm.yy hh.mm.ss type string
Dim TimeNote As DateTime
TimeNote = System.DateTime.Now()
TimeNote.ToString("dd.MM.yyyy HH.mm.ss")
Dim full_date_time As String = TimeNote
Dim ddmmyy As String = Left(full_date_time, Len(full_date_time) - 9)
Dim year_short As String = Right(ddmmyy, 2)
ddmmyy = Left(ddmmyy, Len(ddmmyy) - 4) & year_short
'put zero if it's a single digit day
ddmmyy = ddmmyy.PadLeft(8, "0")

'prepare new backup folder if doesn't exist
Dim new_folder As String = parentFolder & "\arsiv"
My.Computer.FileSystem.CreateDirectory(new_folder)

'clean last 9 digits of name to replace date stamp
Dim fileNameWithoutDate As String = Left(fileNameNoExt, Len(fileNameNoExt) - 9)

Dim existingFile As String = parentFolder & "\" & fileName
Dim archiveFile As String = new_folder & "\" & fileName
Dim newFileName As String = fileNameWithoutDate & "_" & ddmmyy & ".prt"
Dim newFileFullPath As String = parentFolder & "\" & newFileName

'copy file
My.Computer.FileSystem.CopyFile(existingFile, archiveFile, Microsoft.VisualBasic.FileIO.UIOption.AllDialogs, Microsoft.VisualBasic.FileIO.UICancelOption.DoNothing)

'check if already exists
If existingFile <> newFileFullPath Then My.Computer.FileSystem.RenameFile(existingFile, newFileName)
'My.Computer.FileSystem.RenameFile(existingFile, newFileName)

' ----------------------------------------------
' Menu: File->Open...
' ----------------------------------------------
Dim basePart1 As NXOpen.BasePart = Nothing
Dim partLoadStatus1 As NXOpen.PartLoadStatus = Nothing
basePart1 = theSession.Parts.OpenActiveDisplay(newFileFullPath, NXOpen.DisplayPartOption.AllowAdditional, partLoadStatus1)

partLoadStatus1.Dispose()
theSession.ApplicationSwitchImmediate("UG_APP_MODELING")

'open and see inside strings
'Dim lw As ListingWindow = theSession.ListingWindow

'lw.Open()
'lw.WriteLine("date: " & TimeNote)
'lw.WriteLine("ddmmyy: " & ddmmyy)
'lw.Close()

End Sub
End Module

Comments

Thankyou for the Journal to create a Backup file. Can someone help me with assembly file. Our requirement is to create a backup for main part along with all the subparts of the assembly in backup folder. We work on NX 12 and NX 1855 versions.
Thanks in advance.

I too have similar requirements for backing of main assy along with sub-assy, will be greatly appreciated if anyone help on this. thank you in advance

Niranjan