Wednesday, June 18, 2008

Pro/E VB API: Not Just for Visual Basic Anymore

In a previous article entitled "WebLink: What is it anyway?", I discussed Pro/WebLink and how it's not exactly a Javascript interface. Now it's the VB API's turn. The VB API is newly released by PTC with Wildfire 4. It is billed as a Visual Basic interface to PTC's PFC libraries, which is the same core that J-Link and Pro/WebLink are built upon.

While this is true, it's not that limited. Don't let the name scare you away from using your favorite COM accessible programming or scripting language to write Pro/Engineer applications. This is a good thing because it opens the API to a much wider range of applications.

In this article, I'll walk through sample applications written in Visual Basic, VBScript (using Windows Script Host), Perl, and Javascript (in a HyperText Application), all using the VB API. The sample application will connect to a Pro/Engineer session, obtain the session object, display the name of the current model, then disconnect from the Pro/Engineer session. Each program will display the model name in a different way.

Suggested reading is the section on the importance of disconnecting from Pro/Engineer in Pro/E VB API: A First Look.

Excel Macro

This is the standard and documented approach to the sample program. Objects are declared up front, the connection to Pro/Engineer is made, then the session object is obtained. The model name is displayed both in cell A1 and in a MsgBox.

Sub Macro1()

Dim asynconn As New pfcls.CCpfcAsyncConnection
Dim conn As pfcls.IpfcAsyncConnection
Dim session As pfcls.IpfcBaseSession
Dim mdlname

Set conn = asynconn.Connect("", "", ".", 5)
Set session = conn.session

mdlname = session.CurrentModel.Filename
Range("A1").Select
ActiveCell.FormulaR1C1 = mdlname
MsgBox ("Name: " & mdlname)
conn.Disconnect(2)

End Sub
 

VBScript

The VBScript code is almost identical except that VBScript doesn't seem to allow for the type declaration of the objects. As a result, the CreateObject() call is used to instantiate a pfcAsyncConnection object.

The program is executed using the following command line:
  cscript vbapi_script.vbs
 

Dim asynconn
Dim conn
Dim session
Dim mdlname

Set asynconn = CreateObject("pfcls.pfcAsyncConnection")
Set conn = asynconn.Connect("", "", ".", 5)
Set session = conn.session

mdlname = session.CurrentModel.Filename
MsgBox ("Name: " & mdlname)
conn.Disconnect(2)
 

Perl

As with most COM applications, the Perl syntax is also very similar to the VBScript code, but with Perl's own syntactical flavor. Win32::OLE->new() is the Perl equivalent to VBScript's CreateObject(). The Perl program outputs the model name to the command prompt (or standard output).

use Win32::OLE;
$asynconn = Win32::OLE->new("pfcls.pfcAsyncConnection");
$conn = $asynconn->Connect( "", "", ".", 5 );
$session = $conn->Session;
$mdlName = $session->CurrentModel->FileName;

print "mdlName: $mdlName", "\n";
$conn->Disconnect(2);
 

Javascript

Pro/WebLink applications can finally break out of the embedded browser jail using the VB API. This example uses a "Hypertext Application", which is a web page with a special HTA tag and with a file extension of ".hta" instead of ".htm". The pfcUtils.js file cannot be used as-is because it tries to use COM objects with "pfc." prefixes, instead of those associated with VB API which have "pfcls." prefixes.

Other than those differences, it's essentially a Pro/WebLink application. As with the other examples, because it's an asynchronous application, the code must connect to the Pro/Engineer session. This is a step that embedded browser based Pro/WebLink applications don't have to worry about.

<html>
<head>
<title>VB API Test</title>

<HTA:APPLICATION
ID="vbapi-test"
APPLICATIONNAME="VB API Test"
SCROLL="auto"
SINGLEINSTANCE="yes"
>
</head>

<body>

<SCRIPT LANGUAGE="JavaScript">

function HitMe ( ) {
var obj = null;
var elem = document.getElementById("mesg");

if (obj == null) {
try {
obj = new ActiveXObject("pfcls.pfcAsyncConnection");
}
catch (e) {
elem.innerHTML = "Failed to create object";
return;
}
}

var conn = obj.Connect( "", "", ".", 5 );
var session = conn.Session;
var mdlName = session.CurrentModel.FileName;

elem.innerHTML = "mdlName: " + mdlName;
conn.Disconnect(2);
}

</SCRIPT>

<form name="f">
<INPUT name="a" type=button value="Hit me!" onclick="HitMe()">
<br><a id="mesg"></a><br>
</form>

</body>
</html>
 

These are just a few of the possibilities. As you can see, there is nothing really Visual Basic specific about the VB API. It's just an API.

10 comments:

  1. Hi!

    I have successfully learned how to automate Microsoft applications using COM in Autohotkey and starting with Autocad. I would like to also know how to use Creo but I am stuck at the first object. This does not work for me
    asynconn := ComObjCreate("pfcls.pfcAsyncConnection")

    In Autohotkey ComObjCreate is a function that copies the VB function but has slightly different limitations I think.

    Have you used Autohotkey or have any suggestions?

    ReplyDelete
  2. Steve,

    There are two prerequisites to using the ProE/Creo API.

    Did you run the vb_api_register.bat batch file from the install
    to register the COM objects?

    Did you set the PRO_COMM_MSG_EXE environment variable to point
    to ...\obj\pro_comm_msg.exe? The obj subfolder is in the i486_nt
    (32 bit) or x86e_win64 (64bit) folder.

    I have never used Autohotkey, but it probably will work. I have
    used VBScript and Perl. Here's some VBScript code that will
    connect to ProE/Creo (error checking excluded):

    Set acClass = pfccreate("pfcls.pfcAsyncConnection")
    Set conn = acClass.Connect("", "", "", 5)
    Set session = conn.session

    Let me know if it helps or not. The COM stuff can be tricky at
    first, but keep at it. Once you get it, you'll be good to go.

    Marc

    ReplyDelete
  3. Hello Marc,

    I tried using the CREO API using Python without success. I used the following code but I get an error saying that there is no IpfcAsyncConnection module(I ran the vb_api_register.bat batch file from the install
    to register the COM objects, and set the PRO_COMM_MSG_EXE path as explained in the user manual and your explanations...): import win32com.client as win32

    asyncconn = win32.gencache.EnsureDispatch("pfcls.pfcAsyncConnection")

    conn = asyncconn.Connect(None,None,None,None)

    session = conn.Session


    Can you help??
    Stephane

    ReplyDelete
  4. This comment has been removed by the author.

    ReplyDelete
  5. I am able to setup vb api standalone application. Can you somebody tel me the procedure to setup server based applications..

    ReplyDelete
  6. I am able to setup vb api standalone application. Can you somebody tel me the procedure to setup server based applications..

    ReplyDelete
  7. Hello Marc,

    Could you please share a sample code to read the model name (.asm) in excel file and open in proe/creo (xxxxxx.asm)?

    Ramesh

    ReplyDelete
  8. Python example would look like follows:


    from __future__ import print_function
    import win32com.client

    asynconn = win32com.client.Dispatch('pfcls.pfcAsyncConnection')
    conn = asynconn.Connect('','', None, None)
    session = conn.Session

    mdlname = session.CurrentModel.Filename
    print("Name: "+ mdlname)
    conn.Disconnect(2)

    ReplyDelete
  9. Hi,

    Where are the lybraries of Creo to be loaded, if I'm running from Excell macros.

    Regards.

    ReplyDelete
  10. Wynn Hotel and Casino - MapyRO
    Wynn Hotel and Casino. 3121 S Las 안동 출장샵 Vegas Blvd. 89109. (877) 791-5000. Call Now. 안전 토토 사이트 © 2021-2022 하남 출장마사지 MGM Resorts. 정읍 출장마사지 All rights 김천 출장안마 reserved.

    ReplyDelete