许可优化
许可优化
产品
产品
解决方案
解决方案
服务支持
服务支持
关于
关于
软件库
当前位置:服务支持 >  软件文章 >  CATIA多版本/进程对象处理技巧

CATIA多版本/进程对象处理技巧

阅读数 37
点赞 0
article_banner

方法来源:https://stackoverflow.com/questions/17658425/getting-a-specific-instance-of-com-object-in-vb-net#

当CATIA打开多个进程或同时启动多个版本CATIA时,通过getobject只能获取到第一个启动的CATIA对象。

   通过GetRunningObjectTable获取对象,然后进一步获取CATIA对象时,可以发现,存在2个CATIA对象,但这两个对象依旧对应第一个启动的CATIA对象。

   搜索的结果基本是,这是达索的锅,没有办法获取指定的CATIA对象。除了上面链接的那位老兄。

以下是这位大神老兄的方法原文:

1. Make a dll with two functions:
    HRESULT __stdcall CoMarshalToFile(IUnknown* punk, const char* const filePath)
    /* uses `::CreateStreamOnHGlobal`, `::CoMarshalInterface`, `::CoGetMarshalSizeMax`,
     and `::GetHGlobalFromStream` to marshal the IUnknown to the specified file.
    */
    HRESULT __stdcall CoMarshalFromFile(IUnknown** ppunk, const char* const filePath)
    /* uses `::CreateStreamOnHGlobal` and `::CoUnmarshalInterface` to marshal
     from the file to an IUnknown pointer.
    */

2. In CATIA:
  Note: this only needs to be done on the development computer.
  Make a new "VBA projects" macro library. 
    Add "declare" statements for:
        "LoadLibrary" (Windows API)
        "CoMarshalToFile" (DLL specified above)
    Add a function 
        Public Function MarshalCatiaToFile _
            (marshalInstanceFilePath As String, _
                marshalDllFolder As String) As Long

    MarshalCatiaToFile calls "LoadLibrary" to load the C++ DLL
    and then calls CoMarshalToFile (in DLL) to marshal the CATIA instance
    to a file.

  Remove the macro library from CATIA's list of macro libraries.

3. Create a file:
    "C:\Temp\CatiaOnTheFlyCatScripts\OnTheFlyCatScript.catvbs"
  The file can be empty.

4. In CATIA:
      Note: this must be done for *each* user of CATIA on *each* computer used.
      It may be possible to make this available to all users without individual
      setup required: it is saved in "FrameUserAliases.CATSettings"
      It may also be possible to reverse engineer the settings file and set up
      the needed data from outside CATIA.

    Add "C:\Temp\CatiaOnTheFlyCatScripts\" as a new "Directories" macro library.
    Make the added library "current"
    Use "Tools --> Customize --> Commands --> Macros" to assign a
      "User Alias:" to the "OnTheFlyCatScript.catvbs" script file.
      Name the alias "ExecuteOnTheFlyCatScript".
    Remove the macro library from CATIA's list of macro libraries.
    Close CATIA at this point to force the changes to be saved.

5. VB.net / C# program:
      Add the DLL (from step 1) and the CatVBA macro library (from step 2) as
      "Embedded Resource" to the project. 

      During program execution:
        Extract the DLL and macro library to an appropriate location. 
        Load the DLL into session using "LoadLibrary".
        Create the file:
          "C:\Temp\CatiaOnTheFlyCatScripts\OnTheFlyCatScript.catvbs"

        The "OnTheFlyCatScript.catvbs" will be executed in CATIA. It
          uses CATIA.SystemService.ExecuteScript to execute the 
          "MarshalCatiaToFile" function in the CatVBA macro library.
          Add method of choice to this file to indicate success/failure.
          I use a dialog box with the appropriate title.

        To execute the "OnTheFlyCatScript.catvbs":
          Using the Windows API functions, get the window handle for the
            "Power Input" box at the bottom right of the "desired" 
            CATIA window.
          Using the Windows API functions (*NOT* "SendKeys") send 
            "c:ExecuteOnTheFlyCatScript" + {Enter} to the "Power Input".
          Wait for the "completion" signal from the script. If you used
            a dialog box, use the Windows API function to close it.

        Assuming the script succeeded in marshaling the CATIA instance to
          a file, call the DLL function CoMarshalFromFile to get the CATIA
          instance.

相当繁琐,最后作者保证了该方式是能够正确操作多个CATIA对象,以下是该方法的个人理解。
关键点:CATIA内部VBA对象,CATIA命令行执行宏。

   在CATIA的VBA 编辑器  中,自带了CATIA对象。多个CATIA进程时,VBA中的CATIA对象依然能够正常操纵对应的CATIA。所以只要能够获取到这个对象,就可以操作指定的CATIA了。

   因此,调用CATIA宏,通过宏把对象传递至外部 程序 即可。

   接下来就是处理调用宏了,因为不能直接获取指定的CATIA对象,所以不能通过CATIA对象执行宏,原文作者采用了一种机智的办法,通过Windows API向CATIA的命令行发送消息。CATIA中命令行执行宏需要在 “Tools –> Customize –> Commands –> Macros” 中进行设置。之后通过相应的命令就可以直接在CATIA命令行中执行宏了。
总结:

   该方法的主要过程是:

   1、编写能够传递CATIA对象的宏;

   2、为该宏定义快捷方式;

   3、向CATIA窗口的命令行发送消息,执行宏,通过宏获取CATIA对象。

   至于跨进程获取对象,网上能够找到很多相关 资料 ,但是大部分都是对可序列化的对象进行的操作,对于这里的问题,只能是……我也不会,求助专业人士吧。

最后,该方法虽然能够实现多CATIA进程操作,但如果没有办法通过代码自动实现定义宏在CATIA中的快捷方式的话,对于 二次开发  而言,意义不是很大。


免责声明:本文系网络转载或改编,未找到原创作者,版权归原作者所有。如涉及版权,请联系删


相关文章
技术文档
QR Code
微信扫一扫,欢迎咨询~
customer

online

联系我们
武汉格发信息技术有限公司
湖北省武汉市经开区科技园西路6号103孵化器
电话:155-2731-8020 座机:027-59821821
邮件:tanzw@gofarlic.com
Copyright © 2023 Gofarsoft Co.,Ltd. 保留所有权利
遇到许可问题?该如何解决!?
评估许可证实际采购量? 
不清楚软件许可证使用数据? 
收到软件厂商律师函!?  
想要少购买点许可证,节省费用? 
收到软件厂商侵权通告!?  
有正版license,但许可证不够用,需要新购? 
联系方式 board-phone 155-2731-8020
close1
预留信息,一起解决您的问题
* 姓名:
* 手机:

* 公司名称:

姓名不为空

姓名不为空

姓名不为空
手机不正确

手机不正确

手机不正确
公司不为空

公司不为空

公司不为空