Author Topic: How to list unreconciled layers.  (Read 4170 times)

0 Members and 1 Guest are viewing this topic.

borgiedude

  • Guest
How to list unreconciled layers.
« on: November 16, 2015, 04:37:54 PM »
Hello all,

I've been writing code with lisp for a while but I've recently started trying to branch out with visual lisp. I've spent a couple of nights after work Googling for a way to perform a visual lisp check but I can't seem to find a helpful example.

Basically I want to check if there are unreconciled layers in a drawing and for each unreconciled layer, I want to perform some operations, such as freezing the layer and adding its name to a list before reconciling the layer.

My question is, how would I iterate through the list of layers checking for the unreconciled property?

I've gotten as far as (setq layers (vla-get-layers (vla-get-activedocument (vlax-get-acad-object)))), and I can iterate through these with (vlax-for). I think that getextensiondictionary should be used somewhere, but I can't seem to find out how to phrase a logical test.

Can anybody offer a bit of advice? Any help would be greatly appreciated.

ChrisCarlson

  • Guest
Re: How to list unreconciled layers.
« Reply #1 on: November 17, 2015, 08:11:39 AM »
Perhaps this is of some use

Code - Auto/Visual Lisp: [Select]
  1. ;;; PurgeReconciledLayers.LSP
  2. ;;;
  3. ;;; By Jimmy Bergmark
  4. ;;; Copyright (C) 2007 JTB World, All Rights Reserved
  5. ;;; Website: www.jtbworld.com
  6. ;;; E-mail:
  7.  info@jtbworld.com
  8. ;;; 2007-04-05 - First release
  9. ;;; Written for AutoCAD 2008
  10.  
  11. ;;; Purge all information about reconciled layers in the drawing
  12.  
  13. (defun PurgeReconciledLayers ()
  14.                     (vla-get-ActiveDocument
  15.                       (vlax-get-acad-object)
  16.                     )
  17.                   )
  18.     (vl-Catch-All-Apply
  19.       '(lambda ()
  20.          (vla-Remove
  21.            (vla-GetExtensionDictionary
  22.              layer
  23.            )
  24.            "ADSK_XREC_LAYER_RECONCILED"
  25.          )
  26.        )
  27.     )
  28.     (vl-Catch-All-Apply
  29.       '(lambda ()
  30.          (vla-delete
  31.            (vla-GetExtensionDictionary
  32.              layer
  33.            )
  34.          )
  35.        )
  36.     )
  37.   )
  38.   (setvar "LAYEREVAL" 0)
  39.   (setvar "LAYERNOTIFY" 0)
  40.   (princ)
  41. )
  42.  

borgiedude

  • Guest
Re: How to list unreconciled layers.
« Reply #2 on: November 17, 2015, 09:31:43 PM »
Thanks for posting that Chris.

I've actually come across things like this a lot in my searching but I've been stuck with a few questions after viewing these sorts of code.

From what I can tell, the script is removing the unreconciled property from all layers in lines 20-29. But if that's the case I'm not sure what lines 30-39 are doing; removing the extension dictionary altogether?

Also I've seen "ADSK_XREC_LAYER_RECONCILED" pop up a lot, and I know that's the detail which I need to interact with, but I'm not sure how I would find that a layer had this property. What I mean is, if I dump the extension dictionary for a layer, this term "ADSK_XREC_LAYER_RECONCILED" does not show up; how could I know that this is what the property is called in the VBA language?

What I keep thinking is along these lines:

(if (= "ADSK_XREC_LAYER_RECONCILED" some-extension-dictionary-function) (progn ...))

Any idea what 'some-extension-dictionary-function' could be?

ChrisCarlson

  • Guest
Re: How to list unreconciled layers.
« Reply #3 on: November 18, 2015, 10:40:52 AM »
Code - Auto/Visual Lisp: [Select]
  1. (defun isreconciled (layname)
  2. ;;http://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/layereval-layernotify/m-p/4705497
  3.         (not
  4.                 (vl-catch-all-error-p
  5.                         (vl-catch-all-apply
  6.                         'vla-item
  7.                                 (list
  8.                                         (vla-getextensiondictionary
  9.                                                 (vla-item
  10.                                                         (vla-get-layers
  11.                                                                 (vla-get-activedocument
  12.                                                                         (vlax-get-acad-object)
  13.                                                                 )
  14.                                                         )
  15.                                                 layname)
  16.                                         )
  17.                                 "ADSK_XREC_LAYER_RECONCILED"
  18.                                 )
  19.                         )
  20.                 )
  21.         )
  22. )
  23.  

(isreconciled "layer") will return T if reconciled and nill if unreconciled.

borgiedude

  • Guest
Re: How to list unreconciled layers.
« Reply #4 on: November 18, 2015, 04:31:46 PM »
Thank you very much Chris.

After work today I'll have a play around with this and see if I can understand what each line is doing. Then I'll probably try to adapt it to my use and post my results!

borgiedude

  • Guest
Re: How to list unreconciled layers.
« Reply #5 on: November 19, 2015, 03:28:59 AM »
Ok, I think I've put it together. It will probably become apparent from my thinking out loud that I'm very new at this, but hopefully this will help another newbie.

After obtaining the list of layers as a vl object, lines 9 and 15 of the script return the named layer as a vl item.

The extension dictionary is then found for the layer and in lines 5 and 6 the extension dictionary is searched for the vl item identified as "ADSK_XREC_LAYER_RECONCILED". If the vl item is found, line 4 of the script returns nil, since there has been no error, and if the reconciled property is not found, 'vl-item applied to that extension dictionary returns an error, and vl-catch-all-error-p returns T.

Finally, line 3 inverts the T/nil so that logically, isreconciled returns true if the layer is reconciled!

Taking what I've learned, my script can be written as follows:

Code - Auto/Visual Lisp: [Select]
  1. (defun c:getunreclayers ( / layers i unreclist )
  2.         i 0) ; Gather the layers in the active document and set a counter to 0
  3.   (repeat (vla-get-count layers) ; Cycle through every layer
  4.     (progn
  5.       (if       ; Check to see if
  6.         (vl-catch-all-error-p ; An error is returned
  7.           (vl-catch-all-apply 'vla-item ; When searching for
  8.             (list (vla-GetExtensionDictionary (vla-item layers i)) ; The exten-dictionary item
  9.                   "ADSK_XREC_LAYER_RECONCILED") ; indicated by this string
  10.             )
  11.           )
  12.         (setq unreclist
  13.                (append unreclist ; If an error is returned, because the reconciled property is not found
  14.                        (list ; Add the name of the unreconciled layer
  15.                          (vla-get-name  ; To the growing list of unreconciled layers
  16.                            (vla-item layers i)
  17.                            )
  18.                          )
  19.                        )
  20.               )
  21.         )
  22.       (setq i (+ i 1)) ; increase the counter by 1
  23.       )
  24.     )
  25.   unreclist ; return the list of unreconciled layers
  26. )
  27.  

It's not too pretty, I'm sure there are better ways of doing things, and I don't doubt that Lee Mac could write  something functionally identical in just 4 lines, but it works and I'm happy with it.

Thanks again for your help Chris!

Lee Mac

  • Seagull
  • Posts: 12913
  • London, England
Re: How to list unreconciled layers.
« Reply #6 on: November 22, 2015, 12:28:15 PM »
FWIW, here is another way to approach it - from a Vanilla perspective:
Code - Auto/Visual Lisp: [Select]
  1. (defun unreconlayers ( / def dic lay lst )
  2.     (while (setq def (tblnext "layer" (not def)))
  3.         (setq lay (cdr (assoc 2 def))
  4.               dic (cdr (assoc 360 (entget (tblobjname "layer" lay))))
  5.         )
  6.         (or (and  dic (dictsearch dic "adsk_xrec_layer_reconciled"))
  7.             (setq lst (cons lay lst))
  8.         )
  9.     )
  10.     (reverse lst)
  11. )

borgiedude

  • Guest
Re: How to list unreconciled layers.
« Reply #7 on: November 24, 2015, 12:28:11 AM »
Wow, thanks Lee, I didn't know that it was possible to do that with vanilla Lisp.

(THE Lee Mac actually replied to MY thread  :smitten:)


Lee Mac

  • Seagull
  • Posts: 12913
  • London, England
Re: How to list unreconciled layers.
« Reply #8 on: November 24, 2015, 05:50:57 AM »
Wow, thanks Lee, I didn't know that it was possible to do that with vanilla Lisp.

Indeed some data is only accessible through Visual LISP, however Layer Extension Dictionaries are certainly exposed to Vanilla AutoLISP; personally I prefer to access the dictionaries in this way, as the dictsearch function will return nil when the target item is not present, whereas the equivalent ActiveX item method or getobject method will error under such circumstances, requiring the use of a vl-catch-all-apply/error-p statement, which is a bit cumbersome.

(THE Lee Mac actually replied to MY thread  :smitten:)

Haha! You flatter me  :-)