Autodesk Inventor: Copy Tài Liệu Dự Án Sang Thư Mục Khác

Inventor quản lý các tài liệu dự án thiết kế thông qua file Project (.ipj) dưới dạng file .xml. Vì vậy nếu chỉ copy paste ở cửa xổ Windows thì file tài liệu mới sẽ nhớ đường dẫn tới project cũ. Để lưu bản sao tài liệu dự án để lưu trữ hoặc gửi đi thì ta dùng lệnh File/Save As/Pack and Go áp dụng cho toàn bộ dự án hoặc cụm chi tiết.

Để lưu từng file riêng thì ta dùng lệnh Save As hoặc Save Copy As. Nhưng hai lệnh này mặc định mỗi lần lưu vị trí hộp thoại lại xuất hiện tại thư mục tài liệu dự án hiện tại được lưu. Việc chọn đi chọn lại đích lưu sẽ mất thời gian đặc biệt là vị trí lưu trên server dùng chung với đường dẫn dài.

Để lưa Save As và nhớ vị trí vừa lưu trước đó ta dùng Script iLogic. Code như sau:

Sub Main()
If ThisDoc.Document.DocumentType = DocumentTypeEnum.kPartDocumentObject Then
		
    Dim oDoc As Document = ThisDoc.Document
	
    If oDoc Is Nothing Then
        MessageBox.Show("Nothing Document is Opening.")
        Exit Sub
    End If

    Dim oFileDialog As New System.Windows.Forms.SaveFileDialog()
    oFileDialog.Title = "Select Folder"
    oFileDialog.Filter = "(*.ipt, *.iam)|*.ipt;*.iam|All Files (*.*)|*.*"
    
    ' Lấy tên của tài liệu hiện tại và sử dụng nó làm tên mặc định cho tệp sao lưu
    Dim defaultFileName As String = System.IO.Path.GetFileNameWithoutExtension(oDoc.FullFileName)
    If defaultFileName <> "" Then
        oFileDialog.FileName = defaultFileName
    End If

    ' Lấy đường dẫn thư mục lưu trước đó từ tùy chọn người dùng
    Dim lastFolderPath As String = GetUserOption("LastFolderPath")
    If lastFolderPath <> "" Then
        oFileDialog.InitialDirectory = lastFolderPath
    End If

    If oFileDialog.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
        Dim savePath As String = oFileDialog.FileName
        Try
            oDoc.Save2()

            oDoc.SaveAs(savePath, True)
            
            ' Lưu đường dẫn thư mục đã chọn để sử dụng cho các lần chạy rule sau
            SetUserOption("LastFolderPath", System.IO.Path.GetDirectoryName(savePath))

            MessageBox.Show("Document is saved.")
        Catch ex As Exception
			'On Error Resume Next
            'MessageBox.Show("Lỗi 0: " & ex.Message)

        End Try
    Else
        MessageBox.Show("Cancelled.")
    End If
Else 
	MessageBox.Show("Document is not a part. Try again.")
End If
End Sub

Function GetUserOption(optionName As String) As String
    GetUserOption = ""
    Try
        Dim userOptions As Inventor.PropertySet
        userOptions = ThisApplication.TransientObjects.Create.PropertySet
        userOptions.Add("MyAddIn")
        GetUserOption = userOptions.Value(optionName)
    Catch ex As Exception
		'On Error Resume Next
        'MessageBox.Show("Lỗi 1: " & ex.Message)
    End Try
End Function

Sub SetUserOption(optionName As String, optionValue As String)
    Try
        Dim userOptions As Inventor.PropertySet
        userOptions = ThisApplication.TransientObjects.Create.PropertySet
        userOptions.Add("MyAddIn")
        userOptions.Value(optionName) = optionValue
    Catch ex As Exception
		'On Error Resume Next
		'MessageBox.Show("Lỗi 2: " & ex.Message)
    End Try
End Sub

Trong môi trường Assembly, để chọn 1 part để lưu, ta nhấp đôi vào part đó và chạy Script rồi lưu như lệnh Save As bình thường. Chọn part tiếp theo cần lưu, làm tương tự. Lần sau hộp thoại lưu sẽ xuất hiện ở vị trí lưu của lần kề trước. Ở ví dụ này là thư mục New folder như hình bên dưới.

Bình luận

Để lại một bình luận

Email của bạn sẽ không được hiển thị công khai. Các trường bắt buộc được đánh dấu *