📜 ⬆️ ⬇️

Programming in nanoCAD: how to register your team in the nanoCAD environment?

A year ago I wrote an article on how to use scripts in the nanoCAD environment using the example of translating a LISP program to Visual Basic Script: it showed in some detail how to organize user interaction, create new objects, lay them out in layers and how to call the script for execution nanoCAD. I hope you enjoyed this great feature of the simplest automation.

With this article I would like to continue the cycle of publications related to programming under nanoCAD. In particular, it's time to find out how to assign a command to your script, include it in the nanoCAD interface (menu item, button on the toolbar and keyboard shortcuts). Welcome to the world of limitless nanoCAD programming.

image

Initialization


In order to practically accomplish what is written here, you need to do two things:
Of course, everything can be read theoretically, but it is much more interesting to do the described yourself. Go…
')
Actually, the integration of scripts in the nanoCAD environment consists of three steps: the registration of new commands, the loading of these commands into the nanoCAD environment, and the binding of commands to the interface elements. The last step can not be done - in this case, you can call your commands from the command line. Consider each step in more detail.

Step 1. Register team in nanoCAD


Registration of a new command in nanoCAD is carried out through a specialized nsf-file, which is essentially an xml file. Its structure is well described in the ActiveX API Reference Manual — see the section “Registering Scripts as Commands”. In short, the command is described in a file with tags: ‹command› ‹/ command›, which has four attributes: name, weight, cmdtype and caps. If with “name” and “weight”, I hope everything is clear (“name” is actually the name of our team, which we will type on the command line, and “weight” is the command weight, the parameter is optional, the default is 30), then the other two need to figure out a little closer.

The attribute “cmdtype” defines the scope of the script - it can have two values:
0- application script: i.e. from the script there is access to the global name ThisApplication
one- document script: i.e. from the script there is access only to the global name ThisDrawing

The “caps” attribute controls the selection of objects when the script is run:
0- save selection (PickfirstSelectionSet) before starting the command
one- save selection after command execution

It is clear that the command name must be unique - by negligence you can override the basic commands of the platform. For example the construction:
‹command name="save" weight="30" cmdtype="1" caps="0"›
...
‹/command›
will override the save document command.

You can define several commands in one nsf file. And all this is structured as follows:
‹?xml version="1.0" encoding="utf-8"?›
‹package›
‹command name="cmd1" weight="30" cmdtype="1" caps="0"›
...
‹/command›
‹command name="cmd2" weight="30" cmdtype="1" caps="0"›
...
‹/command›
‹command name="cmd3" weight="30" cmdtype="1" caps="0"›
...
‹/command›
...
‹/package›
Here, as you probably already understood, three cmd1, cmd2 and cmd3 commands are defined.

There are two more tags within the ‹command› ‹/ command› tags: ‹description› ‹/ description› and ‹script› ‹/ script›. Again with the first, I hope everything is clear - this is a description of the team. The ‹script› ‹/ script› tag defines the language in which the script is written, either VBScript or JScript. For example, like this:
‹command name="mycommand" weight="30" cmdtype="1" caps="0"›
‹description›‹/description›
‹script lang="JScript"›‹![CDATA[
... ...
]]›‹/script›
‹/command›
Pay attention to the construction of ‹! [CDATA []]› - the script code is written in it.

Now, armed with this knowledge, you can easily create your first nsf file wrapping your script into a command and registering it in the nanoCAD environment:
‹?xml version="1.0" encoding="utf-8"?›
‹package›
‹command name="hello" weight="30" cmdtype="1" caps="0"›
‹description› «, !»‹/description›
‹script lang="JScript"›‹![CDATA[
ThisDrawing.Utility.Prompt(", !")
]]›‹/script›
‹/command›
‹/package›

The hello command described here displays a classic greeting on the command line. Let's save this xml file with the name “userdata.nsf” and we have taken the first step.

Step 2. Download the command file when nanoCAD starts


The NSF file is loaded into the nanoCAD environment of the command with the same name - nsf. Enter it in the command line of the launched nanoCAD, specify the path to the file “userdata.nsf” created in the previous step and you can run the commands described in it - for example, the hello command created in the previous step:
image
Fig. 1. The simplest hello command and our first result.
Of course, doing these steps every time we need our own team is a pleasure below average. Automating ...

Create nsf file loading command


Attention! Starting with nanoCAD 4.5, NSF-files can be loaded using the APPLOAD command , for automatic loading it is enough to place the file in the “Startup Suit” located on the APPLOAD dialog. In nanoCAD 4.5 and higher, you do not need to create a command for loading an NSF file and register it for autoloading.

First we need to create our own team that runs the nsf-file that we specify (i.e., it runs the nsf command with parameters). To do this, we create a text file “userdata.cfg” (we will need this file later) and describe the “load_userdata_nsf” command in it:
[\configman\commands\sload_userdata_nsf]
weight=i30 |cmdtype=i0 | intername=sload_userdata_nsf
RealCommandName=snsf
Keyword=suserdata.nsf^MCloseDocument^MNewDocument^M

With this step, we created our own configuration file in which we described a new command with the internal name load_userdata_nsf, which calls the nsf command (RealCommandName = snsf) with the options described in the Keyword line. Pay attention to the “s” symbol that comes after the equal sign (“=”) - this is a mandatory symbol required by the nanoCAD interpreter to work with the cfg file.

I will not describe the various options of the commands, otherwise the article will turn into a book (those interested can independently study the file “nCad.cfg” - this is the configuration file for nanoCAD), but we will study the most interesting and frequently used options below.

For example, pay attention to the design
Keyword=suserdata.nsf^MCloseDocument^MNewDocument^M
It means that after calling the command (in this case “nsf”), the command “userdata.nsf” and Enter is sent to the command line (ie, the file “userdata.nsf” is loaded), then the CloseDocument + Enter command (i.e. the current document is closed) and, finally, the NewDocument + Enter command (that is, a new document is created). I think that you realized that the characters "^ M" mean Enter.

Automatic nsf file loading

Now we need to automatically load the load_userdata_nsf command when starting nanoCAD. Everything is simple - we create a text file “userdata.ini” with the following lines:
; nanoCAD
[\DefProf\Startup\load_userdata_nsf]


Step 2 is done - it remains for us to put the three files we created in the folder where nanoCAD is installed: userdata.nsf, userdata.ini and userdata.cfg and run the program. Now, if you did everything correctly, then when you start on nanoCAD, the userdata.nsf file will be loaded, and after this a new hello command will be registered. If more commands are written to the nsf file at startup, all of them will be available from the command line.

Step 3. Integrating script commands with the nanoCAD interface


The file “userdata.cfg”, which is already familiar to us, is responsible for the integration of commands with the nanoCAD interface. This file has a specific description structure - in fact, all the elements of the nanoCAD interface are written in files with this extension. I am afraid that within the framework of the article I will not be able to tell all the numerous options and options of this file, but the minimum required set is the following (all commands must be written in the file “userdata.cfg”):

Registration menu in nanoCAD

It is carried out in the following way:
[\menu\mycommans] |name=s
[\menu\mycommans\hello] |name=s HELLO |intername=shello

After that, a new menu item My Commands will appear in nanoCAD with one menu item New HELLO command , which calls the hello command. By adding new lines, you will expand your menu item with commands.

Registration toolbar in nanoCAD

It is carried out in the following way:
[\toolbars\mycommans] |InitialVisible=f1 |name=s
[\toolbars\mycommans\hello] |intername=shello

After this, a new toolbar My Commands will appear and one new hello command on it. The InitialVisible option is responsible for the visibility of the panel at startup (f1 is visible, f0 is invisible).

Assigning a team of specialized icons

In order for the team to have its own icon, you need to redefine the command again, specifying the dll with the image resources. In particular, you can use the file newbtns.dll, which is installed along with nanoCAD (look at the image files registered in dll using the resource manager program):
[\configman\commands\hello]
weight=i30 |cmdtype=i0
intername=shello
BitmapDll=snewbtns.dll | icon=sPENCIL


Hotkeys

The command can also be invoked using hotkeys. To do this, you must register them on the new command as follows:
[\Accelerators]
hello=sCtrl+Shift+1

Now our simple hello command can be called via the key combination Ctrl + Shift + 1.

Final Steps: Interface Registration Features


At the moment, any changes to the interface in nanoCAD are applied only after a single reset of the program settings and registry cleaning. Therefore, we need to add the following lines to userdata.ini:
[\Configuration]
ClearRegistry=f1

Now, by running the program through ncad.exe (important!), We will get a new HELLO command in the environment, a new menu item, a new toolbar and shortcuts. After the launch again, stop resetting the settings by commenting out the “ClearRegistry = f1” line in the userdata.ini file:
[\Configuration]
;ClearRegistry=f1

Why is it important to run the program directly through ncad.exe? The fact is that a regular program shortcut at startup checks the integrity of the program installation, and if we made our changes to the interface, the shortcut will try to restore the nanoCAD files and registry by running the Windows installer. This is not very convenient when you train with scripts.

But if your application is tested and registered in the system, then in the future, the program can still be run via a shortcut — the program, once checking the integrity of key files, will start correctly.

Conclusion


So, we got what we wanted: through three configuration files (userdata.nsf, userdata.cfg and userdata.ini) we were able to expand the functionality of the free nanoCAD platform. At the same time, new commands are added to the nsf file, and the interface settings are added to the cfg file. Together, this links the ini file.
image
Fig. 2. The nanoCAD interface configured on new commands.
Commands that you can use in nanoCAD are described in the ActiveX API help - it is installed in the program folder in the path:% product_dir% \ help \ api \ ncX_devguide.chm.

In principle, the ActiveX API provides the user with enormous opportunities - small automation tools allow you to do without paid applications, eliminate the routine and speed up work. In my opinion, must have for students and highly recommended for experienced CAD users.

In our forum, you can download sample files userdata.nsf, userdata.cfg and userdata.ini . In this case, you do not need to create these files: you can use and edit ours.

By the way, useful commands have already begun to appear on the forum that extend the functionality of nanoCAD: enabling / disabling the frame around the raster, building tangents to two circles, etc. There you can lay out their work. Come, discuss, share and let's enjoy the design!

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


All Articles