|
Run Script 
Access: Open this function from the following location:
-
Select the Run Script function from the Customize dialog and position it as required in the Cimatron window. Invoke subsequently from the new location.
Run a script (*.esc) file.
Scripts can be run within Cimatron to perform specific tasks. You can write your scripts in either vbscript or javascript.
The Script Engine has the ability to handle events such as entering the script, leaving the script, and adding objects to scripts (the objects are created in Cimatron and can be used in the script).
General Interaction
Select the appropriate script from the open file dialog:
Classes:
CScriptRunner |
A class that can run a script file (the filename is required as a parameter). |
CScriptEngine |
The main class in the Script Engine mechanism. The class implements 2 COM interfaces: IActiveScriptSite and IActiveScriptSiteWindow that deal with running scripts. The CScriptEngine can hold a pointer to a CScriptSink (which is provided by the user of the script engine) object that enables you to handle events such as OnEnterScript and OnLeaveScript. |
CScriptSink |
An abstract class that enables you to handle script events (such as OnEnterScript and OnLeaveScript). |
How to use Cimatron scripts:
Running Scripts with CScriptRunner:
void MyClass::RunScript()
{
CString aFileName = "script.esc";
CScriptRunner aScriptRunner;
aScriptRunner.RunScript(aFileName);
}
Running Scripts without CScriptRunner (through the CScriptEngine):
// pszScript is the script as a string
CString szEngineName = "vbscript";
// Load the scripting engine
CScriptEngine aScriptEngine;
HRESULT hr;
hr = aScriptEngine.Create(T2COLE(szEngineName));
// Execute the script
hr = aScriptEngine.Execute(T2COLE(pszScript));
hr = aScriptEngine.Close();
Handling Events:
Create a Sink class derived from CScriptSink:
class CMySink : public CScriptSink
{
CMySink::CMySink() {}
void OnEnterScript()
{
// do something
AfxMessageBox(_T("Entering Script !"));
}
void OnLeaveScript()
{
// do something
AfxMessageBox(_T("Leaving Script !"));
}
};
Attach your sink object to the engine before running the script:
CScriptSinkTest* pSink = new CScriptSinkTest();
CScriptEngine aScriptEngine;
aScriptEngine.SetScriptSink(pSink);
...
Adding objects to the script:
Attach your sink object to the engine before running the script:
CScriptEngine aScriptEngine;
HRESULT hr;
hr = aScriptEngine.Create(T2COLE(szEngineName));
// Add Item
CComPtr<IDispatch> spdispScriptHelpers;
hr = spdispScriptHelpers.CoCreateInstance(CLSID_ScriptHelpers);
hr = engine.AddObject(OLESTR("myobj"), spdispScriptHelpers,
SCRIPTITEM_ISVISIBLE | SCRIPTITEM_GLOBALMEMBERS);
"spdispScriptHelpers" is a pointer to an object that can be added.
"myobj" is the name of the object that can be used in the script.
Example Script:
The following is an example of a script that executes a command:
language=vbscript
dim app,doc,model
set app = CreateObject("Cimatron.Application")
Dim PoolCmd,Cmd
Set PoolCmd = App.GetPoolCommands
If Not (PoolCmd Is Nothing) Then
Set Cmd = PoolCmd.GetCommand("File1", "New")
if not cmd is nothing then
Cmd.Execute
end if
End If
|