TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: vicoco on May 04, 2021, 09:37:37 AM

Title: Modifying layers in multiple files at once
Post by: vicoco on May 04, 2021, 09:37:37 AM
Hello everybody,
I'm using a LeeMac AutoLisp, "Copy2Drawing", to copy some objects into a list of drawings in a directory.
I want to extend the code by adding some more functions. But these new functions only work on the current document, and not on the entire collection of drawings .

The new functions would be:
- set layer "0" as the current layer,
- freeze two layers (if they exist) according to some indicated names.

The code I used as follow (unsuccessfully), between highlighted lines (included in an extract from the original AutoLisp file):

Code - Auto/Visual Lisp: [Select]
  1. (foreach dwg lst
  2.                 (if
  3.                     (or (setq doc (cdr (assoc (strcase dwg) dwl)))
  4.                         (and (not (vl-catch-all-error-p (vl-catch-all-apply 'vla-open (list dbx dwg))))
  5.                              (setq doc dbx)
  6.                         )
  7.                     )
  8.                     (progn
  9.                         (vla-copyobjects acd var
  10.                             (vla-get-block
  11.                                 (cond
  12.                                     (   (_getitem (vla-get-layouts doc) tab))
  13.                                     (   (vla-add  (vla-get-layouts doc) tab))
  14.                                 )
  15.                             )
  16.                         )
  17.                         ;; New code start
  18.                         (setq LayerTable (vla-get-layers acd))
  19.                         (setq layer0 (vla-item LayerTable 0))
  20.                         (vla-put-activelayer acd layer0)
  21.                         (setq layer1 (vla-item LayerTable "LayerName1")
  22.                                 layer2 (vla-item LayerTable "LayerName2")
  23.                         )
  24.                         (vla-put-freeze layer1 :vlax-true)
  25.                         (vla-put-freeze layer2 :vlax-true)
  26.                         ;; New code finish
  27.                         (vla-saveas doc dwg)
  28.                         (setq cnt (1+ cnt))
  29.                     )
  30.                     (princ (apply 'strcat (cons "\nUnable to interface with file: " (cdr (fnsplitl dwg)))))
  31.                 )
  32.             )
  33.  

Note: "layer1" and "layer2" will not always exist. It may be that both exist, only one of them, or that neither exist.

Could you help me about this?
Title: Re: Modifying layers in multiple files at once
Post by: ronjonp on May 04, 2021, 12:25:21 PM
Welcome to TheSwamp :) Try something like this for your layers:
Code - Auto/Visual Lisp: [Select]
  1. (foreach lyr '("LayerName1" "LayerName2")
  2.   (if (not (vl-catch-all-error-p (setq l (vl-catch-all-apply 'vla-item (list layertable lyr)))))
  3.     (vl-catch-all-apply 'vla-put-freeze (list l :vlax-true))
  4.   )
  5. )
I don't think you can set a current layer via odbx, but could be wrong.
Title: Re: Modifying layers in multiple files at once
Post by: Lee Mac on May 04, 2021, 05:45:33 PM
I don't think you can set a current layer via odbx, but could be wrong.

Correct - the activelayer property is not available for an ObjectDBX document, and neither is the setvariable method unfortunately.

@vicoco, this:
Code - Auto/Visual Lisp: [Select]
  1. (setq LayerTable (vla-get-layers acd))

Should be:
Code - Auto/Visual Lisp: [Select]
  1. (setq LayerTable (vla-get-layers doc))

So as to obtain the Layers Collection for the ObjectDBX document as opposed to the Active Document.
Title: Re: Modifying layers in multiple files at once
Post by: vicoco on May 05, 2021, 05:21:25 AM
Welcome to TheSwamp :) Try something like this for your layers:
Code - Auto/Visual Lisp: [Select]
  1. (foreach lyr '("LayerName1" "LayerName2")
  2.   (if (not (vl-catch-all-error-p (setq l (vl-catch-all-apply 'vla-item (list layertable lyr)))))
  3.     (vl-catch-all-apply 'vla-put-freeze (list l :vlax-true))
  4.   )
  5. )
I don't think you can set a current layer via odbx, but could be wrong.

I don't think you can set a current layer via odbx, but could be wrong.

Correct - the activelayer property is not available for an ObjectDBX document, and neither is the setvariable method unfortunately.

@vicoco, this:
Code - Auto/Visual Lisp: [Select]
  1. (setq LayerTable (vla-get-layers acd))

Should be:
Code - Auto/Visual Lisp: [Select]
  1. (setq LayerTable (vla-get-layers doc))

So as to obtain the Layers Collection for the ObjectDBX document as opposed to the Active Document.

Thank you very much to both!!

Because AutoCAD does not allow freezing the current layer, and it's possible that some dwg has one of the layers to freeze as current, I have decided to modify the code, turning the layers off instead of freezing:

Code - Auto/Visual Lisp: [Select]
  1. (foreach lyr '("LayerName1" "LayerName2")
  2.   (if (not (vl-catch-all-error-p (setq l (vl-catch-all-apply 'vla-item (list layertable lyr)))))
  3.     (vl-catch-all-apply 'vla-put-layeron (list l :vlax-false))
  4.   )
  5. )

I am a very beginner in LISP and VL, and I am still learning and understanding many new things every day.
Title: Re: Modifying layers in multiple files at once
Post by: ronjonp on May 05, 2021, 04:20:49 PM
You can freeze the current layer via DBX  :-)
Title: Re: Modifying layers in multiple files at once
Post by: vicoco on May 06, 2021, 04:15:13 AM
You can freeze the current layer via DBX  :-)

Good point!!
This remark is "freezing" my brain!  :wink:
Thanks again.
Title: Re: Modifying layers in multiple files at once
Post by: cmwade77 on May 11, 2021, 07:47:45 PM
You can freeze the current layer via DBX  :-)
Yikes! That sounds like a recipe for disaster, do you have a way using DBX to check the current layer?
Title: Re: Modifying layers in multiple files at once
Post by: ronjonp on May 11, 2021, 09:40:43 PM
You can freeze the current layer via DBX  :-)
Yikes! That sounds like a recipe for disaster, do you have a way using DBX to check the current layer?
I do not.