TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: memerson on April 29, 2010, 07:27:00 PM

Title: Lisp routine to change layers - how can I get choice of target layer?
Post by: memerson on April 29, 2010, 07:27:00 PM
Hi from a Lisp newbie. I have written the following routine to change to target layer "PL1" using the chprop command.

I would like to be able to choose the target layer (from a list of existing layers) from within the routine.  If the drawing contains layers PL1, PL2, PL3 etc. how can I choose the target layer, instead of defining a single layer. I guess it will involve some "if" or "while" or "cond" statements?

; Changes selected objects to Layer PL1
(defun c:setpl1 ()
  (command "_.chprop"
    (ssget ) "" "LA" "PL1" "")
         )

Thanks
Title: Re: Lisp routine to change layers - how can I get choice of target layer?
Post by: Lee Mac on April 29, 2010, 08:10:01 PM
Perhaps look into the getstring function to prompt the user for a layer, or maybe getkword if you want to restrict the user to a selection to choose from. :-)
Title: Re: Lisp routine to change layers - how can I get choice of target layer?
Post by: alanjt on April 29, 2010, 09:15:48 PM
You'll need initget also, if you go the getkword route.
Title: Re: Lisp routine to change layers - how can I get choice of target layer?
Post by: memerson on April 29, 2010, 10:26:10 PM
Thanks guys - I can see that way forward.

 However I was wondering if I could include a list of target layers to choose from, within the Lisp routine?

Thanks again.
Mike
Title: Re: Lisp routine to change layers - how can I get choice of target layer?
Post by: Lee Mac on April 30, 2010, 04:37:15 AM
Taking it slowly, perhaps this is the next stepping stone  :-)

Code: [Select]
(defun c:test ( / layers layer ss )

  (setq layers '("L1" "L2" "L3" "L4"))

  (initget 1 "L1 L2 L3 L4")
  (setq layer (getkword "\nChoose Target Layer [L1/L2/L3/L4] : "))

  (setq layer (car (member layer layers)))

  (if (setq ss (ssget "_:L"))
    (command "_.chprop" ss "" "_LA" layer ""))

  (princ))

Note that it doesn't check if the chosen layer exists.
Title: Re: Lisp routine to change layers - how can I get choice of target layer?
Post by: TimSpangler on April 30, 2010, 07:43:40 AM
Why not allow the user to pick an entity on that layer instead of typing?

Code: [Select]
(defun c:foo (/ layer ss)
(setq layer(cdr (assoc 8 (entget(car (entsel "\n Pick New Current Layer: "))))))

(if (setq ss (ssget "_:L"))
    (command "_.chprop" ss "" "_LA" layer "")
)(princ)
)
Title: Re: Lisp routine to change layers - how can I get choice of target layer?
Post by: TimSpangler on April 30, 2010, 07:45:42 AM
I knew I had one of these somewhere  :wink:


Code: [Select]

;;; ------------------------------------------------------------------------
;;;    LAYER_MPL.LSP Version 1.1
;;;
;;;    Copyright© NOVEMBER, 2001
;;;    Timothy G. Spangler
;;;
;;;    Permission to use, copy, modify, and distribute this software
;;;    for any purpose and without fee is hereby granted, provided
;;;    that the above copyright notice appears in all copies and
;;;    that both that copyright notice and the limited warranty and
;;;    restricted rights notice below appear in all supporting
;;;    documentation.
;;;
;;;    Moves Selected Entity(s) to picked Layer
;;;
;;; ------------------------------------------------------------------------
(defun c:MPL (/ Clayer OldCmdEcho SS Counter EntList)

  (setq OldCmdEcho (getvar "cmdecho"))
(setvar "cmdecho" 0)

(prompt "\nSelect entities to change...\n")
(setq SS (ssget)) 
(prompt "\nSelect object on destination layer: ")
(setq Clayer (cdr (assoc 8 (entget (car (entsel))))))

 (setq Counter (- (sslength SS) 1 ))
(while (/= Counter -1)
(setq EntList (entget (ssname SS Counter)))
(setq EntList (subst (cons 8 Clayer)(assoc 8 EntList) EntList))
(entmod EntList)
(setq Counter (1- Counter))
)
(setvar "cmdecho" OldCmdEcho)
(princ)
)
;;;
;;; Echos to the command line
(princ "\nMoves Selected Entity(s) to picked Layer v1.1© \n Timothy Spangler, \n November, 2009....loaded.")
(terpri)
(princ "C:MPL")
(print)
;;; End echo
Title: Re: Lisp routine to change layers - how can I get choice of target layer?
Post by: alanjt on April 30, 2010, 08:30:47 AM
Why not allow the user to pick an entity on that layer instead of typing?

Code: [Select]
(defun c:foo (/ layer ss)
(setq layer(cdr (assoc 8 (entget(car (entsel "\n Pick New Current Layer: "))))))

(if (setq ss (ssget "_:L"))
    (command "_.chprop" ss "" "_LA" layer "")
)(princ)
)

Just so you know, LayMch will do the same thing. :-)
Title: Re: Lisp routine to change layers - how can I get choice of target layer?
Post by: David Bethel on April 30, 2010, 09:36:46 AM
I would recommend the CHPROP approach due to the fact that if you (entmod) just the header of a POLYLINE it doesn't change the sequential vertices.  My $0.02  -David
Title: Re: Lisp routine to change layers - how can I get choice of target layer?
Post by: curmudgeon on April 30, 2010, 10:48:29 AM
I would recommend the CHPROP approach due to the fact that if you (entmod) just the header of a POLYLINE it doesn't change the sequential vertices.  My $0.02  -David

just a second. does this mean a "real" polyline can have multiple layers?
Title: Re: Lisp routine to change layers - how can I get choice of target layer?
Post by: David Bethel on April 30, 2010, 12:19:25 PM
just a second. does this mean a "real" polyline can have multiple layers?

Unfortunately, 'Yes, it can'.  After (entmod )the polyline header, try a CHPROP LAyer on the entity.  ACAD will respond with <varies> as to the current layer status.  I've done it on purpose at times for PFACE meshes but I would not recommend it as a good habit.  You can also assign different colors, ltypes etc to vertices.  -David
Title: Re: Lisp routine to change layers - how can I get choice of target layer?
Post by: alanjt on April 30, 2010, 12:30:26 PM
just a second. does this mean a "real" polyline can have multiple layers?

Unfortunately, 'Yes, it can'.  After (entmod )the polyline header, try a CHPROP LAyer on the entity.  ACAD will respond with <varies> as to the current layer status.  I've done it on purpose at times for PFACE meshes but I would not recommend it as a good habit.  You can also assign different colors, ltypes etc to vertices.  -David
Wild, I had no idea. I guess it's kind of like being able to freeze the current layer with entmod.
Title: Re: Lisp routine to change layers - how can I get choice of target layer?
Post by: Hangman on April 30, 2010, 01:05:29 PM
Why not allow the user to pick an entity on that layer instead of typing?
 ...

What if the layer doesn't exist ??  Another words, the user wants to add a block to a layer that is not yet there.  I've run into this quite a few times when editing an existing drawing.
Here's what I currently have for allowing the user to choose a layer:
Code: [Select]
...
  (setq LayrSel (strcase (getkword "\nType in the layer you want to make current: (<?> for list of layers)  ")))
    (if (/= LayrSel "?")
      (setq LayrRep 1)
    )
    (if (= LayrSel "?")
      (progn
        (textscr)
        (prompt "\n\n*Layers not currently in the drawing will be created*\n")
        (prompt "\n**CAUTION**  If layer exists but has different settings,")
        (prompt "\n  this command will not override the existing layer.\n")
        (prompt "\nIf selected layer is currently in the drawing,")
        (prompt "\nit will be thawed, turned on, & set as current.\n")
        (prompt "\nTyping <*> will create all the listed layers.\n\n")
        (prompt "\nLayers and their abbreviations: ")
        (prompt "
0                           = 0 or Z        ELC Text            = ET
PIP Text                    = PT            CIV Text            = CT")
        (prompt "
Defpoints                   = DF            Revision            = REV
Viewports                   = V             XRef-Flatshot       = XF
XRefs                       = XR\n")
      );end progn
    );end if
 ...

Notice, I didn't put the INITGET in there, it's better you read up on it from the Help file.

Quote
Just so you know, LayMch will do the same thing.

LayMch -->
Quote
Changes the layer of a selected object to match the destination layer.
LayCur -->
Quote
Changes the layer property of selected objects to the current layer.
LayMCur -->
Quote
Sets the current layer to that of a selected object.

There's too much to remember. 
Title: Re: Lisp routine to change layers - how can I get choice of target layer?
Post by: Kerry on April 30, 2010, 07:31:22 PM
Why not allow the user to pick an entity on that layer instead of typing?
 ...

What if the layer doesn't exist ??  Another words, the user wants to add a block to a layer that is not yet there.  I've run into this quite a few times when editing an existing drawing.
< ... >
There's too much to remember. 

In that case, just use the built-in LAYMCH command  ... no headaches !!!

Title: Re: Lisp routine to change layers - how can I get choice of target layer?
Post by: Lee Mac on May 01, 2010, 06:33:16 AM
Not trying to spoil the fun, but, perhaps the OP was using this as more of a learning example, perhaps not so much for a practical purpose? I could be wrong of course. :-)
Title: Re: Lisp routine to change layers - how can I get choice of target layer?
Post by: Kerry on May 01, 2010, 10:42:04 AM

perhaps, but I wasn't responding to the OP.  :|

it was related to the statement that 'There's too much to remember'
Title: Re: Lisp routine to change layers - how can I get choice of target layer?
Post by: Lee Mac on May 01, 2010, 01:03:53 PM
No worries  :-)
Title: Re: Lisp routine to change layers - how can I get choice of target layer?
Post by: memerson on May 01, 2010, 09:28:10 PM
Thanks Guys - I now have a routine that does what is required! 
Your help is/was appreciated.

One day I wonder if it might be upgraded to a dialogue box where I can click on a Layer Name drop down list???
But I'm just getting greedy. It aint broke, so . . .

Thanks again - you guys ROCK!  :mrgreen:
Title: Re: Lisp routine to change layers - how can I get choice of target layer?
Post by: alanjt on May 01, 2010, 10:23:38 PM
DosLib has a few list box options. Could fill in the gaps while you're starting out.
Title: Re: Lisp routine to change layers - how can I get choice of target layer?
Post by: Hangman on May 04, 2010, 07:02:32 PM
Why not allow the user to pick an entity on that layer instead of typing?
 ...

What if the layer doesn't exist ??  Another words, the user wants to add a block to a layer that is not yet there.  I've run into this quite a few times when editing an existing drawing.
< ... >
There's too much to remember. 

In that case, just use the built-in LAYMCH command  ... no headaches !!!


Now see Kerry, there's one more thing to remember danged'it all !!  :-D
Title: Re: Lisp routine to change layers - how can I get choice of target layer?
Post by: Paulvth on January 12, 2017, 02:23:26 AM
Hey guys,

I'm all new to lisp commands but really need a favor

I'm using layer translator to convert all layers conaining
"*DIM" to layer  0"
"*BAS" to layer "basewall"
"*EQP" to layer "equipment"
etc. etc. etc.

is it possible to create a lisp, because i have over 100 drawings to do....
i don't won't to open laytr every time and type all the "*xxx" and select the new layer from my .dws file...
every drawing has different layer names but the layers contain the letters "*DIM"  /*BAS" "*XXX"

thanks for help in advance!!!
Title: Re: Lisp routine to change layers - how can I get choice of target layer?
Post by: lamarn on January 12, 2017, 03:23:25 AM
I would consider this lisp/script generator..

http://www.lee-mac.com/scriptwriter.html