Overriding existing rule to parts in assy

Overriding existing rule to parts in assy

eric.smyth
Participant Participant
172 Views
1 Reply
Message 1 of 2

Overriding existing rule to parts in assy

eric.smyth
Participant
Participant
I have spliced together some ilogic code to override an existing internal rule for all parts in an assembly. The rule 
it is overriding works fine. When I run this rule in an assembly it seems to work for the first part then I get an error.
"Object reference not set to an instance of an object."
"System.NullReferenceException: Object reference not set to an instance of an object.
at ThisRule.Addbomdescription()
at ThisRule.Main()
at Autodesk.iLogic.Exec.AppDomExec.ExecRuleInAssembly(Assembly assem)
at iLogic.RuleEvalContainer.ExecRuleEval(String execRule)"

The parts are derived from a main sketch part that is not in the assembly. could this be hanging it up?
I have also tried running a bit of code to just run the external rule with no luck.
Here is the "code" any ideas I would greatly appreciate it.

Public
Sub Addbomdescription() ' Get the open top-level assembly. Dim asmDoc As AssemblyDocument = ThisApplication.ActiveDocument ' Get a list of all documents referenced by this assembly. ' This property will return everything at all levels. Dim refDocs As DocumentsEnumerator = asmDoc.AllReferencedDocuments Dim oRuleName As String = "BOM DESCRIPTION" Dim oTxtFileName As String = "C:\Work\CAD Stds\Inventor\Crush iLogic\BOM RULES\BOM DESCRIPTION FEET.txt" Dim oRuleText As String = IO.File.ReadAllText(oTxtFileName) Dim oDoc As Document = ThisApplication.ActiveEditDocument Dim oRuleExists As Boolean = False Dim iLogicAuto = iLogicVb.Automation iLogicAuto.RulesEnabled = True iLogicAuto.RulesOnEventsEnabled = True Dim oRule As iLogicRule ' Iterate through the documents. For Each oDoc In refDocs ' Look for part documents. If TypeOf (oDoc) Is PartDocument Then 'ADD BOM DESCRIPTION INTERNAL RULE Try oRule = iLogicAuto.GetRule(oDoc, oRuleName) Catch oRule = iLogicAuto.AddRule(oDoc, oRuleName, "") End Try oRule.Text = oRuleText iLogicVb.DocumentUpdate End If Next End Sub

 

0 Likes
173 Views
1 Reply
Reply (1)
Message 2 of 2

WCrihfield
Mentor
Mentor

Hi @eric.smyth.  I see a couple things that could be improved upon to possibly avoid some potential problems.  First of all, you should either declare your 'oRule' variable, and your 'oRuleExists' variable down within the loop, or re-set their values to False & Nothing at the start of the loop, to make sure a previous value does not carry over into the next loop.  Next, the GetRule method will not throw an error if the rule is not found, which may break the functionality of your Try...Catch block.  You need to check if oRule Is Nothing after using that method to make sure.  Also, you may need to save your changes to those referenced documents after changing the text of their internal rules, or adding an internal rule.  But you may be doing that manually, by saving the main assembly afterwards.  Maybe just an AssemblyDocument.Update2() when done.

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes