Chạy ổn định trên Inventor 2022
- PHIÊN BẢN 1
Dim rootFolder As String = ThisDoc.Path
Dim stepFolder As String = rootFolder & "\_step"
' Tạo thư mục nếu chưa tồn tại
If Not System.IO.Directory.Exists(stepFolder) Then
System.IO.Directory.CreateDirectory(stepFolder)
End If
' Lấy danh sách file .ipt và .iam
Dim files = System.IO.Directory.GetFiles(rootFolder, "*.ipt")
files = files.Concat(System.IO.Directory.GetFiles(rootFolder, "*.iam")).ToArray()
' Thiết lập tùy chọn STEP
Dim ctx As NameValueMap = ThisApplication.TransientObjects.CreateNameValueMap()
Dim oOptions As NameValueMap = ThisApplication.TransientObjects.CreateNameValueMap()
Dim oContext As TranslationContext = ThisApplication.TransientObjects.CreateTranslationContext()
oContext.Type = IOMechanismEnum.kFileBrowseIOMechanism
Dim oSTEP As TranslatorAddIn
Dim i As Integer
For i = 1 To ThisApplication.ApplicationAddIns.Count
If ThisApplication.ApplicationAddIns.Item(i).ClassIdString = "{90AF7F40-0C01-11D5-8E83-0010B541CD80}" Then
oSTEP = ThisApplication.ApplicationAddIns.Item(i)
Exit For
End If
Next
If oSTEP Is Nothing Then
MessageBox.Show("Không tìm thấy STEP Translator.", "Lỗi")
Return
End If
If Not oSTEP.Activated Then oSTEP.Activate()
' Lặp qua từng file và xuất ra STEP
For Each filePath In files
Try
Dim doc As Document = ThisApplication.Documents.Open(filePath, False)
Dim docName As String = System.IO.Path.GetFileNameWithoutExtension(filePath)
Dim outputFile As String = stepFolder & "\" & docName & ".stp"
Dim oData As DataMedium = ThisApplication.TransientObjects.CreateDataMedium()
oData.FileName = outputFile
oSTEP.SaveCopyAs(doc, oContext, oOptions, oData)
doc.Close(True)
Catch ex As Exception
MessageBox.Show("Lỗi với file: " & filePath & vbCrLf & ex.Message, "Lỗi")
End Try
Next
MessageBox.Show("Xuất STEP hoàn tất!", "Thành công")- PHIÊN BẢN 2
AddReference "System.Windows.Forms"
Imports System.Windows.Forms
Imports System.IO
' Chỉ xử lý file .ipt và .iam trong thư mục gốc, không duyệt thư mục con
' Không đóng mô hình hiện tại sau khi xuất
' Tạo danh sách tùy chọn
Dim options As New ArrayList
options.Add("Xuất tất cả các mô hình trong Project đang mở")
options.Add("Xuất tất cả các mô hình trong thư mục được chọn")
' Hiển thị hộp thoại chọn tùy chọn
Dim selectedOption = InputListBox("Chọn tùy chọn xuất file", options, options(0), "Xuất mô hình sang định dạng STEP", "Danh sách")
' Nếu hủy thì thoát
If selectedOption = "" Then Return
' Xác định thư mục gốc dựa trên tùy chọn
Dim rootFolder As String
If selectedOption = options(0) Then
rootFolder = ThisApplication.FileLocations.Workspace
Else
Dim folderDialog As New FolderBrowserDialog()
folderDialog.Description = "Chọn thư mục chứa các mô hình"
folderDialog.ShowNewFolderButton = False
If folderDialog.ShowDialog() = DialogResult.OK Then
rootFolder = folderDialog.SelectedPath
Else
Return
End If
End If
' Chọn thư mục đích lưu trữ
Dim stepFolder As String
Dim destFolderDialog As New FolderBrowserDialog()
destFolderDialog.Description = "Chọn thư mục đích lưu trữ file STEP"
destFolderDialog.ShowNewFolderButton = True
If destFolderDialog.ShowDialog() = DialogResult.OK Then
stepFolder = destFolderDialog.SelectedPath
Else
Return
End If
' Tạo thư mục đích nếu chưa tồn tại
If Not Directory.Exists(stepFolder) Then
Directory.CreateDirectory(stepFolder)
End If
' Lấy danh sách file .ipt và .iam chỉ trong thư mục gốc (không duyệt thư mục con)
Dim iptFiles = Directory.EnumerateFiles(rootFolder, "*.ipt", SearchOption.TopDirectoryOnly)
Dim iamFiles = Directory.EnumerateFiles(rootFolder, "*.iam", SearchOption.TopDirectoryOnly)
Dim files = iptFiles.Concat(iamFiles).ToArray()
' Thiết lập tùy chọn STEP
Dim ctx As NameValueMap = ThisApplication.TransientObjects.CreateNameValueMap()
Dim oOptions As NameValueMap = ThisApplication.TransientObjects.CreateNameValueMap()
Dim oContext As TranslationContext = ThisApplication.TransientObjects.CreateTranslationContext()
oContext.Type = IOMechanismEnum.kFileBrowseIOMechanism
Dim oSTEP As TranslatorAddIn
Dim i As Integer
For i = 1 To ThisApplication.ApplicationAddIns.Count
If ThisApplication.ApplicationAddIns.Item(i).ClassIdString = "{90AF7F40-0C01-11D5-8E83-0010B541CD80}" Then
oSTEP = ThisApplication.ApplicationAddIns.Item(i)
Exit For
End If
Next
If oSTEP Is Nothing Then
MessageBox.Show("Không tìm thấy STEP Translator.", "Lỗi")
Return
End If
If Not oSTEP.Activated Then oSTEP.Activate()
' Lấy tài liệu hiện tại
Dim activeDoc As Document = ThisDoc.Document
' Lặp qua từng file và xuất ra STEP
For Each filePath In files
Try
Dim doc As Document = ThisApplication.Documents.Open(filePath, False)
' Lấy tên file không có phần mở rộng
Dim docName As String = System.IO.Path.GetFileNameWithoutExtension(filePath)
Dim outputFile As String = stepFolder & "\" & docName & ".stp"
Dim oData As DataMedium = ThisApplication.TransientObjects.CreateDataMedium()
oData.FileName = outputFile
oSTEP.SaveCopyAs(doc, oContext, oOptions, oData)
' Chỉ đóng tài liệu nếu không phải là tài liệu hiện tại
If Not doc Is activeDoc Then
doc.Close(True)
End If
Catch ex As Exception
MessageBox.Show("Lỗi với file: " & filePath & vbCrLf & ex.Message, "Lỗi")
End Try
Next
MessageBox.Show("Xuất STEP hoàn tất!", "Thành công")
Bình luận