iLogic to turn all components from reference to default

iLogic to turn all components from reference to default

Anonymous
Not applicable
1,115 Views
12 Replies
Message 1 of 13

iLogic to turn all components from reference to default

Anonymous
Not applicable

Hello internet friends.

 

I am trying to find an ilogic code that will search an assembly and if the component is reference BOM structure for it to then change to Default. I have tried a lot of different things from the information I could find but still havent been able to run properly. I think it's pretty simple code, I'm just not getting it. Heres the code I have so far for this operation.

 

SyntaxEditor Code Snippet

iLogicVb.RunRule("UPDATE")
GUSSET_PLATES = Parameter("TC####-X-105-A02.ipt.GUSSET_PLATES")
ENCLOSURE_PLATES = Parameter("TC####-X-105-A02.ipt.ENCLOSURE_PLATES")
InventorVb.DocumentUpdate()

Dim doc as AssemblyDocument = ThisDoc.Document  
    Dim oLOD As LevelOfDetailRepresentation  
    Dim oAsmCompDef As ComponentDefinition  
    oAsmCompDef = doc.ComponentDefinition 
InventorVb.DocumentUpdate()

If GUSSET_PLATES="YES"  AndAlso  ENCLOSURE_PLATES = "YES" Then 
    oLOD = oAsmCompDef.RepresentationsManager.LevelOfDetailRepresentations.Item("MASTER").Activate(True)
    
    Component.IsActive("GUSSET:1")=True 
    Component.IsActive("GUSSET:2")=True
    Component.IsActive("ENCLOSUREPL1:1")=True
    Component.IsActive("ENCLOSUREPL1:2")=True
    Component.IsActive("ENCLOSUREPL1:3")=True
    Component.IsActive("ENCLOSUREPL1:4")=True
    Component.IsActive("ENCLOSUREPL2:1")=True
    Component.IsActive("ENCLOSUREPL2:2")=True
    Component.IsActive("ENCLOSUREPL2:3")=True
    Component.IsActive("ENCLOSUREPL2:4")=True
    Component.IsActive("ENCLOSUREPL3:1")=True 
    Component.IsActive("ENCLOSUREPL3:2")=True 
    Component.IsActive("ENCLOSURE_STIFFENER_PL:1")=True
    Component.IsActive("ENCLOSURE_STIFFENER_PL:2")=True
    Component.IsActive("ENCLOSURE_STIFFENER_PL:3")=True
    Component.IsActive("ENCLOSURE_STIFFENER_PL:4")=True

    ElseIf GUSSET_PLATES="YES"  AndAlso  ENCLOSURE_PLATES = "NO" Then
    oLOD = oAsmCompDef.RepresentationsManager.LevelOfDetailRepresentations.Item("ENCLOSURE PLATES SUPPRESSED").Activate(True)
    Component.IsActive("GUSSET:1")=True
    Component.IsActive("GUSSET:2")=True
    Component.IsActive("ENCLOSUREPL1:1")=False
    Component.IsActive("ENCLOSUREPL1:2")=False
    Component.IsActive("ENCLOSUREPL1:3")=False
    Component.IsActive("ENCLOSUREPL1:4")=False
    Component.IsActive("ENCLOSUREPL2:1")=False
    Component.IsActive("ENCLOSUREPL2:2")=False
    Component.IsActive("ENCLOSUREPL2:3")=False
    Component.IsActive("ENCLOSUREPL2:4")=False
    Component.IsActive("ENCLOSUREPL3:1")=False
    Component.IsActive("ENCLOSUREPL3:2")=False
    Component.IsActive("ENCLOSURE_STIFFENER_PL:1")=False
    Component.IsActive("ENCLOSURE_STIFFENER_PL:2")=False
    Component.IsActive("ENCLOSURE_STIFFENER_PL:3")=False
    Component.IsActive("ENCLOSURE_STIFFENER_PL:4")=False
    InventorVb.DocumentUpdate()
    iLogicVb.RunRule("SUPPRESSED TO REFERENCE")
    iLogicVb.RunRule("UPDATE")
    oLOD = oAsmCompDef.RepresentationsManager.LevelOfDetailRepresentations.Item("MASTER").Activate(True)
    
    Else If GUSSET_PLATES="NO"  AndAlso  ENCLOSURE_PLATES = "YES" Then
    oLOD = oAsmCompDef.RepresentationsManager.LevelOfDetailRepresentations.Item("GUSSETS SUPPRESSED").Activate(True)
    Component.IsActive("GUSSET:1")=False
    Component.IsActive("GUSSET:2")=False
    Component.IsActive("ENCLOSUREPL1:1")=True
    Component.IsActive("ENCLOSUREPL1:2")=True
    Component.IsActive("ENCLOSUREPL1:3")=True
    Component.IsActive("ENCLOSUREPL1:4")=True
    Component.IsActive("ENCLOSUREPL2:1")=True
    Component.IsActive("ENCLOSUREPL2:2")=True
    Component.IsActive("ENCLOSUREPL2:3")=True
    Component.IsActive("ENCLOSUREPL2:4")=True
    Component.IsActive("ENCLOSUREPL3:1")=True
    Component.IsActive("ENCLOSUREPL3:2")=True
    Component.IsActive("ENCLOSURE_STIFFENER_PL:1")=True
    Component.IsActive("ENCLOSURE_STIFFENER_PL:2")=True
    Component.IsActive("ENCLOSURE_STIFFENER_PL:3")=True
    Component.IsActive("ENCLOSURE_STIFFENER_PL:4")=True
    InventorVb.DocumentUpdate()
    iLogicVb.RunRule("SUPPRESSED TO REFERENCE")
    iLogicVb.RunRule("UPDATE")
    oLOD = oAsmCompDef.RepresentationsManager.LevelOfDetailRepresentations.Item("MASTER").Activate(True)
    
    Else If GUSSET_PLATES="NO"  AndAlso  ENCLOSURE_PLATES = "NO" Then
    oLOD = oAsmCompDef.RepresentationsManager.LevelOfDetailRepresentations.Item("GUSSETS AND ENCLOSURE PLATES SUPPRESSED").Activate(True)
    Component.IsActive("GUSSET:1")=False
    Component.IsActive("GUSSET:2")=False
    Component.IsActive("ENCLOSUREPL1:1")=False
    Component.IsActive("ENCLOSUREPL1:2")=False
    Component.IsActive("ENCLOSUREPL1:3")=False
    Component.IsActive("ENCLOSUREPL1:4")=False
    Component.IsActive("ENCLOSUREPL2:1")=False
    Component.IsActive("ENCLOSUREPL2:2")=False
    Component.IsActive("ENCLOSUREPL2:3")=False
    Component.IsActive("ENCLOSUREPL2:4")=False
    Component.IsActive("ENCLOSUREPL3:1")=False
    Component.IsActive("ENCLOSUREPL3:2")=False
    Component.IsActive("ENCLOSURE_STIFFENER_PL:1")=False
    Component.IsActive("ENCLOSURE_STIFFENER_PL:2")=False
    Component.IsActive("ENCLOSURE_STIFFENER_PL:3")=False
    Component.IsActive("ENCLOSURE_STIFFENER_PL:4")=False
    InventorVb.DocumentUpdate()
    iLogicVb.RunRule("SUPPRESSED TO REFERENCE")
    iLogicVb.RunRule("UPDATE")
    oLOD = oAsmCompDef.RepresentationsManager.LevelOfDetailRepresentations.Item("MASTER").Activate(True)
End If

I really appreciate any help :)
0 Likes
1,116 Views
12 Replies
Replies (12)
Message 2 of 13

MechMachineMan
Advisor
Advisor

Here is one to try:

 

Sub Main()

	Dim oDoc As Document = ThisApplication.ActiveDocument
	Dim oCompDef As ComponentDefinition = oDoc.ComponentDefinition
	Dim oOccs As ComponentOccurrences = oCompDef.Occurrences
	Dim oOcc As ComponentOccurrence
	
	MsgBox("Total Occ Count: " & oOccs.AllLeafOccurrences.Count)
	
	Dim ocount As Integer = 0
	
	For Each oOcc In oOccs.AllLeafOccurrences
		If oOcc.BOMStructure = BOMStructureEnum.kReferenceBOMStructure Then 'BOMStructureEnum.kDefaultBOMStructure
			oOcc.BOMStructure = BOMStructureEnum.kDefaultBOMStructure
			ocount = ocount + 1
		End If
	Next
	
	MsgBox("Rule Finished!" & vbLf & vbLf & "Number of Occurrences Reset to Default: " & ocount)

End Sub

--------------------------------------
Did you find this reply helpful ? If so please use the 'Accept as Solution' or 'Like' button below.

Justin K
Inventor 2018.2.3, Build 227 | Excel 2013+ VBA
ERP/CAD Communication | Custom Scripting
Machine Design | Process Optimization


iLogic/Inventor API: Autodesk Online Help | API Shortcut In Google Chrome | iLogic API Documentation
Vb.Net/VBA Programming: MSDN | Stackoverflow | Excel Object Model
Inventor API/VBA/Vb.Net Learning Resources: Forum Thread

Sample Solutions:Debugging in iLogic ( and Batch PDF Export Sample ) | API HasSaveCopyAs Issues |
BOM Export & Column Reorder | Reorient Skewed Part | Add Internal Profile Dogbones |
Run iLogic From VBA | Batch File Renaming| Continuous Pick/Rename Objects

Local Help: %PUBLIC%\Documents\Autodesk\Inventor 2018\Local Help

Ideas: Dockable/Customizable Property Browser | Section Line API/Thread Feature in Assembly/PartsList API Static Cells | Fourth BOM Type
Message 3 of 13

kpk7VYPK
Enthusiast
Enthusiast

I know this is an old post, but any idea why i might get an error on this line:

oOcc.BOMStructure = BOMStructureEnum.kDefaultBOMStructure
0 Likes
Message 4 of 13

WCrihfield
Mentor
Mentor

Is it possible that the AssemblyDocument that the 'oOcc' (ComponentOccurrence) is in the context of, is ReadOnly for some reason?  You can check its AssemblyDocument.IsModifiable property value, and if it returns False (meaning not modifiable), then you know why it is throwing an error at that line.  However, that status can be caused by multiple reasons, including if its file is in a 'library' type directory, or if the assembly is an iAssemblyMember, or if it is a ModelState 'member'.  If it is ModelState related, then there may be a way to work around that, but doing so may be a bit complicated.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 5 of 13

kpk7VYPK
Enthusiast
Enthusiast

Yes the files are realeased so that could be the error. How can i add that  AssemblyDocument.IsModifiable property value to the code? Im not that good at ilogic and piece things together by what i find online. 🙂

0 Likes
Message 6 of 13

WCrihfield
Mentor
Mentor

Hi @kpk7VYPK.  Below is an example iLogic rule that will check the main assembly, then check each component, then if the component is set to reference, it will try to change it to default.  Another thing that will cause an error on that line of code you mentioned, is if that component was suppressed.  You can not do hardly anything with a component that has been suppressed without it causing errors, because it has been unloaded from Inventor's memory, to improve performance.  If this code encounters any of those issues, it will let you know about it with a MsgBox.  Another difference is in Line 3, where I am using the 'ThisDoc.FactoryDocument' property.  If the current document is a drawing, it will return Nothing.  If the current document is either a part or assembly, then it will return something, but if it has multiple ModelStates in it, it ensures that you get a reference to the 'factory' version of it, that you can actually make changes to.  The 'factory' only exists when there are multiple ModelStates, and the factory is the Document version that is controlled or associated with the currently 'active' ModelState in that file, no matter which ModelState that might be.

Sub Main
	Dim oInvApp As Inventor.Application = ThisApplication
	Dim oADoc As AssemblyDocument = TryCast(ThisDoc.FactoryDocument, Inventor.AssemblyDocument)
	If oADoc Is Nothing Then Return
	If Not oADoc.IsModifiable Then
		MsgBox("Main Assembly is not modifiable!")
		Return
	End If
	For Each oOcc As ComponentOccurrence In oADoc.ComponentDefinition.Occurrences
		If oOcc.Suppressed Then
			MsgBox("Skipping Occurrence Named '" & oOcc.Name & "' because it is Suppressed.",,"")
			Continue For
		End If
		If oOcc.BOMStructure = BOMStructureEnum.kReferenceBOMStructure Then
			Try
				oOcc.BOMStructure = BOMStructureEnum.kDefaultBOMStructure
			Catch
				MsgBox("Failed to switch Occurrence Named '" & oOcc.Name & "' from Reference to Default!", vbExclamation, "")
			End Try
		End If
	Next
	oADoc.Update2(True)
End Sub

If this solved your problem, or answered your question, please click ACCEPT SOLUTION .
Or, if this helped you, please click (LIKE or KUDOS) 👍.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 7 of 13

kpk7VYPK
Enthusiast
Enthusiast

Wow thanks so much for this, i think the supressed components was the problem. I assume i can just add a ' before the line 

MsgBox("Skipping Occurrence Named '" & oOcc.Name & "' because it is Suppressed.",,"")

if i dont want the message box and i just want to skip over the supressed components? 

0 Likes
Message 8 of 13

WCrihfield
Mentor
Mentor

Yes, simply commenting out that message will work just fine.  I only included it for the sake of testing / debugging, to sort of illustrate what is going on, and when.  Most MsgBox() or MessageBox.Show() type lines of code can be commented out or deleted, unless they are asking an important question, where the rest of the code depends on feedback from the question.  None of the messages in my example code are asking any questions, just informing the user about when something potentially unexpected happens, and sometimes tell the user which component the issue is related to.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 9 of 13

kpk7VYPK
Enthusiast
Enthusiast

This is great! thanks for the help.

0 Likes
Message 10 of 13

kpk7VYPK
Enthusiast
Enthusiast

Hi again

It seems like the rule isnt changing virtual components in a subassebly to default, even though they are not suppressed. Is it because they are virtual components? Is there a line that can be added to the code to include all parts, and virtual parts in sub assamlies?

0 Likes
Message 11 of 13

WCrihfield
Mentor
Mentor

Hi @kpk7VYPK.  My code example was only designed to effect 'top level' stuff, not to recursively dig down into every level of the assembly's structure.  I did not know that you deeded it to do that, and I did not know that you had 'virtual' components.  When we need to 'recursively' process all levels of components in an assembly, we then need one or more separate routines to handle that functionality.  Because a recursive routine will continue to call itself to run again as many times as needed on its own, every time it encounters a 'lower' or 'higher' level collection of entities to 'step up/down into' and process, depending on how its coded.  Below is another example code, similar to the first one, but broken up into 3 routines.  In this example, the 'middle' routine is purely for enabling the 'recursive' functionality, while the 'last' routine is doing the custom task on each component.

Sub Main
	Dim oInvApp As Inventor.Application = ThisApplication
	Dim oADoc As AssemblyDocument = TryCast(ThisDoc.FactoryDocument, Inventor.AssemblyDocument)
	If oADoc Is Nothing Then Return
	If Not oADoc.IsModifiable Then
		MsgBox("Main Assembly is not modifiable!")
		Return
	End If
	RecurseComponents(oADoc.ComponentDefinition.Occurrences, AddressOf ProcessComponent)
	oADoc.Update2(True)
End Sub

Sub RecurseComponents(oComps As ComponentOccurrences, ComponentProcess As Action(Of ComponentOccurrence))
	If oComps Is Nothing OrElse oComps.Count = 0 Then Return
	For Each oComp As ComponentOccurrence In oComps
		ComponentProcess(oComp)
		If oComp.Suppressed = False AndAlso _
			oComp.DefinitionDocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then
			'<<< use this one to effect each 'definition' separately (changes other files) >>>
			'RecurseComponents(oComp.Definition.Occurrences, ComponentProcess)
			'<<< use this one to only make changes from main assembly's perspective >>>
			RecurseComponents(oComp.SubOccurrences, ComponentProcess)
		End If
	Next
End Sub

Sub ProcessComponent(oComp As ComponentOccurrence)
	If (oComp Is Nothing) OrElse oComp.Suppressed Then Return
	If oComp.BOMStructure = BOMStructureEnum.kReferenceBOMStructure Then
		Try
			oComp.BOMStructure = BOMStructureEnum.kDefaultBOMStructure
		Catch
			MsgBox("Failed to switch Occurrence Named '" & oComp.Name & "' from Reference to Default!", vbExclamation, "")
		End Try
	End If
End Sub

If this solved your problem, or answered your question, please click ACCEPT SOLUTION .
Or, if this helped you, please click (LIKE or KUDOS) 👍.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 12 of 13

kpk7VYPK
Enthusiast
Enthusiast

Thanks for the help, but when i run the rule, the virtual sub-components within the sub-assembly are still set to 'reference'

kpk7VYPK_0-1732606137801.png

any idea why this could be?

0 Likes
Message 13 of 13

WCrihfield
Mentor
Mentor

Hi @kpk7VYPK.  This is a somewhat complex scenario, with multiple levels of details involved, so it can be difficult to explain properly by typing in a forum reply.

  • If any of the components at any level of the assembly were suppressed, then that code could not be able to effect them in any way.  Trying to change properties of a suppressed component usually causes code to encounter exceptions (errors that will stop the rule).
  • BOMStructure status can be set at multiple places/levels.

So, if the code changed the component occurrence/instance BOMStructure from reference to default, but the component's definition was still set to reference, then maybe it will still show up in the BOM as reference, I am not sure.  I almost never use the 'all levels' structured BOM data, so I am not super familiar with controlling/managing lower level BOM data with the BOMStructure system.  I suspect that the 'Definition' and the 'occurrence/instance', and the BOMRow would all need to be set to 'default', before it would show up that way in the BOM.  If so, then you may need a different type of recursive routine.  One that recursively iterates through the structured, all levels, BOMView, and when it finds a BOMRow where the value of its ChildRows property is not Nothing, and not zero quantity, then steps down into that collection, and so on.  That way, you can access the BOMRow setting, the occurrences settings, and the definition settings all in one complex process.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes