Hi Alex,
The aim is I'm looking for the solution of bigger iLogic rule / problem.
I've created the rule which is responsible for export of BOM to the excell file.
But this is not only simple export. In fact we need the data to be ready for print as a template we use.
So the rule sends BOM (row by row - missing content center parts or parts marked as "purchased") to the excell file
and then starts the macro which is responsilbe for sorting data to looks as we would like to.
Because at the end I would like to equipped all Inventor user with the rule, I'd like to be sure we are able to avoid any error situation.
Right now the rule works quite fine but there are two problematic situations:
1) When there are any missing parts (unresolved parts) within the assembly. Specialy when we share whole assemblies between users and some parts are missing during that process.
2) When the view representation is not set up as main
Below You can see the complete rule right now i have:
SyntaxEditor Code Snippet
Dim oDoc As Inventor.Document
oDoc = ThisApplication.ActiveDocument
'At first we're chcecking if opened file is an assembly or single part file. For single part system is running other rule.
If oDoc.DocumentType <> kAssemblyDocumentObject Then
If oDoc.DocumentType = kPartDocumentObject Then
UserChoice0 = MessageBox.Show ( "Czy chcesz wygenerowa� Kart� Ci�cia dla pojedy�czej cz�ci?", "iLogic",MessageBoxButtons.YesNo)
If UserChoice0 = vbNo Then
Exit Sub
Else
iLogicVb.RunExternalRule("Karta Ci�cia Cz��")
Exit Sub
End If
End If
End If
'We're checking if the assembly file was already saved.
FilePath = ThisDoc.Path
If FilePath = "" Then
MessageBox.Show("Plik z�o�enia nie zosta� zapisany!" _
& vbLf & " " _
& vbLf & "Zapisz plik, nast�pnie uruchom regu�� ponownie.", "iLogic")
Exit Sub
End If
'Notification for user to be sure he would like to run the rule.
UserChoice2 = MessageBox.Show ( "Czy chcesz wygenerowa� Kart� Ci�cia?" _
& vbLf & " " _
& vbLf & "Upewnij si� �e wszystkie niezb�dne dane zosta�y uzupe�nione.", "iLogic",MessageBoxButtons.YesNo)
If UserChoice2 = vbNo Then
Exit Sub
End If
'Input box to get user name
Opracowal = InputBox("Kart� Ci�cia Opracowa�:", "iLogic","Tomasz Pa�asz")
Dim oAsmCompDef As AssemblyComponentDefinition
oAsmCompDef = oDoc.ComponentDefinition
'Below a peace of code to check if the detail representation - main is set up. Currently it doesn't work I don't know why?
'Dim oLOD As LevelOfDetailRepresentation
'Try
'oLOD = oDoc.RepresentationsManager.LevelOfDetailRepresentations.Item("G��wna").Activate(True)
'Catch
'MessageBox.Show("Wyst�pi� problem z uaktywnieniem reprezentacji widoku - G��wna", "iLogic")
'Exit Sub
'End Try
'Below a peace of code to check if there are any unresolved parts within the assembly. If there are any it would be 'impossible to open BOM
Dim oRefFile As FileDescriptor
For Each oRefFile In oDoc.File.ReferencedFileDescriptors
Dim AsmFilePath As String = oRefFile.FullFileName
If Not System.IO.File.Exists(AsmFilePath) Then
MessageBox.Show("Prawdopodobnie otwarte z�o�enie zawiera nierozwi�zane komponenty." _
& vbLf & "Upewnij si� �e w z�o�eniu nie ma nierozwi�zanych komponent�w." _
& vbLf & "Nast�pnie uruchom regu�� ponownie", "iLogic")
Exit Sub
Else
End If
Next
'Now main part - which is responsible for BOM file export into an Excell file. After that the excell macro starts to sort the data within an excell file.
Dim oBOM As BOM
oBOM = oAsmCompDef.BOM
oBOM.PartsOnlyViewEnabled = True
Dim oBOMView As BOMView
oBOMView = oBOM.BOMViews.Item("Tylko Cz�ci")
oBOMView.Sort("Part Number", True)
oBOMView.Renumber(001)
Dim oBOMRow As BOMRow
'The rule checks for file path where an excell file is located.
Dim Filename As String = "C:\Users\Public\Documents\Autodesk\Inventor 2018\Templates\Karta Ci�cia Wz�r.xlsm" '<---- TUTAJ ZMIE� �CIE�K� !
'If an excell file is already opened the rule will try to close it.
Try
ObjXL = GetObject(Filename)
ObjXL.Application.Quit
Catch
End Try
GoExcel.Open(Filename,"Arkusz2")
i=3
'The rule is counting total number of rows in the BOM
j=0
For Each oBOMRow In oBOMView.BOMRows
If oBOMRow.BOMStructure <> 51973 Then
Dim oCompDef As ComponentDefinition
oCompDef = oBOMRow.ComponentDefinitions.Item(1)
FullFileName = oCompDef.Document.FullFileName
Pozycja=InStrRev(FullFileName,"\", -1)
NazwaModelu=Right(FullFileName,Len(FullFileName)-Pozycja)
NumerCzesci=iProperties.Value(NazwaModelu, "Project", "Part Number")
Opis=iProperties.Value(NazwaModelu, "Project", "Description")
'For every row in the BOM we're trying to catch custom iProperties vaulues
Try
Nazwa=iProperties.Value(NazwaModelu, "Custom", "Nazwa")
Catch
Nazwa=""
End Try
Try
Rodzaj=iProperties.Material(NazwaModelu)
Catch
Rodzaj=""
End Try
Try
Wymiar=iProperties.Value(NazwaModelu, "Custom", "Wymiar")
Catch
Wymiar=""
End Try
Try
Uwagi=iProperties.Value(NazwaModelu, "Custom", "Uwagi")
Catch
Uwagi=""
End Try
Ilosc=oBOMRow.Totalquantity
GoExcel.CellValue("B" & i)=Opis
GoExcel.CellValue("C" & i)=NumerCzesci
GoExcel.CellValue("D" & i)=Nazwa
GoExcel.CellValue("E" & i)=Rodzaj
GoExcel.CellValue("F" & i)=Wymiar
GoExcel.CellValue("H" & i)=Uwagi
GoExcel.CellValue("G" & i)=Ilosc
i=i+1
j=j+1
End If
Next
'Based on total rows number the rule takes proper no. of excell sheet pages and sends it to the excell file.
'[
Select Case j
Case 1 To 12
Iloscstr = 1
Case 13 To 24
Iloscstr = 2
Case 25 To 36
Iloscstr = 3
Case 37 To 48
Iloscstr = 4
Case 49 To 60
Iloscstr = 5
End Select
']
Try
Artykul = iProperties.Value("Project", "Stock Number")
Catch
End Try
GoExcel.CellValue("A1")=iProperties.Value("Project", "Part Number")
GoExcel.CellValue("B1")= Iloscstr
GoExcel.CellValue("A2")= "Nr Art." & " " & Artykul
GoExcel.CellValue("D1")=iProperties.Value("Project", "Description")
GoExcel.CellValue("G1")= Opracowal
GoExcel.DisplayAlerts = False
GoExcel.Save
GoExcel.Close
'Here the rule fires up the macro.
Dim ExApp As Object
ExApp = CreateObject("Excel.Application")
Try 'Tutaj dodano fragment.
For Each wb As Object In ExApp.workbooks
If UCase(wb.fullname) = UCase(filename) Then
'wb.Save
wb.Close
Exit For
End If
Next
Catch
Finally
excelApp = Nothing
End Try 'Tutaj ko�czy si� dodany fragment.
ExApp.workbooks.Open (Filename)
ExApp.Application.Run ("KartaCieciaWzor")
ExApp.Visible = False
ExApp = Nothing
Dim Numer As String
'The rule tries to check if the end excell file was properly created and saved.
Numer = "C:\Users\t.palasz\Desktop\Karty Ci�cia\" & iProperties.Value("Project", "Part Number") & ".xlsx" '<---- TUTAJ ZMIE� �CIEK�
If System.IO.File.Exists(Numer) Then
UserChoice3 = MessageBox.Show ("Karta Ci�cia zosta�a wygenerowana pomy�lnie!" & vbLf & " " & vbLf & "Czy chcesz otworzy� Kart� Ci�cia?", "iLogic",MessageBoxButtons.YesNo)
If UserChoice3 = vbYes Then
ThisDoc.Launch(Numer)
Exit Sub
Else
Exit Sub
End If
Else
MessageBox.Show("Upss... Co poszo nie tak!", "iLogic")
Exit Sub
End If