Monday, March 1, 2010

How To Use NaroCAD To View A Custom Shape (Part II)

For building a shape the second part of this tutorial will target how to create your shapes in NaroCAD.

Functions
What is a function?
A function is a special entity (interpreter) that based on it's dependencies it builds the corresponding TopoDS_Shape.

To create your custom shape you will have to do the following steps:
- to register it so that NaroCAD will be aware of this new shape:
ShapeFunctions project have a class DefaultFunctions where you will need to register your class (similar with your AttributeInterpreter classes)
FunctionFactory.Register<Fillet2dFunction>();
- to inherit from class: FunctionBase
- to define there it's dependencies
- to say that when your function code is called, that based on dependencies, which is your resulting OpenCascade shape

Here is the shortest function definition that exists in NaroCAD's codebase that prove all the points (excluding the registering part and the adding under FunctionNames the Point const string.

public class PointFunction : FunctionBase
{
public PointFunction() : base(FunctionNames.Point)
{
Dependency.AddAttributeType(InterpreterNames.Point3D);
}

public override bool Execute()
{
OCgp_Pnt firstPoint = Dependency[0].Point3D.gpPnt;
Shape = new OCBRepBuilderAPI_MakeVertex(firstPoint).Shape();
return true;
//it should return false if the shape cannot be built
//with it's parameters
}
}

Some extra things you may find useful: Dependencies are added in a List, so they can be added dynamically as you go and they can be accessed by index. Any Dependency from already known types (you may need to customize your dependency if you will need to be notified on custom type from the wide range of supported types like Point3D, Real, Reference) can be easily accessed. Also, by adding dependencies, they will be automatically call the function. Parent property is the node that hosts the Node (see Part I to see what is Node) that contains the shape. This may be useful if you will want to check extra code or nodes that dependency do not set.

Adding to Lua as a custom command
Lua scripting works at the "function level" and it is just to hook functions.
In method: LuaNaroWrapper.RegisterToLua there are registered methods as follows:
Interpreter.RegisterFunction("point", this, this.GetType().GetMethod("Point"));
And bellow is shown the code of Point method:
public int Point(double x, double y, double z)
{
NodeBuilder builder = new NodeBuilder(_document, FunctionNames.Point);
builder.Dependency[0].Point3D = new Point3D(x, y, z);
builder.ExecuteFunction();
return builder.Node.Index;
}
The NodeBuilder class is a decorator that have a lot of setup code for function code like "auto-numbering" names. So, to use a function already made and to test if it works as expected, you can create easy a custom command and you can test it in Lua view.

No comments: