This is in continuation of Partha’s earlier posts on AutoCAD Map 3D API Training content: Part-1 , Part 2, Part-3 and Part-4. In this post, I will talk about the Geospatial geometry API. To start with let me state that there have been a few changes to the API organization over the years and currently, you will find all the APIs used for this post in the following assemblies:
All the selected assemblies above can be found in the Map 3D install folder or its subfolders.
Now, coming back to the API, the Geometry API is organized in a simple manner. There are two basic types. MgGeometry and MgGeometryComponent. MgGeometry is inherited by MgAggregateGeometry, MgCurve, MgPoint and MgRegion. MgGeometryComponent is inherited by MgCurveSegment and MgRing. In general as the names suggests, MgGeometry types represent the geometry itself and MgGeometryComponent types constitute (are contained by) the MgGeometry types:
The relationship between these types is explained in more detail in this presentation. A few points to note when using the geometry API:
- You should use the utility type MgGeometryFactory to create a MgGeometry object
- In the MgLinearRing type, the first and last coordinates in the coordinate collection should be identical
- When connecting segment types (base type MgCurveSegment) in segment collection, the last coordinate of a curve segment should be identical to the first segment of the next segment.
- When forming closed rings(MgLinearRing, MgCurveRing) the direction of traversal of the points in the ring indicates if it is an exterior or interior ring. The direction of traversal is counterclockwise for exterior ring and clockwise for interior rings.
You will find additional details for creating specific geometries in the presentation I provided above.
In addition to the geometry types mentioned above, there are several utility API that form a part of the geometry API. The raw geometry data can be used in three specific forms in the geometry API:
- The text based Autodesk Geometry Format (AGF) which is a plain text format
- The binary based AGF format
- Internal representations in the MgGeometry types
Geometry API comes with utility functions to manage the above types of data. The other utility of APIs allow you to determine the spatial relationship between geometry objects (contains, crosses, touches etc) and create new geometry with boolean operations (union, intersection, difference, symmitric difference).
The coordinate systems supported by the geometry API are Arbitrary X-Y, Geographic and Projected.
Finally, the geometry API allows you to create buffer geometry from existing geometry.
Here is a sample that demonstrates the usage of most of the API mentioned in this post. It implements two commands CRGEOM and CRBUFFER. CRGEOM demonstrates creation of geometry and CRBUFFER demonstrates creation of buffer geometry. In order to use the sample, do the following:
- Open Geometry.sln in Visual Studio 2010 and build the project.
- Delete the SDF if it exists NewFile.sdf (if you have used this sample before)
- Launch Map 3D and open a new drawing
- Load the .NET assembly created in step #1
- Assign the a coordinate system to the new drawing
- Draw one or more polyline entities in the drawing
- Enter the command “crgeom” and select a polyline entity. A line string feature is created from it on the API_Poly_Objects layer and inserted into NewFile.sdf in the .\Data\SDF folder. If this file doesn’t exist, it is created.
- Select a feature on the API_Poly_Objects layer and enter the command “crbuffer”. A buffer polygon is created round the feature. The buffer is created on the “Buffer_Objects” layer and inserted into Buffer.sdf in the.\Data\SDF folder
Note: If you will be revisiting this exercise later, deleting NewFile.sdf and Buffer.sdf before starting would make things easier for you. This is to ensure that both SDFs are created fresh using the same coordinate system, otherwise buffer creation would fail. Also, the Path to the SDF folders are hard coded in the sample so please modify this path to a folder convenient to you before using the sample.