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);
}
}