Problem running rule in Sub Assembly from top level Assembly

Problem running rule in Sub Assembly from top level Assembly

Wowbagger2
Enthusiast Enthusiast
1,105 Views
7 Replies
Message 1 of 8

Problem running rule in Sub Assembly from top level Assembly

Wowbagger2
Enthusiast
Enthusiast

Hi,

 

I have the following rule inside an assembly to change the Component Pattern direction and it works fine, until I try to run it from the top level assembly.

 

 

'Triggers
t = PatternDirection

Dim oDoc As AssemblyDocument
oDoc = ThisApplication.ActiveDocument
 
Dim oAsmCompDef As AssemblyComponentDefinition
oAsmCompDef = oDoc.ComponentDefinition
 
Dim oOccPat1 As OccurrencePattern
oOccPat1 = oAsmCompDef.OccurrencePatterns.Item("Solids Pattern")
Dim oOccPat2 As OccurrencePattern
oOccPat2 = oAsmCompDef.OccurrencePatterns.Item("Bounding box Pattern")
 
 
If PatternDirection = True Then
	oOccPat1.ColumnEntityNaturalDirection = True
	oOccPat2.ColumnEntityNaturalDirection = True
ElseIf PatternDirection = False Then
	oOccPat1.ColumnEntityNaturalDirection = False
	oOccPat2.ColumnEntityNaturalDirection = False
End If

 

I'm suspecting this has something to do with 

oDoc = ThisApplication.ActiveDocument

 As I guess it is not the "ActiveDocument" if you call for this rule from another document?

 

The error I get when I try to run it is the following: 

Error in rule: PatternDirection, in document: OUTLET1.iam

Odefinierat fel (Exception from HRESULT: 0x80004005 (E_FAIL))

Happy for any advice on how to rewrite this rule so it can be triggered from outside the actual assembly.

 

Thanks!

/Fredrik

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

Owner2229
Advisor
Advisor

Hey, my guess is "PatternDirection" is a parameter in the sub-assembly, right? I'm afraid you can't use it as a trigger, but for the rest of the code this should do:

 

'Trigger
t = PatternDirection

Dim oDoc As AssemblyDocument = ThisApplication.ActiveDocument
Dim oCD As AssemblyComponentDefinition = oDoc.ComponentDefinition
'Go through all occurrences in the top assembly For Each oOcc As ComponentOccurrence In oCD.Occurrences
'Check only assembly occurrences
If oOcc.DefinitionDocumentType <> DocumentTypeEnum.kAssemblyDocumentObject Then Continue For
Dim aDoc As AssemblyDocument = oOcc.Definition.Document
Dim aCD As AssemblyComponentDefinition = aDoc.ComponentDefinition
'Check for present patterns
If aCD.OccurrencePatterns.Count = 0 Then Continue For
'Get the parameter from the sub-assembly
'If the parameter is in the top-assembly change the "aCD" to "oCD" on the next row

Dim aPara As Parameter = aCD.Parameters("PatternDirection")
If aPara Is Nothing Then Continue For
Dim aParaVal As Boolean = aPara.Value
'Get the patterns from the sub-assembly Dim oOP1 As OccurrencePattern = aCD.OccurrencePatterns.Item("Solids Pattern") Dim oOP2 As OccurrencePattern = aCD.OccurrencePatterns.Item("Bounding box Pattern") If oOP1 Is Nothing Then Continue For
If oOP2 Is Nothing Then Continue For
'Set the values oOP1.ColumnEntityNaturalDirection = aParaVal oOP2.ColumnEntityNaturalDirection = aParaVal
Next

 

Consider using "Accept as Solution" / "Kudos" if you find this helpful.
- - - - - - - - - - - - - - -
Regards,
Mike

"Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live." - John F. Woods
Message 3 of 8

chandra.shekar.g
Autodesk Support
Autodesk Support

Hi @Wowbagger2,

 

Try the following change in iLogic rule.

 

Dim oAsmCompDef As AssemblyComponentDefinition
oAsmCompDef = ThisDoc.ComponentDefinition

Thanks and regards,


CHANDRA SHEKAR G
Developer Advocate
Autodesk Developer Network



Message 4 of 8

Wowbagger2
Enthusiast
Enthusiast

Hi!

 

My situation is that I have subassemblies with component patterns in them and I want to have a function to flip the pattern direction (with ilogic). My thinking was to just have a True/false parameter in the Top level which links to the same parameter in the subassembly and as it change so should the direction of the pattern in that subassembly (by triggering the rule PatternDirection)

 

Your suggested code is to be run from the top level assembly and loop through right? I'm afraid that will cause longer time to compute if the rule needs to loop several assemblies rather than targeting the effected subassembly directly. The Top-level assembly will be quite complex when its all finished. I'll give it a try though!

 

 

I felt like I was so close of achieving this with the simple rule I posted as it worked inside the document but perhaps not 😞

0 Likes
Message 5 of 8

Wowbagger2
Enthusiast
Enthusiast

@chandra.shekar.g 

Do you mean like this?

 

 

Dim oDoc As AssemblyDocument
oDoc = ThisApplication.ActiveDocument
 
Dim oAsmCompDef As AssemblyComponentDefinition
oAsmCompDef = ThisDoc.ComponentDefinition
 
Dim oOccPat1 As OccurrencePattern
oOccPat1 = oAsmCompDef.OccurrencePatterns.Item("Solids Pattern")
Dim oOccPat2 As OccurrencePattern
oOccPat2 = oAsmCompDef.OccurrencePatterns.Item("Bounding box Pattern")
 
 
If PatternDirection = True Then
	oOccPat1.ColumnEntityNaturalDirection = True
	oOccPat2.ColumnEntityNaturalDirection = True
ElseIf PatternDirection = False Then
	oOccPat1.ColumnEntityNaturalDirection = False
	oOccPat2.ColumnEntityNaturalDirection = False
End If

This gives me a rule compile error 😞

 

Error on Line 8 : ComponentDefinition is not a member of Autodesk.iLogic.Interfaces.ICadDoc.

 

 

0 Likes
Message 6 of 8

Owner2229
Advisor
Advisor
Accepted solution

As for @chandra.shekar.g I'm pretty sure he meant this:

ThisDoc.Document.ComponentDefinition

 

If the parameter is linked from the top-assy, you can read it directly from it forehand and loot through the sub-assies after.

Performance-wise you'll have to test it a bit, but I don't think it'll be that much slower, since at the beginning of every loop I wrote in some checkers which will lead to skipping the unaffected occurrences. It might be even faster, if you remove the parameter linking.

 

'Trigger
t = PatternDirection
Dim oDoc As AssemblyDocument = ThisApplication.ActiveDocument Dim oCD As AssemblyComponentDefinition = oDoc.ComponentDefinition
'Get the parameter
Dim aPara As Parameter = oCD.Parameters("PatternDirection")
If aPara Is Nothing Then Exit Sub
Dim aParaVal As Boolean = aPara.Value
'Go through all occurrences in the top assembly For Each oOcc As ComponentOccurrence In oCD.Occurrences
'Check only assembly occurrences
If oOcc.DefinitionDocumentType <> DocumentTypeEnum.kAssemblyDocumentObject Then Continue For
Dim aDoc As AssemblyDocument = oOcc.Definition.Document
Dim aCD As AssemblyComponentDefinition = aDoc.ComponentDefinition
'Check for present patterns
If aCD.OccurrencePatterns.Count = 0 Then Continue For
'Get the patterns from the sub-assembly Dim oOP1 As OccurrencePattern = aCD.OccurrencePatterns.Item("Solids Pattern") Dim oOP2 As OccurrencePattern = aCD.OccurrencePatterns.Item("Bounding box Pattern") If oOP1 Is Nothing Then Continue For
If oOP2 Is Nothing Then Continue For
'Set the values oOP1.ColumnEntityNaturalDirection = aParaVal oOP2.ColumnEntityNaturalDirection = aParaVal
Next

 

Consider using "Accept as Solution" / "Kudos" if you find this helpful.
- - - - - - - - - - - - - - -
Regards,
Mike

"Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live." - John F. Woods
0 Likes
Message 7 of 8

Anonymous
Not applicable

Keep the rules inside the sub-assemblies that you already have and push the value of your top-level parameter to each assembly that has the same parameter. You'll be surprised how quick it does it, I also have large assemblies and use this code a lot to do a similar thing.

Sub Main()

'Trigger
t = PatternDirection

RuleParametersOutput()
InventorVb.DocumentUpdate()
Dim oAsmDoc As AssemblyDocument oAsmDoc = ThisApplication.ActiveDocument Call Iterate(oAsmDoc.ComponentDefinition.Occurrences, 1) End Sub Private Sub Iterate(Occurrences As ComponentOccurrences, Level As Integer) 'Iterate through Assembly Dim oOcc As ComponentOccurrence For Each oOcc In Occurrences Try Parameter(oOcc.Name, "PatternDirection") = Parameter("PatternDirection") Catch 'MsgBox("Message!") End Try 'Run through the sub assemblies If oOcc.DefinitionDocumentType = kAssemblyDocumentObject Then Call Iterate(oOcc.SubOccurrences, Level + 1) End If Next End Sub  

 

Message 8 of 8

Wowbagger2
Enthusiast
Enthusiast

 

ThisDoc.Document.ComponentDefinition

Thank you! That worked! And sucha small change to my code, I'm very happy! 🙂

 

0 Likes