📜 ⬆️ ⬇️

Import coordinates from a text file to the nanoCAD drawing on the MultiCAD .NET API



In the previous article, we wrote about how to import points from an external text file into a .dwg drawing using the classic .NET API. In this article, we will look at the features of solving the same problem on the cross-CAD system platform MultiCAD .NET API and demonstrate how a built application can be run in nanoCAD and AutoCAD without changing the project settings and recompiling.

Creating and setting up a working draft

The project is created in the same way as for the previous example:
')

All that needs to be done is just to connect the mapimgd.dll library included in the nanoCAD SDK package starting from version 4.0. The configuration of the project remains unchanged for developing applications running both on the nanoCAD platform and on other systems, in particular, AutoCAD. If the previous project had two configurations - Debug NCAD and Debug ACAD, then there will be one - Debug MultiCAD.

Importing coordinates and adding primitives to the drawing database

The structure of the application and the general code (application form, organization of a preview, import of coordinates from a file) remain the same, the classes Importer and Creator , the code of which depends on a specific platform, will be changed.

So, the Creator.createPoints() method, which is responsible for the direct creation of points for the input coordinate array, now looks like this:

 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(); } } 


Recall what the procedure for creating points looked like earlier when using the classic .NET API:

 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(); } } } 


Obviously, the amount of code required to implement this method has been significantly reduced: to create an object, set coordinates, and add to the database it took only three lines of code! And this is another distinctive feature of MultiCAD.NET: in addition to solving its main task - ensuring cross-platform applications - the MultiCAD.NET API allows you to make application code much more compact. This is achieved due to the fact that many auxiliary operations are already “embedded” in the main functionality.

Literally briefly dwell on the procedure for adding points to the drawing. Without going into details (this is the topic of a separate article), we note that the MultiCAD API implements three geometry levels for graphic objects: pure “mathematical” geometry, geometry with the addition of basic properties of primitives: color, thickness and style of lines, etc., and level database objects. In our case, we created a standard DbPoint geometry, set its geometrical parameters, and used the DbEntity property to go to the database level and add an object to the drawing. There is no need to determine the current drawing space: the AddToCurrentDocument() method AddToCurrentDocument() automatically determine which document is current and which workspace is currently being used.

Application loading in nanoCAD and AutoCAD

Now that the code has been compiled and the .NET assembly for the application is built, it's time to talk about the options for running the application under different systems.


The source code of the project is available here .

Discussion of the article is also available on our forum: forum.nanocad.ru/index.php?showtopic=6509 .

Translation of the article into English: Importing coordinates using the MultiCAD.NET API .

Source: https://habr.com/ru/post/188326/


All Articles