Author Topic: Lisp routine to change layers - how can I get choice of target layer?  (Read 11307 times)

0 Members and 1 Guest are viewing this topic.

memerson

  • Guest
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

Lee Mac

  • Seagull
  • Posts: 11836
  • AutoCAD 2015 Windows 7 London, England
Re: Lisp routine to change layers - how can I get choice of target layer?
« Reply #1 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. :-)

alanjt

  • Needs a day job
  • Posts: 5313
  • Standby for witty remark...
Re: Lisp routine to change layers - how can I get choice of target layer?
« Reply #2 on: April 29, 2010, 09:15:48 pm »
You'll need initget also, if you go the getkword route.
Civil 3D 2017 ~ Windohz 7 64bit
Dropbox

memerson

  • Guest
Re: Lisp routine to change layers - how can I get choice of target layer?
« Reply #3 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

Lee Mac

  • Seagull
  • Posts: 11836
  • AutoCAD 2015 Windows 7 London, England
Re: Lisp routine to change layers - how can I get choice of target layer?
« Reply #4 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.

TimSpangler

  • Water Moccasin
  • Posts: 2010
  • CAD Naked!!
Re: Lisp routine to change layers - how can I get choice of target layer?
« Reply #5 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)
)
ACA 2015 - Windows 7 Pro
All Comments and Content by TimSpangler, Copyright © 2016

TimSpangler

  • Water Moccasin
  • Posts: 2010
  • CAD Naked!!
Re: Lisp routine to change layers - how can I get choice of target layer?
« Reply #6 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
ACA 2015 - Windows 7 Pro
All Comments and Content by TimSpangler, Copyright © 2016

alanjt

  • Needs a day job
  • Posts: 5313
  • Standby for witty remark...
Re: Lisp routine to change layers - how can I get choice of target layer?
« Reply #7 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. :-)
Civil 3D 2017 ~ Windohz 7 64bit
Dropbox

David Bethel

  • Swamp Rat
  • Posts: 617
Re: Lisp routine to change layers - how can I get choice of target layer?
« Reply #8 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
R12 Dos - A2K

curmudgeon

  • Newt
  • Posts: 184
Re: Lisp routine to change layers - how can I get choice of target layer?
« Reply #9 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?
Never express yourself more clearly than you are able to think.

David Bethel

  • Swamp Rat
  • Posts: 617
Re: Lisp routine to change layers - how can I get choice of target layer?
« Reply #10 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
R12 Dos - A2K

alanjt

  • Needs a day job
  • Posts: 5313
  • Standby for witty remark...
Re: Lisp routine to change layers - how can I get choice of target layer?
« Reply #11 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.
Civil 3D 2017 ~ Windohz 7 64bit
Dropbox

Hangman

  • Swamp Rat
  • Posts: 553
Re: Lisp routine to change layers - how can I get choice of target layer?
« Reply #12 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. 
Hangman  8)

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Drafting Board, Mechanical Arm, KOH-I-NOOR 0.7mm
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11653
  • class keyThumper<T>:ILazy<T>
Re: Lisp routine to change layers - how can I get choice of target layer?
« Reply #13 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 !!!

Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

--> Donate to theSwamp<--

Lee Mac

  • Seagull
  • Posts: 11836
  • AutoCAD 2015 Windows 7 London, England
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. :-)