iLogic export BOM with template

iLogic export BOM with template

mgFNR8M
Contributor Contributor
1,151 Views
6 Replies
Message 1 of 7

iLogic export BOM with template

mgFNR8M
Contributor
Contributor

Hi.

 

I have a simple iLogic macro to export BOM, but I can't find the way to use the "Template" field as shown in th next screenshot.

 

Can anyone help exporting BOM with this field filled?

 

Screenshot_1.jpg

0 Likes
1,152 Views
6 Replies
Replies (6)
Message 2 of 7

marcin_otręba
Advisor
Advisor

hi it is for exporting partslist from drawing not for BOM.

It must be passed trough options:

 Set oOptions = ThisApplication.TransientObjects.CreateNameValueMap
        oOptions.value("Template") = path & "Szablon XLS.xls"
        oOptions.value("StartingCell") = "A12"

 

Hi, maybe you want to vote my:

Ideas

or check my apps:

DrawingTools   View&ColoringTools   MRUFolders

0 Likes
Message 3 of 7

marcin_otręba
Advisor
Advisor

you can automate proces to generate idw with partslist automatically in background and then export it.

I used to do it, but if in template tyou have any overrides or formulas it will be deleted because inventor will pusz data and overwrite all.

 

right now i flushing data to each cel during export, because i have some formulas inside.

Hi, maybe you want to vote my:

Ideas

or check my apps:

DrawingTools   View&ColoringTools   MRUFolders

0 Likes
Message 4 of 7

mgFNR8M
Contributor
Contributor

Thanks. I tried adding that information, but it failed. I did try using similar code before.

 

Screenshot_1.jpgScreenshot_2.jpg

 

Here is my current iLogic:

 

' PERMITIR GRAVAR "STRUCTURED" OU "PARTS ONLY"
Sub main
	If ThisApplication.ActiveDocument.DocumentType <> kAssemblyDocumentObject Then
		MessageBox.Show("Apenas pode executar esta macro num ficheiro *.iam", "Erro!")
		Return
	End If
	
	Dim oDoc As AssemblyDocument = ThisApplication.ActiveDocument

	Dim XlsFolder As String = ThisDoc.Path & "\PDF Automaticos\4-BOM\"
	If Not System.IO.Directory.Exists(XlsFolder) Then System.IO.Directory.CreateDirectory(XlsFolder)

	Dim XlsFile As String = XlsFolder & (iProperties.Value("Project", "Part Number")) & "_" & (iProperties.Value("Project", "Revision Number")) & ".xls"

	' If the current non-master LOD document is dirty you should save it before you can use the master-LOD document background
	If oDoc.Dirty Then
	    If MsgBox("You should save the document first!" & vbCrLf & "Would you like to save the document?", vbYesNo) = vbYes Then
	        oDoc.Save
	    Else
	        Exit Sub
	    End If
	End If
	
	ThisApplication.ScreenUpdating = False

	Dim sFileName As String = oDoc.FullFileName

	Dim oMasterLODDoc As AssemblyDocument = ThisApplication.Documents.Open(sFileName, False)

	iLogicVb.RunExternalRule("Standards")

	Dim oBOM As BOM = oMasterLODDoc.ComponentDefinition.BOM
	oBOM.PartsOnlyViewEnabled = True

	Dim oBOMView As BOMView
	oBOMView = oBOM.BOMViews.Item("Parts Only")
	oBOMView.Sort("Description", 1)
	oBOMView.Sort("Part Number", 1)
	oBOMView.Renumber(1,1)
	
	oOptions = ThisApplication.TransientObjects.CreateNameValueMap
	oOptions.Value("Template") = "T:\3 - CONFIG\2 - INVENTOR\2022\9-SETTINGS\TEMPLATE_BOM.xlsx"    'this _ExcelTemplate is the path
    oOptions.Value("StartingCell") = "A2"
	oBOMView.Export(XlsFile, kMicrosoftExcelFormat, oOptions) 
	
	'oBOMView.Export(XlsFile, kMicrosoftExcelFormat) 
	ThisApplication.ScreenUpdating = True
End Sub

Sub SortBOM 
	'Declare Variables
	Dim oDoc As AssemblyDocument = ThisApplication.ActiveDocument
	
	Dim oDraw As DrawingDocument

	Dim sFilePath As String= ThisDoc.Path & "\"

	Dim Name As String = oDoc.DisplayName
	Name = Name.Substring(0,Name.Length-13)

	Dim oAssyDef As AssemblyComponentDefinition
	oAssyDef = oDoc.ComponentDefinition 

	Dim repMgr As RepresentationsManager
	repMgr = oAssyDef.RepresentationsManager

	'Switch to master LOD to get BOM object
	Dim oMasterLOD As LevelOfDetailRepresentation
	oMasterLOD = repMgr.LevelOfDetailRepresentations.Item(1)
	oMasterLOD.Activate

	Dim oBOM As BOM
	oBOM = oAssyDef.BOM

	Dim oBOMView As BOMView
	oBOMView = oBOM.BOMViews.Item("Structured")

	'Open dwg and run "Sort" rule in the drawing
	oDraw = ThisApplication.Documents.Open(sFilePath & Name & ".dwg")
	auto = iLogicVb.Automation
	auto.RunRule(oDraw, "Sort")
	oDraw.Save
	oDraw.Close

	'Sort by Item
	oBOMView.Sort("Item",1)

	'Switch back to "Custom" LOD
	oMasterLOD = repMgr.LevelOfDetailRepresentations.Item(5)
	oMasterLOD.Activate  

	oDoc.Save
End Sub

 

It generates this error: 

0 Likes
Message 5 of 7

marcin_otręba
Advisor
Advisor

as i wrote, it will NOT work from assembly BOM, you must create idw, then place your assembly partlist, and then you can go trough partslist and export it.

 

for example i created function for that:

you must change idw template and xls template also code sets up partlist style so change it to yours or comment that line - it is for choosing columns to export you can do it also as shown below.

Function exportPL(oAssemblyDoc)
On Error Resume Next

Dim oDrawDoc As DrawingDocument
Dim obom As BOM= oAssemblyDoc.ComponentDefinition.BOM
' Make sure that the structured view is enabled.
obom.StructuredViewEnabled = True
'set the structured view to 'all levels'
obom.StructuredViewFirstLevelOnly = True
Dim oStructuredBOMView As BOMView  = obom.BOMViews.Item("Structured")
Dim path As String="C:\"
oStructuredBOMView.Sort ("Part Number", True)
oStructuredBOMView.Renumber
 oDrawDoc = ThisApplication.Documents.Add(kDrawingDocumentObject, path & "ISS_PL.idw", True)  'PUT HERE YOUR TEMPLATE FULLFILENAME
     osheet = oDrawDoc.ActiveSheet
      oPoint = ThisApplication.TransientGeometry.CreatePoint2d(1, 1)
   obview = osheet.DrawingViews.AddBaseView(oAssemblyDoc, oPoint, 1, kDefaultViewOrientation, kHiddenLineDrawingViewStyle)
   Dim opartslist1 As PartsList = osheet.PartsLists.Add(obview, oPoint)
        opartslist1 = osheet.PartsLists(1)
       
       opartslist1.Style = oDrawDoc.StylesManager.PartsListStyles.Item("ISS_PL")  ' PUT HERE YOU PARTSLIST STYLE OR COMMENT THIS LINE
       opartslist1.Sort ("PART NUMBER")
       opartslist1.Renumber
        opartslist1.PartsListColumns.Add (PropertyTypeEnum.kCustomProperty,"Laser")
         oOptions = ThisApplication.TransientObjects.CreateNameValueMap
        oOptions.Value("Template") = Path & "Szablon XLS.xls"  'PUT HERE YOUR TEMPLATE FULLFILENAME
        oOptions.value("StartingCell") = "A12"
        'oOptions.Value("TableName") = ThisDoc.FileName(False) 'without extension
        oOptions.value("IncludeTitle") = False
         'oOptions.Value("ExportedColumns") = "Item;QTY;Part Number;QTY;QTY;Description;Grub.;Szer.;Dł.;Materiał;Laser;Wykrawarka;Gilotyna;Krawędziarka;Piła;Prasa/Wierta.;Zgrzewarka;TokFrez;Magazyn;Masa"
        'oOptions.Value("ExportedColumns") = "Item;QTY;Part Number;Description"

        oOptions.value("AutoFitColumnWidth") = True
           
nnname = Mid(oAssemblyDoc.FullFileName, InStrRev(oAssemblyDoc.FullFileName, "\", -1) + 1, Len(oAssemblyDoc.FullFileName) - InStrRev(oAssemblyDoc.FullFileName, "\", -1) - 4)
bomname = Left(oAssemblyDoc.FullFileName, InStrRev(oAssemblyDoc.FullFileName, "\", -1)) & nnname & ".xls"

If Dir(bomname) <> "" Then
Kill (bomname)
Else
End If
        opartslist1.Export (bomname, PartsListFileFormatEnum.kMicrosoftExcel, oOptions)
oDrawDoc.Close (True)
If oModel <> ThisApplication.ActiveDocument Then
oModel.Close (True)
End If
End Function

 

opartslist1.PartsListColumns.Add (PropertyTypeEnum.kCustomProperty,"Laser")

 

Hi, maybe you want to vote my:

Ideas

or check my apps:

DrawingTools   View&ColoringTools   MRUFolders

0 Likes
Message 6 of 7

TechInventor20
Advocate
Advocate

This is the code we use for exporting a Partslist to a excel file

 

oDoc = ThisDoc.Document
oName = ThisDoc.Document


oFileName = ThisDoc.FileName(False)
'oBomName = savelocatie & "\"& oFileName & "-parts only.xls"
'oBomNameT = savelocatie & "\" & oFileName & "-BOM.csv"
'oBomNameT = savelocatie & "\" & oFileName & "-BOM.csv"
oBomNameT = savelocatie & "\" & oFileName & "-B.xls" 'The save location + filename and extension.
oSheet = oDoc.Sheets.Item(1)
oView = oSheet.DrawingViews.Item(1)
oBom = oView.ReferencedDocumentDescriptor.ReferencedDocument.ComponentDefinition.BOM
oBom.StructuredViewFirstLevelOnly = True
oBom.StructuredViewEnabled = True
oPartsonlyBOMView = oBom.BOMViews.Item("Parts Only")
oStructuredBOMView = oBom.BOMViews.Item("Structured")
i = MessageBox.Show("Export bom?", "Unknown ",MessageBoxButtons.YesNo)
If i = vbYes Then : launchviewer = 1 : Else : launchviewer = 0 : End If

'If launchviewer = 1 Then oPartsonlyBOMView.Export (oBomName, kMicrosoftExcelFormat)
'If launchviewer = 1 Then oStructuredBOMView.Export(oBomNameT, kTextFileTabDelimitedFormat)
If launchviewer = 1 Then oStructuredBOMView.Export(oBomNameT, kMicrosoftExcelFormat)

 

0 Likes
Message 7 of 7

Daniel_Stach94
Contributor
Contributor

Here is how I use BoM export with template.
1) export your template - open BoM and use columns you want to export

Daniel_Stach94_0-1669887119182.png

2) use this code, read the description and edit the code according to the description

'Check if assembly is active
oDoc = ThisDoc.ModelDocument
If oDoc.DocumentType = kPartDocumentObject Then
	MessageBox.Show("Assembly must be active!", "BoM Export")
	Return
End If
'-----------------------------------------------------------------------------------------------------
oDoc = ThisApplication.ActiveDocument
Dim oBOM As BOM
oBOM = oDoc.ComponentDefinition.BOM
'-----------------------------------------------------------------------------------------------------
'Import BoM template (change "D:\Projekty\Tryskací zařízení\0KonecVaultu\Sablona_kusovniku_KS.xml" with your location of the template)
Dim oPath As String
oPath = "D:\Projekty\Tryskací zařízení\0KonecVaultu\Sablona_kusovniku_KS.xml"
oBOM.ImportBOMCustomization(oPath)
'-----------------------------------------------------------------------------------------------------
'Path for export
CSVpath = ThisDoc.Path + "\"
'-----------------------------------------------------------------------------------------------------
'Structured view of BoM (maybe you will need to rewrite 'oStructuredBOMView = oBOM.BOMViews.Item("Structured")'. I have Inventor in czech language and I had to write in czech...
	oBOM.StructuredViewFirstLevelOnly = False
	oBOM.StructuredViewEnabled = True
	Dim oStructuredBOMView As BOMView
	oStructuredBOMView = oBOM.BOMViews.Item("Structured")
'-----------------------------------------------------------------------------------------------------	
'Renumber according to custom iProperty 'He_HeOnum' - (change for your iProperty)
	Dim activeAssembly As AssemblyDocument = ThisApplication.ActiveDocument
	Dim bomView As BOMView = activeAssembly.ComponentDefinition.BOM.BOMViews(2)
	Call bomView.Sort("He_HeOnum", True)
	Call bomView.Renumber(1, 1)
'-----------------------------------------------------------------------------------------------------	
'Export BoM to .xls
	oStructuredBOMView.Export(CSVpath + ThisDoc.FileName(False) + ".xls", kMicrosoftExcelFormat)
'-----------------------------------------------------------------------------------------------------	
'Preview
	i = MessageBox.Show("Open preview?", "BoM Preview", MessageBoxButtons.YesNo)
	If i = vbYes Then : launchviewer = 1 : Else : launchviewer = 0 : End If 
	If launchviewer = 1 Then ThisDoc.Launch(CSVpath + ThisDoc.FileName(False) + ".xls")
'----------------------------------------------------------------------------------------------------