Bill Tillman Posted May 14, 2013 Posted May 14, 2013 Trying to learn some new techniques here and need a little advice with this. I have a list of layers I want to create in a new drawing. (defun c:Testing123 (/ a laylist) (setq laylist '("Dim1" "Dim2" "Text1" "Text2" "ConLines" "ThisLayer" "ThatLayer") (foreach a laylist (mLayer (car a)) ) (princ) ) (defun mLayer (LayerName) (vl-load-com) (setq LayerName (vl-catch-all-apply 'vla-add (list (vla-get-layers (vla-get-activedocument (vlax-get-acad-object)) ) Layername ) ) ) (if (vl-catch-all-error-p LayerName) nil LayerName ) ) This does not work of course. So what would it take to get this up on it's feet? Quote
hmsilva Posted May 14, 2013 Posted May 14, 2013 Try (defun c:Testing123 (/ a laylist) (setq laylist '("Dim1" "Dim2" "Text1" "Text2" "ConLines" "ThisLayer" "ThatLayer")) (foreach a laylist (mLayer a) ) (princ) ) Henrique Quote
Lee Mac Posted May 14, 2013 Posted May 14, 2013 Note that you are retrieving the Layers Collection, Document Object and, more importantly, Application Object for every evaluation of the 'mlayer' function (i.e. for every layer to be created); this is incredibly inefficient since the Layers Collections could be retrieved and assigned to a local variable for creation of subsequent layers, e.g.: (defun c:testing123 ( / laycol ) (setq laycol (vla-get-layers (vla-get-activedocument (vlax-get-acad-object)))) (foreach layer '("Dim1" "Dim2" "Text1" "Text2" "ConLines" "ThisLayer" "ThatLayer") (mlayer laycol layer) ) (princ) ) (defun mlayer ( layers layer ) (not (vl-catch-all-error-p (vl-catch-all-apply 'vla-add (list layers layer)))) ) Quote
BlackBox Posted May 14, 2013 Posted May 14, 2013 This does not work of course. So what would it take to get this up on it's feet? Well, you're missing a paren in your Setq call for 'laylist' variable. Also, consider: (foreach a laylist ;;; (mLayer (car a)) (mLayer a) ) Aside from that, you might find this useful: (vl-load-com) (defun c:FOO (/ *error* acDoc) (defun *error* (msg) (if acDoc (vla-endundomark acDoc) ) (cond ((not msg)) ; Normal exit ((member msg '("Function cancelled" "quit / exit abort"))) ; <esc> or (quit) ((princ (strcat "\n** Error: " msg " ** "))) ; Fatal error, display it ) (princ) ) (vla-startundomark (setq acDoc (vla-get-activedocument (vlax-get-acad-object))) ) (foreach layerName '([color="red"]"Dim1" "Dim2" "Text1" "Text2" "ConLines" "ThisLayer" "ThatLayer"[/color] ) (if (not (tblsearch "layer" layerName)) (vla-add (vla-get-layers acDoc) layerName) ) ) (*error* nil) ) Quote
BlackBox Posted May 14, 2013 Posted May 14, 2013 Note that you are retrieving the Layers Collection, Document Object and, more importantly, Application Object for every evaluation of the 'mlayer' function (i.e. for every layer to be created); this is incredibly inefficient since the Layers Collections could be retrieved and assigned to a local variable for creation of subsequent layers, e.g.: I agree with you regarding the root Application, and even Document Objects, but might still prefer to get the Layers Collection after adding a new layer which would preclude accidental duplicates being fed, and avoids the need for vl-Catch-All-Apply altogether. Just my humble opinion, despite efficiency cost. Quote
hmsilva Posted May 14, 2013 Posted May 14, 2013 just a different approach if the layer list is gonna be hard-coded, why not simply use (defun c:test () (command "-layer" "_N" "Dim1,Dim2,Text1,Text2,ConLines,ThisLayer,ThatLayer" "") (princ) ) will work even if the layers exist, been frozen, off or even locked... Henrique Quote
Lee Mac Posted May 14, 2013 Posted May 14, 2013 ...but might still prefer to get the Layers Collection after adding a new layer which would preclude accidental duplicates being fed, and avoids the need for vl-Catch-All-Apply altogether. I don't see your point regarding duplicates - since the local variable is merely a pointer to the Layers Collection, the behaviour of the function would be identical to if the Layers Collection were retrieved for each evaluation, except marginally more efficient of course. Also, note that the vl-catch-all-apply is to catch invalid symbol names, not duplicate items, since if the item supplied to vla-add already resides in the collection, this existing item is returned without error. Quote
BlackBox Posted May 14, 2013 Posted May 14, 2013 I don't see your point regarding duplicates No matter... I was referring to duplicate layer names being supplied to vla-Add calls (without TblSearch), not duplicate Layers Collection assignment/calls... By adding a layer to the layers collection, one has changes the layers collection, thus introducing the possibility for an error were a duplicate layer name supplied. That is all; a non-issue for most. Quote
Lee Mac Posted May 14, 2013 Posted May 14, 2013 By adding a layer to the layers collection, one has changes the layers collection, thus introducing the possibility for an error were a duplicate layer name supplied. See my second point: note that the vl-catch-all-apply is to catch invalid symbol names, not duplicate items, since if the item supplied to vla-add already resides in the collection, this existing item is returned without error. Quote
BIGAL Posted May 15, 2013 Posted May 15, 2013 Bill my $0.05 worth why not just have a read layers subroutine and pass it the name of a text file this way you can have all the parameters set as you like for groups of predefined layers including all the other bits like colour and linetype. eg 1234567890123456789012345678901234567890 Newname Alias name Col Line Type brick--1 brick--1 1 continuous ceiling--2 ceiling--2 6 continuous ceiling--3 ceiling--3 6 continuous ceiling--5 ceiling--5 6 continuous centreline--2 centreline--2 4 center centreline--3 centreline--3 5 center centreline--5 centreline--5 6 center const--1 const--1 1 continuous const--3 const--3 1 continuous Quote
Tharwat Posted May 15, 2013 Posted May 15, 2013 Another . (defun _DoLayers (/ Mlayer col) (defun Mlayer ( col l) (vl-catch-all-apply 'vla-add (list col l)) ) (setq col (vla-get-layers (vla-get-activedocument (vlax-get-acad-object)))) (mapcar '(lambda (x) (Mlayer col x)) (list "Dim1" "Dim2" "Text1" "Text2" "ConLines" "ThisLayer" "ThatLayer")) (princ) ) Quote
BlackBox Posted May 15, 2013 Posted May 15, 2013 See my second point: Forgive my confusion... Nowhere in the Documentation (old or new) is this 'returning of the existing item' behavior stated for the Add Method regarding Layers; but you're correct. I should have tested prior to my retort... As always, thanks for teaching me something new, Lee. Quote
Lee Mac Posted May 15, 2013 Posted May 15, 2013 Nowhere in the Documentation (old or new) is this 'returning of the existing item' behavior stated for the Add Method regarding Layers; Given your experience, did you really expect it to be? As always, thanks for teaching me something new, Lee. No worries dude! Quote
BlackBox Posted May 15, 2013 Posted May 15, 2013 Given your experience, did you really expect it to be? Touché ... I'll see if my new friend Dieter can add this to the growing list of Documentation changes being requested in the Beta forums. Quote
alanjt Posted May 15, 2013 Posted May 15, 2013 OPTIONS! (defun _layers (lst col / layer) (foreach i lst (if (not (vl-catch-all-error-p (setq layer (vl-catch-all-apply 'vla-add (list col (car i)))))) (mapcar (function (lambda (p v) (vl-catch-all-apply 'vlax-put-property (list layer p v)))) '(Color Linetype Plottable Description) (cdr i) ) ) ) ) eg. (_layers '(("ALAN" 1 "Continuous" :vlax-true "it's me!") ("HIDE" 2 "Hidden" :vlax-false "we don't want to see this crap") ) ) Disclaimer: UNTESTED! Quote
BlackBox Posted May 15, 2013 Posted May 15, 2013 ... Those options make me think of this ole' thang. Quote
alanjt Posted May 15, 2013 Posted May 15, 2013 ... Those options make me think of this ole' thang. More options! Quote
Lee Mac Posted May 15, 2013 Posted May 15, 2013 For those of a Vanilla persuasion... http://www.cadtutor.net/forum/showthread.php?65302&p=447101&viewfull=1#post447101 Quote
Bill Tillman Posted May 16, 2013 Author Posted May 16, 2013 Once again, this forum proves to be filled with the most talented programmers to be found on the planet. Many thanks to all who replied and the side discussions became interesting on this one too. 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.