Add parts list to drawing based on document type

Add parts list to drawing based on document type

ppolcynBNVFN
Enthusiast Enthusiast
583 Views
7 Replies
Message 1 of 8

Add parts list to drawing based on document type

ppolcynBNVFN
Enthusiast
Enthusiast

Hey y'all. I am trying to create some code I can run in a drawing document that is going to automatically add a different parts list to my sheet depending on if the current view on the sheet is a part or assembly. I have pieced together some code, but have not had any success when running it. (no errors, but nothing happens) I think the problem is something to do with calling on "oDwgDoc.type" and not the type in the view on the sheet. I have pasted the code below:

 

oDwgDoc = ThisApplication.ActiveDocument
Dim oSheet As Sheet
oSheet = oDwgDoc.ActiveSheet
Dim oBaseView As DrawingView
Dim oPartsList As PartsList 
oBaseView = oSheet.DrawingViews.Item(1)

	If Not oBorder Is Nothing Then
    	oPlacementPoint = oBorder.RangeBox.MaxPoint
	Else
    	oPlacementPoint = ThisApplication.TransientGeometry.CreatePoint2d(oSheet.Width - 2.54/2, oSheet.Height - 2.54)
	End If

	Try
    oPartsList = oSheet.PartsLists(1)
	Catch
 		If oDwgDoc.Type = kAssemblyDocumentObject Then
			oPartsList = oSheet.PartsLists.Add(oBaseView, oPlacementPoint)
	 		oPartsList.Style = oDwgDoc.StylesManager.PartsListStyles.Item("Assembly List")
		Else If oDwgDoc.Type = kPartDocumentObject Then
			oPartsList = oSheet.PartsLists.Add(oBaseView, oPlacementPoint)
	 		oPartsList.Style = oDwgDoc.StylesManager.PartsListStyles.Item("Parts List Dimensionless")
 		End If
	End Try
	
End Sub

 

0 Likes
Accepted solutions (1)
584 Views
7 Replies
Replies (7)
Message 2 of 8

A.Acheson
Mentor
Mentor

Hi @ppolcynBNVFN 

The easiest method is using the ilogic API route. This targets the document at view (1) of sheet (1)

 

 

Dim modelDoc As Document = ThisDoc.ModelDocument

 

The longer method is the Inventor API route helpful article here

 

Then once you have the document you can determine its document type see help page here

Syntax

Document.DocumentType() As DocumentTypeEnum

Property 

If doc.DocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then

'DO SOMETHING

End If

 

If this solved a problem, please click (accept) as solution.‌‌‌‌
Or if this helped you, please, click (like)‌‌
Regards
Alan
0 Likes
Message 3 of 8

JelteDeJong
Mentor
Mentor
Accepted solution

try this:

Dim doc = ThisApplication.ActiveDocument
Dim sheet As Sheet = doc.ActiveSheet
Dim baseView As DrawingView = sheet.DrawingViews.Item(1)
Dim refDoc As Document = baseView.ReferencedDocumentDescriptor.ReferencedDocument

Dim border = sheet.Border
Dim placementPoint = border.RangeBox.MaxPoint
If border Is Nothing Then
    placementPoint = ThisApplication.TransientGeometry.CreatePoint2d(sheet.Width - 2.54 / 2, sheet.Height - 2.54)
End If


Dim partsList As PartsList
Try
    partsList = sheet.PartsLists(1)
Catch
    partsList = sheet.PartsLists.Add(baseView, placementPoint)
End Try

If refDoc.DocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then
    partsList.Style = doc.StylesManager.PartsListStyles.Item("Assembly List")
ElseIf refDoc.DocumentType = DocumentTypeEnum.kPartDocumentObject Then
    partsList.Style = doc.StylesManager.PartsListStyles.Item("Parts List Dimensionless")
End If

Jelte de Jong
Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.

EESignature


Blog: hjalte.nl - github.com

Message 4 of 8

ppolcynBNVFN
Enthusiast
Enthusiast

That worked great!

0 Likes
Message 5 of 8

ppolcynBNVFN
Enthusiast
Enthusiast

I had only tested this on drawing sheets with parts, and it works fine, but when trying it on a sheet with an assembly I get the following error(below). If I place a parts list manually, it will set it to the correct format. It is just unable to place it by itself. 

ppolcynBNVFN_0-1697206081549.png

 

0 Likes
Message 6 of 8

A.Acheson
Mentor
Mentor

Hi @ppolcynBNVFN 

When you add a partlist and intend it to be for an assembly you need to set the PartlistlevelEnum which is parts only or structured all level etc. See partlist.add method API help here.

If you look at the more info tab of the error message this might be explained. Tab 1 is no good for diagnostics and should be ignored.

 

In the API help page navigate to PartsListLevelEnum here and select the correct one.

Syntax

PartsLists.AddViewOrModel As Object, PlacementPoint As Point2d, [Level] As PartsListLevelEnum, [NumberingScheme] As Variant, [NumberOfSections] As Long, [WrapLeft] As Boolean ) As PartsList

 

If this solved a problem, please click (accept) as solution.‌‌‌‌
Or if this helped you, please, click (like)‌‌
Regards
Alan
Message 7 of 8

ppolcynBNVFN
Enthusiast
Enthusiast

That worked! I just had to add in an extra "else if" statement for the assemblies. Here is what I added below in case this helps someone in the future:

 

If refDoc.DocumentType = DocumentTypeEnum.kPartDocumentObject Then
		Try
	   		oPartsList = oSheet.PartsLists(1)
		Catch
			oPartsList = oSheet.PartsLists.Add(baseView, placementPoint)
		End Try
	Else If refDoc.DocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then
		Try
			oPartsList = oSheet.PartsLists(1)
		Catch
			Try
			oPartsList = oSheet.PartsLists.Add(baseView, placementPoint, kFirstLevelComponents)
			Catch
			oPartsList = oSheet.PartsLists.Add(baseView, placementPoint, kPartsOnly)
			End Try
		End Try
	End  If

 

0 Likes
Message 8 of 8

robertast
Collaborator
Collaborator

This code is for the part more interesting. The Part List comes with both number part and quantity.

This code is for the part more interesting. The Part List comes with both number part and quantity.

 

 

 

Sub Main
Dim invDoc As DrawingDocument = ThisApplication.ActiveDocument
Dim oSheet As Sheet = invDoc.ActiveSheet
Dim oSheet1 As Sheet = invDoc.Sheets.Item(1)

Dim oDrawingView As DrawingView = oSheet1.DrawingViews(1)

Dim oBorder As Border = oSheet.Border
Dim oPlacementPoint As Point2d
If Not oBorder Is Nothing Then
oPlacementPoint = oBorder.RangeBox.MaxPoint
Else
oPlacementPoint = ThisApplication.TransientGeometry.CreatePoint2d(oSheet.Width, oSheet.Height) 'Kur ideti partlista
End If
Dim partsListBomType As PartsListLevelEnum = 46593
Dim oPartsList_G As PartsList = oSheet.PartsLists.Add(oDrawingView, oPlacementPoint, partsListBomType)

For Each oRow In oPartsList_G.PartsListRows
	oRow.Visible = True 
Next

Dim oView As DrawingView = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kDrawingViewFilter, "pick drawing view.")
Dim oSheet2 As Sheet = oView.Parent
Dim RefDoc As Document = oView.ReferencedDocumentDescriptor.ReferencedDocument
        Dim RefDocName As String = RefDoc.FullFileName
 Dim oPartList As PartsList = oPartsList_G
Dim PLRows As PartsListRows = oPartList.PartsListRows

For Each PLRow As PartsListRow In PLRows
   Dim ListDoc As Document = PLRow.ReferencedRows.Item(1).BOMRow.ComponentDefinitions.Item(1).Document()
   If Not ListDoc.FullFileName = RefDocName Then
      PLRow.Visible = False
   End If
Next
ThisApplication.CommandManager.ControlDefinitions.Item("DrawingUpdateAllSheetsCmd").Execute
End Sub

 

 

If for example, @A.Acheson   or @JelteDeJong  I adjusted it so that it reacts to both Subassembly and Assembly, it would be generally ideal.

 

 

0 Likes