It is no secret that today AutoCAD is the most popular CAD system used in many branches of human activity. In the basic delivery, it allows you to perform drawing work for any purpose using only standard tools.
However, pure AutoCAD - like the graphics core in the base configuration - gradually loses its relevance. This happens because today it is important for the user to design interconnected objects of the subject area (walls, railroad profiles, wheels ...), and not graphic primitives in the drawing (points, lines, circles ...); and to design quickly, efficiently and in accordance with the standards in force in a particular country and industry.
Naturally, everything that end-users in all countries needed was not possible for Autodesk to be included in the AutoCAD system, so programming tools were introduced into AutoCAD that allow users to adapt it to their own needs [1].
AutoCAD Development Tools. These funds can be divided into the following groups:
LISP and Visual Basic for Applications to automate certain routine actions on drawings.
ObjectARX technology based on the C ++ programming language and giving the possibility of creating your own commands and primitives.
Support for Microsoft .NET technology, which allows you to download arbitrary program code written in any of .NET-compatible programming languages ​​into Autocad.
')
The latest technology is the most powerful, as it is not based on the private development of Autodesk, but on the progressive generally accepted Microsoft .NET Framework programming platform.
In this case, we note that none of these solutions in their original form can connect the drawing with the objects that it displays, that is, with the domain model. In turn, the most common and successful software architecture that implements this behavior is the MVC pattern (Model-View-Controller).
Model-View-Controller ("Model-view-controller"). This is a software architecture in which the application's data model, user interface, and control logic are divided into three separate components, so that modifying one of the components has minimal impact on the other components (Fig. 1).

Fig. 1 - MVC Architecture
Model (Model). The model provides data (usually for the View), and also responds to requests (usually from the controller), changing its state.
View. Responsible for displaying information (user interface).
Controller (Controller). Interprets the data entered by the user, and informs the model and the idea of ​​the need for an appropriate response.
It is important to note that both the presentation and the behavior depend on the model. However, the model does not depend on the performance or behavior. This is one of the key benefits of this separation. It allows you to build a model regardless of the visual presentation, as well as create several different representations for a single model [2].
Let us define the possibility of using the MVC architecture, considering the tools provided by AutoCAD when programming using .NET. Below (Figure 2) is a simplified AutoCAD class diagram through which user code interacts with CAD.
Structure classes AutoCAD.

Fig. 2 - Simplified AutoCAD Class Diagram
The core is the Application class, which is a wrapper (Wrapper) directly above the AutoCAD application. Many documents can be opened in the application, each of which is represented as an object of the Document class. Each document in turn is associated with a database (Database), which stores in itself all the information on the drawing (styles, primitives, blocks ...) in the form of DBObject objects with unique identifiers (Handle). The greatest interest for us are precisely the primitives (Entity), with the help of which the drawing is built. Primitives in AutoCAD are objects: point (Point3d), line (Line), polyline (Polyline), circle (Circle), text (MText), etc. - inherited from the abstract class Entity.
Any changes to an AutoCAD document are made as part of the transactions opened to its database. In addition to ensuring the integrity of information, this transactional approach is also used to implement the Undo (Undo) and Redo (Redo) user interface functions, which either roll back or use the last transaction in the stack, respectively. This feature must be considered when designing the data model of the subject area.
Using AutoCAD transactions in our own managed .Net code, we have the opportunity to build drawings by creating records about the corresponding primitives in the document database. Therefore, we can use an AutoCAD document as a component of the MVC architecture's View.
Now we need to determine the possibility of correct system behavior when the user changes the drawing in order to appropriately change the Model component. One possible solution is to react to events generated by AutoCAD when the elements of the drawing change:
Database.ObjectModified - generated when changing any DBObject object in the document database.
Database.ObjectErased - generated when removing any object from the document database.
By subscribing to these events, we are able to track any changes to the drawing and react to them, by changing the components of the model accordingly. Using the terminology of the MVC architecture, we can say that the functions of the Controller component in this implementation assume both AutoCAD, acting as a generator of the change event of the primitive, and .NET code that makes changes to the model based on this event.
Thus, it is possible to present the MVC architecture as applied to AutoCAD in the following form (Fig. 3):

Fig. 3 - MVC architecture implementation
Serialization. The next question that needs to be addressed is how to provide model data storage between work sessions. The most common approach to solving this problem in the .NET Framework is serialization.
We note two obvious facts:
Saving data in the database (serialization) must be performed immediately before saving the document itself.
Data recovery from the database (deserialization) should be performed immediately after loading the document associated with the domain model.
Turning to rice. 2, we see that these actions can be implemented by subscribing to the following events from AutoCAD:
Document.BeforeSave, which is generated before saving the AutoCAD document to media.
Application.DocumentCreated, which is generated after opening or creating a document.
Transactional. As mentioned above, any changes to the AutoCAD document are performed as part of a transaction, which ensures both the integrity of the information and the ability to perform custom undo and redo commands. The term “transaction” here means the sequence of operations that are performed when a document database is transferred from one consistent state to another [5].
But since in this case AutoCAD performs the functions of presentation and controller, and the main information is stored in the domain model implemented in .NET, it is required:
Ensure that the specified model is transacted.
Ensure that transactions are created, opened, recorded and rolled back for the data model and the AutoCAD database.
These requirements, on the one hand, guarantee the integrity and consistency of the combination of the model and the document database; on the other hand, it is possible to use standard AutoCAD user interface features for undo and redo commands.
The implementation of a data domain model with transaction support is beyond the scope of this article, it is enough to indicate only the direction in which the development of such a solution can be made - this is the use of the “Command” design pattern [6].
Ensuring the synchronization of transactions of both types can be divided between the objects of the intermediate layer (Fig. 3). The controller initiates the simultaneous launch of both transactions in response to the actions taken, makes the necessary changes to the model, and also tracks the invocation of undo and redo commands. At the same time, the Drawer, displaying a drawing in an AutoCAD document, changes the records of the document database in the transaction controller already opened.
Results. On the basis of the principles described in the article were developed:
The basic model, on the basis of which it is possible to build models of various subject areas. It includes: domain components, relationships between them, constraints imposed on components and relationships; also implements a transactional approach.
The base classes of the middle layer between the domain model and the Autocad drawing, which, on the one hand, update the drawing whenever the model changes and, on the other hand, sends all user-made changes to the model.
The main components of the additional user interface that allows you to edit the model data directly, bypassing the drawing.
Thus, to build CAD based on AutoCAD with the use of this development it is necessary:
Identify (program) a specific domain model in terms of the base model.
Determine (program) the graphic view of the components of the model in the drawing and describe their behavior when changing the drawing.
Describe the behavior of the additional user interface, if needed.
The resulting approach is characterized by:
Highly integrated AutoCAD, middleware and model.
Hiding the features of the functioning of AutoCAD for the programmer.
Relative ease of development.
Convenience operation.
LITERATURE
1. Zuev, S. A. and Poleschuk, N. N. AutoCAD-based CAD systems - how to do it. St. Petersburg: BHV-Petersburg, 2004.
2. Model-View-Controller. Wikipedia. [On the Internet]
en.wikipedia.org/wiki/Model-View-Controller3. Serialization. Wikipedia. [On the Internet]
en.wikipedia.org/wiki/Serialization4. Basics of serialization in the .Net Framework. Microsoft Developer Network. [On the Internet]
msdn.microsoft.com/ru-ru/library/ms233836.aspx5. Horafas, D. and Legg, S. Design databases. [transl.] DF Mironova. M .: Mechanical Engineering, 1990.
6. Command. Wikipedia. [Online]
en.wikipedia.org/wiki/Command