Autodesk Inventor: Cập Nhật Part Number và Part Name Cho Tất Cả Part và SubAssambly Dựa Vào Tên File (iLogic & API)

Để dễ quản lý tên file có nhiều quy tắc đặt tên, một ví dụ là “AAAA0000 Part Name”. Với 8 ký tự đầu trước khoảng trắng đặt cho mã số (Part Number), sau khoảng trắng là tên của chi tiết (Part Name). Khi tạo một Part hoặc một Assembly trên phần mềm Autodesk Inventor. Mặc định Part Number sẽ lấy từ tên của file vừa tạo bỏ đi phần mở rộng và tên part là trống.

Sau khi hoàn thành cụm lắp ráp. Để cập nhật lại Part Number và Part Name thay vì mở từng file và sửa trong mục iProperties mất thời gian và dễ nhầm lẫn. Vì vậy một Script tự động cập nhật là cần thiết.

Dưới đây là đoạn Script Rule iLogic của Autodesk Inventor. Chức năng của Script này là lần lượt mở tất cả các component trong file Assemply. Chép các ký tự trước khoảng trắng đặt vào Part Number, và chép các ký tự sau khoảng trắng đặt vào Title.

' iLogic rule để thay đổi Part Number và Title của tất cả các Part và SubAssembly trong một Assembly
Sub Main()
Dim oApp As Inventor.Application
oApp = ThisApplication

Dim oAssemblyDoc As AssemblyDocument
oAssemblyDoc = oApp.ActiveDocument

' Kiểm tra xem có thành phần nào trong danh sách hay không
If Not oAssemblyDoc.ComponentDefinition.Occurrences.Count = 0 Then
    ' Gọi hàm để thay đổi Part Number và Title của tất cả các Part và Assembly
    Call ChangePartAndAssemblyProperties(oAssemblyDoc.ComponentDefinition.Occurrences)
End If
MessageBox.Show("Part Number and Title has been updated.", "Title")

End Sub

Sub ChangePartAndAssemblyProperties(oOccurrences As ComponentOccurrences)
	
	'oOccurrences = oAssemblyDoc.ComponentDefinition.Occurrences
	
    For Each oOccurrence In oOccurrences
        ' Thay đổi Part Number và Title nếu là Part hoặc Assembly
        If oOccurrence.DefinitionDocumentType = DocumentTypeEnum.kPartDocumentObject Or _
           oOccurrence.DefinitionDocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then
            ' Truy cập đến Definition của ComponentOccurrence
            Dim oComponentDef As ComponentDefinition
            oComponentDef = oOccurrence.Definition

		Dim componentName As String
		componentName = oOccurrence.Name
	   	Dim componentFileName As String
		componentFileName = oOccurrence.Definition.Document.FullFileName
		Dim fileNameWithoutPathAndExtension As String
        fileNameWithoutPathAndExtension = System.IO.Path.GetFileNameWithoutExtension(componentFileName)
				Dim D As String
				Dim E As String
				Call SplitFileName(fileNameWithoutPathAndExtension, D, E)
				
            ' Thay đổi Part Number nếu có
            If PropertyExists(oComponentDef.Document.PropertySets.Item("Design Tracking Properties"), "Part Number") Then
                oComponentDef.Document.PropertySets.Item("Design Tracking Properties").Item("Part Number").Value = D
            End If

            ' Thay đổi Title nếu có
            If PropertyExists(oComponentDef.Document.PropertySets.Item("Inventor Summary Information"), "Title") Then
                oComponentDef.Document.PropertySets.Item("Inventor Summary Information").Item("Title").Value = E
            End If
        End If

        ' Nếu là một assembly con, gọi đệ quy để xử lý các thành phần con
        If oOccurrence.DefinitionDocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then
            Call ChangePartAndAssemblyProperties(oOccurrence.SubOccurrences)
        End If
    Next
End Sub


Function PropertyExists(oPropertySet As PropertySet, sPropertyName As String) As Boolean
    On Error Resume Next
    PropertyExists = oPropertySet.Item(sPropertyName) IsNot Nothing
    On Error GoTo 0
End Function


Sub SplitFileName(A As String, ByRef D As String, ByRef E As String)	
	Dim B As Integer = 0
	Dim C As Integer = 0
	Dim j As Integer = 0
	Dim k As String = 0
	
	B = Len(A)
	D = A
	E = A
	For j = 1 To B
		If String.Compare(Mid(A, j, 1), " ", True) = 0 Then
		k = k + 1
			If k = 1 Then
				C = j
				D = Mid(A, 1, C - 1)
				E = Mid(A, C+1, B-C)
				Exit For
			End If
		End If
	Next j
End Sub

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 *