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

New layer for selected objects

11 REPLIES 11
SOLVED
Reply
Message 1 of 12
majdov24
2038 Views, 11 Replies

New layer for selected objects

I need a LISP that create a new layer

and ask me for the name and the other properties (color,lw,lt..)

ask me if I want move the selected object for the new layer (yes is the default value)

 

thanks

11 REPLIES 11
Message 2 of 12
devitg
in reply to: majdov24

 Not as you ask, but it can serve you , use MAKE option at LAYER COMMAND 

 

(if (wcmatch (getvar 'locale) "en*" )


(alert " Design by Gabo CALOS DE VIT from CORDOBA ARGENTINA
\n Copyleft 1995-2013 by Gabriel Calos De Vit
DEVITG@GMAIL.COM
\n Put \n M2L \n at the command line")


(alert
" \n Hecho por Gabo CALOS DE VIT de CORDOBA ARGENTINA
\n Copyleft 1995-2013 por Gabriel Calos De Vit
\n DEVITG@GMAIL.COM
\n Poner \n M2L \n en la linea de comando")

)


(defun C:M2L ()
(command "._layer")
(while (> (getvar 'CmdActive) 0) (command pause))
(setq new-layer (getvar 'clayer))
(prompt (strcat"\n select the ent to move to layer < " new-layer " > " ))
(setq ent-to-move-ss (ssget))
(command "_chprop" ent-to-move-ss "" "la" new-layer "")

)

 

Message 3 of 12
majdov24
in reply to: majdov24

is good but I want something like his

 

commande: m2nl
new layer name<layers1>
move selected to new layer? [Yes No] <yes> :

Message 4 of 12
devitg
in reply to: majdov24

It is as far as I can go .

Sorry.

 

Message 5 of 12
Kent1Cooper
in reply to: majdov24


@majdov24 wrote:

I need a LISP that create a new layer

and ask me for the name and the other properties (color,lw,lt..)

ask me if I want move the selected object for the new layer (yes is the default value)

 

....

I want something like his

 

commande: m2nl
new layer name<layers1>
move selected to new layer? [Yes No] <yes>


Perhaps something like this?  It leaves you in the Layer command after asking for a new-Layer name, to assign any other property or properties you want, if any, so you need to tell it when you're done.  If you prefer, it could be altered to always ask for certain properties [e.g. color, linetype], but that would sometimes be asking questions you don't want to answer, when defaults are acceptable.

 

(defun C:MNLC ; = Make New Layer, Change pre-selected objects into it if desired
  (/ inc layname ss change)
  (setq
    inc 0; for default Layer name offering
    ss (ssget "_I"); pre-selected objects, if any
  ); setq
  (while (tblsearch "layer" (strcat "NewLayer" (itoa inc)))edit base string as desired
    (setq inc (1+ inc)); until it finds one not yet used
  ); while
  (setq layname (getstring (strcat "\nNew Layer name <" (strcat "NewLayer" (itoa inc)) ">: ")))
  (if (= layname "") (setq layname (strcat "NewLayer" (itoa inc)))); Enter for default
  (command "_.layer" "_make" layname)
  (while (> (getvar 'cmdactive) 0) (command pause)); allow color, linetype, etc., settings as desired
  (if ss ; were there any pre-selected objects?
    (progn ; then
      (sssetfirst nil ss); re-highlight
      (initget "Yes No")
      (setq change (getkword "\nMove selected objects to new Layer? [Y/N] <Y>: "))
      (sssetfirst nil); un-highlight
      (if (/= change "No") (command "_.chprop" ss "" "_layer" layname ""))
    ); progn
  ); if
  (princ)
); defun

 

It doesn't yet have any of the usual other controls, etc. [error handling, locked-Layer check, etc.], but see what you think.

Kent Cooper, AIA
Message 6 of 12
gccdaemon
in reply to: Kent1Cooper

You might be able to make a more simple routine by using these 3 basic functions:

 

1. Create a selection set.

2. Create the layer using "-layer" (easier for single command string)

3. Use command copytolayer to move selection set. (built in error handling)

Andrew Ingram
Civil 3D x64 2019
Win 10 x64 Pro
Intel Xeon E5-1620
32 GB Ram
Message 7 of 12
Kent1Cooper
in reply to: gccdaemon


@gccdaemon wrote:

You might be able to make a more simple routine by using these 3 basic functions:

 

1. Create a selection set.

2. Create the layer using "-layer" (easier for single command string)

3. Use command copytolayer to move selection set. (built in error handling)


1. Mine does create a selection set, and I don't see how it could do so any more simply.  The selection presumably already exists, if there is to be one, at least as I interpreted the wording of Messages 1 & 3.

 

2. If this is about using the hyphen, that is not needed in a (command "_.layer" ....) function, which works the command without the dialog box, without the need for the hyphen.  [When using (command), you don't need to suppress the dialog box, but rather need to force it if you want one -- see the (initdia) function.]

 

3. CopyToLayer doesn't move things to the designated Layer as they asked, but makes copies of them there, which means they're also left on their original Layer(s).  The OP would have to say whether that might be wanted, maybe as an option, but that's not what the original question asks for.

 

If I misunderstood any of your suggestions, a more detailed description would help.

Kent Cooper, AIA
Message 8 of 12
gccdaemon
in reply to: Kent1Cooper

I must have had my head somewhere else, I not only did I misunderstand the original post, but I didn't correctly comprehend the script. I believe a "My Bad" is in order...lol.Smiley Embarassed

Andrew Ingram
Civil 3D x64 2019
Win 10 x64 Pro
Intel Xeon E5-1620
32 GB Ram
Message 9 of 12
pbejse
in reply to: Kent1Cooper


@Kent1Cooper wrote:

@majdov24 wrote:

I need a LISP that create a new layer

and ask me for the name and the other properties (color,lw,lt..)

ask me if I want move the selected object for the new layer (yes is the default value)

 

....

I want something like his

 

commande: m2nl
new layer name<layers1>
move selected to new layer? [Yes No] <yes>


Perhaps something like this?  It leaves you in the Layer command after asking for a new-Layer name, to assign any other property or properties you want, if any, so you need to tell it when you're done.  If you prefer, it could be altered to always ask for certain properties [e.g. color, linetype], but that would sometimes be asking questions you don't want to answer, when defaults are acceptable.

 

 ...... 

 


 

In addition to kents' code, to account for space <" "> and to catch invalid characters on the layer name 

 

(while (not (snvalid layname))
  (initget "Yes No")
  (setq	opt (getkword (strcat "\nInvalid name, use pre-set value? \""
			      (setq ln (strcat "NewLayer" (itoa inc)))
			      "\" [Y/N] <Y>: "
		      )
	    )
  )
  (setq	layname	(if (/= opt "No")
		  ln
		  layname
		)
  )
)
(command "_.-layer" "_make" (princ layname))

 

HTH

Message 10 of 12
majdov24
in reply to: Kent1Cooper

Perfect! Thank you so much for the massive effort. This is fantastic
Message 11 of 12
hmsilva
in reply to: pbejse

 


pbejse wrote:

 

...

In addition to kents' code, to account for space <" "> and to catch invalid characters on the layer name 

 ...


Untested...
In addition to kent's code, and pBe's idea,  to catch invalid characters on the layer name, why not test also the layer name pre-existence...
Something like this perhaps.

 

 

 (setq layname (getstring (strcat "\nNew Layer name <" (strcat "NewLayer" (itoa inc)) ">: ")))
  (if (= layname "") (setq layname (strcat "NewLayer" (itoa inc)))); Enter for default
(while (/= (and	(snvalid layname)
		(not (tblsearch "LAYER" layname))
	   )
	   T
       )
  (prompt
    (strcat "\nInvalid name. Layer \"" layname "\" already exists
    \nor \"" layname "\" contains invalid characters such as:  <>/\\""\":"";?*|,=`"))
  (setq	layname	(getstring (strcat "\nNew Layer name <" (strcat "NewLayer" (itoa inc)) ">: ")))
  (if (= layname "")(setq layname (strcat "NewLayer" (itoa inc)))); Enter for default
  )
(command "_.-layer" "_make" (strcat layname))

 

 

HTH

Henrique

 

EESignature

Message 12 of 12
Lee_Mac
in reply to: majdov24

Or maybe something like this:

 

(defun c:mnlc ( / cla def enx idx lay sel tmp )
    (setq cla (getvar 'clayer)
          tmp 0
    )
    (while
        (tblsearch "layer"
            (setq def (strcat "New Layer " (itoa (setq tmp (1+ tmp)))))
        )
    )
    (while
        (progn (setq lay (getstring t (strcat "\nLayer name <" def ">: ")))
            (cond
                (   (= "" lay) (setq lay def) nil)
                (   (not (snvalid lay))
                    (princ "\nLayer name invalid.")
                )
                (   (tblsearch "layer" lay)
                    (princ "\nLayer already exists.")
                )
            )
        )
    )
    (vl-cmdf "_.-layer" "_m" lay)
    (while (= 1 (logand 1 (getvar 'cmdactive))) (vl-cmdf "\\"))
    (if (and (setq sel (cadr (ssgetfirst)))
            (progn
                (initget "Yes No")
                (/= "No" (getkword "\nMove selected objects to new layer? [Yes/No] <Yes>: "))
            )
        )
        (repeat (setq idx (sslength sel))
            (setq enx (entget (ssname sel (setq idx (1- idx)))))
            (entmod (subst (cons 8 lay) (assoc 8 enx) enx))
        )
    )
    (setvar 'clayer cla)
    (princ)
)

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

Post to forums  

Autodesk Design & Make Report

”Boost