How to get a rule if the document isn't the "active document"?

How to get a rule if the document isn't the "active document"?

Guthery1
Enthusiast Enthusiast
513 Views
6 Replies
Message 1 of 7

How to get a rule if the document isn't the "active document"?

Guthery1
Enthusiast
Enthusiast

So I'm struggling with a bit of code.  I have an assembly that I'm creating as a template.  In a sub-part, I want to be able to change the size of holes based on the clearance excel doc.  I have found/modified the code to allow me to do this:

Dim oPDoc As PartDocument = ThisApplication.ActiveDocument
Dim oPDef As PartComponentDefinition = oPDoc.ComponentDefinition
Dim oHoleFeats As HoleFeatures = oPDef.Features.HoleFeatures
Dim oHole As HoleFeature = oHoleFeats.Item("Hole1")
Dim oHCInfo As HoleClearanceInfo = oHoleFeats.CreateClearanceInfo(a1, a2, a3, FastenerFitType.kNormalFitType)
oHole.ClearanceInfo = oHCInfo

The issue is in line 1.  I can get this code to run like a top IF the part is the active document, but the part isn't going to be the active document when I'm  using the template.  

 

Does anyone know how to write lien 1 so it will work if the document isn't the active document?

0 Likes
514 Views
6 Replies
Replies (6)
Message 2 of 7

bradeneuropeArthur
Mentor
Mentor

You mean that the part is used as a template.

If you create a new part from this template the code does not run before the part has been saved, correct?

Regards,

Arthur Knoors

Autodesk Affiliations & Links:
blue LinkedIn LogoSquare Youtube Logo Isolated on White Background


Autodesk Software:Inventor Professional 2025 | Vault Professional 2024 | Autocad Mechanical 2024
Programming Skills:Vba | Vb.net (Add ins Vault / Inventor, Applications) | I-logic
Programming Examples:
Drawing List!|
Toggle Drawing Sheet!|
Workplane Resize!|
Drawing View Locker!|
Multi Sheet to Mono Sheet!|
Drawing Weld Symbols!|
Drawing View Label Align!|
Open From Balloon!|
Model State Lock!
Posts and Ideas:
My Ideas|
Dimension Component!|
Partlist Export!|
Derive I-properties!|
Vault Prompts Via API!|
Vault Handbook/Manual!|
Drawing Toggle Sheets!|
Vault Defer Update!

! For administrative reasons, please mark a "Solution as solved" when the issue is solved !


 


EESignature

0 Likes
Message 3 of 7

Guthery1
Enthusiast
Enthusiast

No, the part that I need to code to run in is inside an assembly.  The code (as shown in OP) works great if I'm in the part, but errors out when I'm in the assembly that the part is inside.

 

0 Likes
Message 4 of 7

Guthery1
Enthusiast
Enthusiast

No, the part that I need to code to run in is inside an assembly.  The code (as shown in OP) works great if I'm in the part, but errors out when I'm in the assembly that the part is inside.

0 Likes
Message 5 of 7

A.Acheson
Mentor
Mentor

Hi @Guthery1 

I'm not sure how you will be driving the rule or where you will be placing it. If in the assembly at the top level you can target the occurrence and then it's definition. The rule below uses a manual pick. You might need a different setup so if you do please provide details of where you intend to put the rule and how you intend to trigger it. 

Dim occ as ComponentOccurrence = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kAssemblyLeafOccurrenceFilter,"Select a component")
Dim oPDef As PartComponentDefinition = occ.Definition
Dim oHoleFeats As HoleFeatures = oPDef.Features.HoleFeatures
Dim oHole As HoleFeature = oHoleFeats.Item("Hole1")
Dim oHCInfo As HoleClearanceInfo = oHoleFeats.CreateClearanceInfo(a1, a2, a3, FastenerFitType.kNormalFitType)
oHole.ClearanceInfo = oHCInfo

 

If this solved a problem, please click (accept) as solution.‌‌‌‌
Or if this helped you, please, click (like)‌‌
Regards
Alan
0 Likes
Message 6 of 7

Guthery1
Enthusiast
Enthusiast

My solution isn't very eloquent, but it works in my small scale testing.

 

I can get the code to run at the part level and update the holes, but it errors out at the assembly level.  The best solution I can find looks like this:

 

1.  Create a rule at the assembly level to push the parameters to the part, open the part, run a rule called "Hole Master" and then close the part.

Parameter(A5 & "A.ipt.HOLE_QTY") = HOLE_QTY
Parameter(A5 & "A.ipt.HOLE1_A") = HOLE1_A
Parameter(A5 & "A.ipt.HOLE1_B") = HOLE1_B
Parameter(A5 & "A.ipt.HOLE1_TYP") = HOLE1_TYP
Parameter(A5 & "A.ipt.HOLE1_SZ") = HOLE1_SZ

A6 = ThisDoc.Path
A7 = ( A6 & "\" & A5 & "A.ipt")

kicker_part = A7
doc = ThisApplication.Documents.Open(kicker_part,True)
auto = iLogicVb.Automation
auto.RunRule(doc, "Hole Master")
doc.Close(True)

RuleParametersOutput()
InventorVb.DocumentUpdate()
iLogicVb.UpdateWhenDone = True

2. Hole Master unsuppresses the rule "Hole Control":

Auto = iLogicVb.Automation
Auto.GetRule(ThisDoc.Document, "Hole Control").IsActive = True
iLogicVb.RunRule("Hole Control")

It is important that this rule has no other way to be triggers.  If it can run at any other time, it will error out.

3. Hole Control suppress/unsuppress the hole features and the rule that control the individual feature.  I'm not going to show all this text because there is just too much due to the number of holes I'm trying to control. I will say it is mostly just standard IF statements

 

4. This leads to the code that controls the individual holes.  I'm not going to show all of this either because there are just too many bits that are specific to my need, but I will share the important parts of the code:

 

'the opener
Dim
oPDoc As PartDocument = ThisApplication.ActiveDocument Dim oPDef As PartComponentDefinition = oPDoc.ComponentDefinition Dim oHoleFeats As HoleFeatures = oPDef.Features.HoleFeatures Dim oHole As HoleFeature = oHoleFeats.Item("Hole1")

<giant IF statement to sort based on options selected>
Dim oHCInfo As HoleClearanceInfo = oHoleFeats.CreateClearanceInfo(a1, a2, a3, FastenerFitType.kNormalFitType)
'a1 is the page name in the spreadsheet.
'a2 is the row for the size hole you need
'a3 is the column that controls the hole dimensions
oHole
.ClearanceInfo = oHCInfo

InventorVb.DocumentUpdate()
iLogicVb.UpdateWhenDone = True
Auto = iLogicVb.Automation
Auto.GetRule(ThisDoc.Document, "Hole Closer").IsActive = True
iLogicVb.RunRule("Hole Closer")

5. Hole Closer is a short bit of code that just suppresses all the other codes that control the holes so they don't run unless I make changes to the controlling form.

 

I think the last few lines can be removed from the code that actually controls the holes and just have "Hole Closer" set to run with traditional triggers.

 

I know I can shave it down, but I haven't gotten to the clean up stage yet.

 

If you have any suggestions on how I can clean this process up (because it is a process) and way more complex than it likely needs to be), I'm open to suggestions.  Creating tools is a small part of my job so I know my understanding of i-logic is limited.

0 Likes
Message 7 of 7

Michael.Navara
Advisor
Advisor

Use ThisDoc.Document instead of ThisApplication.ActiveDocument should help.

ThisDoc refers to the document, where the rule runs.