Ransen CAD Software
CAD programs, consulting, and educational materials

Ransen Cad Home

Pointor

Dexterpen

ADS C++ Book

Consulting

 

 

 

AutoCAD Programming in C/C++

Book Cover

Owen F. Ransen

ISBN 0 471 963 364 Publisher: John Wiley & Sons

Click here to download R14 ARX Source Code Update (approx. 25k)


Overview of "AutoCAD Programming in C/C++"

This book is for you if you want to program AutoCAD in C or C++. Maybe you want to go beyond AutoLISP for reasons of speed or complexity, or maybe you do not know AutoLISP but need to program an AutoCAD application in a standard language like C or C++.

The book covers AutoCAD R12, R13 and, with a few changes, R14. The book is also useful for AutoCAD 11 users who have to program using ADS, though some of the ADS functionality of R12 and R13 is missing in R11. The attached floppy disk contains sources to all the programs in the book.

The book starts with an introduction to programming AutoCAD, and how to choose the language most suitable to your needs,

  • AutoLISP

  • ADS, AutoCAD Development System
  • rxADS, like ADS but faster and more dangerous
  • ARX, AutoCAD Runtime eXtension, the C++ Object Oriented API to AutoCAD

The largest part of the book is dedicated to ADS, which is logical because ADS is very stable now, and to program in ARX a knowledge of ADS is required. There are examples of how to create and change AutoCAD entities, how to read and write extended data (XDATA) into AutoCAD entities. There are explantions of 3D co-ordinate systems form the programmer's point of view

There is a single chapter dedicated to DCL the Dialog Control Language used to create dialog boxes for AutoCAD. While Windows (-95 and -NT) is becoming more important, DCL is still useful if you have to program for other platforms (e.g.UNIX) of if you have some ready made DCL files from your current programs.

About a third of the book is dedicated an introduction to ARX (which will soon be in its second release under the name Object ARX 2.0). ARX offers speed, power and danger due to its close linkage to the actual code of AutoCAD.

The last three chapters are dedicated to more general AutoCAD programmings topics:

Chapter 18 is about the DXF format, containing two examples programs of how to read and write DXF files. DXF is one of the most widely used file formats for graphical and engineering data. There are tips and suggestions for programmers who have to deal with the DXF format.

Chapter 19 covers a few case studies and suggestions for furture applications just waiting to be written and sold. One of the case studies is MOONLITE , a lighting technology application for AutoCAD.

The last chapter is about communication between different ADS-ARx and AutoLISP applications. How do you send data from ADS to AutoLISP and vice versa? For those of you with large AutoLISP programs it is well worth considering porting the slowest parts of the LISP program into functions in C. This chapter will help you!

The book ends with six appendices useful to AutoCAD programmers of all ages!


Contents of "AutoCAD Programming in C/C++"

PREFACE

Who should read this book?

What do I need to start programming AutoCAD in C and C++?

A note on the text

Source code availability

1 PROGRAMMING AUTOCAD

1.2 Why use AutoCAD?

1.3 AutoLISP, C or C++?

1.4 Pure ADS, ARX and rxADS

1.4.1 Pure ADS

1.4.2 ARX

1.4.3 rxADS

1.4.4 Mixed ADS and ARX programming

2 GETTING STARTED IN C FOR AUTOCAD

2.1 Introduction

2.2 Communicating with AutoCAD

2.3 Drawing nested polygons

2.4 Loading ADS programs.

2.4.1 Loading ADS programs manually.

2.4.2 Loading ADS programs automatically.

2.5 Code fragments and the example program.

2.6 Listings

3 GETTING AUTOCAD TO DO THE WORK

3.1 Introduction

3.2 Telling AutoCAD what to do: the ads_command() function

3.2.1 ads_command() with no user input

3.2.2 ads_command(), pausing for user input

3.2.3 ads_command() and wild-cards

3.2.4 Using ads_command() to execute SHELL

3.3 ADS types and type specifiers

3.3.1 Basic ADS C types

3.3.2 Linking in non-ADS files

3.3.3 Type defining codes

3.3 Command echoing

3.4 Listings

4 USER INPUT

4.1 Introduction

4.2 User input with the ads_getxxx() functions

4.3 Using ads_initget() to control user input

4.4 Allowing default responses to ads_getxxx(), RSG_NONULL

4.5 ads_getxxx() return values, gracefully accepting rejection, RTCAN

4.6 Keyword input to help selection

4.7 Two ready-made dialog boxes

4.7.1 The ads_alert() function

4.7.2 The file selection dialog box

4.8 Other text output

4.9 Long loops and aborting commands, ads_usrbrk()

4.10 Listings

5 RESULT BUFFERS, VARIABLES AND SYMBOL TABLES

5.1 Introduction

5.2 Result buffers and data types

5.3 AutoCAD variables

5.3.1 What are AutoCAD variables?

5.3.2 Some commonly used variables

5.3.3 Getting and setting variable values

5.4 Symbol tables

5.4.1 Data contained in symbol tables

5.4.2 Accessing symbol table data

5.4.3 Creating and modifying layers

5.5 Listings

6 ACCESSING THE AUTOCAD DATABASE WITH ADS

6.1 Introduction

6.2 Scanning the drawing database, ads_entnext() and ads_entget()

6.3 ads_ssget()

6.3.1 An overview of ads_ssget()

6.3.2 Prompting the user for entities

6.3.3 Filtering the database to produce a selection set

6.3.4 Filtering the user's selection set

6.3.5 ads_buildlist() and filters with boolean operators

6.4 Creating, modifying and deleting entities

6.4.1 Creating entities with ads_entmake()

6.4.2 Modifying existing entities with ads_entmod()

6.4.3 Deleting and undeleting entities with ads_entdel()

6.5 Saving and restoring the user's settings

6.6 Listings

7 ADS GEOMETRIC UTILITIES

7.1 Introduction

7.2 Finding distance, angle and intersection point

7.3 Matrix and vector utilities

7.4 Listings

8 BLOCKS AND POLYLINES

8.1 Introduction

8.2 Using blocks to save space and time

8.3 A function to create blocks

8.4 External drawings as blocks, XREFS

8.5 Creating names for blocks

8.6 A faster block creating function

8.7 Reading block data

8.8 Polylines

8.8.1 Modifying polylines

8.9 Listings

9 DRAWING IN 3D AND 3D COORDINATE SYSTEMS

9.1 Introduction

9.2 A comparison of two methods for adding entities

9.3 Coordinate systems in AutoCAD

9.3.1 The user coordinate system

9.3.2 The UCS icon

9.3.3 The world coordinate system

9.3.4 The entity coordinate system

9.3.4 The model coordinate system

9.4 3D lines and 2D lines.

9.5 Local and global object positions

9.5.1 The function ads_nentselp()

9.5.2 Distances to robot sensors

9.6 Transformations in ADS

9.7 Listings

10 3D OBJECTS AND RENDERING

10.1 Introduction

10.2 The 3DFACE entity

10.3 Shading and rendering

10.3.1 Shading, SHADEDGE and SHADEDIF

10.3.2 The RENDER command

10.4 Viewing parameters and variables

10.5 Meshes

10.6 Other 3D objects

10.7 Listings

11 WORKING WITH EXTENDED DATA

11.1 Introduction

11.2 Why use extended data?

11.3 The structure of an entity's extended data

11.4 Registering your application, ads_regapp()

11.5 Reading and writing extended string data

11.6 Reading and writing other types of extended data

11.7 Space limits of extended data, ads_xdsize() and ads_xdroom()

11.7.1 Extended data and ARX

11.8 General non-graphical data

11.9 Listings

12 DIALOG BOXES AND THE DIALOG CONTROL LANGUAGE

12.1 Introduction

12.2 What is possible with DCL?

12.2.1 Ready-made DCL files

12.3 A general and systematic dialog box handler in C

12.3.1 The sample DCL file

12.3.2 A dialog box handler in C

12.3.3 DCL functions: ads_load_dialog() and ads_new_dialog().

12.3.4 DCL functions: ads_action_tile() and ads_set_tile()

12.3.5 The callback functions and ads_get_attr_string()

12.3.6 The ads_done_dialog() function

12.3.7 A summary of LST_12_1.C

12.3.8 Adding a new dialog

12.3.9 List handling, ads_start_list(), ads_add_list(), ads_end_list()

12.4 Other features of DCL

12.4.1 Rows and columns

12.4.2 Auditing

12.4.4 Further reading

12.5 Listings

13 PRACTICAL SUGGESTIONS FOR PROGRAMMING AUTOCAD

13.1 Introduction

13.2 Memory management and ADS

13.2.1 Problems with the C standard malloc() function

13.2.2 Safe_Calloc(), a less cumbersome allocator

13.2.3 Avoiding the "Fatal Error" message, check your memory

13.2.4 Free your malloced memory

13.3 Tidier and shorter listings with void_ads functions

13.4 Internationalisation

13.4.1 International date and time

13.5 Software protection of your programs

13.6 When equal points are not

13.7 Compiling the same sources with different compilers

13.8 Unlocking locked drawings

13.9 Do not modify your application as it runs!

13.10 Which error function to use, ads_fail(), ads_abort(), ads_exit()?

14 INTRODUCTION TO ARX

14.1 Introduction

14.2 The goals of ARX

14.3 The ARX implementation and "exposure"

14.4 Which to use, ADS or ARX?

14.4.1 When to use ARX

14.4.2 When to use ADS

14.5 An overview of ARX

14.5.1 The ARX libraries

14.5.2 The ARX view of the AutoCAD drawing

14.7 Deriving custom objects from AutoCAD object classes.

14.8 Notification and reactors

14.9 ARX dictionaries

15 FIRST STEPS WITH ARX

15.1 Introduction

15.2 A minimal ARX program

15.2.1 The include files of an ARX source file

15.2.2 The entry point of an ARX application

15.2.3 Creating your own commands in an ARX application, command groups

15.2.4 Definition files

15.3 Loading and unloading ARX applications

15.3.1 Manually loading ARX applications

15.3.2 Automatic loading of ARX applications

15.3.3 Unlocking and unloading ARX applications

15.4 Creating entities in ARX applications

15.4.3 The definition of a line as a C++ object

15.4.4 Creating, opening, deleting and closing objects

15.4.5 Adding an AcDbEntity to the database

15.4.6 Getting variable settings from the database

15.5 Object identifiers, and ways of opening an object

15.5.1 Object identifiers

15.5.2 Opening an object for read, AcDb::kForRead

15.5.3 Opening an object for write, AcDb::kForWrite

15.5.4 Objects opened for notify, AcDb::kForNotify

15.6 Ownership of objects and root objects

15.7 Changing objects in the database

15.7.1 isKindOf(), cast() and desc()

15.8 Listings

16 ARX ITERATORS, GEOMETRY CLASSES AND CLONING

16.1 Introduction

16.2 ARX iterators

16.2.1 Listing the contents of the model space

16.2.2 Changing the internals of a block

16.2.3 Iterating through the vertices of a polyline

16.3 The ARX geometry library

16.3.1 The difference between AcDbPoint and AcGePoint3d

16.3.2 The three-dimensional point class: AcGePoint3d

16.3.3 The hierarchy of the AcGe geometry class library

16.3.4 The AcGeVector3d and AcGeMatrix3d classes

16.4 Copying entities with clone and deepClone()

16.4.1 Copying simple entities with clone

16.4.2 Copying complex entities with deepClone

16.5 Erasing and unerasing objects

16.5.1 Unerasing objects

16.5.2 Opening erased objects

16.5.3 Opening and unerasing erased objects

16.6 Listings

17 DERIVED ENTITIES AND DICTIONARIES.

17.1 Introduction

17.2 Using inheritance to create custom entities

17.2.1 Which member functions to overload when deriving a custom entity?

17.2.2 Zombie classes

17.2.3 A simple example of deriving a custom class, a special circle

17.2.4 Dynamic sizing of the circle

17.2.5 Handing over entities

17.3 Dictionaries

17.3.1 Extension dictionaries

17.3.2 The named object dictionary

17.3.3 Creating a new dictionary

17.3.4 Giving custom objects persistence

17.3.4 Creating and accessing custom dictionaries

17.3.5 Registering new classes with AutoCAD

17.4 Dictionaries and ADS

17.5 Listings

18 DXF, THE DRAWING EXCHANGE FORMAT

18.1 Introduction

18.2 Using DXF files

18.2.1 The DXFIN and DXFOUT commands

18.2.2 The DXFIX program

18.3 An example DXF file and some DXF groups

18.4 The DXF sections

18.4.1 The HEADER section

18.4.2 The TABLES section

18.4.3 The BLOCKS section

18.4.4 The ENTITIES section

18.5 A DXF writing program

18.6 A DXF reading program

18.7 Future developments of the DXF format

18.8 Listings

19 CASE STUDIES AND APPLICATIONS

19.1 Introduction.

19.2 Case Study: MOONLITE an interior lighting design package

19.3 Case study: DRILLDWG.EXP, a PCB drill hole plotter

19.4 Back to LISP

19.4.1 LISP2C, going from AutoLISP to C automatically

19.4.2 5 VITAL LISP, a LISP compiler for AutoCAD

19.5 GIS, geographical information systems

19.6 Medical engineering CAD

19.7 Constructing models for virtual reality

19.8 Exersises for the reader

19.9 Listings

20 AUTOLISP AND ADS COMMUNICATIONS

20.1 Introduction

20.2 Communicating with AutoLISP

20.2.1 Sending data from AutoLISP to ADS

20.2.2 Returning data from ADS to AutoLISP

20.2.3 Testing LST_20_1.C

20.3 Communication between ADS applications

20.4 Listings.

APPENDIX A COMPILERS AND ENVIRONMENTS

A.1 Introduction

A.1.1 Installing the compiler and AutoCAD

A.2 ADS for Extended DOS AutoCAD Release 12

A.2.1 Compiling the example programs

A.3 Compiling for Release 12 Extended DOS

A.4 ADS for Windows 3.1 and Windows-95, AutoCAD 12, WATCOM 9.0

A.5 ADS for Windows 3.1 and Windows-95, AutoCAD 12, Borland 3.X,4.X

A.6 ADS for Releases 12 & 13, DOS, using WATCOM 10.X

A.7 ADS for Release 13 under Windows-95 compiled using Visual C++

A.8 rxADS for Windows-95 compiled using Microsoft Visual C++

A.9 ARX for Windows-95 compiled using Microsoft Visual C++

A.10 Tips and tricks for ADS and ARX programmers

A.10.1 Searching for examples and class definitions source files

A.10.2 Debugging with Visual C++

A.10.3 Common Visual C++ errors

APPENDIX B CATALOG OF ADS FUNCTIONS

B.1Introduction

B.2Alphabetical listing of ADS and ARX functions

APPENDIX C AUTOCAD SYSTEM VARIABLES

C.1Introduction

C.2 AutoCAD system variables APPENDIX D DXF CODES

D.1Introduction

D.2 Negative group codes

D.3 Positive group codes

D.4 Group codes related to graphical entities

D.4.1 Common group codes

D.4.2 Group codes of specific graphic entities

D.5 Group codes which apply to all non-graphical entities

D.6 Group codes for non-graphical entities by entity

APPENDIX E ERROR CODES

E.1. Introduction

E.2. ADS error codes

E.3 ARX error codes, differences between ERRNO and ARX error values

APPENDIX F FURTHER SOURCES OF INFORMATION

F.1 Introduction

F.2 Books

F.2.1 C programming

F.2.2 C++ programming

F.2.3 Graphics progamming

F.2.4 AutoCAD

F.3 Magazines

F.4. World Wide Web addresses and Internet user groups

F.5. Company and society addresses


Errata (08 Feb 1997)

  1. Autodesk have changed the name of acdbOpenEntity() to acdbOpenObject() so you may have to use the new name depend on the age of your ARX include files or libraries.

  2. New ARX libraries may no longer use getHeaderVar(). So instead of Res = pDb->getHeaderVar ("osmode"); you would write pDb->osmode () ; To write to a variable you will use pDB->setOsmode (32). To know which version you should use look in the RX include file DBMAIN.H.
  3. The above is not true of the variable DWGNAME, which is not part of the AutoCAD database. You may have to use ds_getvar() (even in an ARX C++ program) to get the drawing name. See Chapter 5 Section 5.3.2 of the book, page 61.
  4. ads_getvar() is not in the index. Sorry! See Page 61.
  5. An easier way to link in the ARX libraries in VC4: Delete the ARX libraries from the file view (highlight them and then hit the DEL key) In "Build|Setting|Link|General|Object/Library modules" add the following path: "\R13\WIN\ARX\LIB\*.LIB". The actual path on your computer may be different, so check it.

 

   
www.ransen.com
(c) Ransen Software 2003