Contents

Front Page

Programming style

What are the choices for programming AutoCAD?

What to do with LISP files from the Internet

The History of Entity References

Examples

Links

Just learning...


Advertising

What are the choices for programming AutoCAD?

Bill Kramer – 2001

AutoCAD programming is a broad subject that has multiple entry points. That is, you may be an AutoCAD operator drawing pictures all day long and want to customize the interface to speed your work along. Or you may be a programmer wanting to integrate an existing application into AutoCAD for input or output. As such AutoCAD provides a mixture of programming options. For the advanced programmer there are tools for using languages such as C++ and VB. The non-programmer also has power tools available in the form of predefined graphics and the ability to easily customize the menu system.

The problem is that having this many choices can be confusing as to where to start. Eventually programmers and operators alike will want to learn all the tools. That is only natural. But the question is often where to start and in that question there is the more refined question of "what are my choices?" This article attempts to explain the choices available for programming the various features of AutoCAD and why you might select one over another.

The structure of this quick article is to present some basic concepts as an overview. The options are broken down into levels of complexity and power from a programming point of view. Although programmers should be cautioned that the early levels are easy for operators and do contain several concepts that should be understood in order to achieve a good working application. After a brief description of the various levels, each of the programming languages will be demonstrated. AutoCAD’s programming options are diverse and this simple example is intended to show the amount of coding and complexity involved in making an application using the tools provided.

Levels of Programming

AutoCAD programming can occur at multiple levels. Operators will find the first two levels to be obvious and easy while programmers will be drawn to the last two levels. As you go from one level to the next you need to increase your knowledge of how the computer or AutoCAD works. You may also require a different skill level such as knowledge of how to program using a particular language.

You can mix and match the levels of customization to suite you needs as well. The levels are summarized in the following table.
 

Level Name Concepts Tools
1 Blocks Repeated drawing components. Library of already completed drawing parts. Title blocks, material lists, standard details, notes... Drawing commands and DWG files.
2 Menu and menu macros Arrange in order that makes sense to you. Take ownership of the CAD environment. Diesel Programming Language Text editor for manipulation of menu source files.
3 Procedural scripts Variables, logic, parametric systems, command streams, entity manipulations Visual LISP, VBA, ObjectARX with C++
4 Object Interfaces React to events, direct contact with AutoCAD objects. Visual LISP, VBA, ObjectARX with C++

 

Note that Procedural and Object style programming both involve the same tools. The style of programming is often mixed in any given application however a distinction needed to be made. The reason is that object level interfaces often involve a higher degree of interface with the system itself and a different way of thinking on the part of the programmer.

To understand why object interfaces can sometimes be attractive, let’s take a look at where procedural scripts are weak by comparison. The first and primary weakness in the comparison is the lack of a two way channel of communication. Procedural scripts often rely on COMMAND sequences and if an error happens or something is changed during the execution of the script that results in a new command sequence taking place, the script may fail. And when scripts fail, it can often be with very undesired effects.

Object interfaces provide a two-way channel of communications between the objects being created or modified. In addition they also provide a direct channel into the details of all the objects that make up the AutoCAD system, including those that only exist during the execution of a program. Procedural programs using entity data lists have access to the essential parameters however objects provide methods that greatly simplify things. As an example, a procedural system may know the radius of a circle since that is a basic parameter. An object system knows not only the radius,

Procedural

Easy to conceptualize as an experienced AutoCAD operator.
Limited future growth, must rely on tricks to get something special to happen.
Command sequences.
Object Good interaction with well programmed objects.
Direct access to "guts"
Not always easy to program from an operator’s point of view.
The future of AutoCAD programming.
Program Language Examples

Three program examples are presented that demonstrate how the same simple utility can be defined in each language. The language examples are Visual LISP, VBA, and ObjectARX. The simple example is to perform the COPY command multiple times using the same selection set. Each line of code is not explained as that is up to you to learn on your own by looking at the examples provided. Teaching all the nuances of each language is way beyond the scope of this article.

Program Logical description (Pseudo code)

Operator picks objects to be copied into a selection set.
Operator picks a point to serve as the base point.
While the Operator picks a second point.
   Copy the entities in the selection set to the new point.

Visual LISP
Visual LISP example uses COPY command to produce copies.

(defun C:MCopy ( / SS1 P1 P2)
  (prompt "\nMultiple copy.")
  (setq SS1 (ssget))
  (if SS1 (progn
     (setq P1 (getpoint "\nBase point: "))
     (while (setq P2 (getpoint P1 " too point:"))
         (command "_COPY" SS1 P1 P2)))))
 
 

Visual BASIC
VBA example uses COPY method of entity objects to produce copy command.

Public Sub MCOPY()
 Dim SS1 As AcadSelectionSet
 Dim aObj As AcadEntity
 Dim nObj As AcadEntity
 On Error Resume Next
 Set SS1 = _ ThisDrawing.SelectionSets.Add("SS1")
 If Err.Number <> 0 Then
   Set SS1 = _ ThisDrawing.SelectionSets.Item("SS1")
   SS1.Clear
   Err.Clear
 End If
 SS1.SelectOnScreen
 Dim P1 As Variant
 Dim P2 As Variant
 P1 = ThisDrawing.Utility.GetPoint(, _ "Base point: ")
 While Err.Number = 0
     P2 = ThisDrawing.Utility.GetPoint(P1, _ "Too point: ")
     If Err.Number = 0 Then
        For Each aObj In SS1
          Set nObj = aObj.Copy
          nObj.Move P1, P2
       Next aObj
    End If
 Wend
End Sub
 

ObjectARX with C++
C++ ARX example uses COPY command to produce copies.

#include <adesk.h>
#include "adslib.h"
#include "dbsymtb.h"
#include <rxdefs.h>
#include <aced.h>

void mcopy_run(){
  acutPrintf("\nMCOPY - multiple copy");
  int iRet;
  ads_name SS1;
  ads_point ptBase, ptTo;
  iRet = acedSSGet(NULL,NULL,NULL,NULL,SS1);
  if (iRet == RTNORM){
    iRet = acedGetPoint(NULL,"\nBase point:",ptBase);
    while (iRet == RTNORM){
      iRet = acedGetPoint(ptBase," copy point: ",ptTo);
      if (iRet == RTNORM) {
         acedCommand(RTSTR,"_COPY",RTPICKS, SS1, RTSTR,"",
                     RTPOINT, ptBase,RTPOINT, ptTo,0);
      }
    }
  }
}

// Entry point function
extern "C" AcRx::AppRetCode
  acrxEntryPoint(AcRx::AppMsgCode msg, void* pkt){
  switch (msg) { //msg tells us what to do
    case AcRx::kInitAppMsg:
      acrxDynamicLinker->unlockApplication(pkt);
      acrxDynamicLinker->registerAppMDIAware(pkt);
      acedRegCmds->addCommand("MY_COMMANDS", "MCOPY", "MCOPY",
                              ACRX_CMD_MODAL, mcopy_run);
      break;
    case AcRx::kUnloadAppMsg:
      acutPrintf("\nCommand group removal");
      acedRegCmds->removeGroup("MY_COMMANDS");
      break;
    default:
      break;
  }
  return AcRx::kRetOK;
}

Keep on programmin’