How can I add and run the ILogic code to the iam file remotely with c#?

How can I add and run the ILogic code to the iam file remotely with c#?

hayattangercekler2
Advocate Advocate
420 Views
4 Replies
Message 1 of 5

How can I add and run the ILogic code to the iam file remotely with c#?

hayattangercekler2
Advocate
Advocate

I want to add ilogic code to iam file with inventor api c#. So what I want to do is add my ilogic code to my iam file and run it without manual with this c# code. Because no matter how much I wanted to do it, I couldn't connect to inventor on c# and run " startDoc.Update(); startDoc.Rebuild2(true); " commands. I want to add and run them ilogic with c# and try.

 

How can I add and run the ILogic encoded file with c#?

0 Likes
Accepted solutions (1)
421 Views
4 Replies
Replies (4)
Message 2 of 5

CattabianiI
Collaborator
Collaborator

Start from this post on this forum: https://forums.autodesk.com/t5/inventor-ilogic-and-vb-net-forum/ilogic-api-documentation/m-p/6785772...


Some snippets you may use:

 

' Create a rule in doc document
Dim newRule As iLogicRule = _iLogicAutomation.AddRule(doc, "RuleName", "")

' Add the rule text
newRule.Text = "RULE TEXT HERE"

' Run the rule that was added
 _iLogicAutomation.RunRule(doc, "RuleName")

 

 

Message 3 of 5

WCrihfield
Mentor
Mentor

The following is a link to an article I wrote a couple years ago about creating a new internal rule in another document, and settings its contents.  I have done this from other internal iLogic rules, from external iLogic rules, and from VBA macros.  When done outside of iLogic, you simply need to get the iLogic ApplicationAddIn object, then its Automation object, then use that the same way you can use 'iLogicVb.Automation' within an iLogic rule.

External iLogic Rule (or VBA Macro) To Create Local iLogic Rule 

The only reason the code sets the rule's text in a secondary action, is to avoid the rule automatically running as soon as it is created.  If you set the rule's text in the same line as you are creating the rule, it will run the rule.  If that is the behavior you are looking for, you can slightly shorten the code a bit.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 4 of 5

Zach.Stauffer
Advocate
Advocate
Accepted solution

Here is some code specific to C# to add an iLogic rule and run it in a document.

 

		private void AddiLogicUpdateRule(Document doc)
		{
			//create iLogic rule to update Title Block
			var ruleName = "UPDATE";

			var ilogicAddin = invApp.ApplicationAddIns.ItemById["{3bdd8d79-2179-4b11-8a5a-257b1c0263ac}"];
			var automation = (iLogicAutomation)ilogicAddin.Automation;

			//add ilogic rule, but disable ilogic first. This is because some drawings have an "Update" rule that requires the model to update but fails. Also replace title block
			try
			{
				automation.RulesOnEventsEnabled = false;

				var rule = automation.GetRule(doc, ruleName);

				if (rule != null) automation.DeleteRule(doc, ruleName);
			}
			catch (Exception ex)
			{
				MessageBox.Show($"Couldn't delete existing iLogic rule, error: {ex}");
			}
			finally
			{
				automation.AddRule(doc, ruleName, RuleText);
				automation.RulesOnEventsEnabled = true;
				automation.RunRule(doc, ruleName);
			}
		}

 to get the "RuleText" filled out, the previously mentioned article by WCrihfield shows many ways to do it. I've stored it in the rule directly as part of the class this function is a part of.

 

public string RuleText
		{
			get
			{
				string[] ruleText =
				{
					$"iLogicVb.UpdateWhenDone = True",
					$"Dim fileName As String{System.Environment.NewLine}",
					$"If ThisDrawing.ModelDocument Is Nothing Then",
					$"\t'reset drawing iproperties back to blank",
					$"\tiProperties.Value(\"Summary\", \"Title\") = \"\"",
					$"\tiProperties.Value(\"Project\", \"Part Number\") = \"\"",
					$"\tiProperties.Value(\"Project\", \"Description\") = \"\"",
					$"\tiProperties.Value(\"Project\", \"Designer\") = \"\"",
					$"\tExit Sub",
					$"End If{System.Environment.NewLine}",
					$"fileName = IO.Path.GetFileName(ThisDrawing.ModelDocument.FullFileName){System.Environment.NewLine}",
					$"On Error Resume Next",
					$"iProperties.Value(\"Summary\", \"Title\") = iProperties.Value(fileName, \"Summary\", \"Title\")",
					$"iProperties.Value(\"Project\", \"Part Number\") = iProperties.Value(fileName, \"Project\", \"Part Number\")",
					$"iProperties.Value(\"Project\", \"Description\") = iProperties.Value(fileName, \"Project\", \"Description\")",
					$"iProperties.Value(\"Project\", \"Designer\") = iProperties.Value(fileName, \"Project\", \"Designer\")",
					$"iProperties.Value(\"Project\", \"Creation Date\") = iProperties.Value(fileName, \"Project\", \"Creation Date\")"
				};
				return string.Join($"{System.Environment.NewLine}", ruleText);
			}
		}
0 Likes
Message 5 of 5

Zach.Stauffer
Advocate
Advocate

Double post.

0 Likes