Adding Participants to a feature in a subassembly model from the main assembly model

Adding Participants to a feature in a subassembly model from the main assembly model

jamie.nelson.cubex
Contributor Contributor
402 Views
2 Replies
Message 1 of 3

Adding Participants to a feature in a subassembly model from the main assembly model

jamie.nelson.cubex
Contributor
Contributor

Hello,

 

I have a main assembly containing sub-assemblies with rules that edits features in the sub-assembly. Most of the rules work well as they are simple (changes to dimensions, increasing the number of instances in a pattern); however, one rule does not. 

 

A patterned feature in a sub-assembly has an extended cut affecting it; when I add additional occurrences of the pattern, the extruded cut does not happen to these occurances.  I have found that I need to create a rule to add the occurrences of the pattern to the feature: using this rule:

 

Dim oDoc As AssemblyDocument
oDoc = ThisApplication.ActiveDocument

Dim oDef As AssemblyComponentDefinition
oDef = oAsmDoc.ComponentDefinition

Dim oPat As OccurrencePattern
oPat = oDef.OccurrencePatterns.Item("Component Pattern 4") 

Dim oAssemblyFeatures As Features
oAssemblyFeatures = oDef.Features

Dim oAssemblyFeature As PartFeature
'Iterate through all of the assembly features
For Each oAssemblyFeature In oAssemblyFeatures
    If oAssemblyFeature.Name  = "Extrude 1" Then
   	'look at the pattern elements
   	 Dim oPatE As OccurrencePatternElement
	For Each oPatE In oPat.OccurrencePatternElements
		'look at the pattern element contents
		Dim oOcc As ComponentOccurrence
		For Each oOcc In oPatE.Occurrences
		 'add the element contents to the feature
		 oAssemblyFeature.AddParticipant(oOcc)
		Next
	Next
    End If
Next

Which I found from @Curtis_Waguespack post here https://forums.autodesk.com/t5/inventor-ilogic-and-vb-net-forum/ilogic-commands-for-adding-a-partici... 

 

I then try to run from this from the main assembly with a simple:

ilogicVb.RunRule("A003.1 - Fabric", "Run rules 1")

 

I receive the following error 

Error on line 8 in rule: Include Pattern Parts, in document: A003.1 - Fabric.iam

Unspecified error (Exception from HRESULT: 0x80004005 (E_FAIL))

 

I believe this is caused by the fact that the document containing the rule is not active. This line.

oDoc = ThisApplication.ActiveDocument

 

Is there a simple solution to this? 

 

I have a basic understanding of iLogic, so if you can explain each step, It would be much appreciated. 

 

0 Likes
Accepted solutions (2)
403 Views
2 Replies
Replies (2)
Message 2 of 3

WCrihfield
Mentor
Mentor
Accepted solution

Hi @jamie.nelson.cubex.  Try changing from using 'ThisApplication.ActiveDocument' to using 'ThisDoc.Document' instead.  When using the 'ActiveDocument' reference, it will always be referring to the top level assembly, not the sub assembly.  The same is true when working on a drawing document.  Both an assembly document, and a drawing document can have multiple references to other documents, which all get opened (invisibly in the background) when you open them.  And if you are accessing those other referenced documents, especially by attempting to run a rule within one of them, then the reference being used to refer to that other referenced document should never be 'ThisApplication.ActiveDocument', because that will always be pointing to either the top level assembly, or the main drawing that the other document is being referenced through.  The term 'ThisDoc.Document' is a dynamic term that will attempt to point to the most appropriate document, in different situations, and is very useful when coding in iLogic.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 3 of 3

WCrihfield
Mentor
Mentor
Accepted solution

Hi @jamie.nelson.cubex.  I created a version of your code above, added comments above nearly every line of code, to step you through what is happening.  I condensed a few things, and rearranged a few things in a way that seemed logical.  I also further defined the 'feature' as an ExtrudeFeature, because a generic PartFeature object did not appear to have the AddParticipant method shown as an available option, even though it would work that way.  By defining the feature type more specifically, you see the 'Intellisense' (pop-up suggestions/helper) a bit more clearly/specifically.  I also replaced the phrase 'ThisApplication.ActiveDocument' at the top, with 'ThisDoc.Document', as I mentioned in the last response.  I hope this helps some.

'get a reference to the assembly document you want this code to work on
Dim oADoc As AssemblyDocument = ThisDoc.Document
Dim oADef As AssemblyComponentDefinition = oADoc.ComponentDefinition
'get a reference to a specifically named component pattern, by its name
Dim oPat As OccurrencePattern = oADef.OccurrencePatterns.Item("Component Pattern 4")
'get a reference to the collection of pattern elements within this pattern
Dim oElements As OccurrencePatternElements = oPat.OccurrencePatternElements
'get a reference to all of the extrude type features that exist directly in the main/top assembly
Dim oExtFeats As ExtrudeFeatures = oADef.Features.ExtrudeFeatures
'Iterate through all of the extrude features in the top/main assembly
For Each oExtFeat As ExtrudeFeature In oExtFeats
    If oExtFeat.Name = "Extrude 1" Then
	   	'Iterate through each element within the component pattern's elements
		For Each oElement As OccurrencePatternElement In oElements
			'get a reference to the collection of components within each pattern element
			Dim oEOccs As ComponentOccurrencesEnumerator = oElement.Occurrences
			'Iterate through each component within that collection
			For Each oEOcc As ComponentOccurrence In oEOccs
				'add the component as a participant of the extrude feature
				oExtFeat.AddParticipant(oEOcc)
			Next 'oEOcc
		Next 'oElement
    End If 'end of feature name check
Next 'oExtFeat

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)