Creating Scripts
You can create scripts manually or by using the Script Wizard.
Creating Plug-in Scripts
The following sections give you step-by-step instructions on creating different types of plug-in scripts supported by the Eclipse Scripting API.
Create a Single-File Plug-in with the Script Wizard
To create a single-file plug-in with the Script Wizard, follow these guidelines:
- From the Start menu, select Varian > Eclipse Scripting API > Eclipse Script Wizard.
- Enter a name for the new script.
- Select the Single-file plug-in option.
- To select the location for storing the script, click Browse. By default, the script is stored in the user-specific Documents folder.
- Click Create.
- The Script Wizard creates the following folders in the location that you selected:
- Project folder: Contains a script-specific sub-folder where the Microsoft Visual Studio project file is stored.
- Plugins folder: Contains the source code file for the single-file plug-
The Script Wizard launches Visual Studio.
- Edit the source code file according to your needs. You can use Visual Studio and its IntelliSense support for editing the file, but they are not required.
- You do not have to compile the plug-in, because Eclipse compiles it automatically on the fly.
Create a Binary Plug-in with the Script Wizard
To create a binary plug-in with the Script Wizard, follow these guidelines:
- From the Start menu, select Varian > Eclipse Scripting API > Eclipse Script Wizard.
- Enter a name for the new script.
- Select the Binary plug-in option.
- To select the location for storing the script, click Browse. By default, the script is stored in the user-specific Documents folder.
- Click Create.
Note
The Script Wizard creates the following folders in the location that you selected:
- Project folder: Contains a script-specific subfolder where the Microsoft Visual Studio project file and source code file are stored.
- Plugins folder: Contains the compiled plug-in dlls. From this folder, the dll can be loaded into Eclipse.
Note
The Script Wizard launches Visual Studio.
- Edit the source code file according to your needs.
- Compile the plug-in, for example, by using Visual Studio. The resulting plug-in dll is saved into the Plugins folder. Note that you can also use the MSBuild tool to compile the binary plug-in. For an example, see Chapter "Getting Started with The Eclipse Scripting API", section "Compile Example Scripts". For more information about MSBuild, refer to Microsoft documentation.
Create a Single-File Plug-in Manually
If you want to create a single-file plug-in without the Script Wizard, follow these guidelines. For an example of a source code file, see Chapter Getting Started with The Eclipse Scripting API.
Create an empty C# source code file.
Add the using statements for the System and System.Windows namespaces.
Add the using statements for the following namespaces:
- VMS.TPS.Common.Model.API
- VMS.TPS.Common.Model.Types
- Add a namespace called VMS.TPS.
- To the VMS.TPS namespace, add a public class called Script.
- To the Script class, add a constructor without parameters, and a method called Execute.
- Define the return type of the Execute method as void.
- To the
Execute
method, add the following parameters:
- The context of the running Eclipse instance. The parameter type is VMS.TPS.Common.Model.API.ScriptContext.
- A reference to the child window that Eclipse creates for the user interface components (optional). The parameter type is System.Windows.Window.
- You do not have to compile the plug-in, because Eclipse compiles it automatically on the fly.
Create a Binary Plug-in Manually
If you want to create a binary plug-in without the Script Wizard, follow these guidelines:
- In Microsoft Visual Studio, create a new Class Library project. Select x64 as the Solution Platform.
- Create the source code in the same way as for a single-file plug-in. For instructions, see Chapter "Creating Scripts", Section "Create a Single-File Plug-in Manually".
- Use the following file name extension for the dll: .esapi.dll. In this way, Eclipse recognizes the plug-in and can load it.
- Add references to the following class libraries of the Eclipse Scripting API:
- VMS.TPS.Common.Model.API.dll
- VMS.TPS.Common.Model.Types.dll.
Tip
On the basis of this information, the dll can access the Eclipse Scripting API. The assemblies are located under the installation directory of the Eclipse Scripting API, in the API subdirectory.
- Compile the plug-in into a .NET assembly (a dll), for example, by using Visual Studio.
For more information on how to create a .NET assembly and add references to class libraries, refer to Microsoft documentation.
Storing Plug-in Scripts
If you want to make the created scripts available for all workstations, store them into the System Scripts directory. The System Scripts directory is a shared directory on the Varian System server.
You can access the System Scripts directory by clicking the Open Directory button in the Scripts dialog box.
Creating Stand-Alone Executable Applications
The following sections give you step-by-step instructions on creating stand-alone executables supported by the Eclipse Scripting API.
Create a Stand-Alone Executable with the Script Wizard
To create a stand-alone executable with the Script Wizard, follow these guidelines:
- From the Start menu, select Varian > Eclipse Scripting API > Eclipse Script Wizard.
- Enter a name for the new script.
- Select the Standalone executable option.
- To select the location for storing the script, click Browse.
- Click Create.
- The Script Wizard creates a Projects folder in the location that you selected. The folder contains a script-specific subfolder where the Microsoft Visual Studio project file and source code file are stored. The Script Wizard launches Visual Studio.
- Edit the source code file according to your needs.
Create a Stand-Alone Executable Manually
If you want to create stand-alone executables without the Script Wizard, follow these guidelines:
In Microsoft Visual Studio, create a new project file for the executable. Select x64 as the Solution Platform.
Add references to the following class libraries of the Eclipse Scripting API:
- VMS.TPS.Common.Model.API.dll
- VMS.TPS.Common.Model.Types.dll.
Tip
On the basis of this information, the executable can access the Eclipse Scripting API. The assemblies are located under the installation directory of the Eclipse Scripting API, in the API subdirectory.
- In the main method of the executable file, use the static CreateApplication method to create an instance of the VMS.TPS.Common.Model.API.Application class. This class represents the root object of the data model. The CreateApplication method also initializes the Eclipse Scripting API.
- Dispose of the instance when the stand-alone executable exits to free the unmanaged resources in the Eclipse Scripting API. For more information on disposing of objects, refer to Microsoft documentation of the IDisposable interface.
- Use a single-threaded apartment (STA) as the COM threading model of the executable. The Eclipse Scripting API must only be accessed from a single thread that runs in the default application domain. For more information about threading and application domains, refer to Microsoft documentation.
The following is the code for a sample stand-alone executable in C# language:
using System;
using System.Linq;
using System.Text;
using System.Collections.Generic;
using VMS.TPS.Common.Model.API;
using VMS.TPS.Common.Model.Types;
namespace StandaloneExample
{
class Program
{
[STAThread]
static void Main(string[] args)
{
try
{
using (Application app = Application.CreateApplication())
{
Execute(app);
}
}
catch (Exception e)
{
Console.Error.WriteLine(e.ToString());
}
}
static void Execute(Application app)
{
string message =
"Current user is " + app.CurrentUser.Id + "\n\n" +
"The number of patients in the database is " +
app.PatientSummaries.Count() + "\n\n" +
"Press enter to quit...\n";
Console.WriteLine(message);
Console.ReadLine();
}
}
}
Figure 12 Sample Code for Stand-alone Executable
- Compile the project. The stand-alone executable is ready to be run.
For more information on creating and compiling .NET applications, refer to Microsoft documentation.
Changing Scripts to Be Write-Enabled
You can create and save write-enabled scripts as described below when your installation includes the Eclipse Automation license.
- Create a script with the Eclipse Script Wizard as described in the following sections:
Create a Binary Plug-in with the Script Wizard
Create a Stand-alone Executable with the Script Wizard
- Add the following line of code above the namespace declaration:
[assembly: ESAPIScript(IsWriteable = true)]
- Add a call to the BeginModifications method of the Patient class as shown in the following code example:
using System;
using System.Linq;
using System.Text;
using System.Collections.Generic;
using VMS.TPS.Common.Model.API;
using VMS.TPS.Common.Model.Types;
[assembly: ESAPIScript(IsWriteable = true)]
namespace VMS.TPS
{
class Script
{
public Script()
{
}
public void Execute(ScriptContext context, System.Windows.Window window)
{
Patient patient = context.Patient;
//
// BeginModifications will throw an exception if the system is not
// configured for research use or system is a clinical system and
// the script is not approved.
//
patient.BeginModifications();
//
// After calling BeginModifications successfully it is possible
// to modify patient data.
//
if (patient.CanAddCourse())
{
// E.g. the script adds a new course
Course newCourse = patient.AddCourse();
// Continue with other changes...
}
}
}
}
Figure 13 Sample Code for Write-enabled script
- Save or discard modifications as follows:
In a stand-alone executable script, use the Application class:
To save the modifications to the database, call the SaveModifications method.
To discard the modifications, close the patient by calling the ClosePatient method. You can then open the patient again if the script still needs to access the patient data.
A plug-in script (single-file or binary) modifies the current data context of the Eclipse Treatment Planning application:
- After the script has been executed, save or discard the modifications in the Eclipse Treatment Planning user interface in the same way as any other change.