Author Topic: Freezing/Thawing Layers by Wildcard Match.  (Read 12736 times)

0 Members and 1 Guest are viewing this topic.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Freezing/Thawing Layers by Wildcard Match.
« on: November 10, 2014, 08:10:01 AM »

I'm looking at a project that has about 320 Layers including XRef layers.

I need to Freeze / thaw layers depending on  wildcard matches (multiple combinations of course).
There are about 40 wildcard matches that have to be satisfied.

I've tried entity dxf mods, ActiveX mods and a dot.net plug-in to change the Layer states according to the wildcard strings.

All the above methods require the iteration of the Layer table  and wcmatching the layer name against a ( or several) filters.

I am unable to find any method that is faster than a simple vl-cmdf or command-s command call and pass the multiple filters comma seperated.
ie:
(vl-cmdf "_.layer" "_ON" dataThawList "_thaw" dataThawList "")
(vl-cmdf "_.layer" "_f" "XR*02)*,XR*cotes*" "")

.. and the bonus is the command does not require a regen of the viewport.

Has anyone come across a code solution that is faster than vl-cmdf or command-s for handling complex wildcard nominations.

The command method is actually reasonably fast, but I want to satisfy due diligence.

Regards,
Kerry
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: Freezing/Thawing Layers by Wildcard Match.
« Reply #1 on: November 10, 2014, 08:54:16 AM »
Hello Kerry!  :-)

Publish here an example of your file for testing.
I have a lot of different codes for solving the problem,
 but never did serious research.

 I'm curious!
 :-D

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1453
  • Marco
Re: Freezing/Thawing Layers by Wildcard Match.
« Reply #2 on: November 10, 2014, 12:10:00 PM »
Thanks for pointing this out, I had never tested before and I thought that without command was faster...
Code: [Select]
Comando: (length (ALE_GetTableNames    "LAYER")) >>> 407

Benchmark.lsp | © 2005 Michael Puckett | All Rights Reserved

Elapsed milliseconds / relative speed for 128 iteration(s):
    (CMDF_LAYERON)......1763 / 8.57 <fastest>
    (ALE_LAYERON)......15101 / 1 <slowest>

Elapsed milliseconds / relative speed for 64 iteration(s):
    (CMDF_LAYERON).....1685 / 3.08 <fastest>
    (ALE_LAYERON)......5195 / 1 <slowest>

Elapsed milliseconds / relative speed for 64 iteration(s):
    (CMDF_LAYERON).....1653 / 4.62 <fastest>
    (ALE_LAYERON)......7629 / 1 <slowest>

lapsed milliseconds / relative speed for 128 iteration(s):
    (CMDF_LAYERON)......1981 / 6.51 <fastest>
    (CMDF_LAYERON)......2637 / 4.89
    (CMDF_LAYERON)......3073 / 4.2
    (ALE_LAYERON).......9375 / 1.38
    (ALE_LAYERON)......11793 / 1.09
    (ALE_LAYERON)......12902 / 1 <slowest>


Code: [Select]
(defun cmdf_LayerON ( )
 (vl-cmdf "_.layer" "_Off" "*$???_*" "_Y" "")
 (vl-cmdf "_.layer" "_ON" "*$???_*" "")
)
(defun ALE_LayerON ( )
 (ALE_Layer_PutOnAll *AcAcDwg* *AcLayrs* '(("_ON" . "*$???_*")))
 (ALE_Layer_PutOnAll *AcAcDwg* *AcLayrs* '(("_Off" . "*$???_*")))
)

; Function: ALE_Layer_PutOnAll
;
; Version 1.00 - 16/11/2006

; Description:
;   Put Layers On or Off
;
; Arguments:
;   VlaDoc: IAcadDocument: An AutoCAD drawing or IAxDbDocument: Interface
;   LrsCol: Layer Collection
;   ChgLst: list of dotted pairs Layers commands and names in reverse order
;           (used reverse order because I use cons to build the list rather than append), example:
;           (("_OFF" . "*_Abc*") ("_ON" . "*Com_*") ("_ON" . "~*$*") ("_ON" . "*Xyz_*") ("_OFF" . "*|$*,$*"))
;           first  element = command      > "_ON" or "_OFF"  case insensitive
;           second element = layers names > see WcMatch      case insensitive
;
; Examples:
;   (or *AcadApp* (setq *AcadApp* (vlax-get-Acad-Object)            ))
;   (or *AcAcDwg* (setq *AcAcDwg* (vla-get-ActiveDocument *AcadApp*)))
;   (or *AcLayrs* (setq *AcLayrs* (vla-get-Layers         *AcAcDwg*)))
;
;   (ALE_Layer_PutOnAll *AcAcDwg* *AcLayrs* '(("_OFF" . "*_Abc*") ("_ON" . "*Com_*")))
;   ==> On Layers "*COM_*" than Off Layers "*_ABC*"
;
;   (ALE_Layer_PutOnAll *AcAcDwg* *AcLayrs* '(("_ON" . "*")))
;   ==> On all layers
;
; Return Values: VLA-OBJECT IAcadLayers 
;
(defun ALE_Layer_PutOnAll (VlaDoc LrsCol ChgLst / LyrNam BitVal)
  (setq ChgLst (reverse (read (strcase (vl-prin1-to-string ChgLst)))))
  (vlax-map-Collection
    LrsCol
   '(lambda (LmbDat)
      (setq
        BitVal nil
        LyrNam (strcase (vla-get-Name LmbDat))
      )
      (foreach ForElm ChgLst
        (if (wcmatch LyrNam (cdr ForElm))
          (if (= (car ForElm) "_ON") (setq BitVal 1) (setq BitVal 0))
        )
      )
      (and
        BitVal
        (if (zerop BitVal)
          (vl-catch-all-apply 'vla-put-LayerOn (list LmbDat :vlax-false))
          (vl-catch-all-apply 'vla-put-LayerOn (list LmbDat :vlax-true ))
        )
      )
    )
  )
)

roy_043

  • Water Moccasin
  • Posts: 1895
  • BricsCAD 18
Re: Freezing/Thawing Layers by Wildcard Match.
« Reply #3 on: November 10, 2014, 01:43:11 PM »
@ Marc'Antonio Alessi:
Maybe the vl-catch-all-apply stucture is slowing things down? I am not sure why you use it.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Freezing/Thawing Layers by Wildcard Match.
« Reply #4 on: November 10, 2014, 03:40:21 PM »
Hello Kerry!  :-)

Publish here an example of your file for testing.
I have a lot of different codes for solving the problem,
 but never did serious research.

 I'm curious!
 :-D

Hi Evgeniy,
I don't have a generic sample drawing available at the moment, and I can't publish my clients drawings and xrefs.
I'll attempt to post an example later today.

Hi Marc'Antonio Alessi,
Those are the sort of execution time  relationships I'm seeing as well,


// ----------------

Regards,
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

BlackBox

  • King Gator
  • Posts: 3770
Re: Freezing/Thawing Layers by Wildcard Match.
« Reply #5 on: November 10, 2014, 04:00:22 PM »
.. and the bonus is the command does not require a regen of the viewport.

Has anyone come across a code solution that is faster than vl-cmdf or command-s for handling complex wildcard nominations.

The command method is actually reasonably fast, but I want to satisfy due diligence.

This has been my finding as well; I'm not adept at ARX, CRX, etc. but presumably one of them was used to write the Layer Commands, just not sure if those APIs are exposed, even if proficient at that language (if that makes sense?).



Only loosely related, Autodesk has exposed 'more' in terms of the Bindable Object Layer (BOL), but BOL is used more for near-instant event handling when Layers are created, modified, or deleted, and only really capable of limited modification of same, so not useful for your specific task.

Cheers
« Last Edit: November 10, 2014, 04:09:29 PM by BlackBox »
"How we think determines what we do, and what we do determines what we get."

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1453
  • Marco
Re: Freezing/Thawing Layers by Wildcard Match.
« Reply #6 on: November 10, 2014, 04:07:13 PM »
@ Marc'Antonio Alessi:
Maybe the vl-catch-all-apply stucture is slowing things down? I am not sure why you use it.
It is to avoid problems when I process many files, I think the difference is very small:
Code: [Select]
Elapsed milliseconds / relative speed for 256 iteration(s):
    (CMDF_LAYERON)......2122 / 5.77 <fastest>
    (ALE_LAYERON2).....11606 / 1.06
    (ALE_LAYERON)......12246 / 1 <slowest>

Elapsed milliseconds / relative speed for 256 iteration(s):
    (CMDF_LAYERON)......2012 / 7.75 <fastest>
    (ALE_LAYERON)......12855 / 1.21
    (ALE_LAYERON2).....15600 / 1 <slowest>
Code: [Select]
(defun ALE_Layer_PutOnAll2 (VlaDoc LrsCol ChgLst / LyrNam BitVal)
  (setq ChgLst (reverse (read (strcase (vl-prin1-to-string ChgLst)))))
  (vlax-map-Collection
    LrsCol
   '(lambda (LmbDat)
      (setq
        BitVal nil
        LyrNam (strcase (vla-get-Name LmbDat))
      )
      (foreach ForElm ChgLst
        (if (wcmatch LyrNam (cdr ForElm))
          (if (= (car ForElm) "_ON") (setq BitVal 1) (setq BitVal 0))
        )
      )
      (and
        BitVal
        (if (zerop BitVal)
          (vla-put-LayerOn LmbDat :vlax-false)
          (vla-put-LayerOn LmbDat :vlax-true)
        )
      )
    )
  )
)
Here is my sample.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Freezing/Thawing Layers by Wildcard Match.
« Reply #7 on: November 10, 2014, 04:28:06 PM »
Thanks for the drawing example Marc'Antonio Alessi,

For an even playing field ;

Layers that match the WildCard filter list are to be turned ON and THAWED,
All other Layers are to be turned FROZEN

With the exception of '0' and 'defpoints' which are to stay ON and THAWED.

I'll be extremely surprised if any solution is faster and less error prone than vl-cmdf or command-s

Thanks for the input.
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: Freezing/Thawing Layers by Wildcard Match.
« Reply #8 on: November 10, 2014, 08:15:36 PM »
Dunno how fast it is but quickly coded and posted for fun / your consideration:

Did blind, absolutely painful edit on an iPad, hopefully works.

Code: [Select]
(defun _ThawLayers ( filter invert_filter / @Match @Main )

    ;;  Thaw and turn on layers that match the filter,
    ;;  otherwise freeze and turn off, with special
    ;;  treatment of layer "0" and "Defpoints".
    ;;
    ;;  Syntax:
    ;;
    ;;      Thaw layers matching the filter:
    ;;
    ;;          (_ThawLayers "*support*,*e" nil)
    ;;
    ;;      Thaw layers NOT matching the filter (read another
    ;;      way -- freeze layers matching the filter):
    ;;
    ;;          (_thawlayers "*weld*,*gaskets*,*bolts*" t)

    (vl-load-com)

    (if invert_filter
        (defun @Match ( x ) (not (wcmatch x filter))) ;; filter is a
        (defun @Match ( x ) (wcmatch x filter))       ;; lexical global
    )

    (defun @Main ( doc filter clayer / name off frozen)

        ;;  group existing states etc ...

        (vlax-for layer (vla-get-layers doc)

            (cond

                (   (eq clayer
                        (setq
                            off    (eq :vlax-false (vla-get-layeron layer))
                            frozen (eq :vlax-true (vla-get-freeze layer))
                            name   (strcase (vla-get-name layer))
                        )
                    )
                    (if (@Match name)
                        ;;  you can only feck the on state
                        ;;  for the active layer -- try to
                        ;;  change the freeze state and kaboom
                        (vla-put-layeron layer :vlax-true)
                    )
                )

                (   (member name '("0" "DEFPOINTS"))
                    (vla-put-freeze layer :vlax-false)
                    (vla-put-layeron layer :vlax-true)
                )

                (   off
                    (if frozen
                        (if (@Match name)
                            (progn
                                (vla-put-layeron layer :vlax-true)
                                (vla-put-freeze layer :vlax-false)
                            )
                        )
                        (if (@Match name)
                            (vla-put-layeron layer :vlax-true)
                            (vla-put-freeze layer :vlax-true)
                        )
                    )
                )

                (   (if frozen
                        (if (@Match name)
                            (vla-put-freeze layer :vlax-false)
                            (vla-put-layeron layer :vlax-false)
                        )
                        (if (not (@Match name))
                            (progn
                                (vla-put-layeron layer :vlax-false)
                                (vla-put-freeze layer :vlax-true)
                            )
                        )
                    )
                )
            )
        )

        (princ)

    )

    (@Main
        (vla-get-activedocument (vlax-get-acad-object))
        (strcase filter)
        (strcase (getvar 'clayer))
    )

)

Cheers.
« Last Edit: November 11, 2014, 11:15:48 AM by MP »
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Freezing/Thawing Layers by Wildcard Match.
« Reply #9 on: November 10, 2014, 08:55:13 PM »

Thanks Michael,

I'll test it tonight, but from first glance that looks functionally equivalent to my ActiveX effort
... though mine is not as pretty :-)  .. and not as artistically documented

Regards,
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

BlackBox

  • King Gator
  • Posts: 3770
Re: Freezing/Thawing Layers by Wildcard Match.
« Reply #10 on: November 10, 2014, 11:52:30 PM »
Can you post a snippet / summary of your .NET code? Did you iterate via IEnumerable, or employ LINQ, etc?

Cheers
"How we think determines what we do, and what we do determines what we get."

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: Freezing/Thawing Layers by Wildcard Match.
« Reply #11 on: November 11, 2014, 01:49:48 AM »

Thanks Michael,

I'll test it tonight, but from first glance that looks functionally equivalent to my ActiveX effort
... though mine is not as pretty :-)  .. and not as artistically documented

Regards,

Most welcome Kerry. Revised it (blind). Thinking it should be faster but it might chuck a wobbly. :dunno:
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

roy_043

  • Water Moccasin
  • Posts: 1895
  • BricsCAD 18
Re: Freezing/Thawing Layers by Wildcard Match.
« Reply #12 on: November 11, 2014, 03:16:38 AM »
Using BricsCAD the difference between the command and the Visual Lisp solutions is much smaller. And using vle-start-transaction and vle-end-transaction the speed of the VL solution can even be improved.

Tested with the dwg and original code from Marc'Antonio Alessi and:
Code: [Select]
(defun ALE_LayerON_Transaction ( )
  (vle-start-transaction)
  (ALE_Layer_PutOnAll *AcAcDwg* *AcLayrs* '(("_ON" . "*$???_*")))
  (ALE_Layer_PutOnAll *AcAcDwg* *AcLayrs* '(("_Off" . "*$???_*")))
  (vle-end-transaction)
)

Code: [Select]
Benchmarking .......... elapsed milliseconds / relative timing <64 iterations>

  (CMDF_LAYERON) ................ 1484 / 1.32 <fastest>
  (ALE_LAYERON_TRANSACTION) ..... 1766 / 1.11
  (ALE_LAYERON) ................. 1953 / 1.00 <slowest>

Note: The first speed test will be unreliable because all layers are ON in the drawing.

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1453
  • Marco
Re: Freezing/Thawing Layers by Wildcard Match.
« Reply #13 on: November 11, 2014, 03:27:49 AM »
AutoCAD 2013, maybe this is not the right comparison:
Code: [Select]
(defun _ThawLayers_MP ( )
  (_ThawLayers '("*$???_*") nil)
  (_ThawLayers '("*$???_*") T)
)
Code: [Select]
Elapsed milliseconds / relative speed for 256 iteration(s):
    (CMDF_LAYERON)........1373 / 30.4 <fastest>
    (ALE_LAYERON)........20530 / 2.03
    (_THAWLAYERS_MP).....41746 / 1 <slowest>

stange behaviour on my All_layers.dwg:
 (_ThawLayers '("$???_*") t) => do not freeze "$FDT_NORM"

Now I try in Bricscad V15 with roy_43 suggestion...

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1453
  • Marco
Re: Freezing/Thawing Layers by Wildcard Match.
« Reply #14 on: November 11, 2014, 03:45:25 AM »
Code: [Select]
Bricscad V15 DWG with 3 layers:  0  $LAY_NORM   Defpoints   
Elapsed milliseconds / relative speed for 8192 iteration(s):
    (ALE_LAYERON).........1186 / 15.46 <fastest>
    (_THAWLAYERS_MP)......1809 / 10.13
    (CMDF_LAYERON).......18330 / 1 <slowest>
Code: [Select]
Bricscad V15 All_Layers.dwg:
Elapsed milliseconds / relative speed for 64 iteration(s):
    (ALE_LAYERON)....................1778 / 2.77 <fastest>
    (ALE_LAYERON_TRANSACTION)........2465 / 2
    (CMDF_LAYERON)...................3167 / 1.56
    (_THAWLAYERS_MP).................3307 / 1.49
    (_THAWLAYERS_MP_TRANSACTION).....4930 / 1 <slowest>
Code: [Select]
Bricscad V15 All_Layers.dwg 2° round:
Elapsed milliseconds / relative speed for 32 iteration(s):
    (ALE_LAYERON)....................1077 / 3.56 <fastest>
    (CMDF_LAYERON)...................1326 / 2.89
    (_THAWLAYERS_MP_TRANSACTION).....1747 / 2.2
    (ALE_LAYERON_TRANSACTION)........3089 / 1.24
    (_THAWLAYERS_MP).................3837 / 1 <slowest>

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: Freezing/Thawing Layers by Wildcard Match.
« Reply #15 on: November 11, 2014, 08:20:45 AM »
AutoCAD 2013, maybe this is not the right comparison:
Code: [Select]
(defun _ThawLayers_MP ( )
  (_ThawLayers '("*$???_*") nil)
  (_ThawLayers '("*$???_*") T)
)

Ummm not quite.

Code: [Select]
(defun _ThawLayers_MP ( )
    (_ThawLayers "*$???_*" nil)
)

Curious why you called the function twice.

I gave the insert_filters options because it's often difficult to achieve some filtering otherwise.

For example if you want to freeze the *gasket* and *weld* layers via a thaw function you might think a (wcmatch string "~*gasket*,~*weld*") filter might work, but it will not, where as (not (wcmatch string "*gasket*,*weld*")) will, ergo the invert_filters flag. Guess my in code comments weren't clear enough. Will try to revise latr if I find time.
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: Freezing/Thawing Layers by Wildcard Match.
« Reply #16 on: November 11, 2014, 08:36:12 AM »
stange behaviour on my All_layers.dwg:
 (_ThawLayers '("$???_*") t) => do not freeze "$FDT_NORM"

Shrug, works on my machine.

(_ThawLayers "$???_*") nil) >> Layers "0", "DEFPOINTS" and "$FDT_NORM" on & thawed, all others off & frozen.
(_ThawLayers "$???_*")   t) >> Layer "$FDT_NORM" off & frozen, all others on & thawed.

Works precisely as I intended.

Edit: That said, maybe my intention isn't clear. Apologies -- revising code comments to reflect.
« Last Edit: November 11, 2014, 08:59:35 AM by MP »
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: Freezing/Thawing Layers by Wildcard Match.
« Reply #17 on: November 11, 2014, 08:51:46 AM »
I changed mine. I had made it an option to pass a list of filters. I did this because I read:

I need to Freeze / thaw layers depending on  wildcard matches (multiple combinations of course).
There are about 40 wildcard matches that have to be satisfied.

But no one else appears to be using lists and I'm paying a performance hit for the option, so now it merely a single match string.

I did leave in the invert_filter flag as I think it's quite useful in real world use -- I often have to freeze only welds, gaskets etc.

Cheers.
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1453
  • Marco
Re: Freezing/Thawing Layers by Wildcard Match.
« Reply #18 on: November 11, 2014, 09:01:02 AM »
AutoCAD 2013, maybe this is not the right comparison:
Code: [Select]
(defun _ThawLayers_MP ( )
  (_ThawLayers '("*$???_*") nil)
  (_ThawLayers '("*$???_*") T)
)

Ummm not quite.

Code: [Select]
(defun _ThawLayers_MP ( )
    (_ThawLayers "*$???_*" nil)
)

Curious why you called the function twice.

I gave the insert_filters options because it's often difficult to achieve some filtering otherwise.

For example if you want to freeze the *gasket* and *weld* layers via a thaw function you might think a (wcmatch string "~*gasket*,~*weld*") filter might work, but it will not, where as (not (wcmatch string "*gasket*,*weld*")) will, ergo the invert_filters flag. Guess my in code comments weren't clear enough. Will try to revise latr if I find time.
Can you write an equivalent of:
(defun cmdf_LayerON ( )
 (vl-cmdf "_.layer" "_Off" "*$???_*" "_Y" "")
 (vl-cmdf "_.layer" "_ON" "*$???_*" "")
)
to test?

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1453
  • Marco
Re: Freezing/Thawing Layers by Wildcard Match.
« Reply #19 on: November 11, 2014, 09:05:50 AM »
I changed mine. I had made it an option to pass a list of filters. I did this because I read:

I need to Freeze / thaw layers depending on  wildcard matches (multiple combinations of course).
There are about 40 wildcard matches that have to be satisfied.

But no one else appears to be using lists and I'm paying a performance hit for the option, so now it merely a single match string.

I did leave in the invert_filter flag as I think it's quite useful in real world use -- I often have to freeze only welds, gaskets etc.

Cheers.
OK, now I understand, my function "OFF" also the current layer, while yours not freeze the layer if it is current.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Freezing/Thawing Layers by Wildcard Match.
« Reply #20 on: November 11, 2014, 09:55:39 AM »
Just a quick note ... I'm late for bed.

Thanks for the input so far.


@Blackbox, you asked for the C# ... see attached. It's a little rough.

This test is pretty self explanatory.
and uses  Marc'Antonio's drawing.
Code - Auto/Visual Lisp: [Select]
  1.  
  2. (setvar 'CMDecho 0 )
  3. (BenchMark2 50 '(
  4.                 (command-s "_.layer" "_S" "0" "_t" "*" "")
  5.                 (command-s "_.layer" "_T" "$XP*,*HIDDEN*,$SDT*" "")
  6.                 (command-s "_.layer" "_t" "$WST*,$$DT*" "")
  7.                 (command-s "_.layer" "_t" "*" "")
  8. ;; C# called from Lisp
  9.                 (MyLispFunction (list "$XP*" "*HIDDEN*" "$SDT*"))
  10.                 (MyLispFunction (list "$WST*" "$$DT*"))                  
  11.   )
  12. )
  13. (setvar 'CMDecho 1 )
  14.  

Results :
Benchmarking-V2 :B=50 [M.P.2005 <revised kdub 2005,2014>] ...........
Elapsed milliseconds for 256 iteration(s)/ relative Timing :

    (mylispfunction (LIST "$XP*" "*HIDDE...).....3338 / 30.6239 <slowest>
    (mylispfunction (LIST "$WST*" "$$DT*"))......2246 / 20.6055
    (COMMAND-S "_.layer" "_S" "0" "_t" "...)......249 / 2.2844
    (COMMAND-S "_.layer" "_t" "*" "").............203 / 1.8624
    (COMMAND-S "_.layer" "_T" "$XP*,*HID...)......187 / 1.7156
    (COMMAND-S "_.layer" "_t" "$WST*,$$D...)......109 / 1.0000 <fastest> : 0.425781250000ms Per iteration



Code - C#: [Select]
  1. // (C) Copyright 2014 by  kdub Services
  2. //
  3.  
  4. using System;
  5. using System.Collections;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using System.Linq.Expressions;
  9. using System.Windows.Documents;
  10. using Autodesk.AutoCAD.Runtime;
  11. using Autodesk.AutoCAD.ApplicationServices;
  12. using Autodesk.AutoCAD.DatabaseServices;
  13. using Autodesk.AutoCAD.Geometry;
  14. using Autodesk.AutoCAD.EditorInput;
  15. //
  16. using Application = Autodesk.AutoCAD.ApplicationServices.Core.Application;
  17. using Exception = System.Exception;
  18.  
  19. [assembly: CommandClass(typeof (LayerToggle_03.LayerToggle))]
  20.  
  21. namespace LayerToggle_03
  22. {
  23.     public class LayerToggle
  24.     {
  25.         [CommandMethod("MyGroup", "MyCommand", "MyCommandLocal",
  26.             CommandFlags.Modal)]
  27.         public static void LayerToggle3()
  28.         {
  29.             Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
  30.             Database db = Application.DocumentManager.MdiActiveDocument.Database;
  31.  
  32.             ed.WriteMessage("Hello, this Hal.");
  33.  
  34.             List<string> patternList = new List<string>();
  35.             patternList.Add("STH*");
  36.             patternList.Add("??5*");
  37.  
  38.             ThawtoMatchPattern(db, patternList);
  39.         }
  40.  
  41.         public static void ThawtoMatchPattern(
  42.             Database db,
  43.             List<string> patternList)
  44.         {
  45.             Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
  46.  
  47.             using (Transaction tr = db.TransactionManager.StartTransaction())
  48.             {
  49.                 LayerTable lt =
  50.                     (LayerTable) tr.GetObject(db.LayerTableId, OpenMode.ForRead);
  51.                 var currentLayeriD = db.Clayer;
  52.  
  53.                 foreach (ObjectId obID in lt)
  54.                 {
  55.                     LayerTableRecord ltr =
  56.                         (LayerTableRecord) tr.GetObject(obID, OpenMode.ForWrite);
  57.                     foreach (var pattern in patternList)
  58.                     {
  59.                         var layerName = ltr.Name;
  60.                         if (layerName.Matches(pattern)
  61.                             &&
  62.                             ltr.IsFrozen)
  63.                         {
  64.                             ltr.IsOff = false;
  65.                             ltr.IsFrozen = false;
  66.                             //break;
  67.                             continue;
  68.                         }
  69.                         else
  70.                         {
  71.                             if (!obID.Equals(currentLayeriD)
  72.                                 ||
  73.                                 layerName != "Defpoints")
  74.                             {
  75.                                 ltr.IsFrozen = true;
  76.                             }
  77.                         }
  78.                     }
  79.                 }
  80.                 tr.Commit();
  81.             }
  82.         }
  83.  
  84.         [LispFunction("MyLispFunction", "MyLispFunctionLocal")]
  85.         public int MyLispFunction(ResultBuffer pArgs)
  86.         {
  87.             Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
  88.             Database db = Application.DocumentManager.MdiActiveDocument.Database;
  89.             try
  90.             {
  91.                 List<string> patternList = (from TypedValue typedValue in pArgs
  92.                                             where
  93.                                                 (LispDataType)
  94.                                                 typedValue.TypeCode ==
  95.                                                 LispDataType.Text
  96.                                             select typedValue.Value.ToString())
  97.                     .ToList();
  98.  
  99.                 ThawtoMatchPattern(db, patternList);
  100.                 ed.Regen();
  101.             }
  102.             catch (System.Exception ex)
  103.             {
  104.                 ed.WriteMessage(
  105.                                 "\nError: {0}\nStackTrace: {1}",
  106.                                 ex.Message,
  107.                                 ex.StackTrace);
  108.             }
  109.             return 1;
  110.         }
  111.     }
  112. }
  113.  

Code - C#: [Select]
  1.  
  2. namespace LayerToggle_03
  3. {
  4.     public static class WcMatchExtensionMethods
  5.     {
  6.         // Thanks to Tony T
  7.         //
  8.         public static bool Matches(
  9.             this string str,
  10.             string pattern,
  11.             bool ignoreCase)
  12.         {
  13.             return Autodesk.AutoCAD.Internal.Utils.WcMatchEx(str, pattern, ignoreCase);
  14.         }
  15.  
  16.         public static bool Matches(this string str, string pattern)
  17.         {
  18.             return Autodesk.AutoCAD.Internal.Utils.WcMatchEx(str, pattern, true);
  19.         }
  20.     }
  21. }
  22.  


modification required to code:
original post changed.
Changed break to continue

Code - C#: [Select]
  1.  
  2.                         if (layerName.Matches(pattern)
  3.                             &&
  4.                             ltr.IsFrozen)
  5.                         {
  6.                             ltr.IsOff = false;
  7.                             ltr.IsFrozen = false;
  8.                             // break;
  9.                             continue;
  10.                         }
  11.                         else
  12.                         {
  13.                             if (!obID.Equals(currentLayeriD)
  14.                                 ||
  15.                                 layerName != "Defpoints")
  16.                             {
  17.                                 ltr.IsFrozen = true;
  18.                             }
  19.                         }
  20.  
  21.  
« Last Edit: November 11, 2014, 08:56:26 PM by Kerry »
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Freezing/Thawing Layers by Wildcard Match.
« Reply #21 on: November 11, 2014, 08:50:27 PM »
Regarding the parameters :
I'm not fussed if it's a list of strings or a comma separated string.

My parameters are built from dialog input to define the "Layer Groups" status.
... mainly to target Layers in Xrefs .. which can get fairly complicated.

//--------------------------

Roy,
That's an interesting test result for BricsCAD.
I'll fire up and have a play in V15 when I beat this one to death.

It leads me to wonder about the relative actual speed comparison between drawing engines.

//--------------------------

Just an aside:
I was originally going to do this app in C#.
My prototyping and proof of concept work was done in VisualLisp.
When I noticed how slow the C# process was due to the restrictions caused by Wildcard requirement I went back to VisualLisp and OpenDCL.

Thankfully, I have a couple of toolboxes and a client who just wants it to work, and be reasonably fast.

Thanks for the input.
Regards,


kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

MickD

  • King Gator
  • Posts: 3637
  • (x-in)->[process]->(y-out) ... simples!
Re: Freezing/Thawing Layers by Wildcard Match.
« Reply #22 on: November 11, 2014, 10:15:11 PM »

Roy,
That's an interesting test result for BricsCAD.
I'll fire up and have a play in V15 when I beat this one to death.

It leads me to wonder about the relative actual speed comparison between drawing engines.

Hi Kerry,
just watching from afar when I noticed this comment.
As I understand it the guys at Bricscad have highly optimised their lisp engine and as lisp is required to work on Linux versions they have even gone as far as bypassing the COM calls (vl- ??) straight to native which may explain the smaller differences in speed.

cheers.
"Programming is really just the mundane aspect of expressing a solution to a problem."
- John Carmack

"Short cuts make long delays,' argued Pippin.”
- J.R.R. Tolkien

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Freezing/Thawing Layers by Wildcard Match.
« Reply #23 on: November 11, 2014, 10:28:12 PM »

Roy,
That's an interesting test result for BricsCAD.
I'll fire up and have a play in V15 when I beat this one to death.

It leads me to wonder about the relative actual speed comparison between drawing engines.

Hi Kerry,
just watching from afar when I noticed this comment.
As I understand it the guys at Bricscad have highly optimised their lisp engine and as lisp is required to work on Linux versions they have even gone as far as bypassing the COM calls (vl- ??) straight to native which may explain the smaller differences in speed.

cheers.

Hi Mick,
Yes, I believe your optimisation comment is correct. It's developing into a very neat application.

I think Torsten Moses has a lot to be proud of.

An in-app programming editor and debugger would be really nice though :-)
« Last Edit: November 11, 2014, 11:00:54 PM by Kerry »
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

danallen

  • Guest
Re: Freezing/Thawing Layers by Wildcard Match.
« Reply #24 on: November 11, 2014, 10:38:48 PM »
An in-pp programming editor and debugger would be really nice though :-)

+1

roy_043

  • Water Moccasin
  • Posts: 1895
  • BricsCAD 18
Re: Freezing/Thawing Layers by Wildcard Match.
« Reply #25 on: November 12, 2014, 03:42:18 AM »
Roy,
That's an interesting test result for BricsCAD.
I'll fire up and have a play in V15 when I beat this one to death.
My testing was done with BricsCAD V14. Comparing V14 and V15 seems to show two things. First: (in this situation?) V15 Lisp execution is noticeably slower. Second: using a transaction (vle-start-transaction and vle-end-transaction) probably does not have any speed benefits. This is contrary to my earlier statement.
Note: for this test I have modified Marc'Antonio's *ON functions. They did not all have the off-on order.

Code: [Select]
V15
Benchmarking .......... elapsed milliseconds / relative timing <64 iterations>
   (ALE_LayerOFFON_NOCATCH) ................. 2579 / 1.62 <fastest>
   (ALE_LayerOFFON) ......................... 2812 / 1.48
   (cmdf_LayerOFFON) ........................ 3031 / 1.38
   (ALE_LayerOFFON_TRANSACTION_NOCATCH) ..... 4079 / 1.02
   (ALE_LayerOFFON_TRANSACTION) ............. 4171 / 1.00 <slowest>

 
V14
Benchmarking .......... elapsed milliseconds / relative timing <64 iterations>
   (cmdf_LayerOFFON) ........................ 1313 / 1.54 <fastest>
   (ALE_LayerOFFON_NOCATCH) ................. 1781 / 1.13
   (ALE_LayerOFFON_TRANSACTION_NOCATCH) ..... 1906 / 1.06
   (ALE_LayerOFFON) ......................... 1984 / 1.02
   (ALE_LayerOFFON_TRANSACTION) ............. 2016 / 1.00 <slowest>

Code: [Select]
(or *AcadApp* (setq *AcadApp* (vlax-get-Acad-Object)            ))
(or *AcAcDwg* (setq *AcAcDwg* (vla-get-ActiveDocument *AcadApp*)))
(or *AcLayrs* (setq *AcLayrs* (vla-get-Layers         *AcAcDwg*)))

(defun cmdf_LayerOFFON ( )
  (setvar 'cmdecho 0) ; Slightly faster.
  (vl-cmdf "_.layer" "_Off" "*$???_*" "_Y" "")
  (vl-cmdf "_.layer" "_ON" "*$???_*" "")
  (setvar 'cmdecho 1)
)

(defun ALE_LayerOFFON ( )
  (ALE_Layer_PutOnAll *AcAcDwg* *AcLayrs* '(("_Off" . "*$???_*")))
  (ALE_Layer_PutOnAll *AcAcDwg* *AcLayrs* '(("_ON" . "*$???_*")))
)

(defun ALE_LayerOFFON_NoCatch ( )
  (ALE_Layer_PutOnAll_NoCatch *AcAcDwg* *AcLayrs* '(("_Off" . "*$???_*")))
  (ALE_Layer_PutOnAll_NoCatch *AcAcDwg* *AcLayrs* '(("_ON" . "*$???_*")))
)

(defun ALE_LayerOFFON_Transaction ( )
  (vle-start-transaction)
  (ALE_Layer_PutOnAll *AcAcDwg* *AcLayrs* '(("_Off" . "*$???_*")))
  (ALE_Layer_PutOnAll *AcAcDwg* *AcLayrs* '(("_ON" . "*$???_*")))
  (vle-end-transaction)
)

(defun ALE_LayerOFFON_Transaction_NoCatch ( )
  (vle-start-transaction)
  (ALE_Layer_PutOnAll_NoCatch *AcAcDwg* *AcLayrs* '(("_Off" . "*$???_*")))
  (ALE_Layer_PutOnAll_NoCatch *AcAcDwg* *AcLayrs* '(("_ON" . "*$???_*")))
  (vle-end-transaction)
)

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Freezing/Thawing Layers by Wildcard Match.
« Reply #26 on: November 14, 2014, 12:00:54 AM »
These were my Command and ActiveX tests.

Code - Auto/Visual Lisp: [Select]
  1. (defun LayerOnAndThawTest-ax (/ thawList LayerZero layName)
  2.   (setq thawList '("$XP*,*HIDDEN*" "$SDT*" "Defpoints"))
  3.  
  4.   ;; Ensure Layer '0 is ON, THAWED and ACTIVE (CurrentLayer)
  5.   (setq LayerZero (vla-item *aclayers "0"))
  6.   (vla-put-layeron LayerZero :vlax-true)
  7.   (if (= :vlax-true (vla-get-freeze LayerZero))
  8.     (vla-put-freeze LayerZero :vlax-false)
  9.   )
  10.   (vla-put-activelayer *acdoc LayerZero)
  11.  
  12.   ;; Turn OFF and FREEZE layers except those nominated
  13.   (vlax-for layObj *aclayers
  14.     (setq layName (vla-get-name layObj))
  15.     (if (not (equal LayerZero layObj))
  16.       (if (vl-some '(lambda (pattern) (wcmatch layName pattern)) thawList)
  17.         (progn (vla-put-layeron layObj :vlax-true)
  18.                (vla-put-freeze layObj :vlax-false)
  19.         )
  20.         (progn (vla-put-layeron layObj :vlax-false)
  21.                (vla-put-freeze layObj :vlax-true)
  22.         )
  23.       )
  24.     )
  25.   )
  26.   (princ)
  27. )
  28.  

Code - Auto/Visual Lisp: [Select]
  1. (defun LayerOnAndThawTest-cmd (/ thawList)
  2.   (setq thawList (_listitems->string
  3.                    '("$XP*,*HIDDEN*" "$SDT*" "Defpoints")
  4.                    ","
  5.                  )
  6.   )
  7.  
  8.   (command-s "_.layer" "_On" "0" "_t" "0" "_S" "0" "Freeze" "*" "")
  9.   (command-s "_.layer" "_On" thawList "_T" thawList "")
  10.   (princ)
  11. )
  12.  
  13. ;;//---------------------------------------  
  14. (defun _listitems->string (lst delimiter / result)
  15.   (setq result (car lst))
  16.   (foreach item (cdr lst) (setq result (strcat result delimiter item)))
  17.   result
  18. )
  19.  

Code - Auto/Visual Lisp: [Select]
  1.       *aclayers (vla-get-layers *acdoc)
  2. )
  3. (setvar 'REGENMODE 1)
  4. (command-s "_.layer" "_On" "*" "_t" "*" "_S" "0" "")
  5.  
  6. (setvar 'CMDecho 0)
  7. (BenchMark2
  8.   200
  9.   '((LayerOnAndThawTest-cmd)
  10.     (LayerOnAndThawTest-ax)  
  11.    )
  12. )
  13. (setvar 'CMDecho 1)
  14.  

Benchmarking-V2 :B=200 [M.P.2005 <revised kdub 2005,2014>] ........
Elapsed milliseconds for 32 iteration(s)/ relative Timing :

    (LAYERONANDTHAWTEST-AX)......1669 / 5.9395 <slowest>: 52.15625ms Per iteration
    (LAYERONANDTHAWTEST-CMD)......281 / 1 <fastest>: 8.78125ms Per iteration
« Last Edit: November 14, 2014, 12:06:48 AM by Kerry »
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1453
  • Marco
Re: Freezing/Thawing Layers by Wildcard Match.
« Reply #27 on: November 14, 2014, 06:12:02 AM »
@Kerry: I have modified the function to do exactly what do yours (AutoCAD 2013):
Code: [Select]
(defun ALE_Layer_OnAndThawTest ( / ChgLst LyrNam BitVal)
  (setq
    ChgLst '(("_OFF" . "~0") ("_ON" . "$XP*,*HIDDEN*") ("_ON" . "$SDT*")("_ON" . "Defpoints"))
  )
  (vlax-map-Collection
    *aclayers
   '(lambda (LmbDat)
      (setq
        BitVal nil
        LyrNam (strcase (vla-get-Name LmbDat))
      )
      (foreach ForElm ChgLst
        (if (wcmatch LyrNam (cdr ForElm))
          (if (= (car ForElm) "_ON") (setq BitVal 1) (setq BitVal 0))
        )
      )
      (and
        BitVal
        (if (zerop BitVal)
          (progn
            (vl-catch-all-apply 'vla-put-LayerOn (list LmbDat :vlax-false))
            (vl-catch-all-apply 'vla-put-freeze (list LmbDat :vlax-true))
          )
          (progn
            (vl-catch-all-apply 'vla-put-LayerOn (list LmbDat :vlax-true ))
            (vl-catch-all-apply 'vla-put-freeze (list LmbDat :vlax-false))
          )
        )
      )
    )
  )
  (vla-put-activelayer *acdoc (vla-item *aclayers "0"))
)
Code: [Select]
    (LAYERONANDTHAWTEST-CMD)......1809 / 2.94 <fastest>
    (ALE_LAYER_ONANDTHAWTEST).....4821 / 1.1
    (LAYERONANDTHAWTEST-AX).......5319 / 1 <slowest>

Elapsed milliseconds / relative speed for 32 iteration(s):
    (ALE_LAYER_ONANDTHAWTEST).....1310 / 2.08 <fastest>
    (LAYERONANDTHAWTEST-AX).......1997 / 1.37
    (ALE_LAYER_ONANDTHAWTEST).....2059 / 1.33
    (ALE_LAYER_ONANDTHAWTEST).....2246 / 1.22
    (LAYERONANDTHAWTEST-AX).......2683 / 1.02
    (LAYERONANDTHAWTEST-AX).......2730 / 1 <slowest>
Is is very similar, maybe Vlax-map-Collection is a little bit faster...

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Freezing/Thawing Layers by Wildcard Match.
« Reply #28 on: November 14, 2014, 07:21:27 AM »
Marc'Antonio,

Quote
Is is very similar, maybe Vlax-map-Collection is a little bit faster...

Yes, that may be the case ... worth investigating further.

Also, because you are using the equivalent of :
Code - Auto/Visual Lisp: [Select]
  1. (setq layObj  (vla-item *aclayers "0") )
  2. (vl-catch-all-apply 'vla-put-freeze (list LayObj :vlax-true))
you don't need to test each layer name against the current layer during the iteration of the layer collection
... That may be saving a few ticks. I'll investigate that further when I can find the time.

This is a nice touch.
Code - Auto/Visual Lisp: [Select]
  1. (wcmatch layName "~0")

Thanks for the input.



kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Freezing/Thawing Layers by Wildcard Match.
« Reply #29 on: November 14, 2014, 07:37:54 AM »
Regarding  vlax-map-collection
 

Code - Auto/Visual Lisp: [Select]
  1. (BenchMark2
  2.   500
  3.      *aclayers
  4.      '(lambda (LayerObj)
  5.         (vl-catch-all-apply 'vla-put-freeze (list LayerObj :vlax-true))
  6.         (vl-catch-all-apply 'vla-put-freeze (list LayerObj :vlax-false))
  7.       )
  8.     )
  9.     (vlax-for
  10.      LayerObj
  11.      *aclayers
  12.      (vl-catch-all-apply 'vla-put-freeze (list LayerObj :vlax-true))
  13.      (vl-catch-all-apply 'vla-put-freeze (list LayerObj :vlax-false))
  14.     )
  15.    )
  16. )

It's pretty close :-)

_$
Benchmarking-V2 :B=500 [M.P.2005 <revised kdub 2005,2014>] ........
Elapsed milliseconds for 32 iteration(s)/ relative Timing :

    (VLAX-FOR LAYEROBJ *ACLAYERS (VL-CAT...).....890 / 1.0011 <slowest>: 27.8125ms Per iteration
    (vlax-map-Collection *ACLAYERS (QUOT...).....889 / 1 <fastest>: 27.78125ms Per iteration

 
_$
Benchmarking-V2 :B=500 [M.P.2005 <revised kdub 2005,2014>] ........
Elapsed milliseconds for 32 iteration(s)/ relative Timing :

    (VLAX-FOR LAYEROBJ *ACLAYERS (VL-CAT...).....873 / 1.0175 <slowest>: 27.28125ms Per iteration
    (vlax-map-Collection *ACLAYERS (QUOT...).....858 / 1 <fastest>: 26.8125ms Per iteration

 
_$
Benchmarking-V2 :B=500 [M.P.2005 <revised kdub 2005,2014>] ........
Elapsed milliseconds for 32 iteration(s)/ relative Timing :

    (vlax-map-Collection *ACLAYERS (QUOT...).....874 / 1 <slowest>: 27.3125ms Per iteration
    (VLAX-FOR LAYEROBJ *ACLAYERS (VL-CAT...).....874 / 1 <fastest>: 27.3125ms Per iteration

 
_$
Benchmarking-V2 :B=500 [M.P.2005 <revised kdub 2005,2014>] ........
Elapsed milliseconds for 32 iteration(s)/ relative Timing :

    (vlax-map-Collection *ACLAYERS (QUOT...).....889 / 1 <slowest>: 27.78125ms Per iteration
    (VLAX-FOR LAYEROBJ *ACLAYERS (VL-CAT...).....889 / 1 <fastest>: 27.78125ms Per iteration

 
_$
Benchmarking-V2 :B=500 [M.P.2005 <revised kdub 2005,2014>] ........
Elapsed milliseconds for 32 iteration(s)/ relative Timing :

    (vlax-map-Collection *ACLAYERS (QUOT...).....905 / 1.018 <slowest>: 28.28125ms Per iteration
    (VLAX-FOR LAYEROBJ *ACLAYERS (VL-CAT...).....889 / 1 <fastest>: 27.78125ms Per iteration
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1453
  • Marco
Re: Freezing/Thawing Layers by Wildcard Match.
« Reply #30 on: November 14, 2014, 08:00:59 AM »
A little bit better:
Code: [Select]
(defun ALE_Layer_OnAndThawTest2 ( / ChgLst LyrNam BitVal)
  (setq ChgLst '((0 . "~0") (1 . "$XP*,*HIDDEN*,$SDT*,Defpoints")))
  (vlax-map-Collection
    *aclayers
   '(lambda (LmbDat)
      (setq BitVal nil   LyrNam (strcase (vla-get-Name LmbDat)))
      (foreach ForElm ChgLst
        (if (wcmatch LyrNam (cdr ForElm))
          (if (zerop (car ForElm)) (setq BitVal 0) (setq BitVal 1))
        )
      )
      (and
        BitVal
        (if (zerop BitVal)
          (progn
            (vl-catch-all-apply 'vla-put-LayerOn (list LmbDat :vlax-false))
            (vl-catch-all-apply 'vla-put-freeze (list LmbDat :vlax-true))
          )
          (progn
            (vl-catch-all-apply 'vla-put-LayerOn (list LmbDat :vlax-true ))
            (vl-catch-all-apply 'vla-put-freeze (list LmbDat :vlax-false))
          )
        )
      )
    )
  )
  (vla-put-activelayer *acdoc (vla-item *aclayers "0"))
)
Code: [Select]
Elapsed milliseconds / relative speed for 32 iteration(s):
    (ALE_LAYER_ONANDTHAWTEST2).....1872 / 2.72 <fastest>
    (LAYERONANDTHAWTEST-AX)........2386 / 2.14
    (ALE_LAYER_ONANDTHAWTEST2).....3057 / 1.67
    (ALE_LAYER_ONANDTHAWTEST2).....3338 / 1.53
    (LAYERONANDTHAWTEST-AX)........4898 / 1.04
    (LAYERONANDTHAWTEST-AX)........5101 / 1 <slowest>

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1453
  • Marco
Re: Freezing/Thawing Layers by Wildcard Match.
« Reply #31 on: November 14, 2014, 08:14:08 AM »
Code: [Select]
; Now I scraped the barrel (my apologies for transl.)
(defun ALE_Layer_OnAndThawTest3 ( / ChgLst LyrNam BitVal)
  (setq ChgLst '((0 . "~0") (1 . "$XP*,*HIDDEN*,$SDT*,Defpoints")))
  (vlax-map-Collection
    *aclayers
   '(lambda (LmbDat)
      (setq BitVal nil   LyrNam (strcase (vla-get-Name LmbDat)))
      (foreach ForElm ChgLst
        (if (wcmatch LyrNam (cdr ForElm))
           (and (wcmatch LyrNam (cdr ForElm)) (setq BitVal (car ForElm)))
        )
      )
      (and
        BitVal
        (if (zerop BitVal)
          (progn
            (or
              (= (vla-get-LayerOn LmbDat) ':vlax-false)
              (vl-catch-all-apply 'vla-put-LayerOn (list LmbDat :vlax-false))
            )
            (or
              (= (vla-get-Freeze LmbDat) ':vlax-true)
              (vl-catch-all-apply 'vla-put-freeze (list LmbDat :vlax-true))
            )
          )
          (progn
            (or
              (= (vla-get-LayerOn LmbDat) ':vlax-true)
              (vl-catch-all-apply 'vla-put-LayerOn (list LmbDat :vlax-true))
            )
            (or
              (= (vla-get-Freeze LmbDat) ':vlax-false)
              (vl-catch-all-apply 'vla-put-Freeze (list LmbDat :vlax-false))
            )
          )
        )
      )
    )
  )
  (vla-put-activelayer *acdoc (vla-item *aclayers "0"))
)
Code: [Select]
Elapsed milliseconds / relative speed for 64 iteration(s):
    (ALE_LAYER_ONANDTHAWTEST3).....1544 / 6.33 <fastest>
    (ALE_LAYER_ONANDTHAWTEST3).....1716 / 5.69
    (ALE_LAYER_ONANDTHAWTEST3).....4024 / 2.43
    (LAYERONANDTHAWTEST-AX)........5850 / 1.67
    (LAYERONANDTHAWTEST-AX)........6490 / 1.5
    (LAYERONANDTHAWTEST-AX)........9766 / 1 <slowest>

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Freezing/Thawing Layers by Wildcard Match.
« Reply #32 on: November 14, 2014, 08:24:48 AM »
How about just :

Code - Auto/Visual Lisp: [Select]
  1. (setq BitVal  (car ForElm) )
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1453
  • Marco
Re: Freezing/Thawing Layers by Wildcard Match.
« Reply #33 on: November 14, 2014, 08:36:49 AM »
How about just :

Code - Auto/Visual Lisp: [Select]
  1. (setq BitVal  (car ForElm) )
Yes...  8) old age puts you sunglasses... updated.

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1453
  • Marco
Re: Freezing/Thawing Layers by Wildcard Match.
« Reply #34 on: November 14, 2014, 09:00:39 AM »
I think that to make a good test is necessary to start from the worse condition for all functions:
Code: [Select]
(defun LayerOnAndThawTest-ax (/ thawList LayerZero layName)
     (setq thawList '("$XP*,*HIDDEN*" "$SDT*" "Defpoints"))
     (command-s "_.layer" "_On" "*" "_t" "*" "")
...
)

(defun ALE_Layer_OnAndThawTest(n) ( / ChgLst LyrNam BitVal)
  (setq ChgLst '((0 . "~0") (1 . "$XP*,*HIDDEN*,$SDT*,Defpoints")))
  (command-s "_.layer" "_On" "*" "_t" "*" "")
...
)

(defun LayerOnAndThawTest-cmd (/ thawList)
     (setq thawList (_listitems->string
                      '("$XP*,*HIDDEN*" "$SDT*" "Defpoints")
                      ","
                    )
     )
     (command-s "_.layer" "_On" "*" "_t" "*" "_S" "0" "_Freeze" "*" "_On" thawList "_T" thawList "")
     (princ)
)

Code: [Select]
    (LAYERONANDTHAWTEST-CMD).......1060 / 4.22 <fastest>
    (ALE_LAYER_ONANDTHAWTEST)......1935 / 2.31
    (ALE_LAYER_ONANDTHAWTEST3).....3432 / 1.3
    (LAYERONANDTHAWTEST-AX)........4477 / 1 <slowest>



Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Freezing/Thawing Layers by Wildcard Match.
« Reply #35 on: November 14, 2014, 10:26:49 AM »
< .. >
Yes...  8) old age puts you sunglasses... updated.

I survived the 60's ... so everything is rose tinted for me .
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1453
  • Marco
Re: Freezing/Thawing Layers by Wildcard Match.
« Reply #36 on: November 14, 2014, 10:38:59 AM »
< .. >
Yes...  8) old age puts you sunglasses... updated.

I survived the 60's ... so everything is rose tinted for me .
I am very very close ... :( ............ ;D

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Freezing/Thawing Layers by Wildcard Match.
« Reply #37 on: November 14, 2014, 08:23:59 PM »
Regarding the use of (vl-catch-all-apply in place of testing if the indexed layer is Active :

Code - Auto/Visual Lisp: [Select]
  1. (or *:acapp          (setq *:acapp (vlax-get-acad-object)))
  2. (or *:activedoc    (setq *:activedoc (vla-get-activedocument *:acapp)))
  3. (or *:layers          (setq *:layers (vla-get-layers *:activedoc)))
  4.  
  5. (defun _LayerTest32 ()
  6.   (vlax-for LayerObj *:layers
  7.     (vl-catch-all-apply 'vla-put-freeze (list LayerObj :vlax-true))
  8.     (vl-catch-all-apply 'vla-put-freeze (list LayerObj :vlax-false))
  9.   )
  10. )
  11. (defun _LayerTest33 (/ activeLayer)
  12.   (setq activeLayer (vla-get-activelayer *:activedoc))
  13.   (vlax-for LayerObj *:layers
  14.     (if (not (equal activeLayer LayerObj))
  15.       (vla-put-freeze LayerObj :vlax-true)
  16.     )
  17.     (if (not (equal activeLayer LayerObj))
  18.       (vla-put-freeze LayerObj :vlax-false)
  19.     )
  20.   )
  21. )
  22.  
  23. (BenchMark2
  24.   800  '(
  25.     (_LayerTest32)
  26.     (_LayerTest33)
  27.    )
  28. )
  29.  
  30.  

Benchmarking-V2 :B=800 [M.P.2005 <revised kdub 2005,2014>] ........
Elapsed milliseconds for 32 iteration(s)/ relative Timing :

    (_LAYERTEST32).....936 / 1.1527 <slowest>: 29.25ms Per iteration
    (_LAYERTEST33).....812 / 1 <fastest>: 25.375ms Per iteration

 
Benchmarking-V2 :B=800 [M.P.2005 <revised kdub 2005,2014>] ........
Elapsed milliseconds for 32 iteration(s)/ relative Timing :

    (_LAYERTEST32).....921 / 1.1342 <slowest>: 28.78125ms Per iteration
    (_LAYERTEST33).....812 / 1 <fastest>: 25.375ms Per iteration


I suppose 4 ms which represents about a 15% efficiency difference should be considered.
... and the code is more succinct.

But sometimes spending an hour or two to save 4 ms is questionable
 ... as depicted here:

Translated:-
If you can save 5 minutes daily the break even development time is 6 days.
« Last Edit: November 14, 2014, 08:46:30 PM by Kerry »
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Freezing/Thawing Layers by Wildcard Match.
« Reply #38 on: November 14, 2014, 08:58:09 PM »
In answer to a PM which asked 'why I want to test before freezing or thawing the active Layer'


Code - Auto/Visual Lisp: [Select]
  1.  
  2. (or *:acapp     (setq *:acapp (vlax-get-acad-object)))
  3. (or *:activedoc (setq *:activedoc (vla-get-activedocument *:acapp)))
  4. (or *:layers    (setq *:layers (vla-get-layers *:activedoc)))
  5.  
  6. (setq activeLayer (vla-get-activelayer *:activedoc))
  7.  
  8.       ;;=> error: Automation Error. Invalid layer
  9. (vla-put-freeze activeLayer :vlax-true)        
  10.  
  11.       ;;=> error: Automation Error. Invalid layer
  12. (vla-put-freeze activeLayer :vlax-false)  
  13.  
  14.       ;;=> returns #<%catch-all-apply-error%> locally, BUT does not crash
  15. (vl-catch-all-apply 'vla-put-freeze (list activeLayer :vlax-true))
  16.                                                            
  17.  
  18.  
  19.  
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.