Compost4 Scripting

From Compost Wiki
Revision as of 21:23, 12 February 2014 by Jeffm (talk | contribs) (→Simple Scripts)
Jump to navigation Jump to search

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

  • [APPLICATION INSTAL DIRECTIORY]\Macros
  • [USER PROFILE DIRECTORY]\AppData\Roaming\Gibbs\Compost4\Macros

These scripts are scanned when the application is first started.

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 macros, when run they simply execute 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 InitCompostMacro():
 	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