<script type="text/creojs" src=”weblink.legacy.creojs”></script>
<script>
var features=SubComp.ListFeaturesByType
(true, pfcCreate ("pfcFeatureType").FEATTYPE_COMPONENT);
for (var mi=0; mi < features.Count; mi++)
{
var feature=features.Item(mi);
. . .
}
</script>
<script type="text/creojs" id=”iteratefeatures.creojs”> const features = SubComp.ListFeaturesByType (true, pfcFeatureType.FEATTYPE_COMPONENT); // classical loop for (var mi=0; mi < features.length; mi++) { const feature=features [mi]; // . . . } // range loop for (const feature of features) { // . . . } // functional style with arrow function features.forEach (feature => {/* ... */}) </script>
<script>
var session = null;
function init()
{
var ComObj = pfcCreate ("MpfcCOMGlobal");
if (ComObj) {
session = ComObj.GetCurrentSession ();
alert ("Successfully connected to Creo!");
}
else {
alert ("Cannot connect to Creo!");
}
}
init ()
</script>
<script type="text/creojs" id=”initsession.creojs”> const session = pfcGetCurrentSession () </script>
<script>
var compPath=pfcCreate ("MpfcAssembly").CreateComponentPath
(asmObj, intPath);
</script>
<script type="text/creojs" id=”testglobalfunc.creojs”> const compPath=pfcCreateComponentPath(asmObj, intPath); </script>
<script>
var intPath=pfcCreate ("intseq"); // Create sequence
intPath.Append (1); // Append item
// . . .
intPath.Append (4); // Append item
// Remove last item
intPath.Remove (intPath.Count - 1, intPath.Count);
</script>
<script type="text/creojs" id=”arrayvsseq.creojs”> const intPath=[]; // Create array intPath.push (1); // Push item 1 intPath.push (2,3,4); // Push a group of items 2,3,4 // Remove last item intPath.pop (); </script>
<script type="text/creojs" id=”arrayvsseq.creojs”> const intPath=[1,2,3,4]; // Create and initialize array // Remove last item intPath.pop (); </script>
<script>
var componentType=pfcCreate ("pfcFeatureType").FEATTYPE_COMPONENT
var featureStatus=pfcCreate ("pfcFeatureStatus").FEAT_ACTIVE
var partType=pfcCreate ("pfcModelType").MDL_PART
</script>
<script type="text/creojs" id=”enumerations.creojs”> const componentType=pfcFeatureType.FEATTYPE_COMPONENT const featureStatus= pfcFeatureStatus.FEAT_ACTIVE const partType= pfcModelType.MDL_PART </script>
<script type="text/creojs" id=”enumitemtypes.creojs” function getModelItemTypes () { const types = {} for (const v of pfcModelItemType.values()) { types [v.string()] = v.value() } return types } </script> <script> CreoJS.getModelItemTypes ().then (function (types) { if (types) { var listElement = document.createElement ('ul') document.appendChild (listElement) for (var itemType in types) { var listItem = document.createElement ('ul') listItem.innerHTML = '<b>' + itemType + '</b>: ' + types [itemType] listElement.appendChild (listItem) } } }) </script>
<script type="text/creojs" id=”enumtoprimitive.creojs”> Browser.alert (+pfcFeatureType.FEATTYPE_COMPONENT); </script>
<script type="text/creojs" id=”typeinfoexample.creojs”> let currentModel = pfcGetCurrentSession().CurrentModel print (currentModel.isInstanceOf (“pfcAssembly”)) print (currentModel.isInstanceOf (pfcModel)) print (currentModel.isInstanceOf (pfcSolid)) print (currentModel.isInstanceOf (currentModel.getClassName())) </script>
| • | saveToSession(key, value)—Saves value to the global session map under the name key. |
| • | getFromSession(key)—Returns the global session map value saved under the given name. |
| • | removeFromSession(key)—Removes value from the global session map specified by the given key. |
| • | saveToAppData(relativeFileName, data, roaming=false)—Saves the given data string to a file inside your AppData folder for Creo.JS data (CreoJS/data by default). Relative file name may contain directories. If such a directory does not exist in getCreojsAppDataFolder(roaming), it is created. |
| • | getFromAppData(relativeFileName, roaming=false)—Reads a specified file from your AppData folder for Creo.JS data. The function throws an exception if the file does not exist. On success returns a string with the content of the specified file. |
| • | deleteFromAppData(relativeFileName, roaming=false)—Removes specified file from your AppData folder. The function throws an exception on failure. |
| • | existsAppData(relativeFileName, roaming=false)—Returns true when a file with the given relative name exists in getRoamingAppDataFolder(roaming), false otherwise. The following functions enable you to retrieve the full name of your AppData folders:
|
| • | getAppDataFolder(roaming=false)—Returns the full name of the current user’s AppData folder –Local (default) or Roaming. |
| • | getLocalAppDataFolder()—Returns the full name of the current user’ s Local subfolder inside the AppData folder. Same as %LOCALAPPDATA% or getAppDataFolder(false). |
| • | getRoamingAppDataFolder()—Returns the full name of the current user’s Roaming subfolder inside the AppData folder. Same as %APPDATA% or getAppDataFolder(true). |
| • | getCreojsAppDataFolder(roaming=false)—Returns the full name of the directory inside the current user's AppData designated to store Creo.JS objects. By default, it is CreoJS/data. Can be reconfigured through the setting appdata_subdir in the creo_js_app.conf file. |
| • | getCreojsLocalAppDataFolder()—Same as getCreojsAppDataFolder(false). |
| • | getCreojsRoamingAppDataFolder()—Same as getCreojsAppDataFolder(true). |
| • | uploadFile(URL, filePath, responseHandler=null)—Uploads the specified file to the specified server. If responseHandler in not provided, works in sync mode and returns a URL received from the server on success. The function throws an exception
on failure. If responseHandler is specified, the function returns an empty string and executes the responseHandler upon completing the upload providing HTTP response object as an argument of the responseHandler.
Note
You must have read access to the directory from which you are trying to upload the file.
For example:
function respHandler (httpResponse) {
print ('Response received: ' +
JSON.stringify (httpResponse, null, 4));
}
uploadFile ('http://my.server.com/upload.php', 'my_file.bin',
respHandler); |
| • | uploadFiles(URL, jsonOptions …, responseHandler=null)—Uploads one or more files with a single request. Unlike uploadFile method, this function uses multipart mode. For each file to be uploaded you can specify a name by setting the fieldName as jsonOptions and content type. In sync mode where no response handler provided, the function returns a string representation of a JSON containing information about each uploaded file on success. If response handler is specified, the returned JSON response will be passed as an argument of the response handler.
For example:
let resp = uploadFiles (
'http://my.server.com/filestorage/upload.cgi',
{filePath: 'project.gif', fieldName: 'fileToUpload'},
{filePath: 'config.pro', fieldName: 'anotherFileToUpload'
contentType: 'plain/text'}
); |
| • | downloadFile(URL, filePath, responseHandler=null)—Downloads a file from the server and saves it with the specified name. LikeuploadFile this function also works in sync or async mode. In sync mode it returns the name of the file on success or throws an exception
on failure.
Note
You must have write access to the download directory.
|
| • | uploadString(URL, valueToUpload, responseHandler=null)—Uploads a string to the server. It works like uploadFile, however uploads a simple string and not a file. |
| • | downloadString(URL, responseHandler=null)—Downloads a string from the specified URL. In sync mode, it returns the body of the HTTP response as a string on success or throws an exception on failure. For example:
let resp = uploadString (
'http://my.server.com/uploadMessage.php',
'Message to send to server'
);
print (`Retrieve though: ${resp}`);
let content = downloadString (resp);
print (`downloaded content:\n<div><i>${content}</i></div>`); |
| • | uploadJSON(URL, jsonObject, responseHandler=null)—Uploads a JSON object to the Web server. Works like uploadString applied to JSON stringified jsonObject, however uses Content-Type: application/json when sending the HTTP request. |
| • | downloadJSON(URL, responseHandler=null)—Downloads a string from the specified server and then parses it to convert to a JSON object. In sync mode, returns this object, and in async mode, converts the body of the HTTP response to the JSON object and then invokes the responseHandler. For example:
const respHandler = function (httpResponse) {
if (httpResponse.statusCode == 200 && httpResponse.body) {
print (`Address: ${httpResponse.body.street},
${httpResponse.body.city},
${httpResponse.body.state}
${httpResponse.body.zip}`);
}
};
downloadJSON ('http://my.server.com/download.php&myfile',
respHandler
); |
| • | readFileAsString(filename)—Reads the content of the specified file and returns it as a single string. The parameter filename is either a full or a relative file name. When the relative file name is provided, the file is searched for in the working
directory. The function throws an exception in case of failure.
Note
This function is defined only if the config.pro file contains the option web_link_file_read set as YES.
|
| • | writeFileAsString (filename, text)—Writes specified text to the specified file. The parameter filename is either a full or a relative file name. When the relative file name is provided, the file is searched for in the working
directory. The function, throws an exception in case of failure.
Note
This function is defined only if the config.pro file contains the option web_link_file_read set as YES.
|
| • | startWebServer(port=-1)—Starts the Web server listening on the given port. When port is set to -1, the value of the port is taken from the configuration file creo_js_app.conf. When port is set to 0, server will start on any available port, otherwise the server will try to start on the specified port. |
| • | isWebServerStarted()—Returns true if the Web server is successfully started, false otherwise. |
| • | getWebServerPort()—Returns the port number on which the Web server is listening. If the Web server is not started, the function returns -1. |
| • | addWebSocketServerHandler(path, dataHandler, closeHandler=null, errorHandler=null, openHandler=null) or addWebSocketServerHandler(path, {dataHandler, closeHandler, errorHandler, openHandler})—Sets a web socket handler for the specified path. The second parameter is an object that may contain the following handlers:
|
| • | removeWebSocketServerHandler(path)—Removes a web socket handler that corresponds to the specified path. |
| • | addServerHandler(path, dataHandler, method=RequestMethod.ALL)—Sets a Web server handler for the specified path. Data handler is the function function (WebServerRequest req, WebServerResponse resp) that processes the requests. Request req contains information about the incoming request, while response resp has tools for sending response. The method argument specifies the type or types of requests this handler serves. Following are the possible values:
|
| • | object WebServerRequest—Creo.JS type that contains the following methods:
|
| • | object WebServerResponse—Creo.JS type that has the following methods:
|
| • | removeServerHandler(path)—Removes the Web server handler for the given path. |
| • | stopWebServer()—Stops the currently running Web server. |
| • | require(requiredScriptName)—Behaves in the same way NodeJS require does: embraces the code in the required file with function (module, exports){and },) executes the created function and returnsmodule.exports as the result. Searches for the requiredScriptName in the current script directory, then in the directory one level up, and so on until the file in need is found or directory
under consideration is not an accessible directory, then keeps searching in the getScriptPath(). When a file was successfully required (found and executed) in a context, it gets cached for this context and all consecutive
calls of the require function with this script returns the cached value.
|
| • | forgetRequired(requiredScriptName)—Removes the required file from cache. That is, consecutive calls of require results in reloading and re-executing the required script. This function could be helpful in the developing stage. |
| • | forgetAllRequired()—Removes all cached required scripts from the current context. |
| • | getScriptPath()—Returns an array of folders where require is looking for a required script. The script path is to be set in the creo_js_app.conf file by setting js_path property. |
| • | getScriptFolder()—Returns the full path of the directory JavaScript scripts located in. |
| • | getScriptsList()—Returns a string containing a list of all scripts names from getScriptFolder() separated by commas. Script names are the relative names to getScriptFolder(). |
| • | getScripts()—Returns an array of all script names from getScriptFolder(). |
| • | setCIPSequenceHandler(arrayHandler)—Sets a JavaScript callback that gets executed every time an invoked OTK method returns a CIP sequence. All CIP sequences get converted to JavaScript arrays. This callback allows you to define a function which takes a CIP sequence converted to an array and define some methods. For example, Item(), Set(), Append(), etc. to make it behave similar to WebLink sequences for backward compatibility. See how weblink.legacy.creojs uses this function. |
| • | setUseCIPSequenceHandler(bool on)—Turns on/off the invocation of the CIP sequence handler (if set.) setUseCIPSequenceHandler(false) works like setCIPSequenceHandler(null). If CIP sequence handler is not set, this function has no effect.
|
| • | setOnContextClose(handler)—Sets a JavaScript callback to execute upon closing the current context. For example, when the embedded browser window which is open for the current context, is closed. |
| • | help (name=’’) —Opens a browser window with OTK help for the specified class or class method. The argument name can be either a className or className.MethodName. For example:help ('pfcSession');
help ('pfcSession.GetAppInfo'); |
| • | print(message)—Prints a given message to the current browser window. |
| • | Additional features are introduced on array-like structures, which represent two and three dimensional concepts, such as, pfcPoint3D, pfcPoint2D, pfcMatrix3D, and so on. |
| • | Properties x, y, z, if applicable, representing get(0), get (1), and get(2) respectively. |
| • | Method dims()—Returns an array of sizes for each dimension. |
| • | Method dim(dimensionIndex)—Returns the size of the dimension with specified index. |