gerbaux Posted June 30, 2008 Posted June 30, 2008 I'm quite new here.. so i don/t know if it's correct to ask questions here rather that posting tips.. I'm finding a way to change the current layer by typing the layer name itself without the use of any other command. and i want also to change the layer of a certain object by selecting the objects then typing the layer name. I already got a code from somewhere in the net, but i need to command first LS then type the layername i want to make current.. and it can't change the layer of an object instantly. if you want this code, i can't post it for you guys the original code with the authors name.. thank you in advance... Quote
Cad64 Posted June 30, 2008 Posted June 30, 2008 This can be done pretty simply by using the code: (setvar "clayer" "Layer1") where "Layer1" is the name of the layer you want to switch to. A simple routine to do this would be: (defun c:layer1 () (setvar "clayer" "Layer1") (princ) ) To activate the command and switch to Layer1, you would just type Layer1 at the command line. This command will only work for this layer though. You would need many of these routines for every layer in your drawing. Is there some reason why you want to work this way, instead of using the layer list box on the "Layers" toolbar? You can also use the MATCHPROP command, to select an object and match it's properties to another object, or just select the object and then choose the layer you want to move it to, from the layer list box on the "Layers" toolbar. Quote
gerbaux Posted June 30, 2008 Author Posted June 30, 2008 thank you sir for your prompt reply. the reason for this request is because this company im working for is using standard layernames that has 3 characters(1letter and 1-2 numbers).. that is why i feel it's easier if i can just type the layer name to make it active or to change the layer of an object.. Quote
ASMI Posted June 30, 2008 Posted June 30, 2008 I think: (defun c:ll(/ lName) (if (and (setq lName(getstring T "\nSpecify layer name: ")) (tblsearch "LAYER" lName) ); end and (setvar "CLAYER" lName) (princ(strcat "\n<!> Can't to find layer \"" lName "\" <!>")) ); end if (princ) ); end of c:ll 1 Quote
gerbaux Posted June 30, 2008 Author Posted June 30, 2008 @ASMI thank you sir for your reply, but from my basic knowledge of LISP, i understand that i need first to issue the command LL before typing the layer name. please correct me if i'm wrong. thankz again for your time... Quote
gerbaux Posted June 30, 2008 Author Posted June 30, 2008 Yes, LL and layer name. sorry sir, but i'm trying to look for a way, to use the layer name directly.. thank you again... Quote
ASMI Posted June 30, 2008 Posted June 30, 2008 There isn't problem, look for Cad64 message. I have add several strings to Cad64 lisp: (defun c:0(/ lName) ;CHANGE '0' NAME TO LAYER NAME (setvar "CMDECHO" 0) (if(tblsearch "LAYER"(setq lName "0")) ;CHANGE "0" TO LAYER NAME (progn (command "_.clayer" lName) (princ(strcat "\n<<< Swiched to \"" lName "\" layer >>>")) ); end progn (princ(strcat "\n<!> Can't to find layer \"" lName "\" <!>")) ); end if (setvar "CMDECHO" 0) (princ) ); end of c:0 It's for "0" layer. Quote
gerbaux Posted June 30, 2008 Author Posted June 30, 2008 thankz sir, i'l try this at home as my office hours ends now.. i'l let you know tomorrow if it worked for me.. thankz!!!!! Quote
GhostRider Posted June 30, 2008 Posted June 30, 2008 A few macro or small lisps added to the ACAD.lsp works well for me with standard layernames in my templetes. Something like this you can set your layers on and off as you like, I have my border and titleblock on 0 , but you can set it anyway you need with as many layers on, off , thawed or frozen, as you like... (DEFUN C:lyr1 () (COMMAND "-LAYER" "T" "[color=red]LAYERNAME[/color]" "S" "[color=red]LAYERNAME[/color]" "F" "*" "T" "0,[color=red]LAYERNAME2[/color],defpoints" "") Quote
alanjt Posted June 30, 2008 Posted June 30, 2008 this will allow you to change the layer of selected objects by typing in layer name (if layer does not exist, it's created). ;Change The Layer Of Selected Object(s) ;created by: alan thompson (defun c:CL (/ clay sset pt nl) (princ (strcat "\n***Change The Layer Of Selected Object(s)***")) (setq CLAY (getvar "clayer")) (setq sset (ssget)) (if sset (progn (setvar "CMDECHO" 0) (setq pt (list 0.0 0.0)) (setq nl (getstring "\nLayer to move selected entities to: ")) (if (/= nl "") (progn (if (not (tblsearch "LAYER" nl)) (command "-LAYER" "M" nl "") ) (command "_.UNDO" "_G") (command "_.CHPROP" sset "" "_LA" nl "") (command "_.UNDO" "_E") ) ) ) ) (setq sset nil) (setvar "clayer" clay) (princ) ) Quote
CAB Posted June 30, 2008 Posted June 30, 2008 You may give this a test drive. Load it a run the MakeLayerlisp which will make a lisp file & then load it. The loaded lisp will contain a lisp for each layer with a name matching the filter. Entering the layer name at the command line will set it current. If you selec objects before entering the name it will change them to that layer & set the layer current. Updated Code: Made the Layer name Filter more obvious to the user. ;;;=======================[ LayerLisp.lsp ]======================= ;;; Author: Copyright© 2008 Charles Alan Butler ;;; Version: 1.2 July 2, 2008 ;;; Purpose: To create a lisp file for each layer to set current ;;; and change selected objects layer ;;;========================================================== ;; Running the c:MakeLayerlisp routine will result in ;; the File "LayerLISP.lsp" being created in the search path and will ;; contain lisp routines with a name matching the layers selected with ;; the Layer name Filter. Then the "LayerLISP.lsp" will be loaded ;; Running any of these new lisp routines will set current the layer ;; name you typed to run the lisp & if any objects were pre selected ;; they will be changed to that layer unless they are on a locked layer ;; Needed subroutines, must be loaded. ;; SetLayerCurrent and ChangeLayer ;; set layer current, thaw if necessary (defun SetLayerCurrent(lName / ent elst frz) (and lName (or (setq ent (tblobjname "LAYER" lName)) (prompt (strcat "\n<!> Can't to find layer \"" lName "\" <!>"))) (setq elst (entget ent)) (or (/= 1 (logand 1 (setq frz (cdr (assoc 70 elst))))) (and (entmod (subst (cons 70 (boole 6 1 frz)) (assoc 70 elst) elst)) (vl-cmdf "._regen") )) (setvar "CLAYER" lNAME) (princ (strcat "\n<<< Swiched to \"" lName "\" layer >>>")) ) (princ) ) ;; change the layer of objects to the desired layer ;; will not change objects on locked layers (defun ChangeLayer (ss lay / i ename elst) (setq i -1) (while (setq ename (ssname ss (setq i (1+ i)))) (setq elst (entget ename)) (vl-catch-all-apply 'entmod (list (subst (cons 8 lay) (assoc 8 elst) elst))) ) (princ) ) ;; Routin to make the needed lisp file, contains all the layer names ;; in the form of a lisp file (defun c:MakeLayerlisp (/ lst namelst lname acadfn fn fname LayFilter err) ;; -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*- ;; This is the Filter for Layer names ;; Set to <One String> & <Two Number Characters> (setq LayFilter "?##") ; See the HELP file on wcmatch ;; -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*- ;; get a list of layer names matching filter (while (setq lst (tblnext "layer" (null lst))) (if (wcmatch (setq lname (cdr (assoc 2 lst))) LayFilter) (setq namelst (cons lname namelst)) ) ) (setq namelst (mapcar '(lambda (x) (nth x namelst)) (vl-sort-i namelst '<))) (if (and namelst (or (setq acadfn (findfile "ACAD.PAT")) (prompt "\nACAD.PAT file not found.") (setq acadfn (findfile "ACAD.EXE")) ) ; alternate folder location ) (progn (prompt (strcat "\nPath to LayerLISP: " (vl-filename-directory acadfn))) (setq fname "LayerLISP.lsp" fname (strcat (vl-filename-directory acadfn) "\\" fname) ) (if (setq fn (open fname "w")) (progn ;; create a lisp for each name in one file (foreach lname namelst (princ (strcat "(defun c:" lname " (/ ss)") fn) (princ (strcat " (SetLayerCurrent \"" lname "\")") fn) (princ (strcat " (and (setq ss (ssget \"_I\")) (ChangeLayer ss \"" lname "\"))") fn ) (princ " (princ))" fn) (write-line "" fn) ) ; end while (close fn) (princ (strcat "\nLayerLISP.lsp created with " (itoa (length namelst)) " layer routines.")) (if (vl-catch-all-error-p (setq err (vl-catch-all-apply 'load (list fname)))) ; reload the lisp (alert (vl-catch-all-error-message err)) (princ "\nLayerLISP.lsp Loaded. Enter layer names to run.") ) ) (alert "File failed to open: LayerLISP.lsp") ) ) ) (princ) ) ;; Load the lisp if one already exist (if (vl-catch-all-error-p (setq err (vl-catch-all-apply 'load (list "LayerLISP.lsp")))) (alert (vl-catch-all-error-message err)) (princ "\nLayerLISP.lsp Loaded. Enter layer names to run.") ) Quote
Cad64 Posted June 30, 2008 Posted June 30, 2008 You're unbelieveable CAB. The routines you come up with just amaze me. Quote
CAB Posted June 30, 2008 Posted June 30, 2008 High praise indeed considering the source.:wink: Thanks. Quote
VovKa Posted June 30, 2008 Posted June 30, 2008 will not work for layers with the same names as standart acad commands (line, circle, move etc.) (vl-load-com) (defun LayerCommand (reactor_object obj) (if (vl-some (function (lambda (r) (= (vlr-data r) "LayerCommand"))) (cdar (vlr-reactors :VLR-Command-Reactor)) ) (if (and (car obj) (setq obj (tblobjname "LAYER" (car obj))) (setq obj (vlax-ename->vla-object obj)) ) (progn (vla-put-Freeze obj :vlax-false) (vla-put-ActiveLayer (vla-get-ActiveDocument (vlax-get-acad-object)) obj ) ) ) (vlr-command-reactor "LayerCommand" (list (cons :vlr-unknownCommand 'LayerCommand)) ) ) ) ;;;(LayerCommand nil nil) ;run this once per session ;;;(vlr-remove-all :VLR-Command-Reactor) ;run this to remove the reaction Quote
rookie37 Posted July 2, 2008 Posted July 2, 2008 Here is is a great lisp. I can say that without bragging because I wrote very little of it as it was written by the guys on here. It can be easliy modified to just what I think you want In this lisp my layer is pen-1 I defined the command as lp1. This sets pen-1 as the current layer I also have llp1. This changes the entity to pen-1 However, you can modify this to the layer name to do so change; DEFUN C:LP1 to DEFUN C:pen1 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;PEN (DEFUN C:LP1 () (COMMAND ".LAYER" "SET" "PEN-01" "" "")(princ)) ;;; make this layer current (defun c:llP1 (/ ss) ;;; all entities in box change to this layer (princ "\nSelect object to move to layer") (setq ss (ssget)) (command "_chprop" ss "" "la" "PEN-01" "") (princ) ) (DEFUN C:LP2 () (COMMAND ".LAYER" "SET" "PEN-02" "" "")(princ)) ;;; make this layer current (defun c:llP2 (/ ss) ;;; all entities in box change to this layer (princ "\nSelect object to move to layer") (setq ss (ssget)) (command "_chprop" ss "" "la" "PEN-02" "") (princ) ) (DEFUN C:LP3 () (COMMAND ".LAYER" "SET" "PEN-03" "" "")(princ)) ;;; make this layer current (defun c:llP3 (/ ss) ;;; all entities in box change to this layer (princ "\nSelect object to move to layer") (setq ss (ssget)) (command "_chprop" ss "" "la" "PEN-03" "") (princ) Quote
gerbaux Posted July 2, 2008 Author Posted July 2, 2008 i'm very sorry my dear sirs for failing to visit my post.. sorry for all those who tried helping me.. thankz for all your support.. i'll try all this.. i'll let you know as soon as i get my boss out of my way.. hehehe.. thankz to all!!!! Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.