How to register your command in nanoCAD

Programming in nanoCAD: How to register your command in nanoCAD?

One of the articles, describing how to use the scripts in nanoCAD by the example of the LISP-Script transfer to the VB-Scrip, shows in detail how to organize the user interaction, create new objects, lay them on the layers and how to run a script in nanoCAD. I hope this article was useful and you’ve used the great opportunity of the simplest automation.
Recent articles continue the series of publications related to programming in nanoCAD. It is time to learn how to set a command to your script and include it into nanoCAD’s interface (menu item, button on the toolbar and key combination). Welcome to the world of infinite possibilities of nanoCAD programming.
 

Necessary preparation

To practically execute what is written here, you should have the following things:
Of course, it can be read in theory, but it is much more interesting to execute it in practice.

Let’s start…
The script integration in nanoCAD consists of three steps: registration of new commands, download these commands in the software environment and bind commands to the interface elements. You can ignore the last step - in this case you can invoke commands from the command line. Let’s consider each step in more detail.

Step 1. A command registration in nanoCAD

A new command registration in nanoCAD is performed through a dedicated nsf-file, which is essentially a xml-file. Briefly, the command is described in the tags: ‹command›‹/command›, and has four attributes: name, weight, cmdtype and capsdisable. The understanding of “name” and “weight” attributes is quite clear (“name” is the command name, which is typed in the command line; “weight” is the command weight, also this parameter is optional and equal to 30 by default). It is necessary to understand two other parameters better.

The «cmdtype» attribute defines the script scope and can have two values:

0an application script: it means that script has access to the ThisApplication global name
1a drawing script: it means that script has access to the ThisDrawing global name

The «capsdisable» attribute manages the object selections when the script is run:

0saves selection (PickfirstSelectionSet) before starting the command
1saves selection after finishing the command

Be careful when you name a command. The command name should be unique in order not to override the basic commands. For example, the structure:

‹command name="save" weight="30" cmdtype="1" capsdisable="0"›
    ...
‹/command›
overrides the save document file command.
One nsf-file can define several commands. It is structured in the following way:
‹?xml version="1.0" encoding="utf-8"?›
‹package›
    ‹command name="cmd1" weight="30" cmdtype="1" capsdisable="0"›
        ...
    ‹/command›
    ‹command name="cmd2" weight="30" cmdtype="1" capsdisable="0"›
        ...
    ‹/command›
    ‹command name="cmd3" weight="30" cmdtype="1" capsdisable="0"›
        ...
    ‹/command›
        ...
‹/package›

Here, as you’ve probably already understood, three commands: cmd1, cmd2 and cmd3 are defined.
Within the ‹command›‹/command› tags it is possible to place two more tags: ‹description›‹/description› and ‹script›‹/script›. The first tag defines the command description. The second tag specifies the language of the script writing – VBScript or JScript. For example:

‹command name="mycommand" weight="30" cmdtype="1" capsdisable="0"›
    ‹description›‹/description›
    ‹script lang="JScript"›‹![CDATA[
        ...the script code is located here...
    ]]›‹/script›
‹/command›

I draw your attention to the ‹![CDATA[]]› structure. It prescribes a script code.
Due to this knowledge you can create your first nsf-file, which transforms your script into the command and register it in nanoCAD without any problems:

<?xml version="1.0" encoding="utf-16"?>
<package>
<command name="hello" weight="30" cmdtype="1" capsdisable="0">
<description>A classic program «Hello world!»</description>
<script lang="JScript"><![CDATA[ThisDrawing.Utility.Prompt("Hello world!")]]></script>
</command>
</package>

The described “hello” command displays the classical greeting in the command line. Save this xml-file with the «userdata.nsf» name. The first step is finished.

Step 2. The command file download when nanoCAD is run

To download the nsf-file, enter nsf in the command line of nanoCAD, specify a path of the created «userdata.nsf» file. Now it is possible to invoke commands described in the file, for example, the command created in the previous step – hello: 
 

Fig. 1. The simplest hello command. 
 
It is not expedient to repeat these steps each time when we need to invoke our command. So let’s automate this process.

A command creation of the nsf-file download

We should create a command which will run the created nsf-file. To this effect let’s create the «userdata.cfg» text file and describe «load_userdata_nsf» command:

 [\configman\commands\sload_userdata_nsf]
    weight=i30 |cmdtype=i0 | intername=sload_userdata_nsf
    RealCommandName=snsf
    Keyword=suserdata.nsf^MCloseDocument^MNewDocument^M

The script written above describes the new load_userdata_nsf command, which runs nsf command (RealCommandName=snsf) with the options specified in the Keyword line. I draw your attention to the “s” character, which follows after the equal sign («=»). This symbol is necessary for the nanoCAD’s interpreter to work with the cfg-file.
Let’s consider more interesting and useful options in detail. For example, I draw your attention to the following structure:

Keyword=suserdata.nsf^MCloseDocument^MNewDocument^M

This means that when you run the command (in this case - “nsf”), the «userdata.nsf» and Enter commands will be placed in the command line (i.e. the «userdata.nsf» file will be loaded), and after the CloseDocument+Enter command (i.e. the current drawing will be closed), and finally the NewDocument+Enter command (i.e. a new document will be created). I think you’ve guessed that the «^M» symbol means Enter.

Automatic loading of the nsf-file

The next action allows loading the load_userdata_nsf command automatically when we start nanoCAD. To achieve this, we should create the «userdata.ini» text file with the following lines:

; Registration of the file containing commands at nanoCAD’s startup
    [\DefProf\Startup\load_userdata_nsf]

To finish step 2 we should put three created files: userdata.nsf, userdata.ini and userdata.cfg into the nanoCAD’s installation folder and start the program. If you’ve done everything correctly, the userdata.nsf file will be loaded into nanoCAD automatically and the hello command will be registered. If the nsf-file describes more commands, all of them will be available from the command line.

Step 3. Integration of the script commands to the nanoCAD interface

The «userdata.cfg» file is responsible for command integration with the nanoCAD interface. This file has a specific structure where all elements of the nanoCAD interface are written with the same extension. This article doesn’t allow us to list the numerous possibilities and options of this file, but baseline set is following (all commands need to be registered in the «userdata.cfg» file):

Registration of the nanoCAD menu

This is carried out in the following way:

    [\menu\mycommans] |name=sMy commands
    [\menu\mycommans\hello] |name=sNew command HELLO |intername=shello

You will find the My commands menu item with New command HELLO item, which runs the hello command. You can extend your menu item by adding new lines.

Registration of the nanoCAD’s toolbar

This is carried out in the following way:

    [\toolbars\mycommans] |InitialVisible=f1 |name=sMY commands
    [\toolbars\mycommans\hello] |intername=shello

You will find the My commands toolbar and one new command located on it. The InitialVisible option is responsible for the toolbar visibility when you start nanoCAD (f1 – visible, f0 – invisible).

Appropriation of the specific icon to the command

To appropriate a specific icon to the command, you should override the command by specifying dll with the image resource. In particular, you can use the newbtns.dll file, which is installed with nanoCAD.

    [\configman\commands\hello]
    weight=i30 |cmdtype=i0 |CapsDisable = W0xFF
    intername=shello
    BitmapDll=snewbtns.dll | icon=sPENCIL

Hotkeys

It is also possible to run the command using the hotkeys by registering them in the following way:

    [\Accelerators]
    hello=sCtrl+Shift+1

Now you can invoke the hello command by pressing Ctrl+Shift+1.

Final steps: Features of the interface registration

At the moment any changes in the nanoCAD interface are applied only after a single reset of the program and registry cleaning. Therefore, we should add the following lines in userdata.ini:

    [\Configuration]
    ClearRegistry=f1

Starting the program via the ncad.exe (important!), we get the new HELLO command, menu item, toolbar and hotkeys. Start the software and stop the setting reset by adding the «ClearRegistry=f1» line in the userdata.ini file:

    [\Configuration]
    ;ClearRegistry=f1

Why is it important to run the software directly from ncad.exe? The fact is that when you run the application using standard desktop icon or Startup menu item, it checks the integrity of installation and if you make any changes in the interface, the application will try to restore files and nanoCAD registry record, running the Windows Installer. It will ruin everything what you have done. When you are writing and debugging your scrips proceed with ncad.exe.
But when your have finished and your application is checked and registered in the system, you can run the software using the standard desktop icon or Startup menu item. Once checked the integrity of the key files, the software will be run correctly. 

Conclusion

We have learnt to extend the features of nanoCAD (through three setup files: userdata.nsf, userdata.cfg and userdata.ini). In this case new commands are added into the nsf-file and interface settings are added into the cfg-file. All together, they are linked by the ini-file. 
 

Fig. 2. The adjusted on new commands nanoCAD interface

The commands which you can use in nanoCAD are described in the ActiveX API document. The installation folder is located in the following path:

%product_dir%\help\api\ncX_devguide.chm

The ActiveX API provides the users with great opportunity. Small automation tools save you from routine and speed up your work.
It is possible to download demo files: userdata.nsf, userdata.cfg and userdata.ini. In this case you don’t have to create these files, you can use and edit the downloaded files. 
Copyright 2017 Nanosoft. All Rights Reserved. Privacy policy  |  Terms of use  |  Forgot password?
Follow us: