Tuesday, April 8, 2008

WebLink: What Is It Anyway?

A discussion of what WebLink is, and is not

WebLink is one of the two free API's (JLink being the other) provided by PTC to create Pro/Engineer applications. According to PTC's documentation, WebLink is a Javascript based library for use within the embedded web browser.

That's sounds great, but it's about as accurate as describing a car as a pothole creator. Well, here in Detroit that's more true than not, especially on Van Dyke ... but I digress.

Truthfully, WebLink isn't really Javascript based at all. It's based on Microsoft's COM/ActiveX/OLE objects on Windows, and Mozilla's XP-COM on Unix. You don't even need to use Javascript. On Windows, you can use VBScript, Perl, Python, or any one of your favorite languages that can access COM objects.

Here's an example of using VBScript in a WebLink application:

<SCRIPT type="text/vbscript">
function HitMe ()
dim mdlname
Set pfccomglob = CreateObject("pfc.MpfcCOMGlobal")
mdlname = pfccomglob.GetProESession.CurrentModel.FileName
Document.getElementById("mdlname").innerHTML = "Name: " & mdlname
msgbox("Name: " & mdlname)
end function
<form name="f">
<INPUT name="a" type=button value="Hit me!" onclick="HitMe()">
<div id="mdlname"></div>

To make matters worse, you don't even need to use the web browser! A Pro/Toolkit DLL can access the WebLink COM objects using C/C++ and can do so completely outside the confines of the web browser. The Pro/Toolkit DLL could also go as far as hosting your favorite scripting engine. The web browser is just one of many environments in which a WebLink application can run.

Here's the same example using Perl to access WebLink COM Objects from within a Pro/Toolkit DLL:
use Win32;
use Win32::OLE;
$pfccomglob = Win32::OLE->new('pfc.MpfcCOMGlobal');
$mdlName = $pfccomglob->GetProESession->CurrentModel->FileName;
Win32::MsgBox("Name: $mdlName");

To summarize, WebLink requires neither Javascript nor a web browser. That clears things up, right? Why is it called "WebLink"? Well, you can use it in a web browser, and all PTC software has to have the word "Link", as a result: WebLink!

Can you imagine the response had they called it Pro/COM, or ActiveXLink, or COM.Link? A resounding "huh?" would have echoed through the halls on Kendrick Street. I think the marketing guys got it right this time.

Anyway, using whatever language in whatever environment you desire, the huge benefit to WebLink is that it's a rapid prototyping system for Pro/Engineer applications. The code-test-code-test cycle can be repeated as quickly as you can refresh the web browser. Whether you intend to migrate the application to JLink, or leave it in the web browser, you can get a complex application written very, very quickly. That's what makes WebLink very powerful.

Learn it, you won't regret it.


Aseem said...

Thank you... this was helpful...

But where can I start learning about WebLink / J Link?

Are there any good books available on this? Or some web based tutorials?


Aseem Saxena

Marc Mettes said...

Unfortunately, there isn't much out there. Training classes in J-Link are available from a few 3rd parties, but typically you must rely upon the documentation, which is good, but not great.

You'll get the most useful answers by finding those who are actually using it and asking them. They are out there (some are active on the forums), but might be hard to find.


Joe Varghese said...

Your blog is very nice.. In fact I myself was thinking of doing similar and did a bit but couldnt managae it till now.. Nice job!

Regrding this post: Thats a really usefull posting. I have searched a few regarding j-link, web-link, toolkit... But never heard that I can use VB or probably Tickle/tk as well.. Thats really exciting news.. I must find some time to dig little bit more on it.. Hopefully, i will get one working..


Lucas said...

"On Windows, you can use VBScript, Perl, Python, or any one of your favorite languages that can access COM objects."

Could you point out how to do that? I'm especially interested in using python with weblink

Marc Mettes said...

Perhaps instead of "... you can use ...", I should have said "... theoretically, you could use ...".

Any code that wants to interface with the Pro/WebLink ActiveX controls, must run within the Pro/Engineer process space, which means the embedded web browser or Pro/Toolkit application.

When IronMonkey is released, the ability to use other languages within Internet Explorer will be available.

For now, if you're using the web browser, you're limited to Javascript and VBScript.

Embedding a python interpreter within a Pro/Toolkit application has been done, but is a very complex task.


Lucas said...

Infact I would like to escape from browser. This compilation(java+browser) is just ugly for me ;) I would like to write some code in Python or IronPython like I can do with Solidworks using .NET Framework.

Lucas said...

I'm looking at your Perl example and I think that something similar can be achieved with python and pywin32[1] extension(which adds OLE/COM support) but..

You have said Mark:
"example using Perl to access WebLink COM Objects from within a Pro/Toolkit DLL"
Does it mean that I need Pro/Toolkit? What is the procedure of loading this DLL into Perl code? I'm not familiar with Perl at all. Thanks in advance for any thoughts regarding this topic.

[1] http://sourceforge.net/projects/pywin32/

Marc Mettes said...

With WF4, you can escape the browser by using the VB API with python or any other scripting language.

The perl example uses a special Pro/Toolkit program that I wrote which embeds the perl interpreter, making it able to run perl scripts. You could use the process with python, however if you don't have access to Pro/Toolkit, this will be pretty hard to do.

Another option is to utilize jython (python implementation in Java) together with J-Link (Pro/E Java API). This is something that you could do for free and would be much easier to setup than Pro/Toolkit with embedded python.

I hope that helps,


Lucas said...

Firstly, thank you very much for your help. I'm using WF3 so VB API does not matter.

Currently I'm working with J-Link and writing code in Java.
I've option to install Pro/Toolkit from installation CD but I don't know if using it requires some kind of licence fee.

I'll try this jython implementation but it looks like development of this platform has stopped.

How do you debug your Java code?

Lucas said...

Ok, I've found something about debugging in synchronous mode.

It's quite helpful: http://www.mcadcentral.com/proe/forum/forum_posts.asp?TID=36083&PN=3

Additionally config.pro with appropriate jlink_java_command should be loaded at startup of Pro/e - only then it works for me(Pro/e WF3 version) and also chaning code in the fly works! - great stuff(NetBeans).

Probably there is a way of using jython just by adjusting jlink_java_command but there will still be problem with protk.dat - I'll check this later..

CattoCrociato said...

Hello, and thanks of the post.
You can post an example of using weblink protoolkit by an application in c + +?
Thank you.

Manoj Kumar said...

I tried with python
from win32com.client.dynamic import Dispatch

# Excel
proe = Dispatch('pfc.MpfcCOMGlobal')
mdlName = proe.GetProESession()
#mdlName = proe.GetProEArguments()

but getting this error:

D:\Manoj\WebLink\PythonApproach>C:\Python26\python proe.py
Traceback (most recent call last):
File "proe.py", line 5, in
mdlName = proe.GetProESession()
File "", line 2, in GetProESession
pywintypes.com_error: (-2147352567, 'Exception occurred.', (0, None, u'pfcXNotCo
nnectedToProE', None, 0, -2147352567), None)

Please help!!!