Creating virtual components with vb or ILogic

Creating virtual components with vb or ILogic

Thomas.Long
Advocate Advocate
985 Views
5 Replies
Message 1 of 6

Creating virtual components with vb or ILogic

Thomas.Long
Advocate
Advocate

So, I'm attempting to order a variable number of virtual components, either in Ilogic or VB. However, I'm getting an error when accessing the bill of materials. Let me start off with that I have a "functional" code. The first code below is my old code and runs just fine for very low quantities but whenever I have to create large quantities it can take several minutes or crash the program entirely.

The 2nd code below is based off something used by a coworker, where he has a form where he inputs it by hand and has it create one instance then simply overwrite the quantity in the BOM, which runs much faster. I'm attempting to duplicate this either in iLogic or VB, but both are throwing me errors when attempting to interact with the BOM. (First it gives an error of invalid procedure call or argument on both of the lines where I set the structured view to first level only and enabled, which is odd because it's literally step for step what my coworker did and his works.)

 

Can anyone help me? I'd love to update to the 2nd set of code or else figure out why the first set runs so slowly at high quantities. Placing actual components is so much faster, and I can't for the life of me figure out why.

'Creates a number of virtual parts with the given name and description
Function UpdateVirtParts (sVirtPart, Description, iQty)
	
	If iQty > 0

		'Declaring Document Variables
		Dim asmDoc As AssemblyDocument
		Dim oAsmCompDef As AssemblyComponentDefinition
		Dim asmOcc As ComponentOccurrence
		Dim occs As ComponentOccurrences
		Dim oOcc As Object
		Dim identity As Matrix
		
		asmDoc = ThisApplication.ActiveDocument
		oAsmCompDef = asmDoc.ComponentDefinition
		occs = oAsmCompDef.Occurrences
		identity = ThisApplication.TransientGeometry.CreateMatrix
		
		'create first instance of the virtual part
		Dim virtOcc As ComponentOccurrence
		virtOcc = occs.AddVirtual(sVirtPart, identity)

		'Set Virtual component properties
		iProperties.Value(sVirtPart & ":1", "Project", "Description") = Description
		iProperties.Value(sVirtPart & ":1", "Project", "Part Number") = sVirtPart
		
		iQty = Min(iQty, 10)

		'Add additional components up to specified quantity
		For index As Integer = 2 To iQty
			occs.AddByComponentDefinition(virtOcc.Definition, identity)
		Next
	End If
End Function

  

Sub Main(sPartNumber, sDescription, iQty)

    Dim odoc As AssemblyDocument
    Set odoc = ThisApplication.ActiveDocument
    
    Dim oAssyCompDef As AssemblyComponentDefinition
    Set oAssyCompDef = odoc.ComponentDefinition
    
    Dim oBOM As BOM
    Set oBOM = odoc.ComponentDefinition.BOM
    
    oBOM.StructuredViewFirstLevelOnly = True
    
    oBOM.StructuredViewEnabled = True
    
    Dim oStructuredBOMView As BOMView
    Set oStructuredBOMView = oBOM.BOMViews.Item("Structured")
    
    Dim oMatrix As Matrix
    Set oMatrix = ThisApplication.TransientGeometry.CreateMatrix

    ' add one virtual occurrence
    Dim oNewOcc As ComponentOccurrence
    Set oNewOcc = oAssyCompDef.Occurrences.AddVirtual(sPartNumber, oMatrix)
    
    Dim oCVirtualCompDef As VirtualComponentDefinition
    Set oCVirtualCompDef = oNewOcc.Definition
    
    oCVirtualCompDef.PropertySets.Item("Design Tracking Properties").Item("Description").value = sDescription
    
    'update qty in bill of material to match userform
    oStructuredBOMView.BOMRows.Item(i).TotalQuantity = iQty

End Sub

 

 

Thank you,

Thomas Long

0 Likes
Accepted solutions (2)
986 Views
5 Replies
Replies (5)
Message 2 of 6

Curtis_Waguespack
Consultant
Consultant
Accepted solution

Hi @Thomas.Long 

 

Here is the 2nd rule converted from VBA to work with iLogic.

 

sPartNumber = "123456"
sDescription = "Virtually the best part ever"

iQty = InputBox("QTY of " & sPartNumber , "iLogic", 777)


Dim odoc As AssemblyDocument
odoc = ThisApplication.ActiveDocument

Dim oAssyCompDef As AssemblyComponentDefinition
oAssyCompDef = odoc.ComponentDefinition

Dim oBOM As BOM
oBOM = odoc.ComponentDefinition.BOM

oBOM.StructuredViewFirstLevelOnly = True
oBOM.StructuredViewEnabled = True

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

Dim oMatrix As Matrix
oMatrix = ThisApplication.TransientGeometry.CreateMatrix

Try: Component.InventorComponent(sPartNumber).Delete: Catch: End Try

' add one virtual occurrence
Dim oNewOcc As ComponentOccurrence
oNewOcc = oAssyCompDef.Occurrences.AddVirtual(sPartNumber, oMatrix)
oNewOcc.Name = sPartNumber

Dim oCVirtualCompDef As VirtualComponentDefinition
oCVirtualCompDef = oNewOcc.Definition

oCVirtualCompDef.PropertySets.Item("Design Tracking Properties").Item("Description").Value = sDescription

ilastRow = oStructuredBOMView.BOMRows.Count

'update qty in bill of material to match userform
oStructuredBOMView.BOMRows.Item(ilastRow).TotalQuantity = iQty

 

EESignature

Message 3 of 6

WCrihfield
Mentor
Mentor

Two quick questions...1)If using Inventor 2022 or later, does the assembly have more than just the one original ModelState?  2)Wouldn't it be more logical to enable the structured BOMView before trying to set whether it will include just the first level or all levels?

I only ask about the ModelStates, because it sounds like the errors are happening at the first time it tries to actually make a change to the assembly, which makes it sound like the assembly may be ReadOnly, which sounds like when trying to edit a ModelState 'member' document (instead of the ModelState 'factory' document).

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 4 of 6

Thomas.Long
Advocate
Advocate

This is actually inventor 2019.

 

Yes you are correct that this program has more than one model state as I need to use suppressed components for the basic setup of the file (we work off of a template file). The program goes through, unsuppresses the files that it needs to make the model look correct and calculates how much of a pair of components will be needed to make the model. Then when the assembly looks correct we run a purge function that deletes out everything that is still suppressed, move the model state to the master model state, and deletes the secondary model state.

As a side note, I looked into the BOM to see what was in there and noticed that the structured view was entirely empty in both the master and the "iLogic" modelstates which is starting to make me think that my issues are not code based.

Just as an experiment I went in and turned my model state to master and then ran the program provided above and it ran just fine, but continued to error out whenever i ran it in the ilogic modelstate. I'm guessing then that the BOM can't be manually adjusted while the program is in a non master state. Perhaps I'll move the ordering to a post purge state.

0 Likes
Message 5 of 6

WCrihfield
Mentor
Mentor
Accepted solution

I assume you mean that you have multiple LOD's (LevelOfDetailRepresentations), instead of ModelStates, because ModelStates did not exist until the 2022 release of Inventor.  However, another situation that I did not think about was if the assembly was an iAssemblyMember.  The BOMView object has a Read/Write String type property called iAssemblyMemberName that you can check.  An iAssemblyMember may be ReadOnly too, but I do not recall, because I hardly ever used them, and not for years now.  And it does seem like I have seen others mentioning not having certain abilities related to the BOM while a non-master LOD was active.  You can probably search this forum about that specific situation.

 

Edit:  Here are a couple of links to other forum posts about similar sounding problems, but there are more out there.

https://forums.autodesk.com/t5/inventor-ilogic-and-vb-net-forum/problem-with-bom-api-calls-on-assemb... 

https://forums.autodesk.com/t5/inventor-ilogic-and-vb-net-forum/very-frustrated-change-assemby-uisng... 

https://forums.autodesk.com/t5/inventor-ilogic-and-vb-net-forum/ilogic-and-saving-a-custom-lod-setti... 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 6 of 6

Thomas.Long
Advocate
Advocate

Apologies, you are correct, I meant LOD. However it is not an Iassembly, i've never actually worked with any iparts or iassemblies. Thank you for the information though!

0 Likes