Monday, August 10, 2009

Scripting (IronPython) exposing Naro document API

IronPython was integrated for some time to NaroCAD. But in fact it has two problems:
- it was not able to access NaroCAD shapes or anything from NaroCAD so it was not powerful enough to do something useful, excluding you did want to make a processing task using python
- IronPython implementation (and python in itself) do not work with Generics. So even you had access to Naro's document model, the Naro's extensible model was not accessible to IronPython script programmers.

This is why it was used lazy loading of IronPython as it does useless slowdown on startup of NaroCAD.

Right now both are addressed, exposing to you a document reference that you can create from it using a PyNodeUtil class a shape node. This is fairly important achievement as it can make easier without recompile NaroCAD to migrate your shapes if you can expose to IronPython a non generic class with your shapes.

Here is the proof of concept:
- import the assembly that you expose your shapes or whatever you want to expose like this:
- import the namespace of your class that define the assembly (which is equivalent with C#'s using):
from MyShapeCS.FileImporter import *
- create one instance of your class in IPy that is able to import your shapes. Import if needed that classes
stepImporter = StepFileImporter("MyStepFile.step")
- decide by your IronPython code which shape need to be created, etc.
... python code ...
- create one instance of the shape as PyNodeUtil and use it (I will create one extrude, with first child the shape with it's id 2, at height 32.5 and normal extrude)
shapeNode = PyNodeUtil(document, "Extrude");
shapeNode.SetDependReference(0, 2);
shapeNode.SetDependDouble(1, 32.5)
shapeNode.SetDependInteger(2, 0)
result = shapeNode.FunctionExecute()
document.Commit("Extrude added by scripting")

The result variable will have the bool value that will say if operation succeeds or not.

To see the results you have the Refresh button of scripting window that will update the tree of data and the view of Naro, and if you want low level debugging without updating the OCC view, in the View Menu there is an tool named "Debug Data Tree".

So, if you dislike C# and you like dynamic languages like Python you may do bridges between your application and Naro. Also may make to you to export Naro's object to your external tool without touching Naro's sourcecode.

No comments: