从插件执行iLogic规则

奇异果实名认证 发表于 2021-06-29 23:13 | 显示全部楼层 | 复制链接分享      上一主题  翻页  下一主题
iLogic已经广泛的得到应用,很多用户或者开发者都很亲睐。iLogic并不是万能de , 有些时候,我们可能需要把iLogic和Inventor API开发的插件结合起来。这时就存在一个问题,如何从Inventor API程序调用iLogic,并执行其中的规则?

其实,Inventor提供了iLogic相关的接口,在<Inventor 安装路径>\bin\Autodesk.iLogic.Automation.dll 和 Autodesk.iLogic.Interfaces.dll. 添加这两个dll后,就能开始访问iLogic了。

iLogic也是一种插件,因此首先需要通过插件系统获取插件入口,Autodesk.iLogic.Automation提供的是入口。得到入口后,就能遍历到其中的规则,而iLogic相关对象由Autodesk.iLogic.Interfaces提供。

下面这个简单的代码样例,它在插件中执行一个按钮操作,获取iLogic插件,并查找其中一个名为“MyRule”的规则,最后执行它。


  1. void _buttonDef1_OnExecute(NameValueMap Context)
  2. {
  3.     System.Windows.Forms.MessageBox.Show("Button clicked!!", "Ribbon Demo");

  4.     string progId = "Inventor.Application";
  5.     Inventor.Application m_inventorApplication =
  6.         (Inventor.Application)Marshal.GetActiveObject(progId);

  7.     //iLogic 也是一种插件,有自己的guid
  8.     string iLogicAddinGuid = "{3BDD8D79-2179-4B11-8A5A-257B1C0263AC}";

  9.     Inventor.ApplicationAddIn addin = null;
  10.     try
  11.     {
  12.         // 尝试获取iLogic插件
  13.         addin =
  14.                 m_inventorApplication.ApplicationAddIns.get_ItemById(iLogicAddinGuid);
  15.     }
  16.     catch
  17.     {
  18.         // any error...
  19.     }

  20.     if (addin != null)
  21.     {
  22.         // 若插件还未激活
  23.         if (!addin.Activated)
  24.             addin.Activate();

  25.         // iLogic的入口
  26.         Autodesk.iLogic.Automation.iLogicAutomation _iLogicAutomation =
  27.             (Autodesk.iLogic.Automation.iLogicAutomation)addin.Automation;

  28.         Document oCurrentDoc = m_inventorApplication.ActiveDocument;

  29.         Autodesk.iLogic.Interfaces.iLogicRule myRule = null;
  30.         //遍历规则
  31.         foreach (Autodesk.iLogic.Interfaces.iLogicRule eachRule in _iLogicAutomation.get_Rules(oCurrentDoc))
  32.         {
  33.             if (eachRule.Name == "MyRule")
  34.             {
  35.                 myRule = eachRule;
  36.                 //打印出规则里的代码内容
  37.                 MessageBox.Show( myRule.Text);
  38.                 break;
  39.             }
  40.         }
  41.         //执行规则
  42.         if (myRule != null)
  43.             _iLogicAutomation.RunRule(oCurrentDoc, "MyRule");
  44.     }
  45. }
复制代码

  距米网  

找到您想要的设计

工程师、学生在线交流学习平台
关注我们

手机版- 距米网 |苏公网安备32041102000587号

© 2017-2024 常州居居米智能技术有限公司 苏ICP备18040927号-1