In the previous post we talked about importing point coordinates from an external text file into a .dwg drawing using classic .NET API. This post is going to describe how easy this problem can be solved using the cross-platform MultiCAD API. Here you will find the specifics of building MultiCAD application and the simple steps to launch it in nanoCAD and AutoCAD without changing project settings and rebuilding.
Creating working project
The project should be created in MS Visual Studio in the same way as it was done for the previous example:
- Project type: Visual C#
- Template: Class Library
To set up the project you only need to add a reference to the mapimgd.dll library that is included in the nanoCAD SDK (starting from version 4.0). The project configuration remains the same for applications that run in nanoCAD as well as in other supported CAD programs, particularly in AutoCAD. This compares to the previous project that contains two configurations — Debug NCAD and Debug ACAD. This project will use the only Debug MultiCAD configuration for all cases.
Importing coordinates and adding point entities to the drawing’s database
The structure and common code for creating application form, preview organization, and importing text data from a file remains the same. Implementation of the Importer and Creator classes was dependent on the specific host program, so they will be rewritten to be platform-independent. For example, the Creator.createPoints()
method, which creates point entities by the input array of coordinates, will look as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
public static void CreatePoints(List coords) { for(int i = 0; i < coords.Count-2; i += 3) { // Create a point object DbPoint point = new DbPoint(); // Set the position for the point object point.Position = new Point3d(coords[i], coords[i + 1], coords[i + 2]); // Add the point entity to the current document point.DbEntity.AddToCurrentDocument(); } } |
Let’s recall how the point creating procedure looks in the previous example using classic .NET API:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
public static void CreatePoints(List coords) { DocumentCollection dm = Platform.ApplicationServices.Application.DocumentManager; Database db = dm.MdiActiveDocument.Database; using (db) { // Create a transaction using (Transaction tr = db.TransactionManager.StartTransaction()) { // Get the table block record for current drawing space BlockTableRecord btr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite); for (int i = 0; i < coords.Count - 2; i += 3) { // Create a point object DBPoint point = new DBPoint(new Point3d(coords[i], coords[i + 1], coords[i + 2])); // Append the point to the database btr.AppendEntity(point); // Add the object to the transaction tr.AddNewlyCreatedDBObject(point, true); } btr.Dispose(); // Commit the transaction tr.Commit(); } } } |
You can see that the number of code lines required for implementing this method has been decreased significantly. With MultiCAD.NET creating point objects, setting their coordinates and adding them to the database requires only three code lines! This is an extra advantage in addition to cross-platform support, using the API you can make application code more compact. This is carried out by “embedding” auxiliary operations into the main functionality.
Let’s look briefly at point adding procedure (a full overview will be the subject of future posts). There are three levels of geometry for entities in the MultiCAD.NET API:
- level of clear “math” geometry,
- level of geometry with primitives properties: color, linetype, lineweight etc. and
- level of database object.
Here we have created a DbPoint
object, set its position and used the DbEntity
property to move to the database level and add the point entity to the drawing’s database. Note, there is no need to determine which working space is current, the AddToCurrentDocument()
method automatically finds the active document and adds an entity to currently used space.
Loading application to nanoCAD and AutoCAD
After the code is built and an application .NET assembly is generated you can load it and run in the supported CAD-programs.
- Loading applications to nanoCAD is performed by the standard
NETLOAD
andAPPLOAD
commands. - To load applications to other systems, the special application called Object Enabler should be used. For example, to load an application to AutoCAD, MultiCAD Enabler for AutoCAD should be loaded first, and then use the
NETLOAD
command for loading your application. This standard MultiCAD Enabler for AutoCAD can be downloaded from the nanoCAD Developers’ Club.
Source code of the project is available here.