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

adding prompt to layer renaming lisp

12 REPLIES 12
SOLVED
Reply
Message 1 of 13
robert06
805 Views, 12 Replies

adding prompt to layer renaming lisp

I have this simple tool to rename object's layer. Would it be possible to prompt the old layer name to command line so that the prompted name would be editable - I mean not an 'accept with enter or type a new name kind' of prompt but prompted old name is printed / typed to command line after "Enter new layer name:"  colon in the command sequence, so it would be handy to type a suffix or to type in between the old name etc.

 

(defun c:rel ()
(setq xlay (cdr (assoc 8 (entget(car (entsel "\nSelect object to rename layer: "))))))
(command "-layer" "r" xlay "\\" "")
(princ)
)

 

thank you

 

Robert

12 REPLIES 12
Message 2 of 13
Kent1Cooper
in reply to: robert06

How about in a text-editing box, rather than on the Command:-prompt line?  There's an apparently undocumented function I learned about a few years back called (lisped), that seems perfect for this.  It calls up the text-editing window with the argument string already in it, and returns whatever you edit that into:

 

(defun c:rel (/ xlay)
  (setq xlay (cdr (assoc 8 (entget (car (entsel "\nSelect object to rename layer: "))))))
  (command "-layer" "r" xlay (lisped xlay) "")
  (princ)
)

 

The Rename option in the Layer command is newer than the version I have where I am at the moment, so I couldn't test that.  It could also be done with the RENAME command, which I did test lightly:

 

(defun c:rel (/ xlay)

  (setq xlay (cdr (assoc 8 (entget (car (entsel "\nSelect object to rename layer: "))))))

  (command "_.rename" "_layer" xlay (lisped xlay))

  (princ)

)

 

You may want to expand on the selection prompt to say something about editing the content that comes up, since the text-editing window itself won't include any prompt.

Kent Cooper, AIA
Message 3 of 13
robert06
in reply to: Kent1Cooper

So it is documented now:)

The text window solution is even better than having the prompt on command line, as the text is selected in first place in case you'd like to overwrite or copy the old name. Hitting "full editor" button will result the layer renamed to "-1". Works with both, "-layer" and "-rename". To my mind the selection prompt is good enough as on command line "Enter new layer name:" remains visible while the textbox window is active.

Thank you!

 

Robert

Message 4 of 13
robert06
in reply to: robert06

Is it possible to select and record the text edited in the lisped window?

It would be comfortable to use it in new layer creation, new layer name is based on selected objec't layer and is edited with lisped in turn.

 

(defun c:uki (/ xlay layn newname)
(setq layx (car (entsel "\n Select object for new layer name template: ")))
(setq layn (cdr (assoc 8 (entget layx))))
(lisped layn)

(setq newname ..value from lisped..)
(command "-layer" "n" newname "")
(princ)
)

 

 

Message 5 of 13
hmsilva
in reply to: robert06

Hi Robert,

 

try

(defun c:uki (/ xlay layn newname)
  (if (and (setq layx (car (entsel "\n Select object for new layer name template: ")))
	   (setq layn (cdr (assoc 8 (entget layx))))
	   (setq newname (lisped layn))
	   (= 'STR (type newname))
	   (/= newname layn)
	   (snvalid newname)
	   (not (tblsearch "LAYER" newname))
      )
    (command "-layer" "n" newname "")
  )
  (princ)
)

 

Henrique

 

EESignature

Message 6 of 13
robert06
in reply to: hmsilva


@hmsilva wrote:

Hi Robert,

 

try

(defun c:uki (/ xlay layn newname)
  (if (and (setq layx (car (entsel "\n Select object for new layer name template: ")))
	   (setq layn (cdr (assoc 8 (entget layx))))
	   (setq newname (lisped layn))
	   (= 'STR (type newname))
	   (/= newname layn)
	   (snvalid newname)
	   (not (tblsearch "LAYER" newname))
      )
    (command "-layer" "n" newname "")
  )
  (princ)
)

 

Hi Henrique,

 

Thank you, it works as expected!

 

Now i tried to match also layer colour, linetype and lineweight from the source object layer to newlayer, but during "-layer" the values layl, layc and layw were not returned..

(setq layl (cdr (assoc 6 (entget layx))))

(setq layc (cdr (assoc 62 (entget layx))))

(setq layw (cdr (assoc 370 (entget layx))))

(command "-layer" "l" layl newname "c" layc newname "lw" layw newname "")

 

Robert

 


 

Message 7 of 13
Kent1Cooper
in reply to: robert06


robert06 wrote:

.... 

Now i tried to match also layer colour, linetype and lineweight from the source object layer to newlayer, but during "-layer" the values layl, layc and layw were not returned..

(setq layl (cdr (assoc 6 (entget layx))))

(setq layc (cdr (assoc 62 (entget layx))))

(setq layw (cdr (assoc 370 (entget layx))))

(command "-layer" "l" layl newname "c" layc newname "lw" layw newname "")

.... 


Did you still have a separate (command) function creating that new Layer before the parts quoted above?  If not, you seem to have just omitted the New option from the beginning of the Layer command, to create the Layer before assigning those other properties to it.

 

EDIT:  Just realized -- those (cdr) functions are trying to get characteristics of the selected object, not of the Layer it's on.  For any of those properties that are ByLayer, there will be no such entry in the entity data for the object [things whose Color is ByLayer will not have an (assoc 62) entry], so nil will be returned.  If the idea is to give the new Layer those properties from the Layer of the selected object [which will have all those entries], rather than from the object itself, then you would want to do something like:

 

(setq

  laydata (entget (tblobjname "layer" (cdr (assoc 8 (entget layx))))); data on Layer of selected object

  layl (cdr (assoc 6 laydata)

  layc (cdr (assoc 62 laydata)

  layw (cdr (assoc 370 laydata)

); setq

 

Kent Cooper, AIA
Message 8 of 13
robert06
in reply to: Kent1Cooper

Within the same function, the layer is created with the name intended, but the properties are not returned during "-layer"

 

(defun c:ltemp (/ xlay layn newname layl layc layw)
(if (and (setq layx (car (entsel "\n Select object for new layer template: ")))
(setq layn (cdr (assoc 8 (entget layx))))
(setq newname (lisped layn))
(= 'STR (type newname))
(/= newname layn)
(snvalid newname)
(not (tblsearch "LAYER" newname))
)
(command "-layer" "n" newname "")
)
(setq layl (cdr (assoc 6 (entget layx))))
(setq layc (cdr (assoc 62 (entget layx))))
(setq layw (cdr (assoc 370 (entget layx))))
(command "-layer" "l" layl newname "c" layc newname "lw" layw newname "")
(princ)
)

 

To sort it out I tried also another function to test the procedure, but same result - color, lineweight and linetype of source layer were not returned:

 

(defun c:mal () ;;match layer properties
(setq slay (car (entsel "\n Select template object: ")))
(setq dlayobj (car (entsel "\n Select destination layer: ")))
(setq dlay (cdr (assoc 8 (entget dlayobj))))
(setq layl (cdr (assoc 6 (entget slay))))
(setq layc (cdr (assoc 62 (entget slay))))
(setq layw (cdr (assoc 370 (entget slay))))
(command "-layer" "l" layl dlay "c" layc dlay "lw" layw dlay "")
(princ)
)

 

 

Message 9 of 13
Kent1Cooper
in reply to: robert06


@robert06 wrote:

Within the same function, the layer is created with the name intended, but the properties are not returned during "-layer"

 

....


[Crossed in the mail -- see the EDIT in my previous Reply.]

 

[Also, by the way, a small thing, but....  I notice your localized variables list still uses the variable name xlay, but it's morphed into layx in the rest of the code.]

Kent Cooper, AIA
Message 10 of 13
hmsilva
in reply to: robert06

Robert,

to copy the properties from selected layer to the new layer, to ensure true colors, Color Books, Lineweitghts, I would suggest something like this

 

(defun c:uki (/ layent layn layx newname)
  (if (and (setq layx (car (entsel "\n Select object for new layer name template: ")))
	   (setq layn (cdr (assoc 8 (entget layx))))
	   (setq newname (lisped layn))
	   (= 'STR (type newname))
	   (/= newname layn)
	   (snvalid newname)
	   (not (tblsearch "LAYER" newname))
	   (setq layent (entget (tblobjname "LAYER" layn)))
      )
    (entmake (subst (cons 2 newname) (assoc 2 layent) layent))
    )
  (princ)
)

 

Henrique

EESignature

Message 11 of 13
robert06
in reply to: hmsilva

Kent, thank you for pointing on the mistake.

 

Henrique's right, the first solution does not return correct value of lineweight, I did not test all color options; but your solution seems perfect, thank you.

 

Robert

Message 12 of 13
Kent1Cooper
in reply to: hmsilva


@hmsilva wrote:

... to copy the properties from selected layer to the new layer, to ensure true colors, Color Books, Lineweitghts, I would suggest something like this

 

....
      (setq layent (entget (tblobjname "LAYER" layn)))
    )
(entmake (subst (cons 2 newname) (assoc 2 layent) layent)) ....

I like that approach -- it covers every possible contingency, making a new Layer with the only difference being the name.  I first thought it might be necessary to also weed the handle and entity-name parts out of the source Layer's data, because it won't want two things with the same handle or the same entity name, but it turns out (entmake) just ignores those anyway, and puts in new ones.

Kent Cooper, AIA
Message 13 of 13
hmsilva
in reply to: Kent1Cooper

@robert06 

 

You're welcome, Robert.

 

@Kent1Cooper 

 

I like that approach also, even to to duplicate an entity, I use the entmake function in the same manner, and AutoCAD ignores the supplied handle and entity name, and creates new ones.

 

Henrique

 

EESignature

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

Post to forums  

Autodesk Design & Make Report

”Boost