can't get old layer to become current layer

can't get old layer to become current layer

Anonymous
Not applicable
1,251 Views
6 Replies
Message 1 of 7

can't get old layer to become current layer

Anonymous
Not applicable

 

Hi everyone, I'm trying to write a lisp routine for a co-worker so he can put a dimension on his dwg on the dimension layer.  Trying to grab the CLAYER, change layer to Dim layer for the dimension, and once the dimension is in place I would like the current layer to be on the layer before the routine started.

 

I have this so far:

(defun c:d ()
(setq oldlay (getvar "clayer"))
(command "-layer" "m" "DIMENSION" "c" "1" "DIMENSION" "L" "continuous" "DIMENSION" "lw" ".18" "DIMENSION" "")
(command "dimlinear" (prompt getpoint "Specify first extension line origin:") (prompt getpoint "Specify second extension line origin: ") (prompt getpoint "Specify dimension line location: "))
(setvar  "clayer" oldlay)
(princ)

I can start on any layer, the routine changes the layer to Dim layer, and can drop a dimension in the DWG, but after the dimension is in place, it doesn't go back to original layer.  Any help would be appreciated greatly!

0 Likes
Accepted solutions (1)
1,252 Views
6 Replies
Replies (6)
Message 2 of 7

CodeDing
Advisor
Advisor
Accepted solution

@Anonymous ,

 

Thank you for posting some code! Looks like you're learning, so that's great. Couple things to note from your code:

- looks like you're missing the closing parentheses for your DEFUN

- always try to declare your local variables (even when prototyping)

- you're calling the prompt function incorrectly, there should only be one argument (but you're clearly trying to use the (getpoint) function, but that is also missing its parentheses).

 

Other than that, I haven't tested this but it should work for you. We're going to use command-s for this case, since if the user escapes we only have 1 more line to execute (a byproduct of escaping during the command-s call) and that's ok. Command-s will do the heavy lifting for us and let the user create the dim and also give them all the options of the original command (if that's what you want, but sometimes people don't).

(defun c:D ( / oldLay)
  (setq oldLay (getvar "clayer"))
  (command "-layer" "m" "DIMENSION"
                    "c" "1" "DIMENSION"
                    "L" "continuous" "DIMENSION"
                    "lw" ".18" "DIMENSION" "")
  (command-s "_.DIMLINEAR")
  (setvar "clayer" oldLay)
  (princ)
);defun

Hope this helps. Best,

~DD

Message 3 of 7

Anonymous
Not applicable

@CodeDing 

Yes, I missed the last closing parentheses when I copied my code. Declaring the variable and 'Command-s' was the ticket!

 

Thanks and much appreciated CodeDing

0 Likes
Message 4 of 7

Kent1Cooper
Consultant
Consultant

@CodeDing wrote:

....

....
  (command "-layer" "m" "DIMENSION"
                    "c" "1" "DIMENSION"
                    "L" "continuous" "DIMENSION"
                    "lw" ".18" "DIMENSION" "")
....

....


A few small suggestions about the Layer command:

 

When you use the Make option, it makes the Layer current in the process, so it is the default Layer for assigning color, linetype and lineweight, and you can just use Enter [""] to accept that default Layer name, instead of spelling out the Layer name each time.

 

And all new Layers get Continuous linetype by default, so unless it's possible that the Layer already exists but with a different linetype and you want to fix that, there's no need to use that Linetype option.

 

But if there's any chance the Layer will exist already but be Frozen, it's a good idea to Thaw it before the Make option, because a Frozen Layer can't be made current, so its name will be rejected by the Make option.  If it doesn't yet exist, it won't matter -- a message will go by that it didn't find any matching Layer name(s) to Thaw, but it will just go back to the main Layer prompt and go on.

 

This should be enough:

....
  (command "-layer" "t" "DIMENSION" "m" "DIMENSION"
                    "c" "1" ""
                    "lw" ".18" "" "")
....

 

Kent Cooper, AIA
Message 5 of 7

Sea-Haven
Mentor
Mentor

Just a suggestion its probably good practice to check 1st if exist.

 

 

(if (not (tblsearch "LAYER" "DIMENSION"))
  (command "-layer" "m" "DIMENSION"
                    "c" "1" ""
                    "L" "continuous" ""
                    "lw" ".18" "" "")
(setvar 'clayer "DIMENSION")
)

 

 

  

0 Likes
Message 6 of 7

Kent1Cooper
Consultant
Consultant

@Sea-Haven wrote:

Just a suggestion its probably good practice to check 1st if exist.

....  


That's not necessary, in my opinion.  If it already exists, the Make option will simply make it current, and no (setvar) function will be needed to do that.  The only situation in which I think it's worth checking is if it might already exist, but with a color or linetype or lineweight different from the standard, and  you want to allow it to remain  different, rather than let the code correct it to the standard options.

Kent Cooper, AIA
0 Likes
Message 7 of 7

Sea-Haven
Mentor
Mentor

I knew about "Make" has that nice option of working even if exists.

0 Likes