Author Topic: How to freeze a list of layers?  (Read 6180 times)

0 Members and 1 Guest are viewing this topic.

Andrea

  • Water Moccasin
  • Posts: 2372
Re: How to freeze a list of layers?
« Reply #15 on: September 30, 2008, 02:11:30 PM »
you'r right Se7en....
your code is faster...

Tested for 100 layers.

thanks.


this code below will regen eachtime the drawing if the layer is already frozen.
do wee need to put some check before running the entmod ?


Code: [Select]
;;; begin timing functions by SMadsen
(defun startTimer ()
  (setq time (getvar "DATE"))
  )


(defun endTimer (func)
  (setq time    (- (getvar "DATE") time)
        seconds (* 86400.0 (- time (fix time)))
        )
  (gc)
  (outPut seconds func)
  )

(defun outPut (secs def)
  (gc)
  (princ (strcat "\nTimed " def ": " (rtos secs 2 6)))
  (princ)
  )




(defun c:freeze1 ()
  (startTimer)
(setq Laylst nil)
(setq v 1)
(repeat 100
  (setq LayLst (append LayLst (list (strcat "CALQUE" (itoa v)))))
  (setq v (1+ v))
)

(mapcar '(lambda (x)  
(setq LayEnt (entget (tblobjname "LAYER" x)))
(entmod (subst (cons 70 (boole 6 (cdr (assoc 70 LayEnt)) 1))
     (assoc 70 LayEnt)
     LayEnt)))
     LayLst)

 (endTimer "c:freeze1");;0.016013
 )


(defun c:freeze2 ()
  (startTimer)
(setq Laylst nil)
(setq v 1)
(repeat 100
  (setq LayLst (append LayLst (list (strcat "CALQUE" (itoa v)))))
  (setq v (1+ v))
)

  (foreach l LayLst
  (if (member (strcase l) (mapcar 'strcase (ai_table "LAYER" 4)))     
    (vl-cmdf "._-Layer" "_F" l "")
   )
)

 (endTimer "c:freeze2");;1.124999
 )
Keep smile...

JohnK

  • Administrator
  • Seagull
  • Posts: 10657
Re: How to freeze a list of layers?
« Reply #16 on: October 01, 2008, 05:01:33 PM »
Not quite sure I understand what you mean by ``check entmod'' but you can check each layers group code 70 with a simple logand or meybe even use (boole 7...). It kinda depends on what you want and what you want to check for.

For the new users to Autolisp (Things you should take from this topic):

Dont concern yourself with the speed of entmod/make vs VL functions.

If your not comfortable with using entmod/make or the VL functions you can still have a safe, robust, and good application using command. Command is perfectly fine for most applications and uses, you just need to set yourself up properly (Think before coding). To give you a small example:

If you wanted to use command, here is some code I would try and use vs the previous posted versions all the previous versions start and finish the layer command for every layer in the list (For a list of 100 layers that means that the layer command starts and stops a 100 times).

Here is what my application would look like (Note, i just typed this up and only tested it once to see if it worked--this was a fast quickie-).
Code: [Select]
;;
;; This is my custom layer freezing application
;;
;; created by:
;; date created:
;;
;; notes:
;;
;; TODO:

;; ------------------
;; support procedure
;; ------------------
        (defun addcommas (items)
          (if (null items)
            nil
              (cons
                (if (null (cdr items))
                  (list (car items))
                  (list (car items) ",") )
                (addcommas (cdr items)))) )
;; a quick small procedure to parse and add a comma to each item in a list

;; ...

(setq layer-lst
      '("G8010001" "G8010004" "G8010010" "G8010015" "G8010021" "G8010026" "G8010027"
        "G8010030" "G8010032" "G8010035" "G8010036" "G8010052" "G8010057" "G8010059"
        "G8010071" "G8010072" "G8010098" "G8010321" "G8010374" "G8010376" "G8010382"
        "G8010388" "G8010397" "G8010400" "G8010570" "G8010572" "G8011000" "G8011005"
        "G8011006" "G8011009" "G8011010")
;; Set up layer name list.
      layer-lst (apply 'strcat (apply 'append (addcommas layer-lst))) )
;; Make a comma seperated list from the layer names.

(command "-layer" "fr" layer-lst "")
;; freeze the layers

;; ...

This is also the answer to the initial question (Here is your `push' Scott).

Quote
> How to freeze a list of layers?
> « on: Today at 06:02:50 am »
>    
> I have the following list of layers (filtered_layerlist)
>
> ("G8010001" "G8010004" "G8010010" "G8010015" "G8010021" "G8010026" "G8010027"
> "G8010030" "G8010032" "G8010035" "G8010036" "G8010052" "G8010057" "G8010059"
> "G8010071" "G8010072" "G8010098" "G8010321" "G8010374" "G8010376" "G8010382"
> "G8010388" "G8010397" "G8010400" "G8010570" "G8010572" "G8011000" "G8011005"
> "G8011006" "G8011009" "G8011010")
>
> I was trying to use the following to freeze all the layers in the above list
>
> (mapcar '(lambda (x) (command "layer" "freeze" x "") filtered_layerlist))
>
> But I know the syntax is not correct for a start - can anyone give
> me a push in the right direction.
>
> Cheers
>
> Scott
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

Andrea

  • Water Moccasin
  • Posts: 2372
Re: How to freeze a list of layers?
« Reply #17 on: October 01, 2008, 06:15:07 PM »
You'r right Se7en.  :-)

Code: [Select]
(command "[color=red]._[/color]-layer" "[color=red]_[/color]fr" layer-lst "")
Keep smile...

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: How to freeze a list of layers?
« Reply #18 on: October 01, 2008, 06:24:54 PM »
Personally I'm a believer in simplicity.

Most of us spend longer reading code and working out exactly what it does than could be saved by a 50 millisecond decrease in speed for 8000 iterations over the posted list.

This seems demonstrably correct and transparent ... a big advantage !
Code: [Select]
(setq LayerList '("G8010001"      "G8010004"      "G8010010"
                  "G8010015"      "G8010021"      "G8010026"
                  "G8010027"      "G8010030"      "G8010032"
                  "G8010035"      "G8010036"      "G8010052"
                  "G8010057"      "G8010059"      "G8010071"
                  "G8010072"      "G8010098"      "G8010321"
                  "G8010374"      "G8010376"      "G8010382"
                  "G8010388"      "G8010397"      "G8010400"
                  "G8010570"      "G8010572"      "G8011000"
                  "G8011005"      "G8011006"      "G8011009"
                  "G8011010"
                 )
)

(defun c:testmake (/)
    (foreach layerName LayerList (command "_-layer" "_M" layerName ""))
    (princ)
)

(defun c:testThaw (/)
    (foreach layerName LayerList (command "_-layer" "_T" layerName ""))
    (princ)
)

(defun c:testFreeze (/ StringList)
    (foreach layerName LayerList (command "_-layer" "_F" layerName ""))
    (princ)
)
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.

alanjt

  • Needs a day job
  • Posts: 5352
  • Standby for witty remark...
Re: How to freeze a list of layers?
« Reply #19 on: October 01, 2008, 09:01:40 PM »
you are absolutely right kerry, but even the simplest of procedures can be improved with a very simple modification. granted, one can just continue to say that until they've turned it into something complicated again.
i would change one thing from your route, that is for it to iterate only through the function for each layer, rather than iterating through the full function each time, mainly for freeze, thaw, and off you can just hit the layerp once to restore previous layer states. i'm probably just being nit-picky; i also just wanted to join in on the fun.

Code: [Select]
(setq LayerList '("G8010001"      "G8010004"      "G8010010"
                  "G8010015"      "G8010021"      "G8010026"
                  "G8010027"      "G8010030"      "G8010032"
                  "G8010035"      "G8010036"      "G8010052"
                  "G8010057"      "G8010059"      "G8010071"
                  "G8010072"      "G8010098"      "G8010321"
                  "G8010374"      "G8010376"      "G8010382"
                  "G8010388"      "G8010397"      "G8010400"
                  "G8010570"      "G8010572"      "G8011000"
                  "G8011005"      "G8011006"      "G8011009"
                  "G8011010"
                 )
)


(defun c:testmake (/)
  (command "_-layer"
  (foreach layername LayerList  (command "_M" layername))
  (command "")
  (princ)
)

(defun c:testThaw (/)
  (command "_-layer"
  (foreach layername LayerList  (command "_T" layername))
  (command "")
  (princ)
)

(defun c:testFreeze (/)
  (command "_-layer"
  (foreach layername LayerList (command "_F" layername))
  (command "")
  (princ)
)
Civil 3D 2019 ~ Windohz 7 64bit
Dropbox

JohnK

  • Administrator
  • Seagull
  • Posts: 10657
Re: How to freeze a list of layers?
« Reply #20 on: October 02, 2008, 09:32:40 AM »
Im only iterating 3 times...So *Ptthhht!* :p

Kerry, I agree with your desire for clean easy code but in my opinion that iteration of the layer command for every layer is sloppy; I understand your point about us having a set list of layers and the unnecessary need to `set up' but I dont like to insinuate that this is the correct process for all situations.

Why? Because: 8000 list iterations is going to be a lot faster then 100 command iterations any day.  I would rather teach someone to think about how they can iterate a list ten times over in an effort to get it done right the first time then to waste time later trying to speed up a seeming simple process they havent spent enough time thinking about in the first place.


TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

alanjt

  • Needs a day job
  • Posts: 5352
  • Standby for witty remark...
Re: How to freeze a list of layers?
« Reply #21 on: October 02, 2008, 12:31:43 PM »
very good point. seems best to learn to work smart, not fast and simple.
Civil 3D 2019 ~ Windohz 7 64bit
Dropbox

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: How to freeze a list of layers?
« Reply #22 on: October 02, 2008, 03:28:23 PM »
....  8000 list iterations is going to be a lot faster then 100 command iterations any day.  ...

time for a wager I think ??


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.

JohnK

  • Administrator
  • Seagull
  • Posts: 10657
Re: How to freeze a list of layers?
« Reply #23 on: October 02, 2008, 03:49:03 PM »
....  8000 list iterations is going to be a lot faster then 100 command iterations any day.  ...

time for a wager I think ??


*lol* ...If its going to be for a beer, we'll have to settle for a check made out for the amount cause the dry ice and the shipping would be a pain in the butt.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

Krushert

  • Seagull
  • Posts: 13679
  • FREE BEER Tomorrow!!
Re: How to freeze a list of layers?
« Reply #24 on: October 02, 2008, 09:17:13 PM »
Dont concern yourself with the speed of entmod/make vs VL functions.

Thank you Seven for this lesson, and Thanks to Cab's post about Command verses "Code"  and to everyone else that chimed. 

But my question is why put it into a "SETQ" list to begin with.  Why not throw the list right into the Script code since the you are using the COMMAND and the list is known.  I can understand culling something for a list but with it known it makes little sense.  For example I have use this code below to freeze certain layers with a file. 

Code: [Select]
;;;  THAWING OF RCP LAYERS AND FREEZING FLOOR PLAN LAYERS.
(defun c:RCPT ()

  (vl-load-com)
  (vl-cmdf ".undo" "BEGIN")
  (setvar "CMDECHO" 0)

  (VL-CMDF "-LAYER" "T" "*RCP*" "")
  (VL-CMDF
    "-LAYER"
    "F"    "*PLUMB*,*WIND*,*DOOR*,*ADA*,*AREA*,*REFERENCE*,*FURN*,*APPL*,*CASEWORK*,*CABINETS*,*DIM*,*STAIR*,*STAIR*TEXT*,*ELEC*,*HANDRAIL*,*PARTIT*"
    ""
  )

  (setvar "CMDECHO" 1)
  (vl-cmdf ".undo" "END")
  (princ)
)

Please understand that my lisping capabilities are equal to that of an 3rd grader especially when compared to you professors with doctorates.   So I am becuase I know there is a good reason as to why it being coded with a SETQ list and I want to find out.   Again thank you for the lessons.  :-)
I + XI = X is true ...  ... if you change your perspective.

I no longer CAD or Model, I just hang out here picking up the empties beer cans

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: How to freeze a list of layers?
« Reply #25 on: October 02, 2008, 10:17:47 PM »



But my question is why put it into a "SETQ" list to begin with.  Why not throw the list right into the Script code since the you are using the COMMAND and the list is known.  I can understand culling something for a list but with it known it makes little sense.  For example I have use this code below to freeze certain layers with a file. 


The user MAY require a list for several disciplines, or for several clients, and the list MAY also be dynamic .. dependant on other criteria.

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: How to freeze a list of layers?
« Reply #26 on: October 03, 2008, 12:56:35 AM »
A tangent but ... what should be central to one's coding philosophy are strategies that minimize code maintenance and maximize code reuse. Often just a little extra effort to attempt to forecast how might a particular algorithm may be used down the road will make apparent a more effective way to define and code a function, and that's exactly what Kerry is pointing out. The user MAY require a list for several disciplines, or for several clients, and the list MAY also be dynamic ... dependent on other criteria.

Two things you can do to address this are (1) design as generically as is practical, but not to the point where a function is so ambitious it becomes an unwieldy behemoth, and (2) design an interface (function signature and return type) you can continue to honor over time so that client functions are not broken when changes or improvements to the implementation occur.
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

scottcd

  • Newt
  • Posts: 52
Re: How to freeze a list of layers?
« Reply #27 on: October 03, 2008, 04:43:48 AM »
I am in the middle of developing a dynamic layer filter and dockable dialogue box and this was part of the process - how to pass the filtered layer list to the layer command.

Thanks for all the help and insight.

I have attached an image to give you an idea of what I am trying to - with ODCL.

Cheers

Scott

AutoCAD Dos R9 - 2018 and BricCAD 18.2