Creating Script Operating with Certain Event


You can execute a script triggered by an event such as when a drawing file is saved, or a command is executed.

  1. Describe the process to be created in C# programming language in a text file, just like an ordinary script.
  2. Create a folder with an event name in the scripts folder, and save a file with the extension of .csx in the folder.
    The scripts folder is the one to be opened by clicking in [Open Scripts Folder] on [Scripts] menu in RootPro CAD.
    Any name can be used for a script file name.
    Supplemental
    To save a script of LayerAdded event in Application class, create a folder named "Application.LayerAdded," and save a script file in the folder.
    For event names in each class, see RootPro CAD SDK Help.
    The folder with event name can contain multiple script files. When an event occurs, the script files in the folder will be executed in order.
    Files must be saved using Unicode (UTF-8) as character codes. In Notepad, modify [Encoding] box on the left of [Save] button on [Save As] dialog box to "UTF-8."
  3. Restart RootPro CAD.
    Then the script will be executed when an event occurs.

Event Script Samples

Sample codes for a script to display the message box when a layer is added are shown below.
When creating a folder named "Application.LayerAdded" in the scripts folder and saving a script file under a desired name in the folder, the script will be executed when a layer is added.
The lines starting with "//" are comments, which are not required to input.

// To refer to assemblies other than those in RootPro CAD, describe "
#r "Assembly Name"". #r "System.Windows.Forms" 

// To load another script file, describe as "#load "File Path Name"". 
// Specify a file path name in either an absolute path or a relative path. 
//#load "samplescript.csx" 

// To use a special type without specifying the name space, describe "using NameSpaceName." 
using System.Windows.Forms; 

// To call a property or method in RootPro CAD, use Application global object. 
// To retrieve the current drawing, describe as follows. 
var doc = Application.ActiveDocument; 

// To retrieve a parameter of the event, use EventSender and EventArgs global objects. 
// EventSender and EventArgs are of System.Object type and System.EventArgs type, respectively. 
// To retrieve an event parameter with LayerAdded event of Application class, describe as follows. 
var e = (LayerEventArgs)EventArgs; 

// The message box will be displayed. 
// MessageBox class is that of System.Windows.Forms name space standardly provided by .NET Framework. 
MessageBox.Show(string.Format("Layer "{0}" has been added." , e.Layer.Name));
Supplemental
Script functions are only used in RootPro CAD Professional. RootPro CAD Free cannot use these functions.
When modifying a code in an existing script file after starting up RootPro CAD, it will be immediately reflected without restarting RootPro CAD.

Related topics