private void Handle_Form_Load(object sender, EventArgs e) { _document = AdnInventorUtilities.InvApplication.ActiveDocument; this.KeyPreview = true; this.KeyDown += new KeyEventHandler(LinkParametersForm_KeyDown); this.Text = "Link Parameters [Rule: " + _ruleName + "]"; _ruleOptionsForm = new RuleOptionsForm(); //Retrieve rule properties if rule exists IiLogicAutomation ruleApi = iLogicUtilities.GetiLogicAutomation(); iLogicRule rule = ruleApi.GetRule(_document, _ruleName); if (rule != null) { _ruleOptionsForm.FireDep = rule.FireDependentImmediately; _ruleOptionsForm.DontRunAuto = !rule.AutomaticOnParamChange; //_ruleOptionsForm.UpdateWhenDone = } _sourceParameterDict = new Dictionary <Parameter, ListViewItem>(); _targetParameterDict = new Dictionary <Parameter, ListViewItem>(); _currentParamFilter = ParamFilterEnum.kAll; Command.SourceComponent = null; Command.TargetComponent = null; _mappings = ParameterMappingInfo.GetMappings(_document, _ruleName); }
public static List <ParameterMappingInfo> GetMappingsFromRule(iLogicRule rule) { List <ParameterMappingInfo> result = new List <ParameterMappingInfo>(); string[] lines = rule.Text.Split('\n'); bool bIsLinkParametersSection = false; foreach (string line in lines) { //Not in a section, look for section start if (!bIsLinkParametersSection) { if (line.Contains("<LinkParameters:Section:Start>")) { bIsLinkParametersSection = true; } continue; } //we are in a section, look for section end if (line.Contains("<LinkParameters:Section:End>")) { bIsLinkParametersSection = false; continue; } //Now process section... int pos = line.IndexOf('='); if (pos < 0) { continue; } string leftSide = line.Substring(0, pos).Trim(); string rightSide = line.Substring(pos + 1).Trim(); string targetComponent, targetParameter, sourceComponent, sourceParameter; ParseParameterExpression(leftSide, out targetComponent, out targetParameter); ParseParameterExpression(rightSide, out sourceComponent, out sourceParameter); ParameterMappingInfo mapping = new ParameterMappingInfo() { SourceComponentName = sourceComponent, SourceParameterName = sourceParameter, TargetComponentName = targetComponent, TargetParameterName = targetParameter, }; result.Add(mapping); } return(result); }
public static List <ParameterMappingInfo> GetMappings(Document document, string ruleName) { IiLogicAutomation ruleApi = iLogicUtilities.GetiLogicAutomation(); System.Collections.IEnumerable rules = iLogicUtilities.GetiLogicAutomation().get_Rules(document); iLogicRule rule = ruleApi.GetRule(document, ruleName); if (rule != null) { return(ParameterMappingInfo.GetMappingsFromRule(rule)); } return(new List <ParameterMappingInfo>()); }
public static void UpdateMappings( Document document, ComponentOccurrence targetOcc, List <ParameterMappingInfo> erasedMappings, List <ParameterMappingInfo> newMappings, string ruleName, bool fireDepRules, bool dontRunAuto, bool updateWhenDone) { try { IiLogicAutomation ruleApi = iLogicUtilities.GetiLogicAutomation(); iLogicRule rule = ruleApi.GetRule(document, ruleName); if (rule == null) { rule = ruleApi.AddRule(document, ruleName, string.Empty); } //First we will remove erased mappings foreach (ParameterMappingInfo mapping in erasedMappings) { string mappingStr = CreateMappingString(mapping); if (rule.Text.Contains(mappingStr)) { rule.Text = rule.Text.Replace(mappingStr, string.Empty); } } //Clean up rule, we will remove all empty sections here if (erasedMappings.Count > 0) { RemoveEmptySections(rule); } //Then add new mappings List <string> newMappingsStr = new List <string>(); foreach (ParameterMappingInfo mapping in newMappings) { string mappingStr = CreateMappingString(mapping); if (!rule.Text.Contains(mappingStr)) { newMappingsStr.Add(mappingStr); } } string updateStr = "iLogicVb.UpdateWhenDone = " + (updateWhenDone ? "True" : "False"); if (!rule.Text.Contains(updateStr)) { newMappingsStr.Add(updateStr); } if (newMappingsStr.Count > 0) { string startTag = "\n'<LinkParameters:Section:Start> This code section was generated by LinkParameters Add-In (Do NOT modify)\n"; string endTag = "'<LinkParameters:Section:End>" + " [Date: " + System.DateTime.Now.ToString() + "]\n"; rule.Text += startTag; foreach (string line in newMappingsStr) { rule.Text += line + "\n"; } rule.Text += endTag; } rule.FireDependentImmediately = fireDepRules; rule.AutomaticOnParamChange = !dontRunAuto; if (!dontRunAuto) { ruleApi.RunRuleDirect(rule); } } catch { } }
private static void RemoveEmptySections(iLogicRule rule) { List <string> lines = rule.Text.Split('\n').ToList <string>(); bool bIsLinkParametersSection = false; int nbNonEmptyLines = 0; int indexStart = 0; int index = -1; List <int> indicesToRemove = new List <int>(); foreach (string line in lines) { ++index; //Not in a section, look for section start if (!bIsLinkParametersSection) { if (line.Contains("<LinkParameters:Section:Start>")) { bIsLinkParametersSection = true; nbNonEmptyLines = 0; indexStart = index; } continue; } //we are in a section, look for section end if (line.Contains("<LinkParameters:Section:End>")) { bIsLinkParametersSection = false; if (nbNonEmptyLines == 0) { for (int removedIndex = indexStart; removedIndex <= index; ++removedIndex) { indicesToRemove.Add(removedIndex); } } continue; } if (line != "\r") { ++nbNonEmptyLines; } } indicesToRemove.Reverse(0, indicesToRemove.Count); foreach (int idx in indicesToRemove) { lines.RemoveAt(idx); } string text = string.Empty; foreach (string line in lines) { text += line + "\n"; } rule.Text = text; }