从插件执行iLogic规则
                2021/06/29 23:13:39            
                        
                                iLogic已经广泛的得到应用,很多用户或者开发者都很亲睐。iLogic并不是万能de , 有些时候,我们可能需要把iLogic和Inventor API开发的插件结合起来。这时就存在一个问题,如何从Inventor API程序调用iLogic,并执行其中的规则?
其实,Inventor提供了iLogic相关的接口,在\bin\Autodesk.iLogic.Automation.dll 和 Autodesk.iLogic.Interfaces.dll. 添加这两个dll后,就能开始访问iLogic了。 
iLogic也是一种插件,因此首先需要通过插件系统获取插件入口,Autodesk.iLogic.Automation提供的是入口。得到入口后,就能遍历到其中的规则,而iLogic相关对象由Autodesk.iLogic.Interfaces提供。
下面这个简单的代码样例,它在插件中执行一个按钮操作,获取iLogic插件,并查找其中一个名为“MyRule”的规则,最后执行它。
                
            
                             其实,Inventor提供了iLogic相关的接口,在
iLogic也是一种插件,因此首先需要通过插件系统获取插件入口,Autodesk.iLogic.Automation提供的是入口。得到入口后,就能遍历到其中的规则,而iLogic相关对象由Autodesk.iLogic.Interfaces提供。
下面这个简单的代码样例,它在插件中执行一个按钮操作,获取iLogic插件,并查找其中一个名为“MyRule”的规则,最后执行它。
- void _buttonDef1_OnExecute(NameValueMap Context)
 - {
 -     System.Windows.Forms.MessageBox.Show("Button clicked!!", "Ribbon Demo");
 -  
 -     string progId = "Inventor.Application";
 -     Inventor.Application m_inventorApplication =
 -         (Inventor.Application)Marshal.GetActiveObject(progId);
 -  
 -     //iLogic 也是一种插件,有自己的guid
 -     string iLogicAddinGuid = "{3BDD8D79-2179-4B11-8A5A-257B1C0263AC}";
 -  
 -     Inventor.ApplicationAddIn addin = null;
 -     try
 -     {
 -         // 尝试获取iLogic插件
 -         addin =
 -                 m_inventorApplication.ApplicationAddIns.get_ItemById(iLogicAddinGuid);
 -     }
 -     catch
 -     {
 -         // any error...
 -     }
 -  
 -     if (addin != null)
 -     {
 -         // 若插件还未激活
 -         if (!addin.Activated)
 -             addin.Activate();
 -  
 -         // iLogic的入口
 -         Autodesk.iLogic.Automation.iLogicAutomation _iLogicAutomation =
 -             (Autodesk.iLogic.Automation.iLogicAutomation)addin.Automation;
 -  
 -         Document oCurrentDoc = m_inventorApplication.ActiveDocument;
 -  
 -         Autodesk.iLogic.Interfaces.iLogicRule myRule = null;
 -         //遍历规则
 -         foreach (Autodesk.iLogic.Interfaces.iLogicRule eachRule in _iLogicAutomation.get_Rules(oCurrentDoc))
 -         {
 -             if (eachRule.Name == "MyRule")
 -             {
 -                 myRule = eachRule;
 -                 //打印出规则里的代码内容
 -                 MessageBox.Show( myRule.Text);
 -                 break;
 -             }
 -         }
 -         //执行规则
 -         if (myRule != null)
 -             _iLogicAutomation.RunRule(oCurrentDoc, "MyRule");
 -     }
 - }