PDF

Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

AutoCAD

Programming
in C/C++
Owen Ransen

JOHN WILEY & SONS


Chichester • New York • Weinheim • Brisbane • Singapore • Toronto
Contents

PREFACE XV
Who Should Read This Book? xv
What Do I Need To Start Programming Autocad In C And C++? xvi
A Note On The Text xvii
Source Code Availability xvii
Acknowledgements xviii
Contacting The Author xviii

1 PROGRAMMING AUTOCAD 1
1.1 Introduction 1
1.2 why Use Autocad? 1
1.3 Autolisp, C Or C++? 2
1.4 Pure Ads, Arx And Rxads 5
1.4.1 Pure Ads 5
1.4.2 Arx 6
1.4.3 Rxads 7
1.4.4 Mixed Ads And Arx Programming 7

2 GETTING STARTED IN C FOR AUTOCAD 8


2.1 Introduction 8
2.2 Communicating With Autocad 8
2.3 Drawing Nested Polygons 12
2.4 Loading Ads Programs. 15
vi AutoCAD Programming in C/C++

2.4.1 Loading Ads Programs Manually. 15


2.4.2 Loading Ads Programs Automatically 16
2.5 Code Fragments And The Example Program. 16
2.6 Listings 18

3 GETTING AUTOCAD TO DO THE WORK 24


3.1 Introduction 24
3.2 Telling Autocad What To Do: The Ads_Command() Function 24
3.2.1 Ads_Command() With No User Input 24
3.2.2 Ads_Command(), Pausing For User Input 26
3.2.3 Ads_Command() And Wild-Cards 27
3.2.4 Using Ads_Command() To Execute Shell 27
3.3 Ads Types And Type Specifiers 28
3.3.1 Basic Ads C Types 28
3.3.2 Linking In Non-Ads Files 28
3.3.3 Type Defining Codes 29
3.3 Command Echoing 30
3.4 Listings 31

4 USER INPUT 35
4.1 Introduction 35
4.2 User Input With The Ads_Getxxx() Functions 35
4.3 Using Ads_Initget() To Control User Input 36
4.4 Allowing Default Responses To Ads_Getxxx(), Rsg_Nonull 37
4.5 Ads_Getxxx() Return Values, Gracefully Accepting Rejection, Rtcan 38
4.6 Keyword Input To Help Selection 38
4.7 Two Ready-Made Dialog Boxes 40
4.7.1 The Ads_Alert() Function 40
4.7.2 The File Selection Dialog Box 41
4.8 Other Text Output 42
4.9 Long Loops And Aborting Commands, Ads_Usrbrk() 43
4.10 Listings 45

5 RESULT BUFFERS, VARIABLES AND SYMBOL TABLES 52


5.1 Introduction 52
5.2 Result Buffers And Data Types 52
5.3 Autocad Variables 58
5.3.1 What Are Autocad Variables? 58
5.3.2 Some Commonly Used Variables 59
5.3.3 Getting And Setting Variable Values 61
5.4 Symbol Tables 61
5.4.1 Data Contained In Symbol Tables 61
5.4.2 Accessing Symbol Table Data 62
5.4.3 Creating And Modifying Layers 64
Contents vii

5.5 Listings 65

6 ACCESSING THE AUTOCAD DATABASE WITH ADS 80


6.1 Introduction 80
6.2 Scanning The Drawing Database, Ads_Entnext() And Ads_Entget() 80
6.3 Ads_Ssget() 82
6.3.1 An Overview Of Ads_Ssget() 82
6.3.2 Prompting The User For Entities 84
6.3.3 Filtering The Database To Produce A Selection Set 84
6.3.4 Filtering The User's Selection Set 85
6.3.5 Ads_Buildlist() And Filters With Boolean Operators 86
6.4 Creating, Modifying And Deleting Entities 87
6.4.1 Creating Entities With Ads_Entmake() 87
6.4.2 Modifying Existing Entities With Ads_Entmod() 88
6.4.3 Deleting And Undeleting Entities With Ads_Entdel() 88
6.5 Saving And Restoring The User's Settings 89
6.6 Listings 90

7 ADS GEOMETRIC UTILITIES 102


7.1 Introduction 102
7.2 Finding Distance, Angle And Intersection Point 102
7.3 Matrix And Vector Utilities 103
7.4 Listings 105

8 BLOCKS AND POLYLINES 113


8.1 Introduction 113
8.2 Using Blocks To Save Space And Time 113
8.3 A Function To Create Blocks 114
8.4 External Drawings As Blocks, Xrefs 115
8.5 Creating Names For Blocks 115
8.6 A Faster Block Creating Function 116
8.7 Reading Block Data 117
8.8 Polylines 119
8.8.1 Modifying Polylines 120
8.9 Listings 122

9 DRAWING IN 3D AND 3D COORDINATE SYSTEMS 134


9.1 Introduction 134
9.2 A Comparison Of Two Methods For Adding Entities 134
9.3 Coordinate Systems In Autocad 137
9.3.1 The User Coordinate System 137
viii AutoCAD Programming in C/C++

9.3.2 The Ucs Icon 139


9.3.3 The World Coordinate System 140
9.3.4 The Entity Coordinate System 140
9.3.4 The Model Coordinate System 141
9.4 3d Lines And 2d Lines. 141
9.5 Local And Global Object Positions 144
9.5.1 The Function Ads_Nentselp() 144
9.5.2 Distances To Robot Sensors 145
9.6 Transformations In Ads 148
9.7 Listings 150

10 3D OBJECTS AND RENDERING 164


10.1 Introduction 164
10.2 The 3dface Entity 164
10.3 Shading And Rendering 168
10.3.1 Shading, Shadedge And Shadedif 168
10.3.2 The Render Command 168
10.4 Viewing Parameters And Variables 168
10.5 Meshes 171
10.6 Other 3d Objects 172
10.7 Listings 174

11 WORKING WITH EXTENDED DATA 181


11.1 Introduction 181
11.2 Why Use Extended Data? 181
11.3 The Structure Of An Entity's Extended Data 182
11.4 Registering Your Application, Ads_Regapp() 183
11.5 Reading And Writing Extended String Data 184
11.6 Reading And Writing Other Types Of Extended Data 187
11.7 Space Limits Of Extended Data, Ads_Xdsize() And Ads_Xdroom() 187
11.7.1 Extended Data And Arx 188
11.8 General Non-Graphical Data 188
11.9 Listings 189

12 DIALOG BOXES AND THE DIALOG CONTROL LANGUAGE 199


12.1 Introduction 199
12.2 What Is Possible With Del? 199
12.2.1 Ready-Made Del Files 201
12.3 A General And Systematic Dialog Box Handler In C 201
12.3.1 The Sample Del File 201
12.3.2 A Dialog Box Handler In C 203
12.3.3 Del Functions: Ads_Load_Dialog() And Ads_New_Dialog(). 206
Contents ix

12.3.4 Del Functions: Ads_Action_Tile() And Ads_Set_Tile() 206


12.3.5 The Callback Functions And Ads_Get_Attr_String() 209
12.3.6 The Ads_Done_Dialog() Function 210
12.3.7 A Summary Of Lst_12_l.C 210
12.3.8 Adding A New Dialog 210
12.3.9 List Handling, Ads_Start_List(), Ads_Add_List(), Ads_End_List() 211
12.4 Other Features Of Del 212
12.4.1 Rows And Columns 212
12.4.2 Auditing 212
12.4.4 Further Reading 213
12.5 Listings 213

13 PRACTICAL SUGGESTIONS FOR PROGRAMMING AUTOCAD 231


13.1 Introduction 231
13.2 Memory Management And Ads 231
13.2.1 Problems With The C Standard Malloc() Function 231
13.2.2 Safe_Calloc(), A Less Cumbersome Allocator 232
13.2.3 Avoiding The "Fatal Error" Message, Check Your Memory 233
13.2.4 Free Your Malloced Memory 235
13.3 Tidier And Shorter Listings With Void_Ads Functions 236
13.4 Internationalisation 236
13.4.1 International Date And Time 237
13.5 Software Protection Of Your Programs 238
13.6 When Equal Points Are Not 239
13.7 Compiling The Same Sources With Different Compilers 240
13.8 Unlocking Locked Drawings 241
13.9 Do Not Modify Your Application As It Runs! 241
13.10 Which Error Function To Use, Ads_Fail(), Ads_Abort(), Ads_Exit()? 242

14 INTRODUCTION TO ARX 244


14.1 Introduction 244
14.2 The Goals Of Arx 244
14.3 The Arx Implementation And "Exposure" 245
14.4 Which To Use, Ads Or Arx? 246
14.4.1 When To Use Arx 246
14.4.2 When To Use Ads 246
14.5 An Overview Of Arx 247
14.5.1 The Arx Libraries 248
14.5.2 The Arx View Of The Autocad Drawing 248
14.7 Deriving Custom Objects From Autocad Object Classes. 249
14.8 Notification And Reactors 250
14.9 Arx Dictionaries 251

15 FIRST STEPS WITH ARX 253


X AutoCAD Programming in C/C++

15.1 Introduction 253


15.2 A Minimal Arx Program 253
15.2.1 The Include Files Of An Arx Source File 254
15.2.2 The Entry Point Of An Arx Application 255
15.2.3 Creating Your Own Commands In An Arx Application,
Command Groups 256
15.2.4 Definition Files 258
15.3 Loading And Unloading Arx Applications 258
15.3.1 Manually Loading Arx Applications 258
15.3.2 Automatic Loading Of Arx Applications 259
15.3.3 Unlocking And Unloading Arx Applications 259
15.4 Creating Entities In Arx Applications 260
15.4.3 The Definition Of A Line As A C++ Object 260
15.4.4 Creating, Opening, Deleting And Closing Objects 261
15.4.5 Adding An Acdbentity To The Database 263
15.4.6 Getting Variable Settings From The Database 265
15.5 Object Identifiers, And Ways Of Opening An Object 266
15.5.1 Object Identifiers 266
15.5.2 Opening An Object For Read, Acdb::Kforread 268
15.5.3 Opening An Object For Write, Acdb::Kforwrite 268
15.5.4 Objects Opened For Notify, Acdb::Kfornotify 269
15.6 Ownership Of Objects And Root Objects 269
15.7 Changing Objects In The Database 270
15.7.1 Iskindof(), Cast() And Desc() 270
15.8 Listings 272

16 ARX ITERATORS, GEOMETRY CLASSES AND CLONING 282


16.1 Introduction 282
16.2 Arx Iterators 282
16.2.1 Listing The Contents Of The Model Space 282
16.2.2 Changing The Internals Of A Block 283
16.2.3 Iterating Through The Vertices Of A Polyline 285
16.3 The Arx Geometry Library 285
16.3.1 The Difference Between Acdbpoint And Acgepoint3d 285
16.3.2 The Three-Dimensional Point Class: Acgepoint3d 286
16.3.3 The Hierarchy Of The Acge Geometry Class Library 289
16.3.4 The Acgevector3d And Acgematrix3d Classes 290
16.4 Copying Entities With Clone And Deepclone() 293
16.4.1 Copying Simple Entities With Clone 293
16.4.2 Copying Complex Entities With Deepclone 295
16.5 Erasing And Unerasing Objects 297
16.5.1 Unerasing Objects 297
16.5.2 Opening Erased Objects 298
16.5.3 Opening And Unerasing Erased Objects 298
Contents xi

16.6 Listings 298

17 DERIVED ENTITIES AND DICTIONARIES. 313


17.1 Introduction 313
17.2 Using Inheritance To Create Custom Entities 313
17.2.1 Which Member Functions To Overload When Deriving A
Custom Entity? 314
17.2.2 Zombie Classes 315
17.2.3 A Simple Example Of Deriving A Custom Class, A Special Circle 316
17.2.4 Dynamic Sizing Of The Circle 317
17.2.5 Handing Over Entities 319
17.3 Dictionaries 320
17.3.1 Extension Dictionaries 320
17.3.2 The Named Object Dictionary 320
17.3.3 Creating A New Dictionary 320
17.3.4 Giving Custom Objects Persistence 321
17.3.4 Creating And Accessing Custom Dictionaries 322
17.3.5 Registering New Classes With Autocad 325
17.4 Dictionaries And Ads 325
17.5 Listings 325

18 DXF, THE DRAWING EXCHANGE FORMAT 339


18.1 Introduction 339
18.2 Using Dxf Files 340
18.2.1 The Dxfin And Dxfout Commands 340
18.2.2 The Dxfix Program 341
18.3 An Example Dxf File And Some Dxf Groups 342
18.4 The Dxf Sections 344
18.4.1 The Header Section 344
18.4.2 The Tables Section 345
18.4.3 The Blocks Section 345
18.4.4 The Entities Section 346
18.5 A Dxf Writing Program 346
18.6 A Dxf Reading Program 348
18.7 Future Developments Of The Dxf Format 349
18.8 Listings 349

19 CASE STUDDZS AND APPLICATIONS 359


19.1 Introduction. 359
19.2 Case Study: Moonlite An Interior Lighting Design Package 359
19.3 Case Study: Drilldwg.Exp, A Pcb Drill Hole Plotter 363
19.4 Back To Lisp 364
xii AutoCAD Programming in C/C++

19.4.1 Lisp2c, Going From Autolisp To C Automatically 364


19.4.2 5 Vital Lisp, A Lisp Compiler For Autocad 364
19.5 Gis, Geographical Information Systems 365
19.6 Medical Engineering Cad 366
19.7 Constructing Models For Virtual Reality 366
19.8 Exersises For The Reader 367
19.9 Listings 368

20 AUTOLISP AND ADS COMMUNICATIONS 371


20.1 Introduction 371
20.2 Communicating With Autolisp 371
20.2.1 Sending Data From Autolisp To Ads 371
20.2.2 Returning Data From Ads To Autolisp 372
20.2.3 Testing Lst_20_l.C 372
20.3 Communication Between Ads Applications 373
20.4 Listings. 375

APPENDIX A COMPILERS AND ENVIRONMENTS 383


A.l Introduction 383
A. 1.1 Installing The Compiler And Autocad 384
A.2 Ads For Extended Dos Autocad Release 12 384
A.2.1 Compiling The Example Programs 384
A.3 Compiling For Release 12 Extended Dos 387
A.4 Ads For Windows 3.1 And Windows-95, Autocad 12, Watcom 9.0 390
A.5 Ads For Windows 3.1 And Windows-95, Autocad 12, Borland 3.X,4.X 390
A.6 Ads For Releases 12 & 13, Dos, Using Watcom 10.X 391
A.7 Ads For Release 13 Under Windows-95 Compiled Using Visual C++ 392
A. 8 Rxads For Windows-95 Compiled Using Microsoft Visual C++ 394
A.9 Arx For Windows-95 Compiled Using Microsoft Visual C++ 395
A. 10 Tips And Tricks For Ads And Arx Programmers 396
A. 10.1 Searching For Examples And Class Definitions Source Files 396
A. 10.2 Debugging With Visual C++ 397
A. 10.3 Common Visual C++Errors 397

APPENDIX B CATALOG O F ADS FUNCTIONS 400


B.l Introduction 400
B.2 Alphabetical Listing Of Ads And Arx Function 400

APPENDIX C AUTOCAD SYSTEM VARIABLES 424


C.l Introduction 424
Contents xiii
C.2 Autocad System Variables 425

APPENDIX D DXF CODES 452


D.l Introduction 452
D.2 Negative Group Codes 452
D.3 Positive Group Codes 452
D.4 Group Codes Related To Graphical Entities 454
D.4.1 Common Group Codes 454
D.4.2 Group Codes Of Specific Graphic Entities 454
D.5 Group Codes Which Apply To All Non-Graphical Entities 465
D.6 Group Codes For Non-Graphical Entities By Entity 465

APPENDIX E ERROR CODES 472


E.l Introduction 472
E.2 Ads Error Codes 472
E.3 Arx Error Codes, Differences Between Errno And Arx Error Values 474

END ix F FURTHER SOURCES O F INFORMATION 478


F.l Introduction 478
F.2 Books 478
F.2.1 C Programming 478
F.2.2 C++ Programming 478
F.2.3 Graphics Progamming 479
F.2.4 Autocad 479
F.3 Magazines 480
F.4 World Wide Web Addresses And Internet User Groups 481
F.5 Company And Society Addresses 481

INDEX 483

You might also like