API Script Example: COGO Point Generation

Creates new nodes in Autodesk Dynamo for Civil 3D for creating points, adding points to a point group, and creating a surface from a point group.

Sample Dynamo script: CoGoPoints.dyn

Sample project: CoGoPoints.dwg

  1. Create a new C# project called CoGoPoints Class Library(.Net Framework) with .NET Framework 4.7.
  2. Add files: Point.cs, PointGroup.cs, and Surface.cs to the project.
  3. Add references for these files such as AeccDbMgd.dll and so on from the Civil 3D installer path.
  4. Build the solution and then generate CoGoPoints.dll.
  5. Run Civil 3D and open CoGoPoints.dwg.
  6. Click Manage tab Visual Programming panel Dynamo .
  7. Select File Import Library and select the CoGoPoints.dll.

    The nodes that are created when you import CoGoPoints.dll are shown in the following illustration.

  8. Open and run the CoGoPoints.dyn script.

    COGO points are created in the drawing and added to a point group, and a surface is created from the point group.

Two classes are provided in Dynamo to create new nodes and add a trace mechanism to the new nodes.
  1. Autodesk.AutoCAD.DynamoNodes.ObjectBase: Provides the encapsulation of Autodesk.AutoCAD.DatabaseServices.DBObject
  2. Autodesk.AutoCAD.DynamoNodes.Object: Derived from Autodesk.AutoCAD.DynamoNodes.ObjectBase to extend the properties or functions according to Autodesk.AutoCAD.DatabaseServices.Object

Dynamo offers two ways to realize a trace mechanism. One is to use the existing method (CommonConstruct) when creating new nodes. The other is to write trace code with a template.

To realize a trace mechanism for new nodes, classes must be derived from Autodesk.AutoCAD.DynamoNodes.ObjectBase or Autodesk.AutoCAD.DynamoNodes.Object. A traced node must have an output of the created class of the node to make the trace valid.

Using Trace Method (CommonConstruct)

The CommonConstruct function is created in both Autodesk.AutoCAD.DynamoNodes.ObjectBase and Autodesk.AutoCAD.DynamoNodes.Object.

Autodesk.AutoCAD.DynamoNodes.ObjectBase Autodesk.AutoCAD.DynamoNodes.Object

You can create classes to inherit either Autodesk.AutoCAD.DynamoNodes.ObjectBase or Autodesk.AutoCAD.DynamoNodes.Object. The methods above can be called when you create new nodes according to the inherited relationship. For example, notice the following node PointGroup.CreateFromPoints.

The node creates a point group in Civil 3D from a list of points.

Autodesk.Civil.DatabaseServices.PointGroup defines a point group in Civil 3D derived from Autodesk.AutoCAD.DatabaseServices.Object. A new class called PointGroup derived from Autodesk.AutoCAD.DynamoNodes.Objectbase needs to be created to encapsulate the Civil 3D point group type. The attribute DynamoServices.RegisterForTrace can be added to the class PointGroup to realize a trace mechanism. This is not required because Dynamo supports this by default, but it is a good coding convention.

The CreateFromPoints node can now be defined in PointGroup. Because of the inherent relationship, CommonConstruct in Autodesk.AutoCAD.DynamoNodes.ObjectBase can be called.

The TNode method is the type of the node and required to inherit Autodesk.AutoCAD.DynamoNodes.ObjectBase.

In this example:
TNode = CogoPoints.PointGroup
TObject is the type of the AutoCAD object required to inherit Autodesk.AutoCAD.DatabaseServices.DBObject.
In this example:
TObject = Autodesk.Civil.DatabaseServices.PointGroup
The objectCreator function creates an object. In this example it is defined as the creation of a new point group.

The objectUpdater function updates an object. In this example it is defined as the update of an existing point group.

The CommonConstruct function tries to find existing objects from the traced handle. When found, it updates with the call objectUpdater with isExisting==true. If updating fails, objectCreator calls to create a new object and then the objectUpdater calls isExisting==false.

The CommonConstruct function tries to update existing objects in the database with the same traced handle value. If that fails, it will create a new object. If your object does not support updates you can return isExisting==false. This means you will creating a new object and delete the old object. CommonConstruct will still try to maintain the same handle value in this case.

The complete sample codes are displayed in PointGroup.cs and the CommonConstruct method in Autodesk.AutoCAD.DynamoNodes.Object is as described above.

Using a Trace Code Template

You can write trace code with the following template. However, the class for new nodes still needs to inherit Autodesk.AutoCAD.DynamoNodes.Object or Autodesk.AutoCAD.DynamoNodes.Entity.

You can fill in both the create and the update code to realize the trace code in the above template.

About DocumentContext

DocumentContext ensures that the document lock is obtained for read/write access of the database and when making a transaction to open database objects. Any node that wants to read or write database objects, needs to use the above method.