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

Published on July 11, 2011

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

    A year ago, I wrote an article on how to use scripts in the nanoCAD environment using the example of translating a LISP program into Visual Basic Script: I showed in sufficient detail how to organize user interaction, create new objects, lay them out in layers and how to call a script for execution in the environment nanoCAD. I hope that you have taken this wonderful opportunity of simple automation.

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

    image

    Initialization


    In order to practically fulfill what is written here, you need to do two things:
    • Install nanoCAD CAD system ;
    • load any text editor for writing scripts (I use Notepad ++ , you can use regular Windows Notepad);
    Of course, everything can be read theoretically, but it’s much more interesting to do it yourself. Let's go ...

    Actually, the integration of scripts in the nanoCAD environment consists of three steps: registering new commands, loading these commands into the nanoCAD environment and linking the commands to the interface elements. The last step can be omitted - in this case you can call your commands from the command line. Consider each step in more detail.

    Step 1. Register a team in nanoCAD


    Registering a new team in nanoCAD is done through a specialized nsf file, which is essentially an xml file. Its structure is well described in the ActiveX API reference guide - see the "Registering Scripts as Commands" section. Briefly, the command is described in the file with the 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 weight of the command, the parameter is optional, by default it is 30), then the other two need to be dealt with closer.

    The cmdtype attribute 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 running the script:
    0 - save selection (PickfirstSelectionSet) before starting a command
    one - save selection after command execution

    It is clear that the team name must be unique - inadvertently, you can override the base platform commands. For example, the construct: overrides the save document command. You can define multiple commands in one nsf file. And all this is structured as follows: Here, as you probably already understood, three commands cmd1, cmd2 and cmd3 are defined. Within the ‹command› ‹/command› tags, you can place two more 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 the script is written in - either VBScript or JScript. For example, like this: Pay attention to the construction ‹! [CDATA []]› - this is where the script code is written.
    ‹command name="save" weight="30" cmdtype="1" caps="0"›
        ...
    ‹/command›



    ‹?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›



    ‹command name="mycommand" weight="30" cmdtype="1" caps="0"›
        ‹description›‹/description›
        ‹script lang="JScript"›‹![CDATA[
            ...здесь расположен код скрипта...
        ]]›‹/script›
    ‹/command›


    Now armed with this knowledge, you can easily compose your first nsf file that wraps your script in a command and registers it with nanoCAD: The hello command described here displays a classic greeting on the command line. We save this xml file with the name "userdata.nsf" and we have taken the first step.
    ‹?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›



    Step 2. Download the command file when starting nanoCAD


    The NSF file is loaded into the nanoCAD environment of the same command - nsf. Enter it in the command line of running nanoCAD, specify the path to the userdata.nsf file 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, to take such steps every time we need our own team - pleasure is below average. Automate ...

    Creating an nsf file upload 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 Suitcase" located on the APPLOAD dialog. In nanoCAD 4.5 and above, you do not need to create an NSF file load command and register it at startup.

    First, we need to create our own command, which launches the nsf file specified by us (i.e., runs the nsf command with parameters). To do this, create a text file “userdata.cfg” (we will need this file later) and describe the command “load_userdata_nsf” 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 symbol "s" that comes after the equal sign ("=") - this is a required symbol required by the nanoCAD interpreter to work with the cfg file.

    I won’t describe all possible options for the commands, otherwise the article will turn into a book (those who are interested can study the file “nCad.cfg” on their own - 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 are sent to the command line (that is, the file “userdata.nsf” is loaded), then the command CloseDocument + Enter (i.e. closes the current document) and, finally, the NewDocument + Enter command (i.e. a new document is created). I think you guessed that the characters “^ M” mean Enter.

    Automatically download nsf file

    Now, when we start nanoCAD, we need to automatically load the load_userdata_nsf command. Everything is simple here - we create a text file “userdata.ini” with the following lines: Step 2 is done - we just need to put in the folder in which nanoCAD is installed the three files we created: userdata.nsf, userdata.ini and userdata.cfg and run the program. Now, if you did everything correctly, then when you start the nanoCAD environment, the userdata.nsf file will be loaded, and after that a new hello command will be registered. If more commands are written in the nsf file at startup, then all of them will be available from the command line.
        ; Регистрация файла с командами при запуске nanoCAD
        [\DefProf\Startup\load_userdata_nsf]




    Step 3. Integration of script commands with nanoCAD interface


    For the integration of commands with the nanoCAD interface, the already familiar file “userdata.cfg” is responsible. This file has a certain description structure - in fact, all elements of the nanoCAD interface are registered in files with this extension. I'm afraid that within the framework of the article I will not be able to tell all the numerous options and options for this file, but the minimum necessary set is as follows (all commands must be written in the file "userdata.cfg"):

    Register a menu in nanoCAD

    It is carried out in the following way: 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.
        [\menu\mycommans] |name=sМои команды
        [\menu\mycommans\hello] |name=sНовая команда HELLO |intername=shello



    Register toolbar in nanoCAD

    It is carried out in the following way: After that a new toolbar My commands will appear and on it one new hello command . The InitialVisible option is responsible for the visibility of the panel at startup (f1 - visible, f0 - invisible).
        [\toolbars\mycommans] |InitialVisible=f1 |name=sМои команды
        [\toolbars\mycommans\hello] |intername=shello



    Assigning a Team Specialized Icon

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


    Hotkeys

    You can also call a command using hot keys. To do this, you need to register them with a new command as follows: Now our simple hello command can be called via the keyboard shortcut Ctrl + Shift + 1.
        [\Accelerators]
        hello=sCtrl+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 and cleaning the registry. Therefore, we need to add the following lines to userdata.ini: Now, having launched the program through ncad.exe (important!), We will get a new HELLO command , a new menu item, a new toolbar, and hot keys in the environment . After a multiple launch, we stop the reset, commenting out the line “ClearRegistry = f1” in the userdata.ini file:
        [\Configuration]
        ClearRegistry=f1


        [\Configuration]
        ;ClearRegistry=f1

    Why is it important to run the program directly through ncad.exe? The fact is that the regular shortcut of the program at startup checks the integrity of the installation of the program, and if we made our changes to the interface, the shortcut will try to restore the nanoCAD files and registry by starting 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 launched through 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 got the opportunity to expand the functionality of the free nanoCAD platform. In this case, 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 for new commands.
    The commands that you can use in nanoCAD are described in the help for the ActiveX API - it is installed in the program folder along 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, save you from routine and speed up work. In my opinion, must have for students and is highly recommended for experienced CAD users.

    On our forum you can download the demo 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 started appearing on the forum that expand the functionality of nanoCAD: enabling / disabling the frame around the raster, building tangents to two circles, etc. There you can lay out your best practices. Come-discuss-share and let's enjoy the design!