2014-10-31



Introduction

About three weeks ago I received an email request from Mohammed. He asked me if it is possible to send AutoCAD commands directly form Excel. To cut a long story short, the answer is yes and the proposed solution is based on the SendCommand method of AutoCAD, which sends a command string from a VB or VBA application to the document to be processed. If the drawing specified isn’t active, it will be made active. This method processes any AutoCAD command-line function, including LISP expressions. So, based on AutoCAD VBA help, the structure of SendCommand method is given below:

object.SendCommand(Command)

Object: (AutoCAD) Document. The object this method applies to.
Command: String; input-only. The command to send to the document.

Note that the sample workbook that you will find in the Downloads section works only with valid AutoCAD commands (obviously). Moreover, unlike the typical AutoCAD behavior, you must first select an object and then apply any modifications to it. So, for example, if you want to move a polyline, you must first send the select command and then the move; the opposite will NOT work!

VBA code

A large portion of the code is used to initialize the AutoCAD object, as well as the active/new drawing. The code is consisted of two loops; the first one concatenates the contents of every column into a single string, whereas the second one sends the concatenated string to AutoCAD via SendCommand method. One tricky part here was the usage of carriage-return character (vbCr) on Select and Select All (AI_SELALL) commands, which allows the objects to remain selected after the loop moves to the next command (which will make the object editing).

Note that if you have AutoCAD 2010 or a newer version, you will have to download and install the VBA module, otherwise the code will probably fail.

AutoCAD 2010 VBA module 32-bit

AutoCAD 2010 VBA module 64-bit

AutoCAD 2011 VBA module 32-bit

AutoCAD 2011 VBA module 64-bit

AutoCAD 2012 VBA module 32-bit

AutoCAD 2012 VBA module 64-bit

AutoCAD 2013 VBA module 32-bit

AutoCAD 2013 VBA module 64-bit

AutoCAD 2014 VBA module 32-bit

AutoCAD 2014 VBA module 64-bit

AutoCAD 2015 VBA module 32-bit

AutoCAD 2015 VBA module 64-bit

All links were copied from the Autodesk's website.

Warnings and suggestions

Please bear in mind that the particular code was NOT tested for every AutoCAD command that can be executed via command line. However, tests for the most “popular” ones shown that it works without problem (see the demonstration video). You just have to write the command sequence in Excel very carefully.

When you select an object try to create a selection rectangle that will include that object. This means that the selection rectangle – its coordinates – should be larger than the object dimensions.

The coordinates should be given in the format x,y for 2D and x,y,z for 3D objects; i.e. 0,100 and 200,200,100.

The "angular" coordinates should be given in the x<y for 2D and in x<y<z format for 3D; i.e. 0<45 and 100<45<90.

Note that the cells in the sample workbook are formatted as text, in order to avoid problems when the user enters decimal coordinates and the decimal symbol in his/her Windows settings (in Region and Language option at Control Panel) is set to be the comma (",") and not the dot ("."). Tip: AutoCAD uses a dot/point for decimal separator as a standard for displaying and entering decimal numbers. However, Dimensions are the exception to this rule, but, you can set their decimal separator by using the DIMDSEP command.

Probably the best way to use the sample workbook is to try to replicate every step that you follow when you are using AutoCAD. So, try to not forget command arguments or leave commands unfinished. Needless to say that some experimentation is required in order to get the expected result, but I think it worth the time.

Demonstration video

The short video below demonstrates the result of the above VBA code (31 AutoCAD commands were used); the blog name will be drawn into a new AutoCAD document using lines, polylines as well as AutoCAD objects.

Downloads



The file can be opened with Excel 2007 or newer. Please enable macros before using it.

Read also

Insert Blocks In AutoCAD Using Excel & VBA
Drawing Points In AutoCAD Using Excel & VBA
Add Text In AutoCAD Using Excel & VBA
Drawing Circles In AutoCAD Using Excel & VBA
Excel Range To AutoCAD Table Add-In

Show more