We already
wrote that the application written for nanoCAD can be run in AutoCAD. But the opposite is much more interesting: using nanoCAD code that was developed for AutoCAD. As always, I decided to do it playfully.
It turns out that the idea that you can make a game for CAD software does not come to us alone. For example, Kean Walmsley, the leading evangelist for the AutoCAD .NET API, has
an article about Minesweeper on the
blog . That's exactly this sapper we will let under nanoCAD. See the details under the cut.
I think everyone who develops .NET applications under AutoCAD is familiar with Kean Walmsley's
“Through the Interface” blog. In
one of the posts the author writes about the game “Minesweeper”, aka “Minesweeper”, made for AutoCAD.
There are no files to download in the article, so you have to copy the code of two VB files and create a project yourself. The article is accompanied by a number of comments, allowing in detail to understand how the game works, but this is not our goal. Our goal is to launch Minesweeper under nanoCAD.
To do this, we need to do three things:
- add .NET nanoCAD library to References: hostdbmgd.dll, hostmgd.dll;
- replace Autodesk.AutoCAD with Teigha or HostMgd in the Imports block (see below);
- replace the full AutoCAD class names (where they occur) with the full nanoCAD class names.
Consider each of the points in more detail.
The first. Adding the hostdbmgd.dll, hostmgd.dll libraries is necessary because they contain all the classes for working with the .NET API nanoCAD. Without them, we can not do anything. It's clear.
')
The second. In AutoCAD, all classes are contained in the Autodesk.AutoCAD namespace. In nanoCAD, there are two namespaces: the database classes (that is, the drawing and its contents) are in the Teigha space, and the classes associated with the application, with the document description, with the command line, are in the HostMgd namespace. I did this:
Imports Teigha.Runtime Imports Teigha.Geometry Imports Teigha.DatabaseServices Imports HostMgd.ApplicationServices Imports HostMgd.EditorInput
And finally, the third. In the code, there are sometimes explicit indications of class names. For example, on Autodesk.AutoCAD.DatabaseServices.TransactionManager. In this case, you need to make the same replacement as in paragraph two.
As a result, we get the compiled .NET assembly. It can be loaded into nanoCAD and run the game with the command “MINESWEEPER”.
When I adapted the Minesweeper code for nanoCAD, it turned out to be extremely convenient and almost necessary to compile my project for both platforms at once.
I made two configurations: the AutoCAD configuration and the nanoCAD configuration.
We configure both configurations: specify the paths to acad.exe and ncad.exe, set the directories to which the libraries should be going. In the nanoCAD configuration, we set the NCAD constant.
Namespace connections are framed with #IF THEN ... #END IF preprocessing directives. To bind full class names we use aliases:
Imports Platform = HostMgd Imports PlatformDb = Teigha
The result is the following:
#If NCAD Then Imports Teigha.Runtime Imports HostMgd.ApplicationServices Imports Teigha.DatabaseServices Imports Teigha.Geometry Imports HostMgd.EditorInput Imports System Imports Platform = HostMgd Imports PlatformDb = Teigha #Else Imports Autodesk.AutoCAD.Runtime Imports Autodesk.AutoCAD.ApplicationServices Imports Autodesk.AutoCAD.DatabaseServices Imports Autodesk.AutoCAD.Geometry Imports Autodesk.AutoCAD.EditorInput Imports System Imports Platform = Autodesk.AutoCAD Imports PlatformDb = Autodesk.AutoCAD #End If
Now you can collect the same code for both configurations. Profit!