首页 > 解决方案 > 如何使用 python 应用程序(java、c++、vb api 可用)与桌面应用程序通信?

问题描述

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

我有兴趣使用 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;
    }

标签: javapythonc++apiclient

解决方案


推荐阅读