Jump to content

Macro to Command - Revived


Recommended Posts

Posted

I had posted a Thread back in September 2008 regarding turning macro's into LISP Routines.

In AutoCAD, I write custom macros to be used with tool bars, but I am wanting to see about turning those macros into commands, because I am not a big fan of tool bars. Here is an example of a command I would like:

 

^C^Cucs;;ucs;x;;

 

I wasn't sure if anyone was still watching it, so I wanted to go into a little bit more details about what I am going for. The macro that I gave as an example was not my best choice, so here are a few other ones that I currently have mapped to Keyboard Shortcuts, which is all well and good, but I would really like to learn how to write LISP so I can go further.

 

The draftsman that worked at this company before me wasn't too keen on keeping our construction documents standardized, so a lot of times, I am having to go in and "clean up" after him. So, one thing I did was create this macro to basically prep the old drawing so that I could start my janitor work.

^C^C-insert;Z_STD_SETUP;0,0;1;;0;E;L;;-STYLE;SHEET;tahoma.TTF;0;1;0;N;N;-LAYER;S;0-STONE;;-DIMSTYLE;R;16;-LTSCALE;6;PDMODE;3;PDSIZE;0;

Basically all it does is import a template into the existing drawing (to carry all of the dim, layer, and style information) and then sets up the other common settings. I would love to be able to just turn this into a list routine, and maybe add more information/settings to it...Unless there is an easier way to go about the whole task without needing a LISP or macro.

 

 

Another macro that I made was to change a selected object from one layer to another. In order to accomplish this macro, though, I have to grip the objects first and THEN click the icon (well Keyboard Shortcut now). I was never able to get down the "Pause for User Input" macro code to avoid it.

^C^Cchange;p;la;"0-stone details";c;bylayer;lt;bylayer;;

What I would really like to do is be able to do this as a command, and some how tie it into LAYISO. So, two commands, I guess. One that just changes the object to said layer, and another that performs a LAYISO first and LAYUNISO afterward.

 

And the last example I will give (but I do have more!) is being able to make an MLINE that automatically explodes to a specific layer at a specific dimension.

^C^Cmline;st;standard;s;.25;j;z;\\;xplode;l;;la;mortar;

That one has the possibility for a few other applications with different spacing....

 

So all in all, what I am wanting to do is be able to learn some of the basics and be able to explore making LISPs and then move up from there. I have been working with AutoCAD since R10, and I feel as though I missed out on a really cool party that I am just now getting the invite to. Lots of catching up to do!

 

---------------------------------------

 

Oh, and on a side note, does ACAD 2009 have a command to erase lines that are sitting on top of each other? Also, is there a command to bring all objects in a drawing back to Z's zero axis? Some drawings have been imported from DataCAD, so lines will appear in various distances up and down the Z-Axis (including DIM nodes)

 

-Sorry for the long read!-:unsure:

Posted

Ok, Firstly:

Oh, and on a side note, does ACAD 2009 have a command to erase lines that are sitting on top of each other?

I'm thinking OverKill in the Express tools:

 

Quote from Help:

 

OVERKILL

Removes unneeded objects by deleting duplicates and combining line and arc segments that overlap.

Command: OVERKILL

Select objects: Use a selection method to select annotation objects

 

OVERKILL removes objects whose geometry is redundant. For example:

  • Duplicate copies of objects are deleted
  • An arc drawn directly over a portion of a circle so that the arc cannot be seen is deleted.
  • Two lines drawn at the same angle so that they partially overlap are combined to form a single line.
  • Duplicate line and/or arc segments within polylines are removed.

Also, is there a command to bring all objects in a drawing back to Z's zero axis? Some drawings have been imported from DataCAD, so lines will appear in various distances up and down the Z-Axis (including DIM nodes)
Possibly Flatten? Also in the Express tools :thumbsup:

 

Quote from Help:

 

FLATTEN

Flatten creates a 2D representation of selected objects and projects selected objects on to the current viewing plane, forcing object elevations and thickness to 0.

 

Command: FLATTEN

Select objects: Select objects to convert to 2D

Select objects: Press ENTER

Remove hidden lines? :

 

You can use FLATTEN to create a 2D drawing from a 3D model or you can use it to force the thickness and elevations of selected objects to 0.

 

Unlike many traditional methods of 3D to 2D conversion, FLATTEN preserves as much of the drawing's original intelligence as possible. The common practice of plotting to a DXB file and then using DXBIN to re-import, causes the loss of drawing intelligence such as the loss of color and layer information. FLATTEN gets the job done in one step and does not have this problem. Using FLATTEN results in 2D objects that retain their original layers, linetypes, colors and object types where possible.

 

FLATTEN is similar to a plot of the current view or display. FLATTEN projects selected objects onto the current viewing plane. If your view is not plan when you invoke FLATTEN, the current UCS will be temporarily set to view for the duration of the operation. With respect to UCS=view, the objects that result will be flat.

 

Note:

The FLATTEN command is similar in nature to a Plot To 2D type of operation. As with plot conversion, some degradation of drawing precision may occur. This precision loss is usually insignificant and it occurs mainly when the Hide option is selected or the perspective view is on.

I'll see what I can do about converting your macros to LISP. :)

 

Hope this helps somewhat :)

 

Cheers

 

Lee

Posted

Maybe:

 

Another macro that I made was to change a selected object from one layer to another. In order to accomplish this macro, though, I have to grip the objects first and THEN click the icon (well Keyboard Shortcut now). I was never able to get down the "Pause for User Input" macro code to avoid it.

	^C^Cchange;p;la;"0-stone details";c;bylayer;lt;bylayer;;

 

 

(defun c:laychng (/ ss lay)
 (if (and (setq ss (ssget)
        lay (getstring "\nSpecify Layer to Change to >  ")))
   (progn
   (setvar "cmdecho" 0)
   (if (not (tblsearch "Layer" lay))
     (command "-layer" "m" lay ""))
   (command "_chprop" ss "" "LA" lay "C" "BYLAYER" "LT" "BYLAYER" "")
   (setvar "cmdecho" 1)))
 (princ))

 

Only typed quickly - OK as a starter thought I suppose. :wink:

Posted

That is a great start. The only thing different would be that instead of it asking which layer to change to, it would just change to that layer (a command for each layer needed)

 

I would take a stab at trying to change the code where it needs to, however I am somewhat clueless on some of the script...

 

I know I didn't put the lisp into code blocks, I just figured the reply would get messy. If I should have done it anyway, please let me know...don't wanna break any rules!

 

 

The first part: (defun c:laychng I get. You are telling Autocad that this is the command name. This part: (/ ss lay) no idea.

 

No idea on this...(except the \nSpecify Layer to Change to > is what AutoCad prompts the user.

(if (and (setq ss (ssget)

lay (getstring "\nSpecify Layer to Change to > ")))

(progn

(setvar "cmdecho" 0)

(if (not (tblsearch "Layer" lay))

I am guessing that this is what base commands you are telling AutoCAD to reference for the LISP

(command "-layer" "m" lay ""))

(command "_chprop" ss "" "LA" lay "C" "BYLAYER" "LT" "BYLAYER" "") however, I am not sure what ss" and lay are for. Does lay have something to do with the first line of code?

 

And I think this is telling autocad that it can repeat the command or it can end it...

(setvar "cmdecho" 1)))

(princ))

 

I don't mean to butcher your work, I am just eager to learn it so I don't have to keep bugging the proffessionals!

Posted

OK, so this isn't the best example to learn from, as I just wrote it on the fly, but you can still learn some key elements.

 

OK, I'll go through it line by line:

 

(defun c:laychng

Defines the function, (defun c:... as opposed to just (defun... means that the function can be invoked via the command line.

 

(/ ss lay)

This is called localizing the variables and its good programming practice. It will wipe the variables of their values once the function has completed. This is useful as there is a chance you might have other programs using variables with similar names.

 

  (if (and (setq ss (ssget)
                           lay (getstring "\nSpecify Layer to Change to >  ")))

Ok, this is a bit harder to explain, it may be best if you use a tutorial/reference manual instead.

 

(if ... is an IF statement meaning:

 

(if -- this is true

Then do this

Else do this

"AND" statements will return T if both statements are true, thus, I require the user to select the objects AND specify the layer.

 

(setq ss (ssget)

I am asking the user to create a selection set (pickset) and storing this pickset to a variable named ss.

 

lay (getstring "\nSpecify Layer to Change to >  ")))

Because both statements include a (setq) I don't need to repeat it, just not close the (setq) bracket after setting the first variable.

 

Getstring does just that - prompts the user for a string - and I set the result of this prompt to a variable called "lay". Obviously this is also encompassed by the "and" statement, and so the function will only continue if the string is not nill.

 

(progn

Ok, this one is harder to describe, but in essence it is a code wrapper and is used in conjunction with the IF statement as mentioned earlier - it is probably best you see here:

 

http://www.cadtutor.net/forum/showpost.php?p=173196&postcount=10

 

(setvar "cmdecho" 0)

Ok, this one is easy - I set the Command Echo Variable to 0, i.e. turning the Command Echo off so when I invoke commands such as -layer, chprop - the user doesn't see all the prompts whilst the program is running.

 

(if (not (tblsearch "Layer" lay))

Yet another "IF" statement, I ask the function to search the Layer Table Definitions for the layer specified by the user in the getstring prompt (lay). And, if this cannot be found - then (not (tblsearch... will return True and so the IF function will operate.

 

(command "-layer" "m" lay ""))

The result of the IF function operating (i.e. the layer name not being found), I instruct the function to invoke the -layer command and thus create the layer specified by the user.

 

(command "_chprop" ss "" "LA" lay "C" "BYLAYER" "LT" "BYLAYER" "")

This is the business end of the LISP, using the chprop command to change the layer of the items in the Selection set (ss) to the layer specified (lay).

 

This could be accomplished in many ways - I could use DXF Group Tables on each entity in the selection set and alter the value of (cdr (assoc 8 ) then follow with an (entmod), or alternatively, I could transform each entity in the set to a VLA-Object and use vla-put-layer to change the layer.

 

(setvar "cmdecho" 1)))

 

Finally, return the value of the Command Echo Variable to 1 - this isn't strictly "kosher", as I should have probably retrieved the existing value of the command echo before switching it off and then return the command echo variable to its original value, instead of just resetting it to 1. But as I said, I typed it up quickly and so things may not be as they should be.

 

(princ))

 

And this one is easy to explain - this "exits cleanly", and prints a new-line to the command line, thus suppressing the last function invoked.

 

 

 

Right, that is the whole LISP dissected! I don't think I have ever looked at it in that much detail...

 

Hope this helps somewhat - but I shall re-iterate - I would recommend studying a tutorial and/or reference manual for LISP.

 

Take a look at Jeffery Sanders Website - he is a good tutor.

Also, AfraLISP is a good site for resources, although you may have to alter your colour settings if using Mozilla Firefox. :)

 

Anyway, thats all for now!

 

Cheers

 

Lee

Posted

Just a simple layiso

 

set current layer then (command -la "off" "*" )

 

layon

(command -la "on" "*")

 

I think its needs a couple more "" but its the idea shown

Posted

Wow Lee!

 

I will have to say that you are a great help! Granted, I think, now that I know the basics of it, I am a little more confused, but I will take your advice and use that as a guide.

 

And I am so thankful that you have given me something to do today. We are slow at work right now, so this is the perfect oppertunity to further my AutoCAD knowledge so I could be like you guys some day.

 

Hats off Lee!

Posted

No Probs, I had a bit of time last night.

 

Hope the explanation helps - I would seriously recommend taking a look at Jefferey Sanders website and also the AfraLISP website - both are good resources.

 

Also, see if you can download a AutoLISP reference manual - if you don't know where to find one, just PM me with your email address and I'll send my copy to ya :thumbsup:

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