In this Article...
In my last post for CAD Notes, I showed you how to take a task that you perform frequently, and automate it by turning it into an AutoCAD script.
In this post we will look at another automation tool – the command Macro. We will look at the difference between a Script and a Macro and discuss the possibilities of both.
To review:
- An AutoCAD Script Is an ASCII text file, with the file extension .SCR, that contains a number of instructions for AutoCAD to perform at the command line.
- An AutoCAD command Macro is also a list of instructions for AutoCAD to follow, but a Macro is run from the AutoCAD UI (A tool bar button, menu pull down or short cut key combination). A Macro usually lives within your CUI or on a Tool palette.
I hope to demonstrate that the process of writing a Macro is the same as that of writing a Script. All you really need to automate AutoCAD is a good understanding of the sequence of commands that you need to complete to get the job done.
AutoCAD Scripts
The Good – AutoCAD scripts can contain long lists of instructions for AutoCAD to follow. AutoCAD scripts can be stored in a central location on your server. Should you want to update a Script file, you only need to change it in one place for everyone to get the changes. AutoCAD scripts can contain comments, to remind you what the script does.
If you are using the full version of AutoCAD, you can use the LISP programming language within your script. This is not available to AutoCAD LT users.
The Bad – Loading Script files can be a fiddle, and your users will need to know where to find the Script files. Some internal training will be required to run your Scripts.
AutoCAD Macros
The Good – AutoCAD command Macros can be distributed via the CUI or tool palettes, making it much more intuitive for your users to use. AutoCAD Command Macro’s can contain LISP code, and they can also contain DIESEL code (which is good for AutoCAD LT users). Macros can contain pauses for user input, but be careful – there is no error handling method in a Macro.
The Bad – Writing long, complicated command Macro’s can be very difficult to debug.
Tip: You can of course, get the best of both worlds by using a command Macro to call a Script…
Automate your plotting – reloaded
In the last post, we learned how to examine what we had previously written at the command line to write a script. We end up with this script to create an A1 PDF plot in our default plotting location:
-plot
y
DWG To PDF.pc3
ISO A1 (841.00 x 594.00 MM)
m
l
n
l
1:1
0.00,0.00
y
monochrome.ctb
y
n
n
n
n
y
To write the same routine as a macro, we would put it all on one line, like this:
^C^C-plot;y;;DWG To PDF.pc3;ISO A1 (841.00 x 594.00 MM);m;l;n;l;1:1;0.00,0.00;y;monochrome.ctb;y;n;n;n;;n;y;
Note that, instead of a carriage return macros use the semi-colon to indicate where you would usually press return on your keyboard. Spaces are also interpreted as hitting the return key (just like when you are operating AutoCAD manually). However, using spaces can make your code difficult to read and debug so I suggest that you replace the spaces with semi-colons.
The ^C^C at the beginning of the command is equivalent to pressing ‘Esc’ on the keyboard twice. This should cancel any other commands that are running before your Macro runs. You can read more about the Syntax for AutoCAD Macros here.
Running and Debugging your Macro.
You can run your Macro from the AutoCAD CUI (See Edwin’s post on creating new AutoCAD commands) however I am going to show you how to run your command from a tool palette.
First use the keyboard combination shortcut CTRL+3 to open your tool palettes. Right click over any blank area on your palette and chose ‘New Palette’ to create a new blank palette which we will use for testing our macro.
Tip: These instructions are for AutoCAD 2012. Previous releases may be a little different. If you are new to working with Tool palettes, read Edwin’s post ‘creating your own AutoCAD palette’ for more information on creating and working with tool palettes.
(click image to enlarge)
- Right click over any blank area on your new palette and choose ‘Customize commands’. This will bring up the ‘Customize user interface’ dialogue.
- Type ‘script’ into the search box to bring up the run script command.
- Drag and drop the run script command from the customize user interface dialogue onto your palette.
You don’t have to use the run script command for testing Macro’s, but there isn’t a ‘Run Macro’ command, so it will have to do!
To add your command Macro to the new toolbar button, right click on the ‘Run script’ tool palette button, and chose ‘Properties’. You can now paste your Macro in under ‘Command string’.
(click image to enlarge)
Click on the OK button to close the dialogue and save your Macro.
Finally, click on your new button… and see what happens!
You now have all the fun of debugging your Macro. I hope that it works for you first time. If it doesn’t – Hit ‘F2’ to bring up the AutoCAD text window to see what happened and what you might need to adjust to get your Macro working.
Macro examples
Here are some examples of command Macros that you could use, see if you can work out the commands and options used.
^C^C_fillet;r;0;
Start the fillet command with a radius of zero – Handy for joining reluctant polylines.
^C^C_pedit;m;\\;j;;;
Join multiple lines or arcs into a polyline in one go (this Macro assumes that the variable ‘PEDITACCEPT’ is set to 1)
^C^C_break;\@;
Break a line at the point you click on.
^c^_copy;\;\\_rotate;l;;@;
Copy and then rotate your selection
^c^_zoom;e;_zoom;0.95x;
Zoom a bit more than extents
^C^C-purge;r;*;n;
Purges all registered applications from memory.
^C^C_erase;all;;
Erase everything! – be careful with this one :D
What will you write a Macro for?
Writing Macro’s for AutoCAD can be simple and fun. You can create simple quick Macro’s that will save you time and effort every time you have to create a drawing. You could create a Macro to plot your drawings at the click of a button, update your title block or create geometry on your company standard layers. Or you could create Macro’s to pull AutoCAD commands together that you use frequently.
What will you create today?
-Paul-
Hi, I used to be a C programmer but now I’m a CAD drafter. I’ve never tried using macros before but I’d like to write some kind of script that will allow me to take a user input (length in inches) and click a starting point, and then paste several 48″ long CAD blocks together end to end from left to right (these are 48″ long by 4″ wide panels) adding up to the user’s inputted length. If the inputted length is not a multiplier of 48″ it would throw away the remainder and only paste as many blocks as would fit. Is it possible to do something like this in autocad? Here is some psudocode:
start program
user input: 360″
abs(360/48) = 7
user input mouse click = location_coordinates
loop (7 times) (location_coordinates) {
insert block @ location_coordinates
location_coordinates = location_coordinates + 48″ in Y direction
}end loop
end program
Heyy Paul, what’s the macro for making a block of objects at same place in one click?
Hi.
My question is how to automate all my dwg/dxf files by data excel using c# or vb.net?
Hi
My name is sunob
If there is x multiplied with 10, then how the macro is written? X value is asked each time when run the macro… Please give a solution
Thank you
You might be able to do this with Diesel in a command macro, but not in a script.
If you have full AutoCAD, you can use LISP.
https://knowledge.autodesk.com/support/autocad-lt/learn-explore/caas/CloudHelp/cloudhelp/2016/ENU/AutoCAD-LT/files/GUID-F94A885A-4DA2-432B-AC1A-EB49CC6C1C72-htm.html
Paul
Hi Nathan,
As long as the block is contained within a file that is in AutoCAD’s search path – it should work.
Can you try it and let us know how you get on?
Paul
Do you know if it is possible to insert a block found in another drawing using a macro or diesel?
I am using a macro to insert a block, accept user input, and correct attribute rotation. Our block library consists of single drawings containing our blocks. I am trying to avoid converting our block library to individual drawings for each block in a folder(s).
Thanks.
Hi Nathan,
Yes, this is possible – sort off. You can use the INSERT command to insert the DWG file that contains the block in your current drawing, then explode the DWG, then delete any blocks you don’t really need.
This is probably better done via a tool palette or LISP/VB.
Paul
Thanks for the great article.
I am trying to set the Author name by clicking a custom command button.
Your blog got me 90% of the way there… I can get the macro to open the drawing properties dialog, but cant figure out how to set the Author….
Any ideas?…
Hi Cody.
You can’t intrtact with dialogues through comman macros :( sorry.
Could you post the code you have on the Autodesk forums?
http://forums.autodesk.com/t5/autocad/ct-p/8
Don’t forget to come back here and leave a link!
Paul
I am new to programming within / for Acad. Is Macro my best means for running checks or enquiries on a data set (drawing) or should I be using VB or Lisp?
My task is not so much a list of commands repeated but, a search through a selection of the drawing and action according to yes / no checks.
I can go into further detail with exactly what I am trying to achieve if you like but I am rather planning on learning and writing the ‘program’ myself.
Many thanks in advance.
Hi D,
It sounds to me like you need some heavy lifting!
AutoCAD has a number of API’s. Scripts,Diesel, Macros and the action recorder are the simplest to learn and implement.
If you want to search the DWG database I recommend that you move up a notch to Lisp, AutoLisp and Visual Lisp.
If you have a background in coding, and you want to run super fast searches you can operate at a code level using ObjectARX, Visual Basic and C.
The best place to find out about all your options is Lee Ambrosious’s blog:
http://www.hyperpics.com/
Does this help?
Paul
Very informative article, i’m regular reader of your website.
I noticed that your website is outranked by many other blogs
in google’s search results. You deserve to be in top ten. I know what can help you, search in google
for:
Omond’s tips outsource the work
Hi There,
I have a macro string in my tool palette I want to repeat using the right mouse button, instead of repeating from the start it repeats from the last command in the string. Now if I put a * in the front I have read it should repeat, it does but it just keeps looping.
my string
^c^c_SSX;;E;LWPOLYLINE,POLYLINE,LINE;;COPY;P;;0,0,0;0,0,0;FLATTEN;Y;P;;CS;P;;LA;2D;;RE;__d;\per;\SSX;;LA;2D;;ERASE;P;;
this copys all lines to a 2D layer and then flattens, calls up a distance lisp so I can measure perp distances then deletes all lines on the 2d layer.
Is there a way I could get this to repeat with the right click?
thanks
al
Hi Al,
The short answer is no – but let me explain.
When AutoCAD is running a Macro (a list of commands) – it is basically running one command from your list (Macro) at a time – just like you were typing them. If you hit the F2 key after running your Macro you will see the command history.
This means that, when you right click, you are running the last command in the list.
To run the whole sequence again you would need to wrap this Macro into a command.
You can do this easily with Lisp or VBA. If you want to do this with AutoCAD LT, you could try putting your list of commands in a script – and then using the Macro to call the script?
Let us know how you get on.
Paul
Hi Paul,
Thanks for your reply and help. I have made a start on trying to learn Lisp. Seems probably to be the best option at this stage. I used to be ‘very’ experienced in programming using Pascal in the ‘good ol’ days’, and so VB and C++ etc should not be too much of a struggle. I’s just getting hold of the info in relation to AutoCAD that could be the challenge. Plenty on Google searches though.
Many thanks for the assist.
Cheers,
D
Great stuff D,
If you get into Lisp – you will find this resource very useful:
http://www.afralisp.net/index.php
Look into Visual Lisp – It is object orientated and very powerful.
I also recommend that you look up Robert Green’s classes on the Autodesk University website. Robert has a gift for explaining Lisp in simple lamguage:
http://au.autodesk.com/au-online/classes-on-demand/search?full-text=robert+green
Remember to check back in and let us know how you get on!
Paul
Thanks Paul. Much appreciated. The AfraLisp site certainly looks very interesting.
I’ll keep you posted as to how I get on. Don’t hold your breath though. Could a while before I can get anything to work successfully :-)
Big Thanks.
D