Creating Script Files with Excel

by Donnie Gladfelter on September 18, 2007

in AutoCAD,AutoCAD 2006,AutoCAD 2007,AutoCAD 2008

image Script files seem to be one of the many forgotten frontiers of AutoCAD.  “Back in the day” I knew of many CAD users who couldn’t go a day without running a script file or two.  These days – not so much.  Even still, the trick of yesterday can still help solve many tedious and mundane tasks today.

So what is an AutoCAD script file?  Quite simply a script file is a batch file for AutoCAD.  Windows batch files carry the BAT extension, whereas an AutoCAD script file carries a SCR extension.  The power of AutoCAD script files is in their simplicity.  Look under the hood, and they are nothing more than an ASCII text file containing an AutoCAD command on each line.

Admittedly as AutoCAD has progressed to a more and more graphical of an interface, it has become tougher to figure out the command line version of some commands.  Generally speaking a good place to start is by typing the command with either a “.”, “-”, or “_”.  From that point pay attention to the command line prompts, and begin building your script file.

Recently I received an e-mail from one of my readers inquiring about an easier way translate some raw data in Excel into AutoCAD linework.  Essentially the user had a couple thousand points, that he needed to draw lines between (and you thought connect the dots was only a game for kids).  So the question remained, how could one make AutoCAD play connect the dots based on Excel data.

My answer – get Excel to make an AutoCAD script.

True, one could create a LISP, VBA, or even a .NET application.  But for something like this, simplicity is favored over the advanced stuff offered in the other methods.

So how did I accomplish this feat?

  1. Take a look at the LINE command in AutoCAD
    I’ll admit, this seems a little elementary, but we need to be sure we know what prompts are included in the AutoCAD LINE command.
    So the LINE command prompts me for first a “start point”, and then the “next point”.  The “next point” prompt will continue until enter is pressed.  Knowing that I can begin structuring my script.
  2. Structuring your script
    Knowing the command sequence of the LINE command, I can now sit down and write the framework of my AutoCAD script.  In this case I chose to do the following:
    LINE #,# #,#
    Notice the spaces between each the command, and each coordinate value.  AutoCAD interprets this as an enter.
  3. Writing your Excel formula
    I structured my Excel table such that my first column was my X value, the second my Y value, and finally my third column the AutoCAD script.
    image

    My Excel formula looks like this:
    =”line “&A2&”,”&B2&” “&A3&”,”&B3&” ”
    image

  4. Creating the script file
    Once your Excel table is set up properly, simply copy and paste the “Script” column to a new Notepad file, and save it with a SCR extension (rather than a TXT) extension.
  5. 5. Run the Script file
    Running a script file is actually pretty easy.  Simply drag-and-drop the file into the drawing area of AutoCAD.

Scenarios like the one above are often unique.  Since you’re only going to use the solution once, its hard to justify more complex tools like VBA.  On the other hand script files are small, lightweight, and east to create, making them an AWESOME tool for such one-time-only scenarios.  Although Excel is not built to be a script file writer, it sure does do a good job (in my own opinion).

  • zlatan24

    I heard about not bad application-how to recover Excel files, it supports almost all existing versions of XLS and XLSX formats: Microsoft Excel 97, 2000, XP, 2002, 2003 and 2007 (xlsx repair), it helps to recover practically all the significant information, Program can repair Excel files Microsoft Windows NT 4.0, Windows 2000, Windows XP, Windows XP SP2, Windows 2003, Windows Vista. Software can to do the xlsx repair of Microsoft Ofiice 2007.

  • zlatan24

    I heard about not bad application-how to recover Excel files, it supports almost all existing versions of XLS and XLSX formats: Microsoft Excel 97, 2000, XP, 2002, 2003 and 2007 (xlsx repair), it helps to recover practically all the significant information, Program can repair Excel files Microsoft Windows NT 4.0, Windows 2000, Windows XP, Windows XP SP2, Windows 2003, Windows Vista. Software can to do the xlsx repair of Microsoft Ofiice 2007.

  • http://iwpparts.com Conrad

    You can also skip the creation of a script file and simply copy the “script” column from Excel and paste it directly into the AutoCAD command line. However you do it, using Excel to create scripts for AutoCAD can be a great time saver.

  • http://iwpparts.com/ Conrad

    You can also skip the creation of a script file and simply copy the “script” column from Excel and paste it directly into the AutoCAD command line. However you do it, using Excel to create scripts for AutoCAD can be a great time saver.

  • http://www.thecadgeek.com Donnie Gladfelter

    Herb – Depending on what you are trying to accomplish – spaces can cause problems with script files. There are a couple ways around this. What I generally default to is using LISP inside a script file. Thus things would look something like (Command “line” “1,1″…)

    Paul – When putting together this post I hadn’t thought about the polyline method. You’re right – that is probably a better way for this particular example. Thanks for the comment.

    • Allan M.

      Mr Donnie how to script text to 2007 fron excell to paste to 2007, please see sample below

      TEXT J TC 657752,2741632 0 allan
      TEXT J TC 657693,2741655 0 santaigo

      in R14 there was no problem but in higher Cadd i can't do. text is not coming because of "space" not recognize as enter.
      Hope u can help, thanks.

  • http://thecadgeek.com/ Donnie Gladfelter

    Herb – Depending on what you are trying to accomplish – spaces can cause problems with script files. There are a couple ways around this. What I generally default to is using LISP inside a script file. Thus things would look something like (Command “line” “1,1″…)

    Paul – When putting together this post I hadn’t thought about the polyline method. You’re right – that is probably a better way for this particular example. Thanks for the comment.

    • Allan M.

      Mr Donnie how to script text to 2007 fron excell to paste to 2007, please see sample below

      TEXT J TC 657752,2741632 0 allan
      TEXT J TC 657693,2741655 0 santaigo

      in R14 there was no problem but in higher Cadd i can't do. text is not coming because of "space" not recognize as enter.
      Hope u can help, thanks.

  • Paul

    There is another way that may be cleaner, especially if you want to create ‘polylines’ instead of ‘lines’. In Excel, simply save your original data file with the file-type ‘common-separated values” or .CSV extension. This will create a text file with commas inserted between the x and y (and z) values. Edit this file in Notepad and add an extra line at the beginning for the command you need (i.e. _polyline), then save the file with a .SCR extension. Voila, a script that will start the ‘polyline’ command and continue drawing through the remaining data points.

  • Paul

    There is another way that may be cleaner, especially if you want to create ‘polylines’ instead of ‘lines’. In Excel, simply save your original data file with the file-type ‘common-separated values” or .CSV extension. This will create a text file with commas inserted between the x and y (and z) values. Edit this file in Notepad and add an extra line at the beginning for the command you need (i.e. _polyline), then save the file with a .SCR extension. Voila, a script that will start the ‘polyline’ command and continue drawing through the remaining data points.

  • Herb

    Just a note – if using this method for writing a script command involving text, I believe you will have to use some kind of workaround for the “space interpreted as enter” trick, since the text commands see a “space” as a “space”

  • http://www.twitter.com/herbhalstead Herb Halstead

    Just a note – if using this method for writing a script command involving text, I believe you will have to use some kind of workaround for the “space interpreted as enter” trick, since the text commands see a “space” as a “space”

    • Cadimetya

      From the better late than never department, the work around for “enter” is to use a hard return in the text file, moving the next entry to another line.

Previous post:

Next post: