Sample Script to Modify Current Layer When Executing Command


Sample codes for the script to modify a layer named "Dimension" to the current layer when the Dimension command is executed are shown below.
When exiting the command, return the current layer to the previous status.
It is necessary to preliminarily prepare a layer named "Dimension."

A script for the event to start a command

Create a script for the event to start a command.

  1. Create a new file in a text editor such as Notepad.
  2. Copy the following codes and paste them on a text editor.
    // Retrieve current drawing.
    var doc = Application.ActiveDocument;
    
    // Retrieve the current drawing layer table.
    var layerTable = doc.LayerTable;
    
    // Retrieve a unique name for the command executed.
    var currentCommand = Application.CommandManager.CurrentCommand;
    if(currentCommand == null)
    	return;
    var commandName = currentCommand.UniqueName;
    
    // Check that the command unique name is "RootPro.Shape.*Dimension" (* is any alphabet).
    System.Text.RegularExpressions.Regex r =
        new System.Text.RegularExpressions.Regex(
            @"RootPro\.Shape\.[A-Za-z]+Dimension",
            System.Text.RegularExpressions.RegexOptions.None);
    
    System.Text.RegularExpressions.Match m = r.Match(commandName);
    while (m.Success)
    {
        // Retrieve the layer named "Dimension."
        Layer layer = layerTable.RootLayer.ChildLayers["Dimension"];
        if(layer != null)
        {
            // Save the current layer name.
            //  Variables are System.Collections.Generic.Dictionary type global variables shared by all scripts.
            // Use it to pass values between different events.
            Variables["OldCurrentLayer"] = layerTable.CurrentLayer.Name;
    
            // UNDO process will be started.
            doc.UndoManager.BeginUndoUnit();
    
            // Modify the layer named "Dimension" to the current layer.
            layerTable.CurrentLayer = layer;
    
            // UNDO process will be finished.
            doc.UndoManager.EndUndoUnit();
        }
        break;
    }
    
  3. Create a folder with the name "Command.Begin" 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
    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."

A script for the event to end a command

Create a script for the event to end a command.

  1. Create a new file in a text editor such as Notepad.
  2. Copy the following codes and paste them on the text editor.
    // Retrieve the current drawing.
    var doc = Application.ActiveDocument;
        
    // Retrieve the current drawing layer table.
    var layerTable = doc.LayerTable;
    
    // Retrieve a unique name for the command executed.
    var commandName = Application.CommandManager.CurrentCommand.UniqueName;
    
    // Check that the command unique name is "RootPro.Shape.*Dimension" (* is any alphabet).
    System.Text.RegularExpressions.Regex r =
        new System.Text.RegularExpressions.Regex(
            @"RootPro\.Shape\.[A-Za-z]+Dimension",
            System.Text.RegularExpressions.RegexOptions.None);
    
    System.Text.RegularExpressions.Match m = r.Match(commandName);
    while (m.Success)
    {
        // Retrieve the old current layer name from a global variable.
        // Variables are System.Collections.Generic.Dictionary type global variables shared by all scripts.
        // Use it to pass values between different events.
        if(Variables.ContainsKey("OldCurrentLayer"))
        {
            string layerName = (string)Variables["OldCurrentLayer"];
    
            // Return to the old current layer.
            Layer layer = layerTable.RootLayer.ChildLayers[layerName];
            if(layer != null)
            {
                // UNDO process will be started.
                doc.UndoManager.BeginUndoUnit();
    
                layerTable.CurrentLayer = layer;
    
                // UNDO process will be finished.
                doc.UndoManager.EndUndoUnit();
            }
        }
        break;
    }
    
  3. Create a folder with the name "Command.End" 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
    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."

Checking script operation

  1. Start up RootPro CAD.
  2. Create a layer with the name of "Dimension," and modify the current layer to another layer.
  3. Execute the dimension command.
    The current layer will be automatically modified to the layer with the name of "Dimension."
  4. Exit the dimension command.
    The current layer will be automatically modified to the previous status.
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