iLogic rule to include/exclude derived parameter in all parts from an assembly

iLogic rule to include/exclude derived parameter in all parts from an assembly

israel.paquette
Explorer Explorer
2,314 Views
9 Replies
Message 1 of 10

iLogic rule to include/exclude derived parameter in all parts from an assembly

israel.paquette
Explorer
Explorer

Hi, I'm currently working on an assembly that contain over 300 parts.
Almost each of those parts contain a derived component. 
I need to include a parameter in each derived components in all parts.
So I'm trying to write an iLogic rule that I will run from the assembly and will include that specific parameter in each derived components of all parts.


I'm using this rue that I found on DevBlog. It's working perfectly if I run it from the specific part that contain the derived component but I can't find the proper way to run it from my main assembly.

'If ThisApplication.ActiveDocument.DocumentType <> kPartDocumentObject Then
'    MsgBox("Make a Part Document the active document")
'End If

Dim oDerPart As PartDocument
oDerPart = ThisApplication.ActiveDocument
Dim oDerPartComp As DerivedPartComponent

If oDerPart.ComponentDefinition.ReferenceComponents.DerivedPartComponents.Count < 1 Then
    MsgBox("No Derived Part Components in this part")
End If

oDerPartComp = oDerPart.ComponentDefinition.ReferenceComponents.DerivedPartComponents(1)

Dim oDerivedPartDef As DerivedPartUniformScaleDef
oDerivedPartDef = oDerPartComp.Definition

Dim oDerEntity As DerivedPartEntity

For Each oDerEntity In oDerivedPartDef.Parameters
    If (oDerEntity.ReferencedEntity.Name = "BLADE_ATTACK_ANGLE") Then
        oDerEntity.IncludeEntity = True
        Exit For
    End If
Next

'Set Definition back, so DerivedPart Document is updated
oDerPartComp.Definition = oDerivedPartDef

 

This is my try to run it from the assembly but it's not doing anything...

Dim oAsmCompDef As AssemblyComponentDefinition
oAsmCompDef = ThisApplication.ActiveDocument.ComponentDefinition
Dim oOccurrence As ComponentOccurrence

Dim oDoc As Document = ThisDoc.Document
Dim oCompDef As Inventor.ComponentDefinition = oDoc.ComponentDefinition
Dim oCompOcc As Inventor.ComponentOccurrence
On Error Resume Next

For Each oOccurrence In oCompDef.Occurrences
	Parameter.Quiet = True

' **** CHANGE TO DO ****
'	INCLUDE A DERIVED COMPONENT PARAMETER

	Dim oDerPartComp As DerivedPartComponent
	oCompOcc = oOccurrence.Definition
	oDerPartComp = oCompOcc.ComponentDefinition.ReferenceComponents.DerivedPartComponents(1)

	Dim oDerivedPartDef As DerivedPartUniformScaleDef
	oDerivedPartDef = oDerPartComp.Definition
	Dim oDerEntity As DerivedPartEntity

	For Each oDerEntity In oDerivedPartDef.Parameters
		If (oDerEntity.ReferencedEntity.Name = "BLADE_ATTACK_ANGLE") Then
		oDerEntity.IncludeEntity = True
	    Exit For
	    End If
	Next
	'Set Definition back, so DerivedPart Document is updated
	oDerPartComp.Definition = oDerivedPartDef

' **** END ****

On Error Resume Next
Next
InventorVb.DocumentUpdate()


I hope that someone will be able to correct my code! Smiley Happy
Best regards

0 Likes
Accepted solutions (2)
2,315 Views
9 Replies
Replies (9)
Message 2 of 10

lmc.engineering
Advocate
Advocate

Hi @israel.paquette 

 

This should work for you

 

' Check whether open document is an assembly
oDoc = ThisDoc.ModelDocument
If oDoc.DocumentType = kPartDocumentObject Then
MessageBox.Show("This rule can only be run in an assembly file - exiting rule", "iLogic")
Return
End If

Dim doc As Document = ThisDoc.Document
For Each doc In oDoc.AllReferencedDocuments
	If doc.DocumentType = 12290 Then 'part file
		Dim oDPComp As DerivedPartComponent
		Dim oDPComps As DerivedPartComponents
		oDPComps = doc.ComponentDefinition.ReferenceComponents.DerivedPartComponents
		
			For x = 1 To oDPComps.Count
				oDPComp = oDPComps.Item(x)
				Dim oDPDef As DerivedPartDefinition = oDPComp.Definition
				Dim oDPEnts As DerivedPartEntities = oDPDef.Parameters
				Dim oDPEnt As DerivedPartEntity
			
					For Each oDPEnt In oDPEnts
						If oDPEnt.ReferencedEntity.Name = "Test" Then ' Add your parameter name here!
							oDPEnt.IncludeEntity = True
						End If
					Next
			oDPComp.Definition = oDPDef
			Next
	End If
Next

Regards

Message 3 of 10

israel.paquette
Explorer
Explorer

Hi,

Thanks for your help.

Unfortunately, I can't that rule. 
I created a new rule in my assembly and pasted your code in it. I, of course, edited the parameter name.

When I try to run it, I obtain that error message:

 

System.Runtime.InteropServices.COMException (0x80004005): Erreur non spécifiée (Exception from HRESULT: 0x80004005 (E_FAIL))
at System.RuntimeType.ForwardCallToInvokeMember(String memberName, BindingFlags flags, Object target, Int32[] aWrapperTypes, MessageData& msgData)
at Inventor.DerivedPartComponent.set_Definition(DerivedPartDefinition )
at LmiRuleScript.Main()
at Autodesk.iLogic.Exec.AppDomExec.ExecRuleInAssembly(Assembly assem)
at iLogic.RuleEvalContainer.ExecRuleEval(String execRule)

 

Maybe I'm doing something wrong!


0 Likes
Message 4 of 10

Sergio.D.Suárez
Mentor
Mentor
Accepted solution

Hello, try this rule, executed in an assembly

Dim openDoc As AssemblyDocument
openDoc = ThisDoc.Document
Dim doc As Document
For Each doc In openDoc.AllReferencedDocuments
	
	On Error Resume Next
	Dim oDerPartComp As DerivedPartComponent
	oDerPartComp = doc.ComponentDefinition.ReferenceComponents.DerivedPartComponents(1)
	Dim oDerivedPartDef As DerivedPartUniformScaleDef
	oDerivedPartDef = oDerPartComp.Definition
	Dim oDerEntity As DerivedPartEntity

	For Each oDerEntity In oDerivedPartDef.Parameters
	    If (oDerEntity.ReferencedEntity.Name = "BLADE_ATTACK_ANGLE") Then
	        oDerEntity.IncludeEntity = True
	        Exit For
	    End If
	Next
	oDerPartComp.Definition = oDerivedPartDef
	
Next

Parameter.UpdateAfterChange = True
iLogicVb.UpdateWhenDone = True

 I hope it will work for you and help you solve your problem


Please accept as solution and give likes if applicable.

I am attaching my Upwork profile for specific queries.

Sergio Daniel Suarez
Mechanical Designer

| Upwork Profile | LinkedIn

Message 5 of 10

lmc.engineering
Advocate
Advocate
Accepted solution

The issues lies with the derived part definition so I have modified the way it is defined. Maybe you can try this again:

 

oDoc = ThisDoc.ModelDocument
If oDoc.DocumentType = kPartDocumentObject Then
MessageBox.Show("This rule can only be run in an assembly file - exiting rule", "iLogic")
Return
End If

Dim doc As Document = ThisDoc.Document
For Each doc In oDoc.AllReferencedDocuments
	If doc.DocumentType = 12290 Then 'part file
		Dim oDPComp As DerivedPartComponent
		Dim oDPComps As DerivedPartComponents
		oDPComps = doc.ComponentDefinition.ReferenceComponents.DerivedPartComponents
		
			For x = 1 To oDPComps.Count
				oDPComp = oDPComps.Item(x)
				Dim oDPDef As DerivedPartUniformScaleDef = oDPComp.Definition ' Changed this line
				Dim oDPEnts As DerivedPartEntities = oDPDef.Parameters
				Dim oDPEnt As DerivedPartEntity
			
					For Each oDPEnt In oDPEnts
						If oDPEnt.ReferencedEntity.Name = "test1" Then ' Add your parameter name here!
							oDPEnt.IncludeEntity = True
							Exit For
						End If
					Next
			oDPComp.Definition = oDPDef
			Next
	End If
Next
Message 6 of 10

israel.paquette
Explorer
Explorer

Thank you very much to you guys!

Both solutions worked perfectly!

0 Likes
Message 7 of 10

forbillian
Advocate
Advocate

Hi Sergio,

 

This was very helpful.  Thankyou.

 

One question,  I have been trying to run the same rule but only searching for 

derived parts that are contained in parts that aren't reference.  I have tried adjusting your code with this (see below), but I am still getting every derived part back - & I want to ignore derived parts in reference parts.  Any advice?

 

 

If doc.Component.Defintion.BOMStructure = BOMStructureEnum.kDefaultBOMStructure Then

 

 

Thanks Sergio

0 Likes
Message 8 of 10

forbillian
Advocate
Advocate

Hi @lmc.engineering ,

 

Is there a way - when looping through referenced docs to locate derived parts - to ignore any referenced docs that have a Reference BOM State.  

 

I have tried something like this but I still get derived parts called which are located in referenced parts - I only want the rule to apply to derived parts contained within parts that appear in the BOM - if that makes sense?:

 

If oDoc.Component.Defintion.BOMStructure = BOMStructureEnum.kDefaultBOMStructure Then

Thanks @lmc.engineering  

0 Likes
Message 9 of 10

lmc.engineering
Advocate
Advocate

Hi @forbillian 

 

I think the easiest way to do this is change the way we go through the assembly, from AllReferencedDocuments to recursively looking at the ComponentOccurrences. The reason being is that the document reference only picks up on the BOM structure assigned to the file itself and not any assembly level overrides. I've not tested the below hugely but hopefully it works for you

 

Sub Main()
	oDoc = ThisDoc.ModelDocument
	If oDoc.DocumentType = kPartDocumentObject Then
	MessageBox.Show("This rule can only be run in an assembly file - exiting rule", "iLogic")
	Return
	End If
	
	Dim doc As AssemblyDocument = ThisDoc.Document
	Dim paramName As String = "testparam" ' Your parameter name
	TraverseAssembly(doc.ComponentDefinition.Occurrences,paramName)
	
End Sub

Sub TraverseAssembly(Occurrences As ComponentOccurrences,paramName As String)
	
	Dim oOcc As ComponentOccurrence
	
	For Each oOcc In Occurrences
		If oOcc.BOMStructure = BOMStructureEnum.kReferenceBOMStructure Then Continue For ' Skips all referenced components
		
			If TypeOf oOcc.Definition.Document Is AssemblyDocument Then
				TraverseAssembly(oOcc.SubOccurrences,paramName)
			Else
				Dim oDPComp As DerivedPartComponent
				Dim oDPComps As DerivedPartComponents
				oDPComps = oOcc.Definition.ReferenceComponents.DerivedPartComponents
				For x = 1 To oDPComps.Count
					oDPComp = oDPComps.Item(x)
					Dim oDPDef As DerivedPartUniformScaleDef = oDPComp.Definition
					Dim oDPEnts As DerivedPartEntities = oDPDef.Parameters
					Dim oDPEnt As DerivedPartEntity
				
						For Each oDPEnt In oDPEnts
							If oDPEnt.ReferencedEntity.Name = paramName Then
								oDPEnt.IncludeEntity = True
								Exit For
							End If
						Next
				oDPComp.Definition = oDPDef
				Next
			End If	
	Next	
End Sub
Message 10 of 10

forbillian
Advocate
Advocate

Thanks you so much @lmc.engineering  for this code.

 

I have used your previous code to run another rule (see my rudimentary attempt below) which returns all parts, assemblies, derived parts & derived assemblies that are contained within a top GA Assembly file & BOM. 

 

Just to explain  we use derived parts & derived assemblies for our machining method, but the disadvantage of this approach is that the derived parts/assemblies aren't visible in the TOP LEVEL BOM.

 

1.  I am having difficulties with my DerivedAssemblies rule (the DerivedParts rule seems to work) but having problems getting all derived assemblies contained in parts at lower levels

 

2.  I also need to be able to access parts & assemblies contained within any DerivedAssembly 

 

Anyway I am hoping when you see my code below there you may find some obvious flaws or methods to streamline what I am trying to achieve.

 

'ITERATE THROUGH ALL ITEMS & RETURN ALL BOM PARTS, ASSEMBLIES INLCUDING DERIVED PARTS & DERIVED ASSEMBLIES

Sub Main ReturnPartsAssembliesDerived()
	
	    Dim oAsmDoc As AssemblyDocument 
        oAsmDoc = ThisApplication.ActiveDocument 
		
	
		'LOOP THROUGH STD PARTS & ASSEMBLIES
                    Call TraverseAssembly(oAsmDoc.ComponentDefinition.Occurrences, 1)
		'LOOP THROUGH DERIVED PARTS
		    Call TraverseAssembly1(oAsmDoc.ComponentDefinition.Occurrences, 1)
		''LOOP THROUGH DERIVED ASSEMBLIES
		    Call TraverseAssembly2(oAsmDoc.ComponentDefinition.Occurrences, 1)
   
        
End Sub




'NORMAL PARTS & ASSEMBLIES RULE
Private Sub TraverseAssembly(Occurrences As ComponentOccurrences, Level As Integer) Dim oOcc As ComponentOccurrence Dim i As Integer i = 1 For Each oOcc In Occurrences Dim oBrowserNode As String oBrowserNode = oOcc.Name If oOcc.BOMStructure = 51970 Then '............................................................................. MsgBox(oOcc.Name) 'RETURNS ALL PARTS & ASSEMBLIES AT THE FIRST Level '............................................................................. If oOcc.DefinitionDocumentType = kAssemblyDocumentObject Then 'RETURNS ALL PARTS & ASSEMBLIES AT NEXT LEVEL Call TraverseAssembly(oOcc.SubOccurrences, Level + 1) Else End If End If Next oOcc End Sub 'DERIVED PARTS RULE Sub TraverseAssembly1(Occurrences As ComponentOccurrences,Level As Integer) Dim oOcc As ComponentOccurrence Dim i As Integer i = 1 For Each oOcc In Occurrences Dim oBrowserNode As String oBrowserNode = oOcc.Name If oOcc.BOMStructure = 51970 Then If oOcc.DefinitionDocumentType = kAssemblyDocumentObject Then Call TraverseAssembly(oOcc.SubOccurrences, Level + 1) Else Dim oDPComp As DerivedPartComponent Dim oDPComps As DerivedPartComponents oDPComps = oOcc.Definition.ReferenceComponents.DerivedPartComponents For x = 1 To oDPComps.Count oDPComp = oDPComps.Item(x) Dim oDPDef As DerivedPartUniformScaleDef = oDPComp.Definition If (TypeOf oDPDef Is DerivedPartUniformScaleDef) Then Dim oDerPartUScaleDef As DerivedPartUniformScaleDef oDerPartUScaleDef = oDPDef 'THIS CONDITION IGNORES ANY MIRRORED VERSIONS OF DERIVED PARTS If oDerPartUScaleDef.MirrorPlane = 27396 Then '............................................................................. MsgBox(oDPComp.Name) 'RETURNS ALL DERIVED PARTS '.............................................................................. End If oDPComp.Definition = oDPDef End If Next End If End If Next oOcc End Sub 'DERIVED ASSEMBLIES RULE Sub TraverseAssembly2(Occurrences As ComponentOccurrences,Level As Integer) Dim oOcc As ComponentOccurrence Dim i As Integer i = 1 For Each oOcc In Occurrences Dim oBrowserNode As String oBrowserNode = oOcc.Name If oOcc.BOMStructure = 51970 Then If TypeOf oOcc.Definition.Document Is AssemblyDocument Then TraverseAssembly2(oOcc.SubOccurrences, Level + 1) Else On Error Resume Next Dim oDAComp As DerivedAssemblyComponent Dim oDAComps As DerivedAssemblyComponents oDAComps = oOcc.Definition.ReferenceComponents.DerivedAssemblyComponents For x = 1 To oDAComps.Count oDAComp = oDAComps.Item(x) '............................................................................. MsgBox(oDAComp.Name) 'RETURNS ALL DERIVED ASSEMBLIES '.............................................................................. oDAComp.Definition = oDADef Next End If End If Next oOcc End Sub

 

0 Likes