Visual LISP, AutoLISP and General Customization
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

LISP to use specific layers for commands

20 REPLIES 20
SOLVED
Reply
Message 1 of 21
FMM_MK
5228 Views, 20 Replies

LISP to use specific layers for commands

Hello guys and greetings from germany,

 

I don't know anything about creating LISPs for AutoCAD - but I'm looking for some to change the current layer (after executing a command) to a specific one and then swicht back to the current.

 

I need this for the following commands:

 

Current layer = 0 - command "_mtext" with layer = text - switch layer back to current layer after executing the command

Current layer = 0 - command "_text" with layer = text - switch layer back to current layer after executing the command

Current layer = 0 - command "_mleader" with layer = mleader - switch layer back to current layer after executing the command

Current layer = 0 - command "_insert" with layer = block - switch layer back to current layer after executing the command

 

I already know how to do this with the tool palette - but I don't want to work with it, cause I'm using the toolbars.

 

Hope you guys can help me!

 

Thank you for your help in advance!

20 REPLIES 20
Message 2 of 21
ВeekeeCZ
in reply to: FMM_MK

If you look around web you will find some solutions...

 

One of them is HERE by CadStudio

The second HERE by Lee Mac

....

 

Und Willkommen hier nach Forums! Du musst entschuldigung mir... 🙂 for my poor Deutsch..... no more.

Message 3 of 21
john.uhden
in reply to: FMM_MK

It could work something like this:

 

(command "_.undefine" "mtext")
(defun c:mtext ( / *error* clayer)
  (vl-load-com)
  (defun *error* (err)
    (setvar "clayer" clayer)
    (setvar "cmdecho" 1)
    (vla-endundomark *doc*)
    (cond
      ((not err))
      ((wcmatch (strcase err) "*CANCEL*,*QUIT*"))
      (1  (princ (strcat "\nERROR: " err)))
    )
    (princ)
  )
  (or *acad* (setq *acad* (vlax-get-acad-object)))
  (or *doc* (setq *doc* (vla-get-ActiveDocument *acad*)))
  (vla-endundomark *doc*)
  (vla-startundomark *doc*)
  (setq clayer (getvar "clayer"))
  (setvar "cmdecho" 0)
  (command "_.layer" "M" "TEXT")
  (command "_.mtext")
  (setvar "cmdecho" 1)
  (while (> (getvar "cmdactive") 0)(command pause))
  (*error* nil)
)

John F. Uhden

Message 4 of 21
Kent1Cooper
in reply to: FMM_MK


@ibfmm wrote:

... LISPs for AutoCAD - but I'm looking for some to change the current layer (after executing a command) to a specific one and then swicht back to the current.

 

....

I believe the Architectural overlay program has this capability built in, but I'm not sure whether other overlay programs do.

 

Another approach would be to not change the current Layer before drawing something, but so that you don't have to change the current Layer back afterwards, instead change the something to the desired Layer after drawing it.  In simplest terms:

 

(command "_.undefine" "INSERT")

(defun C:INSERT ()

  (command "_.layer" "_new" "BLOCK" ""); in case it doesn't exist yet; does not make it current

  (command "_.insert")

  (while (> (getvar 'cmdactive) 0) (command pause))

  (command "_.chprop" "_last" "" "_layer" "BLOCK" "")

); defun

 

Or, you could take advantage of a peculiarity I discovered some time ago -- if you give an object a Layer using (subst) & (entmod), if the Layer doesn't yet exist, it will create the Layer in the process [using default color 7 and continuous linetype], so you don't even need to account for the possibility that it's not in the drawing yet:

 

(command "_.undefine" "INSERT")
(defun C:INSERT ()
  (command "_.insert")
  (while (> (getvar 'cmdactive) 0) (command pause))
  (entmod (subst (cons 8 "BLOCK") (assoc 8 (setq edata (entget (entlast)))) edata))
); defun

 

However, those approaches have at least one drawback that I can think of in the TEXT command.  You can make more than one Text object in a single command, by picking new insertion points along the way.  Those approaches would put only the last one on the designated Layer.  But a more sophisticated routine could account for that [mark the last object in the drawing before starting, and change the Layer of everything newer than that when done].

Kent Cooper, AIA
Message 5 of 21
Kent1Cooper
in reply to: FMM_MK

Welcome to these Forums, by the way!

As an aside, I do wonder whether this is an advisable thing to do.  I certainly do not want all Blocks always inserted on the same Layer.  I want [in my case in architectural drawings] furniture Blocks on a furniture Layer, and plumbing-fixture Blocks on a plumbing-fixtures Layer, and so on.  Nor do I always want all Text objects always on the same Layer -- one for Room Names, one for Drawing Titles, one for....

Kent Cooper, AIA
Message 6 of 21
john.uhden
in reply to: Kent1Cooper

Yes, you could appreciate the myriad layers we civils use, but my pool liner people use a total of only about 4 or 5 layers and all their text is on one layer.

John F. Uhden

Message 7 of 21
Ranjit_Singh2
in reply to: FMM_MK

This is a neat trick and many organizations use it. Use LeeMac's routine as a starting point, @ВeekeeCZ posted the link for you. Although I agree with @Kent1Cooper that I do not want certain graphical entities always on the same layer, I do like my Xrefs and Viewports on their respective layers. See screencast below. Start with Lee Macs routine and post your modified version and we can help you.

Message 8 of 21
FMM_MK
in reply to: ВeekeeCZ

Hey guys,

 

at first thanks for all your replies!

 

I took the easiest way and tried the FLay application from cadstudio.cz

 

After editing the Flay.tab file for my personal setup, everything worked as I wanted! Really cool!

 

If the specific layer doesn't exist in the drawing, the application creates it.

 

This is exactly what I was looking for.

 

A nice weekend to you all!

Message 9 of 21
FMM_MK
in reply to: FMM_MK

Hey guys,

 

now I have a little problem with the Flay application.

 

The VLX gets loaded automatically at the startup, but i need to load the TAB file manually everytime by myself!

 

So I tried to go another way by manipulating the makro in the toolbar, for example from

 

^C^C_MLEADER

 

to

 

^C^C_-LAYER;_SE;MLEADER;;_MLEADER

 

This works - but after executing the command, the layer won't switch back to the current (usually to layer 0).

 

Does somebody knows, which things I need to add to the makro, so that AutoCAD will switch back to the current layer or layer 0?

Message 10 of 21
ВeekeeCZ
in reply to: FMM_MK

 

Don't know how loads the TAB file, maybe the author @vladimir_michl could explain.

 

About the macro - this is not the job for a macro. You need to use a LISP for that. See the examples what the other guys gave you... One modified for your MLeader

 

(defun c:LayMleader ( / *error* clayer)
  
  (defun *error* (err)
    (setvar "clayer" clayer)		; reset clayer
    (cond ((not err))
          ((wcmatch (strcase err) "*CANCEL*,*QUIT*"))
          (1  (princ (strcat "\nERROR: " err))))
    (princ)
  )
  
  (setq clayer (getvar "clayer")) 	; save the previous current layer

  (command "_.layer" "M" "_SE")   	; set new current layer (create if it does no exist)
  (command "_.MLEADER")

  (while (> (getvar "cmdactive") 0) 	; stay within the routine while the command is active
    (command pause))

  (*error* nil)				; go to *error* which resets the current layer back
)

 

Message 11 of 21
vladimir_michl
in reply to: FMM_MK

No, FLAY loads automatically and it also automatically loads the FLAY.TAB file (the first one in AutoCAD search path). So you can use FLAY fully transparently.

 

Vladimir Michl, www.cadstudio.cz  www.cadforum.cz

 

Message 12 of 21
FMM_MK
in reply to: vladimir_michl

... I missed to paste the TAB file in one of the support paths from AutoCAD - now it works!

 

Do you know, why it doesn't work for the command "tcircle" from the express tools?

 

I also get the following note in the command line after typing in flay:

 

FLAY>> Possible unknown command(s) in the .TAB file: REVCLOUD TCIRCLE

 

Hitting the revcloud button in the toolbar sets my layer to revcloud like I want it - strange...

 

Message 13 of 21
FMM_MK
in reply to: FMM_MK

... hey everybody!

 

Does anybody know why the TCIRCLE command from the express tools doesn't work wit the FLay app?

Message 14 of 21
vladimir_michl
in reply to: FMM_MK

TCIRCLE is technically not a command but a LISP function (C:TCIRCLE). While it is possible to process also LISP functions in the FLay reactor, I am afraid it would slow down invoking of all LISP functions.

 

Vladimir Michl, www.cadstudio.cz  www.cadforum.cz

 

Message 15 of 21

New FLay version 2.1 (see www.cadstudio.cz/freeware) now supports also LISP-based commands. But you will need to enable the LISP-reactor manually, by setting:

(setq FL:LISPREACTOR T)

as it slightly slows down any LISP function invoked in your AutoCAD session (see readme in FLAY.TXT).

 

Vladimir Michl, www.cadstudio.cz  www.cadforum.cz

 

Message 16 of 21
FMM_MK
in reply to: vladimir_michl

... awesome, thank you

Message 17 of 21
ActivistInvestor
in reply to: FMM_MK


@FMM_MK wrote:

... awesome, thank you


You might want to check to see if any of the reactor-based solutions you've chosen to use are disabling REDO, because my experience is that many of them do.

 

Changes made to the editor in the 'command ended' reactor has the potential to disable REDO, which I regard as a complete show-stopper.

Message 18 of 21
mbracciahubbard
in reply to: FMM_MK

i know this is an old thread, but does anyone know if the there is a solution like the one posted by Lee Mac, but with the ability to use alias commands? for instance, i use the "line" command for different items and need them on different layers at different times. but i'd love if i could use a custom "1" command and have that do a line that's on layer "text" and custom "2" command and have that do a line that's on layer "beam".

Message 19 of 21


@mbracciahubbard wrote:

... i'd love if i could use a custom "1" command and have that do a line that's on layer "text" and custom "2" command and have that do a line that's on layer "beam".


This is pretty basic AutoLisp command-definition stuff.  In very simplest terms:

 

(defun C:1 ()
  (setvar 'clayer "text")
  (command "_.line")
)

(defun C:2 ()
  (setvar 'clayer "beam")
  (command "_.line")
)

 

They can be enhanced, such as to ensure the Layer exists in the drawing, and/or if you want, to save the current Layer first and return you to that afterwards [you may not want that, if you're often likely to want to draw more on the Layer in question than just one Line command's worth].

Kent Cooper, AIA
Message 20 of 21

thanks. yes, keeping it simple is very helpful. i was overcomplicating it.

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

AutoCAD Inside the Factory


Autodesk Design & Make Report