Compost4 Scripting
Overview
Compost 4 contains a built in scripting system built around the Iron Python(http://ironpython.net/) scripting language.
Scripts
Compost4 scripts are loaded from the following paths
- [USER PROFILE DIRECTORY]\AppData\Roaming\Gibbs\Compost4\Scripts
- [OS DRIVE]\ProgramData\Gibbs\Compost4\Scripts
- [APPLICATION INSTALL DIRECTORY]\Scripts
These scripts are scanned when the application is first started and loaded. The Scripts are loaded from the user directory first, then the Program Data directory, then finally the application installation location. If the same script is found in multiple places, the one that is in the User Folder is preferred over the one in the Program Data over the one in the Application Directory.
Script Types
There are 2 main types of scripts that can be made, simple scripts, and Callback Scripts.
Simple Scripts
Simple Scripts contain the code for a single function and are automatically added to the end of the Scripts menu in Compost Application. When executed from the menu the entirety of the script is run from the first command in a similar way to how post processors are run. These scripts are very similar to simple macros, when run they execute a set of predefined commands then exit.
Callback Scripts
A Callback Script is capable of much tighter integration with the Compost Editor, and works by implementing an Initialization function that is called on start-up and then registering one or more script functions with the system that can be called during various events. These events are how scripts can create complex menu structures with multiple options and install hooks into other features such as custom error checking and axis set building.
ScriptAPI
All Scripts have access to a Class that is called the ScriptAPI. This is a .net class that gives a script information about the current documents that are open as well as compost specific functions that they can call. Some examples include:
- Functions to create menus and sub-menus
- Access to the currently selected document
- Access to the currently selected POST form
- Access to the Prog code for the current POST
- Access to the MDD for the selected POST
- Functions to compile a post and display errors
- Functions to manipulate the text area for the current document
As compost is developed more functions and features will be added to this class to give scripts the powers they need. The All the data from the class can be seen in Visual Studio's object browser if Compost4.exe is included as a reference.
The Init Function
All Callback macros must implement the init system. This is done by creating the following function
def InitCompostScript(): return;
In this function you have access to the global variable:
ScriptAPI.API
This is where a script would register one or more menu names using a call to one of the RegisterMenuItem function variants
RegisterMenuItem void RegisterMenuItem(string menuName, string functionName) void RegisterMenuItem(ScriptMenu.MenuItem parrent, string menuName, string functionName) void RegisterMenuItem(ScriptMenu.MenuItem parrent, string menuName, string shortcutKey, string functionName)
The functionName item will be the name of a function in the script file that will be called when the menu is selected by the user
The parent should be set to null for top level menus or the result of a call to the CreateMenuFolder function
CreateMenuFolder ScriptMenu.MenuItem CreateMenuFolder(string menuName) ScriptMenu.MenuItem CreateMenuFolder(ScriptMenu.MenuItem parrent, string menuName)
Accessing Documents
Compost currently supports 4 types of documents, POST, GCODE, PYTHON, and TXT. Each of these document types has a common set of text editing functions as well as a number of document specific attributes.
The API class allows scripts to access any open document and make modifications to it. Some utility functions are provided for some of the more complicated document types (POST)
- ScriptAPI.API.CurrentDocument contains a class that represents the current document, this can be any of the document types that Compost Supports
- ScriptAPI.API.OpenDocuments contains a the list of document classes that are currently open
- ScriptAPI.API.CurrentDocTextArea() will return the text editor class for the current document, this includes things such as the current text selection
- ScriptAPI.API.CurrentForm() will return the form data for the current document if it is a POST
- ScriptAPI.API.CurrentMachDef() will return the MDD data for the current document if it is a POST
- ScriptAPI.API.GetCurrentProgWindow().GetProgText() will return the Prog data for the current document if it is a POST
Using Visual Studio for online help
While Compost4 is a good editor for it's own Python scripts it can be somewhat tricky to know all the things that can be done with the various objects in Scripting. We are working on a feature that will let Compost4 show the user what script commands,functions, and objects are available using online help and auto-complete but that feature will take some time.
Until we finish this feature it is possible to use the free version of Visual C# to inspect the ScriptAPI object in order to know what functions can be accessed.
The free version of Visual C# can be found here. http://www.visualstudio.com/downloads/download-visual-studio-vs
Once you have it installed get and open the sample plugin file from the Post SVN https://svn.gibbscam.com/svn/post_dev/compost4/Samples/Plugins/SampleCompostPlugin/
While you do not need to do anything specific with the plug-in, it is a simple way to access the object browser and view the contents of the Compost4 Assembly file.
Open the "references" section in the Solution list. Double click the "Compost4" item to open the Object Browser. Then simply scroll down to the "ComPost4.Scripting" Namespace and find the "ScriptAPI" object.
This object will show you all of the functions and methods that are available for use. Some functions will return references to other objects and you can use the object browser to locate information on them as well.