Autodesk Inventor: Xuất Danh Sách Vật Tư (BOM) Tùy Chỉnh Excel bằng iLogic và API

Trong Autodesk Inventor cũng có công cụ để xuất danh sách vật tư, đó là lệnh Bill Of Materials. Tuy nhiên, công cụ này chỉ có một ít tùy chỉnh như thêm bớt số cột, tùy chỉnh định dạng xuất,… Nếu có nhu cầu xuất danh sách vật tư với một mẫu thống nhất và lặp đi lặp lại. Thì việc xuất bảng vật tư bằng iLogic và API là rất hữu ích. Các bước để xuất bảng vật tư bằng iLogic và API như sau:

Tạo một file Excel mẫu

Tạo một file Excel mẫu dùng để xuất dữ liệu từ Inventor vào mẫu có sẵn này. Đặt tên file Excel mẫu này là BOM_Template.xlsx, tên Sheet là BOM. Ví dụ tạo một bảng mẫu như sau:

Tạo Script iLogic

Mở File lắp ráp trên phần mềm Autodesk Inventor. Vào Manage/Add Rule. Hoặc tạo Rule ngoài bằng cách tạo file Script đặt tên là BOMExport2Excel.txt. Nhập đoạn code sau. Tùy chỉnh đoạn code theo yêu cầu xuất ra.

Sub Main()
	' Khai bao doi tuong
	Dim oAssyDoc As AssemblyDocument 
	oAssyDoc = ThisApplication.ActiveDocument
	Dim oAssyCompDef As AssemblyComponentDefinition
	oAssyCompDef = oAssyDoc.ComponentDefinition

	' Lay duong dan cua Assembly
	Dim sPath As String = ThisDoc.Path

	' Dat Master LevelOfDetail. Can co Master LoD de thao tac voi BOM
	Dim oLOD As LevelOfDetailRepresentation
	Try
		oLOD = oAssyCompDef.RepresentationsManager.LevelOfDetailRepresentations.Item("Master") 
	Catch ex As System.ArgumentException
	Finally 
		oLOD.Activate(True)
	End Try

	' Lay BOM tu Assembly
	Dim oBOM As BOM
	oBOM = oAssyCompDef.BOM
	Logger.Trace("oBOM is set")
	
	oBOM.StructuredViewEnabled = True
	' oBOM.PartsOnlyViewEnabled = True
	
	Dim oBOMView As BOMView
		oBOMView = oBOM.BOMViews.Item("Structured")
	' oBOMView = oBOM.BOMViews.Item("Parts Only")
	Logger.Trace("oBOMView is set")

	oBOM.StructuredViewFirstLevelOnly = False

	' Xuat BOM
	' Sao chep thong tin tu file Template sang file moi va dat ten file moi
	Dim sJobBOMTempName As String = String.Concat(sPath,"\BOM_Template.xlsx")
	Dim sJobBOMName As String = String.Concat (sPath, "\",ThisDoc.FileName(False) & " Part list" & ".xlsx")
	Logger.Trace("sJobBOMName: " & sJobBOMName)

	Try
		IO.File.Copy(sJobBOMTempName, sJobBOMName, True)
	Catch ex As Exception
		MessageBox.Show(ex.Message)
	End Try

	Dim sMySheet As String = "BOM" ' Bat dau voi sheet co ten la BOM
	Static iCurrRow As Integer = 9 ' Bat dau tu hang so 9

	GoExcel.DisplayAlerts = False
	GoExcel.Open(sJobBOMName, sMySheet)


	' Ghi gia tri tu BOM sang Excel
	Call QueryBOMRowProperties(oBOMView.BOMRows, sJobBOMName, iCurrRow)
	oAssyDoc.Save
	
	
	GoExcel.CellValue("B2:F5") = String.Concat(iProperties.Value("Summary", "Title"))
	GoExcel.CellValue("H2") = String.Concat(iProperties.Value("Project", "Part Number"))
	GoExcel.CellValue("H3") = String.Concat(today())
	GoExcel.CellValue("H3") = DateTime.Now.ToString("dd/MM/yyyy")
	GoExcel.CellValue("I3:J5") = String.Concat("NHO")
	
	' Luu va dong Excel
		GoExcel.Save
		GoExcel.Close

	' Set to iLogic LevelOfDetail Try
	Try
		oLOD = oAssyCompDef.RepresentationsManager.LevelOfDetailRepresentations.Item("iLogic")
	Catch ex As System.ArgumentException
		oLOD = oAssyCompDef.RepresentationsManager.LevelOfDetailRepresentations.Add("iLogic")
	Finally
		oLOD.Activate(True)
	End Try

	' Hien thong bao khi hoan thanh
	Dim sMessage As String = "BOM đã được xuất." & " File BOM tại " & sPath & "\" & ThisDoc.FileName(False) & " Part list" & ".xlsx"
	Dim sCaption As String = "BOM Export Complete" 
	Dim oButtons As MessageBoxButtons = MessageBoxButtons.OK
	Dim oIcons As MessageBoxIcon = MessageBoxIcon.Information

	Dim oResult As DialogResult

	' Hien MessageBox
	oResult = MessageBox.Show(sMessage, sCaption, oButtons, oIcons)

End Sub


Public Sub QueryBOMRowProperties(oBOMRows As BOMRowsEnumerator, JobSpreadsheetName As String, iCurrRow As Integer)
	' Lap lai thong qua noi dung cua hang BOM
	Static CurrentRow As Integer = iCurrRow
	Dim i As Long
	Dim sWriteCell As String

	For i = 1 To oBOMRows.Count

	' Lay dong hien tai
	Dim oRow As BOMRow
	oRow = oBOMRows.Item(i)

	' Dat tham chieu den dinh nghia linh kien chinh cua hang
	Dim oCompDef As ComponentDefinition
	oCompDef = oRow.ComponentDefinitions.Item(1)

	Dim oPropSets As PropertySets
	oPropSets = oCompDef.Document.PropertySets

	oDesignTrackingPropertySet = oPropSets.Item("Design Tracking Properties")
	oInventorSummaryPropertySet = oPropSets.Item("Inventor Summary Information")
	oCustomPropertySet = oPropSets.Item("Inventor User Defined Properties")

	' Lay cac thuoc tinh file duoc yeu cau
	oItemNumberProperty = oRow.ItemNumber
	oPartNumProperty = oDesignTrackingPropertySet.Item("Part Number")
	oDescripProperty = oDesignTrackingPropertySet.Item("Description")
	oTitleProperty = oInventorSummaryPropertySet.Item ("Title")

	Trace.WriteLine("iLogic: oItemNumberProperty: " & oItemNumberProperty & " | " & oPartNumProperty.Value & " | " & oDescripProperty.Value) 

	sWriteCell = "A" & CurrentRow
	GoExcel.CellValue(sWriteCell) = oItemNumberProperty


	sWriteCell = "B" & CurrentRow
	D = CountDots(oItemNumberProperty)
		If D = 0 Then
			GoExcel.CellValue(sWriteCell) = "●"
		Else
			GoExcel.CellValue(sWriteCell) = " "
		End If 

	sWriteCell = "C" & CurrentRow
	D = CountDots(oItemNumberProperty)
		If D = 1 Then
			GoExcel.CellValue(sWriteCell) = "●"
		Else
			GoExcel.CellValue(sWriteCell) = " "
		End If

	sWriteCell = "D" & CurrentRow
	D = CountDots(oItemNumberProperty)
		If D = 2 Then
			GoExcel.CellValue(sWriteCell) = "●"
		Else
			GoExcel.CellValue(sWriteCell) = " "
		End If

	sWriteCell = "E" & CurrentRow
	D = CountDots(oItemNumberProperty)
		If D = 3 Then
			GoExcel.CellValue(sWriteCell) = "●"
		Else
			GoExcel.CellValue(sWriteCell) = " "
		End If	

	sWriteCell = "F" & CurrentRow
	D = CountDots(oItemNumberProperty)
		If D = 4 Then
			GoExcel.CellValue(sWriteCell) = "●"
		Else
			GoExcel.CellValue(sWriteCell) = " "
		End If

	sWriteCell = "G" & CurrentRow
	GoExcel.CellValue(sWriteCell) = oPartNumProperty.Value

	sWriteCell = "H" & CurrentRow
	GoExcel.CellValue(sWriteCell) = oTitleProperty.Value

	sWriteCell = "I" & CurrentRow
	GoExcel.CellValue(sWriteCell) = oRow.ItemQuantity
	
	' Tang hang hien tai
	CurrentRow = CurrentRow + 1

	' Lap lai de quy cac hang con neu co
	If Not oRow.ChildRows Is Nothing Then
		Call QueryBOMRowProperties(oRow.ChildRows, JobSpreadsheetName, CurrentRow)
	Else
	End If

	Next i
End Sub

Function CountDots(oItemNumberProperty) As Integer
    Dim dotCount As Integer
    Dim i As Integer
    A = CStr(oItemNumberProperty)
    dotCount = 0
    
    For i = 1 To Len(A)
        If Mid(A, i, 1) = "." Or Mid(A, i, 1) = "," Or Mid(A, i, 1) = "-" Or Mid(A, i, 1) = ":" Or Mid(A, i, 1) = "/" Or Mid(A, i, 1) = "\" Then
            dotCount = dotCount + 1
        End If
    Next i
    
    CountDots = dotCount
End Function

Ở đây có thể tùy chỉnh các giá trị:

  • Có thể tùy chỉnh tên file Excel xuất ra ở dòng: Dim sJobBOMName As String = String.Concat (sPath, "\",ThisDoc.FileName(False) & " Part list" & ".xlsx")
  • Tùy chỉnh dòng bắt đầu xuất các thông tin component: Static iCurrRow As Integer = 9
  • Định dạng ngày tháng và người tạo: GoExcel.CellValue("H3") = DateTime.Now.ToString("dd/MM/yyyy") GoExcel.CellValue("I3:J5") = String.Concat("NHO")

Sau khi tạo xong. Vào tab Manage/iLogic Browser/iLogic/External Rules tab/Manual Added/Phải chuột chọn Add External Rule/Chọn file BOMExport2Excel.txt vừa tạo/Phải chuột/Run Rule.

Kết quả

Sau khi chạy Rule BOMExport2Excel từ iLogic. Một file Excel sẽ được tạo ra ở cùng đường dẫn với file Assembly và có tên giống với tên Assembly cộng với phần đuôi là Part list.xlsx.

4 bình luận trong “Autodesk Inventor: Xuất Danh Sách Vật Tư (BOM) Tùy Chỉnh Excel bằng iLogic và API

  1. Một công cụ hữu ích . Tôi đã chạy rule kết quả trả về như sau , bạn có thể giúp tôi sửa lỗi này được không . tôi cảm ơn .
    Rule Compile Errors in BOMExport2Excel, in Disk Mill Machine.iam

    Error on Line 23 : ‘Logger’ is not declared. It may be inaccessible due to its protection level.
    Error on Line 31 : ‘Logger’ is not declared. It may be inaccessible due to its protection level.
    Error on Line 39 : ‘Logger’ is not declared. It may be inaccessible due to its protection level.

    1. Tôi đã chạy thử để kiểm tra nhưng không phát hiện lỗi. Bạn sử dụng phần mềm Inventor phiên bản nào?
      Các đoạn code ở dòng 23, 31, 39 chỉ mục đích để ghi log lại. Không ảnh hưởng đế chương trình chính.
      Nên bạn có thể bỏ qua chúng bằng cách comment (‘) hoặc xóa chúng khỏi code.

Đã đóng bình luận.