HELP! Pick a layer to use before insert

HELP! Pick a layer to use before insert

pmercader
Advocate Advocate
908 Views
8 Replies
Message 1 of 9

HELP! Pick a layer to use before insert

pmercader
Advocate
Advocate

I want to pick a layer using multiple options within a command before inserting a block.

Run Lisp > Choose from 3 options [Option1 Option2 Option3] > 3 > Insert block with Option3 Layer.

 

Where can I insert that option in this lisp below. Thanks!

 

(defun c:test ();

	;Error Handling Start
	(setq temperr *error*			
        *error* errortrap
        varlst '("CMDECHO" "DRAGMODE" "CLAYER");edit variables as necessary
        oldvar (mapcar 'getvar varlst)
	)
	
	(setq a (getvar 'cmdecho)) ;gets current cmdecho value and stores it for future use
	(setvar "cmdecho" 1) ;sets  cmdecho value to 1
	(setq b (getvar 'dragmode)) ;gets current dragmode value and stores it for future use
	(setvar "dragmode" 2) ;sets dragmode value to 2
	(setq c (getvar 'clayer)) ;gets the current layer and stores it for future use
	
	
	(Command "-layer" "m" "TestLayer1" "C" "green" "" "") ;creates new layer if not already existing
	(Command "-layer" "m" "TestLayer2" "C" "green" "" "") ;creates new layer if not already existing
	(Command "-layer" "m" "TestLayer3" "C" "green" "" "") ;creates new layer if not already existing
	
	
	
	;inserting blocks
	;;inserting first block
	(setq pt (getpoint "\n::Pick Location")) ;prompt for insert point A
	(Command "-insert" "../test.dwg" pt "" "")
	
	;restoring previous values
	(setvar "cmdecho" a) ;gets stored cmdecho value and sets it as current
	(setvar "dragmode" b) ;gets stored dragmode value and sets it as current
	(setvar "clayer" c) ;gets stored dragmode value and sets it as current
	
	(Command "Zoom" "o" "l" "") ;zooms in last inserted object
	(Command "Regen" "");regenerates drawing
	
	
	;Error Handling End
	(defun errortrap (msg)
	(if oldvar (mapcar 'setvar varlst oldvar))
    (setq *error* temperr)
	(or (wcmatch (strcase msg) "*BREAK,*CANCEL*,*EXIT*")
        (princ (strcat "\n<< Error: " msg " >>"))
    )
	(princ)
	)
	
	;End of Command
	(Princ);
);defun;
0 Likes
Accepted solutions (1)
909 Views
8 Replies
Replies (8)
Message 2 of 9

CodeDing
Advisor
Advisor

@pmercader ,

 

There's some smart people on here, so there Might be a better way to accomplish this. But this way isn't half bad either:

Change This:

(Command "-layer" "m" "TestLayer1" "C" "green" "" "") ;creates new layer if not already existing
(Command "-layer" "m" "TestLayer2" "C" "green" "" "") ;creates new layer if not already existing
(Command "-layer" "m" "TestLayer3" "C" "green" "" "") ;creates new layer if not already existing

To This:

(setq lyrs '("TestLayer1" "TestLayer2" "TestLayer3"))
(initget 1 "1 2 3")
(setq ans (getkword (strcat "\nSelect Layer [1..." (nth 0 lyrs) "/2..." (nth 1 lyrs) "/3..." (nth 2 lyrs) "]: ")))
(Command "-layer" "m" (nth (1- (atoi ans)) lyrs) "C" "green" "" "") ;creates new layer if not already existing

 

...If you were ever going to expand the amount of layers to provide for the user to pick.. then I would recommand to re-visit this issue and create a helper function to accomplish this task.

 

Best,

~DD

0 Likes
Message 3 of 9

pmercader
Advocate
Advocate

it work! Though I failed to edit that the layers need to be different.

 

(Command "-layer" "m" "TestLayer1" "C" "green" "" "") ;creates new layer if not already existing
(Command "-layer" "m" "TestLayer2" "C" "red" "" "") ;creates new layer if not already existing
(Command "-layer" "m" "TestLayer3" "C" "yellow" "" "") ;creates new layer if not already existing

 

Do it just do this?

 

(Command "-layer" "m" (nth (1- (atoi ans)) lyrs) "C" "green" "" "")
(Command "-layer" "m" (nth (2- (atoi ans)) lyrs) "C" "red" "" "")
(Command "-layer" "m" (nth (3- (atoi ans)) lyrs) "C" "yellow" "" "")
0 Likes
Message 4 of 9

CodeDing
Advisor
Advisor

Close:

(Command "-layer" "m" (nth (1- (atoi ans)) lyrs) "C" "green" "" "")
(Command "-layer" "m" (nth (1- (atoi ans)) lyrs) "C" "red" "" "")
(Command "-layer" "m" (nth (1- (atoi ans)) lyrs) "C" "yellow" "" "")
0 Likes
Message 5 of 9

pmercader
Advocate
Advocate

Thanks, it works... but.. all the layers are created yellow. 

0 Likes
Message 6 of 9

CodeDing
Advisor
Advisor
Accepted solution

@pmercader,

 

Lol my mistake, sorry I tried to answer that last question on my phone.

 

Here you go:

(setq lyrs '("TestLayer1" "TestLayer2" "TestLayer3"))
(setq colors '("green" "red" "yellow"))
(initget 1 "1 2 3")
(setq ans (1- (atoi (getkword (strcat "\nSelect Layer [1..." (nth 0 lyrs) "/2..." (nth 1 lyrs) "/3..." (nth 2 lyrs) "]: ")))))
(Command "-layer" "m" (nth ans lyrs) "C" (nth ans colors) "" "") ;creates new layer if not already existing

Best,

~DD

0 Likes
Message 7 of 9

pmercader
Advocate
Advocate

Thank you for your time! I have a hard time creating this on 3 screens, I cant imagine doing it on a phone. haha Thanks again

0 Likes
Message 8 of 9

Sea-Haven
Mentor
Mentor

Down load my multi radio buttons.lsp from Cadtutor / Downloads.

 

You can have as many layername as you like up to screen limits.

 

;;inserting first block
(if (not AH:Butts)(load "Multi Radio buttons.lsp"))
(if (= but nil)(setq but 1))
(setq ans (ah:butts but "v"  '("Choose layer" "Layername1" "Layername2" "layername3"))) ; ans holds the button picked value
(setvar 'celayer ans)
(setq pt (getpoint "\n::Pick Location")) ;prompt for insert point A

screenshot153.png

Using codeding (setq ans (ah:butts but "v"  (cons "Please choose layer" lyrs)))

 

0 Likes
Message 9 of 9

pmercader
Advocate
Advocate

this looks really neat. I might have to try this one too, a link to the download would be great.

0 Likes