Compost4 Scripting: Difference between revisions
No edit summary |
|||
| (7 intermediate revisions by 3 users not shown) | |||
| Line 48: | Line 48: | ||
'''RegisterMenuItem''' | '''RegisterMenuItem''' | ||
void RegisterMenuItem(string menuName, string functionName) | void RegisterMenuItem(string menuName, string functionName) | ||
void RegisterMenuItem(ScriptMenu.MenuItem | void RegisterMenuItem(ScriptMenu.MenuItem parent, string menuName, string functionName) | ||
void RegisterMenuItem(ScriptMenu.MenuItem | void RegisterMenuItem(ScriptMenu.MenuItem parent, 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 ''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 | ||
| Line 92: | Line 92: | ||
* '''ScriptAPI.JumpToProgSubroutine(subName)''' Jumps the editor to the specified sub name. Works identically to the right mouse menu item. | * '''ScriptAPI.JumpToProgSubroutine(subName)''' Jumps the editor to the specified sub name. Works identically to the right mouse menu item. | ||
* '''ScriptAPI.SetPostMDD(newMDD)''' Sets the post processor to use the specified MDD filename, Works identically to the menu item. | * '''ScriptAPI.SetPostMDD(newMDD)''' Sets the post processor to use the specified MDD filename, Works identically to the menu item. | ||
* '''ScriptAPI.GetCurrentProgName()''' Returns the name of prog for the current document if it's a post processor | * '''ScriptAPI.GetCurrentProgName()''' Returns the name of prog for the current document if it's a post processor. | ||
* '''ScriptAPI.GetCurrentMDDName()''' Returns the name of MDD associated with the current document if it's a post processor | * '''ScriptAPI.GetCurrentMDDName()''' Returns the name of MDD associated with the current document if it's a post processor. | ||
* '''ScriptAPI.GetCurrentVMMName()''' Returns the name of VMM attached to the MDD associated with the current document if it's a post processor | * '''ScriptAPI.GetCurrentMDDVersion()''' Returns the version of MDD associated with the current document if it's a post processor. | ||
* '''ScriptAPI.GetCurrentMDDRevision()''' Returns the revision of MDD associated with the current document if it's a post processor | |||
* '''ScriptAPI.GetCurrentVMMName()''' Returns the name of VMM attached to the MDD associated with the current document if it's a post processor. | |||
* '''ScriptAPI.GetCurrentVMMVersion()''' Returns the version of VMM attached to the MDD associated with the current document if it's a post processor. | |||
====Editor Menu Functions==== | ====Editor Menu Functions==== | ||
| Line 116: | Line 119: | ||
* '''ScriptAPI.GetSelectionStart()''' Returns the starting position of the selection in the current document. | * '''ScriptAPI.GetSelectionStart()''' Returns the starting position of the selection in the current document. | ||
* '''ScriptAPI.GetSelectionEnd()''' Returns the ending position of the selection in the current document. | * '''ScriptAPI.GetSelectionEnd()''' Returns the ending position of the selection in the current document. | ||
* '''ScriptAPI.GetSelectedText()''' Returns the text that is selected in the current document. | |||
* '''ScriptAPI.SelectText(start, end)''' Selects the text in the specified range in the current document. | * '''ScriptAPI.SelectText(start, end)''' Selects the text in the specified range in the current document. | ||
* '''ScriptAPI.FindText(text, select)''' Finds the specified text in the current document from the start of the file and returns the position of it, if the select argument is true then it also selects the found text. | * '''ScriptAPI.FindText(whatToFind, select)''' Finds the specified text in the current document from the start of the file and returns the position of it, if the select argument is true then it also selects the found text. | ||
* '''ScriptAPI.FindText(whatToFind, select, wholeWord, matchCase)''' Finds the specified text in the current document from the start of the file and returns the position of it, checking for the entire word if '''wholeWord''' is true, and matching the case if '''matchCase''' is true, if the select argument is true then it also selects the found text. | |||
* '''ScriptAPI.FindText(whatToFind, startingFrom, endingAt, wholeWord, matchCase, searchUp, select)''' Finds the specified text in the specified range in the current document from the start of the file and returns the position of it, checking for the entire word if '''wholeWord''' is true, and matching the case if '''matchCase''' is true, and searching in the direction specfied by '''searchUp''', if the select argument is true then it also selects the found text. | |||
* '''ScriptAPI.FindText(whatToFind, whereToFindIt, wholeWord, matchCase, searchUp)''' Finds the specified text in the specified range (using a ScintillaNET.Range object) in the current document from the start of the file and returns the position of it, checking for the entire word if '''wholeWord''' is true, and matching the case if '''matchCase''' is true, and searching in the direction specfied by '''searchUp'''. | |||
* '''ScriptAPI.InsertText(text, position)''' Inserts the specified '''text''' into the current document at the specified '''position''' and returns the end position of the inserted text. | * '''ScriptAPI.InsertText(text, position)''' Inserts the specified '''text''' into the current document at the specified '''position''' and returns the end position of the inserted text. | ||
* '''ScriptAPI.GetTextFromRange(start, end)''' Returns the text in the specified range in the current document. | |||
* '''ScriptAPI.GetCurrentTextPostion()''' Returns the current position of the cursor in the current document. | |||
* '''ScriptAPI.PositionIsEndOfText(postion)''' Returns true if the specified position is the end of the current document. | |||
====Bookmark Functions==== | ====Bookmark Functions==== | ||
| Line 145: | Line 155: | ||
These functions are simply common things that scripts may need to do. | These functions are simply common things that scripts may need to do. | ||
* '''ScriptAPI.ShowMessageBox(message)''' Brings up a pop-up message box with the specified text | * '''ScriptAPI.ShowMessageBox(message)''' Brings up a pop-up message box with the specified text. | ||
* '''ScriptAPI.GetKeywords()''' Returns a copy of the current keyword list as a KeyworList object | * '''ScriptAPI.GetKeywords()''' Returns a copy of the current keyword list as a KeyworList object. | ||
* '''ScriptAPI.GetCurrentUserName()''' Returns the name of the user currently running ComPost | * '''ScriptAPI.GetCurrentUserName()''' Returns the name of the user currently running ComPost. | ||
* '''ScriptAPI.GetParentHandle()''' Returns a .NET Form object for the application frame, useful for attaching customc GUI elements to the running application. | |||
* '''ScriptAPI.GetGibbsCAMDirectory()''' Returns the path to the linked version of GibbsCAM. | |||
* '''ScriptAPI.GetGibbsCAMVersionString()''' Returns the version of the linked version of GibbsCAM. | |||
* '''ScriptAPI.GetCompostVersionString()''' Returns the version of the running version of ComPost. | |||
* '''ScriptAPI.RefreshDocumentDialogs()''' Forces all dialog boxes associated with the current document to refresh and check for data changes. | |||
===Section List=== | |||
These functions allow access to the section (sub/op) list | |||
* '''ScriptAPI.GetCurrentSectionFilter()''' Returns the current filter in use by the section list. | |||
* '''ScriptAPI.SetCurrentSectionFilter(filter)''' Sets the filter used by the section list, returns false if the list is not displayed. | |||
* '''ScriptAPI.GetCurrentFilteredSections()''' Returns an array of the currently displayed section named based on the current filter, null if the section list is not displayed. | |||
* '''ScriptAPI.GetDocumentSections()''' Returns an array of the current documents sections even if the section dialog is not up. Always returns an unfiltered list. | |||
* '''ScriptAPI.JumpToSection(section)''' Jumps the document selection to the specified section. | |||
===Document Types=== | ===Document Types=== | ||
| Line 167: | Line 191: | ||
* '''ScriptAPI.GetCurrentMachDef()''' will return the MDD data for the current document if it is a POST | * '''ScriptAPI.GetCurrentMachDef()''' will return the MDD data for the current document if it is a POST | ||
* '''ScriptAPI.GetCurrentProgWindow().GetProgText()''' will return the Prog data for the current document if it is a POST | * '''ScriptAPI.GetCurrentProgWindow().GetProgText()''' will return the Prog data for the current document if it is a POST | ||
===Testing=== | |||
The API includes a few features that can be used to test parts and posts in GibbsCAM. | |||
* '''ScriptAPI.PostAllPartsWithAllPosts(DirectoryInfo scriptDir, DirectoryInfo partDir, DirectoryInfo outDir, bool doCompare, FileInfo summary, FileInfo errors)''' Posts all the parts in the specified parts dir with all the post in the same dir, putting the output in the specified outDir. Uses the specified scritpDir as a temporary working folder. | |||
* '''TestingResults ScriptAPI.TestPartWithPost'''(string part, string baseline, string postProcessor) Posts a single part with a single post processor and compares it to the specified baseline file. Returns the results of the test as a TestingResults structure. | |||
===Using the Script Help Plug-in=== | |||
ComPost4 ships with a plug-in that will generate basic ScriptAPI documentation. It can be accessed from the Scripts menu, Script Help, Toggle Script Help. This will generate HTML documentation for all scripting related APIs and show them in an internal mini-browser. The documentation currently is just the function names and arguments, but additional information will be added later. | |||
===Using Visual Studio for online help=== | ===Using Visual Studio for online help=== | ||
Latest revision as of 16:40, 3 June 2015
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
Functions are added to the ScriptAPI as needed by script makers and post developer needs.
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
This will provide access to the ScriptAPI class defined above.
ScriptAPI Function Reference
Menu Functions
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 parent, string menuName, string functionName) void RegisterMenuItem(ScriptMenu.MenuItem parent, 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 Menu Folder functions
FindOrCreateMenuFolder ScriptMenu.MenuItem FindOrCreateMenuFolder(ScriptMenu.MenuItem parent, string menuName) Will look to see if the desired menu folder already exists, or will create it if it does not.
CreateMenuFolder ScriptMenu.MenuItem CreateMenuFolder(string menuName) ScriptMenu.MenuItem CreateMenuFolder(ScriptMenu.MenuItem parent, string menuName) Creates a menu folder with the specified name attached to the root menu or the parent if defined.
Document Callbacks
Like menu callbacks a script can hook itself into document events as well. These events are called when the user does the defined action.
- ScriptAPI.RegisterOpenCallback(functionName) Calls the specified function after any document is opened.
- ScriptAPI.RegisterSaveCallback(functionName) Calls the specified function before any document is saved.
- ScriptAPI.RegisterCompileCallback(functionName) Calls the specified function before post processor is compiled. Posts may be compiled before save if the prog has been edited.
- ScriptAPI.RegisterDocumentChangedCallback(functionName) Calls the specified function after a document window is made the current, or the last document is closed.
Common Tasks
Action Functions
These functions trigger actions within the compost application itself, such as opening and closing files.
- ScriptAPI.OpenDocument(filename) Opens the file at the specified path as a new document.
- ScriptAPI.CloseDocument(document) Closes the specified document window, uses the current document if no document is specified.
- ScriptAPI.DirtyDocument(document) Marks the specified document document as dirty (needing to be saved), uses the current document if no document is specified.
- ScriptAPI.SaveDocument(document) Saves the specified document, uses the current document if no document is specified.
- ScriptAPI.SaveDocumentAs(document, newFileName) Saves the specified document as the defined file name, uses the current document if no document is specified.
- ScriptAPI.ChangeToDocument(document) Makes the specified document be the current document.
Post Processor Functions
These functions deal with items specific to Post Processor Documents.
- ScriptAPI.BuildPost() Triggers a compile and save of the current post processor, returns true if there are no errors.
- ScriptAPI.ShowCompostKeywordHelp(word) Triggers the help system for the specified keyword, may open a browser window for wiki access.
- ScriptAPI.JumpToProgSubroutine(subName) Jumps the editor to the specified sub name. Works identically to the right mouse menu item.
- ScriptAPI.SetPostMDD(newMDD) Sets the post processor to use the specified MDD filename, Works identically to the menu item.
- ScriptAPI.GetCurrentProgName() Returns the name of prog for the current document if it's a post processor.
- ScriptAPI.GetCurrentMDDName() Returns the name of MDD associated with the current document if it's a post processor.
- ScriptAPI.GetCurrentMDDVersion() Returns the version of MDD associated with the current document if it's a post processor.
- ScriptAPI.GetCurrentMDDRevision() Returns the revision of MDD associated with the current document if it's a post processor
- ScriptAPI.GetCurrentVMMName() Returns the name of VMM attached to the MDD associated with the current document if it's a post processor.
- ScriptAPI.GetCurrentVMMVersion() Returns the version of VMM attached to the MDD associated with the current document if it's a post processor.
Editor Menu Functions
These functions allow access to items from the Edit Menu and always work on the current document.
- ScriptAPI.Undo() Triggers the Undo menu item.
- ScriptAPI.Redo() Triggers the Redo menu item.
- ScriptAPI.DocumentCut() Triggers the Cut menu item.
- ScriptAPI.DocumentCopy() Triggers the Copy menu item.
- ScriptAPI.DocumentPaste() Triggers the Paste menu item.
- ScriptAPI.DocumentSelectAll() Triggers the Select All menu item.
- ScriptAPI.DocumentDeleteSelection() Triggers the Delete menu item.
Editor Functions
These functions are used to manipulate the text and text editor window of the current document.
- ScriptAPI.GoToDocumentPosition(position) Jumps the editor to the specified position in the current document.
- ScriptAPI.GoToDocumentLine(line) Jumps the editor to the specified line in the current document.
- ScriptAPI.GetSelectionStartingLine() Returns the starting line of the selection in the current document.
- ScriptAPI.GetSelectionEndingLine() Returns the ending line of the selection in the current document.
- ScriptAPI.GetSelectionStart() Returns the starting position of the selection in the current document.
- ScriptAPI.GetSelectionEnd() Returns the ending position of the selection in the current document.
- ScriptAPI.GetSelectedText() Returns the text that is selected in the current document.
- ScriptAPI.SelectText(start, end) Selects the text in the specified range in the current document.
- ScriptAPI.FindText(whatToFind, select) Finds the specified text in the current document from the start of the file and returns the position of it, if the select argument is true then it also selects the found text.
- ScriptAPI.FindText(whatToFind, select, wholeWord, matchCase) Finds the specified text in the current document from the start of the file and returns the position of it, checking for the entire word if wholeWord is true, and matching the case if matchCase is true, if the select argument is true then it also selects the found text.
- ScriptAPI.FindText(whatToFind, startingFrom, endingAt, wholeWord, matchCase, searchUp, select) Finds the specified text in the specified range in the current document from the start of the file and returns the position of it, checking for the entire word if wholeWord is true, and matching the case if matchCase is true, and searching in the direction specfied by searchUp, if the select argument is true then it also selects the found text.
- ScriptAPI.FindText(whatToFind, whereToFindIt, wholeWord, matchCase, searchUp) Finds the specified text in the specified range (using a ScintillaNET.Range object) in the current document from the start of the file and returns the position of it, checking for the entire word if wholeWord is true, and matching the case if matchCase is true, and searching in the direction specfied by searchUp.
- ScriptAPI.InsertText(text, position) Inserts the specified text into the current document at the specified position and returns the end position of the inserted text.
- ScriptAPI.GetTextFromRange(start, end) Returns the text in the specified range in the current document.
- ScriptAPI.GetCurrentTextPostion() Returns the current position of the cursor in the current document.
- ScriptAPI.PositionIsEndOfText(postion) Returns true if the specified position is the end of the current document.
Bookmark Functions
These functions provide access to the bookmark system.
- ScriptAPI.LineHasBookmark(line) Returns true if the line in the current document has a bookmark.
- ScriptAPI.SetLineBookmark(line) Sets a bookmark on the specified line and returns true if it did not already have one.
- ScriptAPI.ClearLineBookmark(line) Clears the bookmark from the specified line and returns true if the line had a bookmark.
- ScriptAPI.GetBookmarkedLines() Returns an array of lines with bookmarks from the current document.
- ScriptAPI.GoToNextBookmark() Jumps the editor to the next bookmark.
- ScriptAPI.GoToPreviousBookmark() Jumps the editor to the previous bookmark.
Error Log Functions
These functions allow access to the error list that is provided with each window. Error list items can be linked to a specific location in the document so that when they are double clicks the editor will jump to that potion.
- ScriptAPI.AddInfoError(description, offset) Adds an error to the current documents error list with the "Information" heading, the specified description, and linked to the defined offset into the text file.
- ScriptAPI.AddWarningError(description, offset)Adds an error to the current documents error list with the "Warning" heading, the specified description, and linked to the defined offset into the text file.
- ScriptAPI.AddSyntaxError(description, offset) Adds an error to the current documents error list with the "SyntaxError" heading, the specified description, and linked to the defined offset into the text file.
- ScriptAPI.AddError(errorType, description, offset) Adds an error to the current documents error list with the heading specified by the CompilerError.ErrorType enumeration, the specified description, and linked to the defined offset into the text file.
- ScriptAPI.AddError(error) Adds an error to the current documents error list as defined in the specified CompilerError object.
- ScriptAPI.AddErrors(errorList) Adds a list of errors to the current documents error list as defined by the specified CompilerError objects.
- ScriptAPI.GetCurrentErrors() Returns an array of CompilerError objects that are attached to the current document.
- ScriptAPI.ClearErrors(hide) Removes all errors for the current document, and hides the error window if the hide argument is true.
Utilities
These functions are simply common things that scripts may need to do.
- ScriptAPI.ShowMessageBox(message) Brings up a pop-up message box with the specified text.
- ScriptAPI.GetKeywords() Returns a copy of the current keyword list as a KeyworList object.
- ScriptAPI.GetCurrentUserName() Returns the name of the user currently running ComPost.
- ScriptAPI.GetParentHandle() Returns a .NET Form object for the application frame, useful for attaching customc GUI elements to the running application.
- ScriptAPI.GetGibbsCAMDirectory() Returns the path to the linked version of GibbsCAM.
- ScriptAPI.GetGibbsCAMVersionString() Returns the version of the linked version of GibbsCAM.
- ScriptAPI.GetCompostVersionString() Returns the version of the running version of ComPost.
- ScriptAPI.RefreshDocumentDialogs() Forces all dialog boxes associated with the current document to refresh and check for data changes.
Section List
These functions allow access to the section (sub/op) list
- ScriptAPI.GetCurrentSectionFilter() Returns the current filter in use by the section list.
- ScriptAPI.SetCurrentSectionFilter(filter) Sets the filter used by the section list, returns false if the list is not displayed.
- ScriptAPI.GetCurrentFilteredSections() Returns an array of the currently displayed section named based on the current filter, null if the section list is not displayed.
- ScriptAPI.GetDocumentSections() Returns an array of the current documents sections even if the section dialog is not up. Always returns an unfiltered list.
- ScriptAPI.JumpToSection(section) Jumps the document selection to the specified section.
Document Types
Compost currently supports 4 types of documents, POST, NCF(g-code), and PYTHON are the normal editable types. The 4th type is a DiffWindow and is not editable in the normal sense. Each of these document types has a common set of text editing functions as well as a number of document specific attributes.
- ScriptAPI.CurrentDocIsPost() Returns true if the current document is a post processor
- ScriptAPI.CurrentDocIsNCF() Returns true if the current document is a NCF file
- ScriptAPI.CurrentDocIsPython() Returns true if the current document is a python script
- ScriptAPI.CurrentDocIsEditor() Returns true if the document has a text editor, true for all documents except DiffWindow. This command is useful to check if it's OK to call text editor functions.
Accessing Raw Documents
The API class allows scripts to access any open document and make modifications to it. Many Scripts do not need direct access to the document and can use the Utility functioins abeove. More complex scripts or plug-ins may need the power that direct access provides.
- ScriptAPI.GetCurrentDocument() Returns a class that represents the current document, this can be any of the document types that Compost Supports
- ScriptAPI.GetOpenDocuments() Returns the list of document classes that are currently open
- ScriptAPI.GetCurrentDocTextArea() will return the text editor class for the current document, this includes things such as the current text selection
- ScriptAPI.GetCurrentForm() will return the form data for the current document if it is a POST
- ScriptAPI.GetCurrentMachDef() will return the MDD data for the current document if it is a POST
- ScriptAPI.GetCurrentProgWindow().GetProgText() will return the Prog data for the current document if it is a POST
Testing
The API includes a few features that can be used to test parts and posts in GibbsCAM.
- ScriptAPI.PostAllPartsWithAllPosts(DirectoryInfo scriptDir, DirectoryInfo partDir, DirectoryInfo outDir, bool doCompare, FileInfo summary, FileInfo errors) Posts all the parts in the specified parts dir with all the post in the same dir, putting the output in the specified outDir. Uses the specified scritpDir as a temporary working folder.
- TestingResults ScriptAPI.TestPartWithPost(string part, string baseline, string postProcessor) Posts a single part with a single post processor and compares it to the specified baseline file. Returns the results of the test as a TestingResults structure.
Using the Script Help Plug-in
ComPost4 ships with a plug-in that will generate basic ScriptAPI documentation. It can be accessed from the Scripts menu, Script Help, Toggle Script Help. This will generate HTML documentation for all scripting related APIs and show them in an internal mini-browser. The documentation currently is just the function names and arguments, but additional information will be added later.
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.
TODO
- Document Compiler Hooks (LexemeProcessedHandlers)
- Document Mode Hooks (ModeCopyHandler)
- Document Common Scintilla Functions
- Document Common MDD Functions