1.125 Computer Aided Engineering

(AutoCAD presentation)

Quick start in ADS


Follow this recipe and make your own function that creates an "M" in AutoCAD by drawing a sequence of circles, giving the idea of animation.

1. Copy the template below into a text editor (emacs, for instance). This template is a standard to establish the link between the executable file in C and AutoCAD. Name your file with extension .c since it is a ANSI-C file

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <adslib.h>
#include <aplib.h>

static int loadfuncs();
int my_function();

void
main(argc, argv)

  int argc;
  char *argv[];
{
    int stat;
    short scode = RSRSLT;             /* This is the default result code */

    ads_init(argc, argv);             /* Initialize the interface */

    for ( ;; ) {                      /* Note loop conditions */

        if ((stat = ads_link(scode)) < 0) {

            printf("TEMPLATE: bad status from ads_link() = %d\n", stat);

            /* Can't use ads_printf to display 
               this message, because the link failed */
            fflush(stdout);
            exit(1);
        }

        scode = RSRSLT;               /* Default return value */

        /* Check for the following cases here */
        switch (stat) {

        case RQXLOAD:                 /* Register your ADS external functions.
                                         Register your function handlers if you
                                         want your ADS functions to be called
                                         transparent to this dispatch loop.
                                         Required for all applications.  */
          
            scode = loadfuncs() ? RSRSLT : RSERR;
            break;

        case RQSUBR:                  /* This case is normally expanded to 
                                         select one of the application's 
                                         external functions */
            break;

        case RQXUNLD:                 /* Do C program cleanup here.
                                         Not required unless you need to
                                         clean up your own allocated resources.

                                         Note: You don't have to undefine ADS
                                         functions.  LISP does it for you.  */
            break;

        case RQSAVE:                  /* AutoCAD SAVE command notification.
                                         You can use it for your own database
                                         synchronization.  Not required.  */
            break;

        case RQQUIT:                  /* AutoCAD QUIT command notification.
                                         Not required.  */
            break;

        case RQEND:                   /* AutoCAD END command notification.
                                         Not required.  */
            break;

        default:
            break;
        }
    }
}

/* LOADFUNCS  --  Define external functions with AutoLISP.

                  Normally expanded to call ads_defun() once for each
                  external function to be defined, assigning each one a
                  different ADS function code.  ads_regfunc() is then
                  called to specify the handler function for each ADS
                  function code.
*/
static int loadfuncs()
{
    if (ads_defun("C:my_function", 0) == RTNORM) {   /* Define function */
        ads_regfunc(my_function, 0);      /* Register handler */
        return 1;
    } else
        return 0;
}

/* Sample handler for ADS function code 0.

                This function will handle (ADSFUNC) calls.  You can have
                one handler for each external function, or use one handler
                for several functions.  ads_getfuncode() tells the handler
                which function (ADS function code) it's dealing with.

                If you choose to use the RQSUBR method, you should place
                this function call following the RQSUBR switch statement.
*/


int my_function()
{

/****************************************************************
*                                                               *
*   HERE YOU WRITE YOUR CODE AS IN A NORMAL 'main()' FUNCTION   *
*                                                               *
****************************************************************/

return RSRSLT;
}
2. Replace the flag HERE YOU WRITE ... FUNCTION with the piece of code below. This piece executes the commands that will create the "M".
int i;
float dx;
ads_point p;

dx = acos(-1.0)/100.0;

ads_command(RTSTR, "color", RTSTR, "green", 0);

for(i=0 ; i < 200 ; i++){
  p[X] = i;
  p[Y] = 200.0 * pow(sin(i * dx), 2.0);
  p[Z]= 0.0;

  ads_command(RTSTR, "circle", RT3DPOINT, p, RTSTR, "5", 0);
}
3. Copy the Makefile presented in the second topic of the previous screen of this presentation and change the name of the file to compile your code.

4. Open AutoCAD and type (xload "name_of_the_executable_file_in_C") in the command prompt area, include the parentheses. Make sure that your executable file is in your home directory, otherwise type the complete filename which includes that directory. Now, just type my_function to run the new function created. You will see a green "M" being drawn on the screen.

If you don't see anything, don't panic! You don't need new glasses... . Type zoom a to see the green "M". You can erase this "M" using the command erase and redraw it by typing my_function again.


Date: February 26, 1996