View Full Version : -Layer if Layer already exists, not to create it but set it current
fletch97
2006-02-10, 09:32 PM
I have a quick little lisp routine (see below) that works well with no problems, yet I was wondering how to modify it so that if the layer already exists, not to create it but set it current?
(defun c:EC1 ()
(setq DS (getvar "dimscale"))
(command "-layer" "n" "E-COMM" "c" "3" "E-COMM" "S" "E-COMM" "")
(command "insert" "s:/jce-library/blocks/symbols/communications/ec-001.dwg" "scale" DS)
(princ)
)[ Moderator Action = ON ] What are [ CODE ] tags... (http://forums.augi.com/misc.php?do=bbcode#code) [ Moderator Action = OFF ]
I usually use the make option. It creates it if it isn't there.
Hope this helps.
Tim
fletch97
2006-02-10, 09:51 PM
WOW....that works and yet so simple!
Thanks!!
T.Willey
2006-02-10, 11:15 PM
For future reference, you can use
(if (tblsearch "layer" "happy")
(setvar "clayer" "happy")
(command "_.-layer" "_n" "happy" "")
)
Hi Tim,
Ive been using this function for a while:
;;Function to check if a layer is present. If not, the layer is created
;;using the color and linetype arguments (col & lt). Nil response to lt
;;defaults to continuous linetype (to save typing) and nil to col defaults
;;to 7 (white/black).
;;Example (lchk "test" nil nil) = layer test with continuous linetype and color
;;white/black; (lchk "test" "dashed" "3") = layer test with dashed linetype and green
(defun lchk (lname col ltyp)
(if (not ltyp) (setq ltyp "CONTINUOUS"))
(if (not col) (setq col "7"))
(if (not (tblsearch "layer" lname))
(command "layer" "n" lname "c" col lname "lt" ltyp lname "")
)
(princ)
);defun
Do you know of a way to test if the linetype is in an accessible file to be loaded, or trap the error and set the linetype to continuous (or something)?
As it works now, if the linetype is in acad.lin, it will automatically load it; if not, it returns an error:
"linetype_x" not found. Use LINETYPE command to load it.
Thanks,
Tim Creary
T.Willey
2006-02-11, 12:16 AM
In the code posted, I don't see where it is loading the linetype. If the line type dosn't exist in the drawing, then it just makes it continueous. You need to find the part where it is checking/loading the linetype. I forgot how I did this before, I think I did it with the linetype command. I will check, but post back with the code that loads the linetype.
As it is now, "-layer" loads the linetype if it is not loaded but finds it in acad.lin; the routine only passes "continuous" to -layer through "ltyp" if the argument "ltyp" is specified by the programmer as nil.
What I am trying to do is check to make sure the linetype is defined in acad.lin or trap the error that is returned from -layer (maybe?).
Maybe a better question would be: can I check if a linetype is available to load if it's not already loaded?
Thanks for your help!
Tim Creary
T.Willey
2006-02-11, 12:41 AM
One way to trap it is to use activex methods.
(defun LoadLT (LtName / LtCol)
; Return if in drawing, or got loaded, nil if not.
(setq LtCol (vla-get-Linetypes (vla-get-ActiveDocument (vlax-get-Acad-Object))))
(if (vl-catch-all-error-p (vl-catch-all-apply 'vla-Item (list LtCol LtName)))
(if (vl-catch-all-error-p (vl-catch-all-apply 'vla-Load (list LtCol LtName "acad.lin")))
nil
T
)
T
)
)
Cool! 8-) I'm on my way out the door, but I will try it when I get back.
Thanks a bunch!
Tim Creary
Perfect!
Since it returns nil if nil is passed to it, it fell right into place. :-)
(if (not (loadlt ltyp))(setq ltyp "CONTINUOUS"))
Thanks,
Tim Creary
T.Willey
2006-02-11, 04:55 AM
You're welcome.
rkmcswain
2006-02-11, 12:57 PM
For future reference, you can use
(if (tblsearch "layer" "happy")
(setvar "clayer" "happy")
(command "_.-layer" "_n" "happy" "")
)
That doesn't set it current though. Here is another alternative
(if (not (tblsearch "layer" "happy"))
(entmake
(list
(cons 0 "Layer")
(cons 100 "AcDbSymbolTableRecord")
(cons 100 "AcDbLayerTableRecord")
(cons 2 "HAPPY")
(cons 70 0)
(cons 62 7)
(cons 6 "Continuous")
)
)
)
(setvar "clayer" "HAPPY")
kennet.sjoberg
2006-02-11, 01:43 PM
or
(if (tblsearch "layer" "happy")
(setvar "clayer" "happy")
(command "_.-layer" "_m" "happy" "")
)
: ) Happy Computing !
kennet
rkmcswain
2006-02-11, 02:02 PM
or
(if (tblsearch "layer" "happy")
(setvar "clayer" "happy")
(command "_.-layer" "_m" "happy" "")
)
...sure, if you like to use the (command) function and work 11X slower :-)
Create 1000 layers using (entmake) = 0.8 sec.
Create 1000 layers using (command) = 9.2 sec.
Sure if you are only making one layer, it won't make a difference, but who is to say this function may not be needed at some point to make 100, 500, or 1000 layers? If you are building a reuseable code library, there is no reason to choose a much slower method.
kennet.sjoberg
2006-02-11, 04:01 PM
...sure, if you like to use the (command) function and work 11X slower :-)
The speed is not the main thing, if anybody accidently change all your 1000 layers to wrong color or linetype you must use command to reset it, because "LAYER" is a symbol table entries that cannot be modified by entmod or entupd. { if not talking (vl- or VB }
: ) Happy Computing !
kennet
BTW in the beginning of this thread it was only one layer to treat ;)
rkmcswain
2006-02-12, 02:56 PM
The speed is not the main thing, if anybody accidently change all your 1000 layers to wrong color or linetype you must use command to reset it, because "LAYER" is a symbol table entries that cannot be modified by entmod or entupd. { if not talking (vl- or VB }
Wrong. It's very easy.
(setq tabent (entget (tblobjname "Layer" "0")))
(setq newlst (subst (cons 62 2)(assoc 62 tabent) tabent))
(entmod newlst)
Also, very fast.
Change color of 1000 layers using (entmod) = 1.2 sec
Change color of 1000 layers using (command) = 61.6 sec
BTW in the beginning of this thread it was only one layer to treat ;)
I understand that. That is why I said:
who is to say this function may not be needed at some point to make 100, 500, or 1000 layers? If you are building a reuseable code library, there is no reason to choose a much slower method.
kennet.sjoberg
2006-02-12, 05:04 PM
Wrong. It's very easy.
Yes you are right. Sorry for my temporary brain death. I can not understand where I have got that disinformation about entmod, entupd and layer from
I do use it myself in my programs, I take back what I wrote about entmod and layers.
And of course, when seconds and thousands of layers will be very important for me
I promise I will buy a super computer and figure out how to use all those layers.
: ) Happy Computing !
kennet
rkmcswain
2006-02-12, 10:36 PM
And of course, when seconds and thousands of layers will be very important for me
I promise I will buy a super computer and figure out how to use all those layers.
Super computers not needed :-)
We have many drawings that push 500 layers, and when it comes time to modify something in bulk - (entmod) is always faster than (command). We tested vs. the vla functions once, but I don't remember the exact outcome. That was back on 2000. I seem to remember it being close to (entmod), but slightly slower.
If I get time, I'll check that out again.
Have a good one.
kennet.sjoberg
2006-02-12, 11:06 PM
. . . If I get time, I'll check that out again.
Have a good one.
Great, and. . .
: ) Happy Computing !
kennet
vBulletin® v3.6.7, Copyright ©2000-2010, Jelsoft Enterprises Ltd.