Creating Script


You can create a script using a text editor such as Notepad.

  1. Create a new file in a text editor such as Notepad.
  2. Describe a process to be executed in C# programming language.
  3. Save a file with the extension of .csx in the scripts folder.
    The scripts folder is the one to be opened by clicking in [Open Scripts Folder] on [Scripts] menu in RootPro CAD.
    The saved file name will be the menu name displayed on [Scripts] menu.
    Supplemental
    · 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".
  4. Restart RootPro CAD.
    The script added will be displayed on [Scripts] menu.
  5. Click the script menu.
    The process in the script will be executed.
  6. When there is an error in the script, the line number and error details will be displayed.
    After opening the script file using a text editor and modifying the error, execute the script again to check operation.
    Supplemental
    When modifying a code in the script, the modified content will be immediately reflected without restarting RootPro CAD.

Script Samples

Sample codes for a script to modify the color of the shape selected are shown below.
Saving the code as a file name of "Modify Line Color.csx" will display "Modify Line Color" in [Scripts] menu.
The lines starting with "//" are comments, which are not required to input.
Turn off the group selection mode in RootPro CAD beforehand.

// 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 a 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; 

// Retrieve the selected shape. 
var selectedShapes = doc.SelectionManager.SelectedShapes; 

// Using a language-integrated query LINQ for .NET Framework, you can easily retrieve an object with a certain condition in the collection. 
// It retrieves only basic shapes (lines, circles, etc.) from selected shapes. 
var basicShapes = from selectShape in selectedShapes 
				where selectShape.Shape is BasicShape 
				select (BasicShape)selectShape.Shape; 

if(basicShapes.Count() == 0) 
{ 
	// Displays the message box. 
	// MessageBox class is that of System.Windows.Forms name space standardly provided by .NET Framework. 
	MessageBox.Show("Select a basic shape such as a line and circle.") ; 
	return; 
} 

// UNDO process will be started. 
doc.UndoManager.BeginUndoUnit(); 

// Modify the color of basic shapes in the selected shape to red. 
foreach(var shape in basicShapes) 
{ 
	shape.ColorNumber = 2; 
} 

// UNDO process will be finished. 
doc.UndoManager.EndUndoUnit(); 

// The message box will be displayed. 
MessageBox.Show(string.Format("The color of {0} shapes has been modified." , basicShapes.Count()));
Supplemental
Script functions are only used in RootPro CAD Professional. RootPro CAD Free cannot use these functions.

Related topics