如何使用python应用程序与桌面应用程序通信(java、c++、vb api可用)?

问题描述 投票:0回答:1

我是一名机械工程师,一名编程初学者,这是我的第一篇文章 - 所以提前道歉。

我有兴趣使用Python补充两个不同的Windows桌面应用程序(Polyworks和Creo 5.0)中的功能。 我想使用我的 python 应用程序来自动执行内部任务,并使用自定义 GUI 交互式地指导用户。

Creo 提供了一个 Java API,它对不同的函数有相当不错的覆盖,而 VB API 则较弱。 Polyworks SDK“使用 Microsoft 的 COM 架构来实现大部分功能(还通过纯 C++ API 提供附加功能)”。

在 Creo 中 - 我已经成功执行了与使用 VB、Java、VBA 的程序交互的示例程序,并通过调用用 Python 生成的内部宏语言文件(跟踪文件)。

我对使用 Python 通过 API 与这两个桌面应用程序进行有效通信的更一般技巧感兴趣?或者说这样做是否会导致效率极低?

重要的第一步是什么?创建一个函数库,记录函数的正常用法和嵌套,然后开始编写调用 Python 中创建的不同模块的脚本?

预先感谢您的任何建议。

只是为了让它更真实:

Creo Java API 应用程序示例

import com.ptc.cipjava.*;
import com.ptc.pfc.pfcSession.*;
import com.ptc.pfc.pfcModel.*;
import com.ptc.pfc.pfcSolid.*;
import com.ptc.pfc.pfcModelItem.*;
import com.ptc.pfc.pfcFeature.*;
import com.ptc.pfc.pfcExceptions.*;

/**
 * The pfcInstallTest makes sure that J-Link has been properly
 * installed on your machine.
 * It test the pfc and cipjava package functionality
 * to make sure that those appear correctly in your CLASSPATH
 **/


public class pfcInstallTest{

  private Session proeSession;
  private int numberOfFeatures;
  private int numberOfModels;
  private Feature proeFeature;
  private Model proeModel;
  private Models proeModels;
  private ModelItems proeModelItems;

  public pfcInstallTest(Session inSession){

    proeSession=inSession;
    String ModelName = "pfcinstalltest"; 
    boolean status;

    status = loadModel(ModelName);

    //sleep 2 sec
    try{
      Thread.sleep(2000);
    }
    catch(InterruptedException e){
      e.printStackTrace();
    }

    if (status) 
      {
    //Print the information about each feature in the model
    //Test the functionality of the cip package using stringsequences
    // Test exception throwing/handling
    // Test action listeners
    // Unlooad the model
    status = getInfo() && testCIP() && testException() && testActionListeners() && unloadModel() && testSequences();

      }
    try
    {
        if (status)
          {
              proeSession.UIShowMessageDialog("Install Test Sucessful", null);

          }
        else
          {
              proeSession.UIShowMessageDialog("Install Test Has Failed", null);
          }
    }
      catch (Throwable e)
      {
        e.printStackTrace();
      } 
  }

  /**
   * This function loads the model and displays it in the Pro/E Session
   **/

  public boolean loadModel(String sampleName){
    ModelType mdl_type = ModelType.MDL_PART;

    try{
      ModelDescriptor proeModelDescriptor=
        pfcModel.ModelDescriptor_Create (mdl_type ,sampleName, "");
        printMsg("Created model descriptor: "+(sampleName)+ " type: "+mdl_type.getValue());

        proeModel = proeSession.RetrieveModel(proeModelDescriptor);
    proeModel.Display();
        printMsg("Model was retrieved)");
    }
    catch(jxthrowable x){
      printMsg("pfcException: " + x);
      x.printStackTrace ();
      return false;
    }
    return true;
  }

Polywork C++ 客户端应用程序示例

/* *****************************************************************************
 * File: MainReadAndFit.cpp
 *
 * Description :
 *
 *      Defines the entry point for the console application.
 *      Reads an ASCII file of a point cloud containing points with coordinates and normals.
 *      The points are sent to PolyWorks|Inspector, and a sphere is then fitted to them.
 *      Then, the application verifies if the fit was successful after running a script MSCL.
 * -----------------------------------------------------------------------------
 * InnovMetric Inc. Copyright ( C ) 2002
 *                            All rights reserved
 * ************************************************************************** */

// ===========================================================================
// INCLUDED FILES
// ===========================================================================
#include <comdef.h>
#include <iostream>
#include <fstream>
#include <stdio.h>
#include <tchar.h>

#include "PolyWorksSDK/COM/IIMInspect.h"
#include "PolyWorksSDK/COM/IIMInspect_i.c"
#include "PolyWorksSDK/COM/IIMPointCloud.h"
#include "PolyWorksSDK/COM/IIMPointCloud_i.c"
#include "PolyWorksSDK/COM/IIMSceneCamera.h"
#include "PolyWorksSDK/COM/IIMSceneCamera_i.c"
#include "PolyWorksSDK/COM/IMInspect_i.c"

#pragma warning(disable : 4996)

// ===========================================================================
// CONSTANTS
// ===========================================================================
#define COEF        1
#define BUFFLEN     COEF* 1020

// ===========================================================================
// GLOBAL VARIABLES
// ===========================================================================
int nbImages = 0;

// color table
const int    GRP_RGBA_NB                     = 25;
const double _rgba_table[ GRP_RGBA_NB ][ 3 ] =
{
    { 1.0, 0.7, 0.7 },
    { 0.7, 1.0, 0.7 },
    { 0.7, 0.7, 1.0 },
    { 1.0, 1.0, 0.6 },
    { 1.0, 0.6, 1.0 },
    { 0.6, 1.0, 1.0 },
    { 0.6, 0.8, 1.0 },
    { 1.0, 0.6, 0.8 },
    { 0.8, 1.0, 0.6 },
    { 1.0, 0.8, 0.6 },
    { 0.6, 1.0, 0.8 },
    { 0.8, 0.6, 1.0 },
    { 0.6, 0.6, 0.6 },
    { 1.0, 0.4, 0.4 },
    { 0.4, 1.0, 0.4 },
    { 0.4, 0.4, 1.0 },
    { 1.0, 1.0, 0.2 },
    { 1.0, 0.2, 1.0 },
    { 0.2, 1.0, 1.0 },
    { 0.2, 0.6, 1.0 },
    { 1.0, 0.2, 0.6 },
    { 0.6, 1.0, 0.2 },
    { 1.0, 0.6, 0.2 },
    { 0.2, 1.0, 0.6 },
    { 0.6, 0.2, 1.0 }
};

// ===========================================================================
// FONCTION PROTOTYPES
// ===========================================================================
wchar_t* CharToWChar( const char* pSrc_, wchar_t* pDest_, size_t size_ );

int FillArraysFromFile( char*           filename_,
                        int             nbPoints_,
                        float**         aCoords_,
                        float**         aNormals_,
                        unsigned char** aColors_ );

int FillSafeArray( int             nbBlock_,
                   int             sizeBlock_,
                   float**         aCoords_,
                   float*          ppArrayCoord_,
                   float**         aNormals_,
                   float*          ppArrayNormal_,
                   unsigned char** aColors_,
                   unsigned char*  ppArrayColor_ );

int FillSafeArrayLast( int             startIdx_,
                       int             nbRemainPt_,
                       int             nbTPoints_,
                       float**         aCoords_,
                       float*          ppArrayCoord_,
                       float**         aNormals_,
                       float*          ppArrayNormal_,
                       unsigned char** aColors_,
                       unsigned char*  ppArrayColor_ );

int NbPointsFromFileGet( char* filename_ );

int Prompt( char* buffer );

// ===========================================================================
// FONCTION DEFINITIONS
// ===========================================================================

/* *****************************************************************************
 * Name: main
 *
 * Description :
 *
 *   Main routine
 *
 * Parameter: argc   : nb of arguments
 * Parameter: argv[] : array of arguments
 * -------------------------------------------------------------------------- */
int main( int argc, char* argv[] )
{
    //----------------------------
    // Initializes the COM library
    CoInitializeEx( nullptr, COINIT_APARTMENTTHREADED );


    //----------------------------
    //Global access to a PolyWorks|Inspector
    HRESULT     hr         =  0;
    IIMInspect* pIMInspect = nullptr;

    CLSID progID = CLSID_IMInspect;
    // With the line above, your program will attempt to connect exclusively with the version of PolyWorks|Inspector
    // you're currently working with. If the user upgrades to a more recent version of PolyWorks|Inspector, your program
    // will not attempt to connect to it.
    // Instead, to let your program attempt to work with the latest version of PolyWorks|Inspector installed on the
    // user's computer, uncomment the following line and make sure hr is not an error code before continuing.
    //hr = CLSIDFromProgID( L"InnovMetric.PolyWorks.IMInspect", &progID );

    hr = CoCreateInstance(
        progID,
        nullptr,
        CLSCTX_SERVER,
        IID_IIMInspect,
        ( void** ) &pIMInspect );

    if ( !SUCCEEDED( hr ) )
    {
        CoUninitialize();
        return 1;
    }
java python c++ api client
1个回答
0
投票

您可以尝试使用creopyson 请参阅此处 --> https://creopyson.readthedocs.io/en/latest/readme.html 您需要设置 creoson 才能与 creo 通信。

© www.soinside.com 2019 - 2024. All rights reserved.