Creating Script Files with Excel

|

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).

30 thoughts on “Creating Script Files with Excel

  1. This is very helpful and save time. Is there any program which can do reverse? ie.from autocad figures to be converted to x,y,z and exported to excel.

  2. Hi everyone,
    I want to know if there is a possibility of using script to link excel with autocad,previously i made a database which will be my basis for my drawing,somehow it took time for me to make two activity after one another.please give me a sample or attachment with my post..thank you so much

  3. Is there a script program that I can run one script file for multipal dwg files, Say I wanted to insert a xref into multipal drawing files I want it to open a drawing file insert xref then save it and close it, then open another and repeat the process.

  4. Hi Everyone. i want you help me for connecting between excel and autocad
    please post code for me about this. i want inter data to cell of excel and click on the commanbutton it draw plan in to autocad automatec.
    i hope to receive your attactment code to my email chhunlengly@yahoo.com
    thanks so much………………..

  5. mR. Herb did find an answer how to do the text script form excel to cad?, its been my long problem. or any body knows how to do that in higer autocadd.

  6. mR. Herb did find an answer how to do the text script form excel to cad?, its been my long problem. or any body knows how to do that in higer autocadd.

    • To Allen M.,
      Use in stead of
      TEXT J TC 657752,2741632 0 allan
      TEXT J TC 657693,2741655 0 santaigo
      the following lines
      -TEXT J TC 657752,2741632 0 allan
      -TEXT J TC 657693,2741655 0 santaigo

      HofCAD CSI.

  7. 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.

  8. 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.

  9. thnx for all the information but gues im too late to read it. can u please help me to create an excel command which can create an offset of a line in excel and join those two lines with two straight lines again. thnx in advance

  10. thnx for all the information but gues im too late to read it. can u please help me to create an excel command which can create an offset of a line in excel and join those two lines with two straight lines again. thnx in advance

  11. could you just give me an example of how to enter values of certain drawing in excel sheet so that i get the desired gear drawing in autocad please help me i want help on this topic badly as my project on design is pending

    • Sumit – I am not a mechanical engineer, so I’m probably the last person you want creating such a script. To that end the concept outlined above should still work for you. If you don’t know the exact XY coordinate of each point, you may need to brush up on relative coordinates. What I am talking about is prefixing the coordinate with @ or #. If you’re not familiar with this method take a look at the “Enter Polar Coordinates” document in the AutoCAD help file.

  12. could you just give me an example of how to enter values of certain drawing in excel sheet so that i get the desired gear drawing in autocad please help me i want help on this topic badly as my project on design is pending

    • Sumit – I am not a mechanical engineer, so I’m probably the last person you want creating such a script. To that end the concept outlined above should still work for you. If you don’t know the exact XY coordinate of each point, you may need to brush up on relative coordinates. What I am talking about is prefixing the coordinate with @ or #. If you’re not familiar with this method take a look at the “Enter Polar Coordinates” document in the AutoCAD help file.

  13. 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.

  14. 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.

  15. 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.

  16. 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.

  17. 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.

    • 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.

  18. 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.

    • 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.

  19. 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.

  20. 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.

  21. 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”

  22. 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”

    • 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.

Leave a Reply

Pin It on Pinterest

Shares
Share This
%d bloggers like this: