Jump to content

Beginner Macro Help


matma7

Recommended Posts

Hello Everyone,

I stumbled across this website a while ago and regularly browsed it whilst I’m on my lunch break. I'd call myself a light CAD user as I do simple 2D fabrication drawings. I'm an avid world of warcraft player and have just started making macros for my various toons. I wondered whether I could make some for AutoCAD and it turns out that you can.

I'm totally lost - this is what I want to do:

Delete layouts

Switch back to model space

Lock layer (this will always be called LINTEL)

Select all

Erase

Purge (yes to all - do not verify)

Audit (Yes fix all errors)

I do this about 20 times per day when putting Parts on the Laser profiler. I could save up to 20 minutes with this macro.

Can anyone help me out with this.

Kind regards Matt

Link to comment
Share on other sites

  • Replies 24
  • Created
  • Last Reply

Top Posters In This Topic

  • Lee Mac

    9

  • dbgeek

    5

  • matma7

    5

  • dbroada

    3

Top Posters In This Topic

First off, Welcome to CADTutor, hope you like it here :)

 

I would approach this problem with LISP or VBA as opposed to a macro approach - it will make it far simpler and still achieve the desired result.

 

I'll see what I can come up with :)

Link to comment
Share on other sites

Cool, Thanks for your quick responce. You seem to be quite the helper on here!

 

:) :) :)

 

I'm no expert (pretty much self taught) but i might be able to help out in beginner forum.

 

Regards Matt

Link to comment
Share on other sites

This should do the trick:

 

(defun c:ea () (c:erase_all)) ; Program Shortcut

(defun c:erase_all (/ *error* varlist oldvars laylist ss)
   (defun *error* (msg) ; Error Trap
   (mapcar 'setvar varlist oldvars)
   (if (= msg "")
       (princ "\nFunction Complete.")
       (princ "\nError or Esc Pressed...")
   ) ;_  end if
   (princ)
   ) ;_  end defun
   (setq varlist (list "CMDECHO" "CTAB" "CLAYER")
     oldvars (mapcar 'getvar varlist)
   ) ;_  end setq
   (setvar "cmdecho" 0)
   (if    (not (tblsearch "LAYER" "LINTEL"))
   (command "-layer" "M" "LINTEL" "")
   ) ;_  end if
   (setq laylist (layoutlist))
   (foreach x laylist
   (command "-layout" "D" x)
   ) ;_  end foreach
   (setvar "ctab" "MODEL")
   (command "-layer" "LO" "LINTEL" "")
   (setq ss (ssget "X" (list (cons 410 "MODEL"))))
   (command "_erase" ss "")
   (command "-purge" "A" "*" "N")
   (command "_audit" "Y")
   (*error* "") ; Force error to reset variables.
   (princ)
) ;_  end defun

Link to comment
Share on other sites

You seem to be quite the helper on here!

 

I try my best, I'm still learning to be honest - my Visual LISP knowledge is lacking big time... but there are so many knowledgeable and helpful guys on here, it makes learning pretty easy.

Link to comment
Share on other sites

Wow, thanks man. That was super quick. when i first posted i thought it might take several days to get a reply.

 

:) I really appreciate your help! :)

 

Kind Regards Matt

Link to comment
Share on other sites

No probs Matt,

 

Hope it all works for you. If you have any further queries or questions let me know; also, if you need help saving, loading or running the LISP, the FAQ is probably the first place to look, and if you are still perplexed just give us a shout. :)

 

Cheers,

 

Lee

Link to comment
Share on other sites

It would take several days, except Lee wrote a LISP routine which removes the necessity for him to sleep. As soon as he completes his Man Machine Interface LISP and Super Relativity LISP he'll actually be able to write the LISP before you even think to ask for it. So when LISPs randomly begin opening in your drawing, be sure to come back here and thank him.

Link to comment
Share on other sites

It would take several days, except Lee wrote a LISP routine which removes the necessity for him to sleep. As soon as he completes his Man Machine Interface LISP and Super Relativity LISP he'll actually be able to write the LISP before you even think to ask for it. So when LISPs randomly begin opening in your drawing, be sure to come back here and thank him.

 

 

haha, Damn you're onto me... :P

Link to comment
Share on other sites

Nice.

 

I program Lasers and Plasmas and I have written a autohotkey script to do this.

A lisp solution would be a cleaner solution but for some reason I never thought of using lisp :oops:

 

Thanks Lee Mac I will use your script for inspiration.

 

I had a quick search and could not find a way to saveas in lisp what I want to do is save the dwg as a dxf in a different folder after it has been striped down.

any ideas?

 

Thanks

Link to comment
Share on other sites

Nearly there but the file is saved as .dwg.dxf because the function I used to get the name includes the file extension.

I also added a part to unlock all layers because the "cutting" layer was left locked at the end of the function.

 

(defun c:ea () (c:erase_all)) ; Program Shortcut

(defun c:erase_all (/ *error* varlist oldvars laylist ss)
   (defun *error* (msg) ; Error Trap
   (mapcar 'setvar varlist oldvars)
   (if (= msg "")
       (princ "\nFunction Complete.")
       (princ "\nError or Esc Pressed...")
   ) ;_  end if
   (princ)
   ) ;_  end defun
   (setq varlist (list "CMDECHO" "CTAB" "CLAYER")
     oldvars (mapcar 'getvar varlist)
   ) ;_  end setq
   (setvar "cmdecho" 0)
   (if    (not (tblsearch "LAYER" "tool path"))
   (command "-layer" "M" "tool path" "")
   ) ;_  end if
   (setq laylist (layoutlist))
   (foreach x laylist
   (command "-layout" "D" x)
   ) ;_  end foreach
   (setvar "ctab" "MODEL")
   (command "-layer" "LO" "tool path" "")
   (setq ss (ssget "X" (list (cons 410 "MODEL"))))
   (command "_erase" ss "")
   (command "-purge" "A" "*" "N")
   (command "_audit" "Y")
   (command "_.Layer" "unlock" "*" "")
   (command "_.saveas" "DXF" "12" (strcat "c:\\dxf\\" (getvar "dwgname")) )
   (*error* "") ; Force error to reset variables.
   (princ)
) ;_  end defun

Not quite sure why the "program shortcut" part is there either.

Link to comment
Share on other sites

After reading the FAQ's lastnight i came into work today, loaded up the Lisp and it works perfectly. I didnt have any doubt about code but was my ability to implement it.

 

I have to say that the code is a lot more complicated than i thought it would be. I was originally trying to copy text from the command line as they would appear if i had carried them out. Now i've got a taste for some advanced stuff it's really whetted my appetite for what AutoCAD can do. I'm thinking of getting the AutoCAD Bible to increase my meager knowledge.

 

:) Once again thanks Lee for your help! :)

 

One last question - Is it possible to have the Lisp always loaded. ie. saved into a template or does it require loading each time?

Link to comment
Share on other sites

now that Lee has given you an elegant solution I can say "I wouldn't have done it that way". :D

 

For something like this I would write a script. For those of you with 2009 there is a built in script editor (I haven't tried it yet) but for older versions you just need notepad.

 

If you have a sequence you are always repeating scripts are brilliant. (we do all our batch plotting using scripts) Check out the help file or browse this forum - there are many examples of scripts.

Link to comment
Share on other sites

nothing wrong with LISP. In this case it is an elegant solution that took Lee only a little while to write. I'm just offering an alternative solution, especially helpful if you have no programming knowledge.

 

the type of script I would use is an AutoCAD script. One of the best kept secrets with AutoCAD. Basically it is a list of commands typed into a text file in the same order you would type them on the command line. Save the file with a .scr extension and it can be run from within AutoCAD.

Link to comment
Share on other sites

i'll bear that in mind regarding the scripts. Maybe that will be my next area for research as creating Lisps is way beyond my ability at the moment.

 

Kind Regards Matt

Link to comment
Share on other sites

like I said, one of the best kept secrets :)

 

if it can be done from the command line scripts are brilliant. There is no error trapping so you do need care but if you find that you often repeat tasks they are a big time saver.

 

A script of mine here, although unusual in that it calls a VBA module. :lol:

sdi
1
new
acadiso
vbastmt
DxfImport("C:\08_0.dxf")
saveas
2000
"C:\08"
new
acadiso
vbastmt
DxfImport("C:\09_0.dxf")
saveas
2000
"C:\09"
new
acadiso
vbastmt
DxfImport("C:\11_0.dxf")
saveas
2000
"C:\11"
resume

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...