CATIA多版本/进程对象处理

方法来源: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中的快捷方式的话,对于二次开发而言,意义不是很大。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值