The Classic First Example Program for WebLink
Getting started with WebLink is a challenge because there aren't many examples provided by PTC. There are a few good examples in there, but they represent only a small fraction of the API. This together with the sometimes complex browser security issues can make WebLink seem unapproachable.
In the interest of getting you up and running with WebLink, I'll discuss some of the major hurdles and provide a good, basic starting point.
On Windows, the Internet Explorer browser security model may require the following:
- WebLink HTML page must be served by a web server
- URL may need to be fully qualified (i.e. http://srv1.yourcompany.com/... not just http://srv1/...)
- Internet Explorer should consider your web server as a "trusted host"
- config.pro option WEB_ENABLE_JAVASCRIPT must be set to ON
For WebLink that may be enough, but if using other COM objects, such as interfacing with MS Excel for example, changes to IE security settings may be required to grant your application more privileges. This is also a security risk, so be careful when doing this.
The Hello World example is self contained other than loading the (PTC provided) pfcUtils.js file. This file can be placed in the same folder on the web server as the HTML file. The example has a small form containing an anchor tag (with id of "mesg") and a button. The button executes the HitMe() function which populates the contents of the anchor tag.
In nearly every application the pfcGetProESession() function is called, which is contained in the pfcUtils library file. The try/catch block around it verifies that the embedded browser is used, which is essential.
Once we have the session object, the current model object is obtained, from which we can get the name of the model. This value is displayed along with a message in the anchor tag. The try/catch block here helps verify that there is an active model (part, assembly, or drawing), because "model" will be null if there isn't. Trying to call any method against null is pretty much guaranteed to throw an exception.
WebLink Hello World Example:
<HTML>
<SCRIPT LANGUAGE="JavaScript" type=text/javascript src="pfcUtils.js"></SCRIPT>
<BODY>
<SCRIPT LANGUAGE="JavaScript">
function HitMe() {
if (!pfcIsWindows())
netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
var form_elem = document.getElementById("mesg");
var modelname = "no model";
var session = null;
var model = null;
try {
session = pfcGetProESession();
}
catch (e) {
form_elem.innerHTML = "ERROR: Cannot connect to Pro/Engineer session!";
return;
}
try {
model = session.CurrentModel;
modelname = model.FileName;
}
catch (e) {
// probably no model
form_elem.innerHTML = "Make sure a model is active!";
return;
}
form_elem.innerHTML = "Hello! My name is: " + modelname;
}
</SCRIPT>
<form name="f">
<INPUT name="btn1" type=button value="Hit me!" onclick="HitMe()">
<br><a id="mesg"></a>
</form>
</BODY>
</HTML>
If you have any questions, please ask. Comments and questions are welcome.
1 comment:
everything is ok but where should I write these codes?
Post a Comment