Author Topic: Layers frozen in viewports ...  (Read 13147 times)

0 Members and 1 Guest are viewing this topic.

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Layers frozen in viewports ...
« on: February 11, 2005, 02:11:14 PM »
In the past, like AutoCAD versions < 2000, I relied on the fact that layers frozen in a viewport were embedded in the viewport's extended data, in dxf group 1003 ...

Code: [Select]
(
   (-1 . <Entity name: 7ef55eb8>)
   (0 . "VIEWPORT")

   ...

   (-3
      (
         "ACAD"
         (1000 . "MVIEW")
         (1002 . "{")

         ...

         (1002 . "{")  
         (1003 . "LAY1") ;; layers
         (1003 . "LAY2") ;; frozen
         (1003 . "LAY3") ;; in
         (1003 . "LAY4") ;; this
         (1003 . "LAY5") ;; viewport
         (1002 . "}")
         
         ...
         
         (1002 . "}")
      )
   )
)

Since 2000, the frozen layers also appear as enames in dxf group 331 ...

Code: [Select]
(
   (-1 . <Entity name: 7ef55eb8>)
   (0 . "VIEWPORT")

   ...

   (331 . <Entity name: 7ef55ec8>) <--+
   (331 . <Entity name: 7ef55ed8>)    |
   (331 . <Entity name: 7ef55ee8>)    |
   (331 . <Entity name: 7ef55ef8>)    |
   (331 . <Entity name: 7ef55f08>) <--+--+
                                      |  |
   ...                                |  |
                                      |  |
   (-3                                |  |
      (                               |  |
         "ACAD"                       |  |
         (1000 . "MVIEW")             |  |
         (1002 . "{")                 |  |
                                      |  |
         ...                          |  |
                                      |  |
         (1002 . "{")                 |  |
         (1003 . "LAY1") <------------+  |
         (1003 . "LAY2")                 |
         (1003 . "LAY3")                 |
         (1003 . "LAY4")                 |
         (1003 . "LAY5") <---------------+
         (1002 . "}")

         ...

         (1002 . "}")
      )
   )
)

As a result, one can yank out the frozen layer names by examining the 331 groups ...

Code: [Select]
(defun GetLayersFrozenInViewport ( viewportEname )
    (   (lambda ( data / pair data2 result )
            (while (setq pair (assoc 331 data))
                (if
                    (member
                       '(0 . "LAYER")
                        (setq data2
                            (entget
                                (setq ename
                                    (cdr pair)
                                )
                            )
                        )
                    )
                    (setq result
                        (cons
                            (cdr (assoc 2 data2))
                            result
                        )
                    )
                )
                (setq data
                    (cdr
                        (member pair data)
                    )
                )
            )
            result
        )
        (entget viewportEname)
    )
)

Can anyone see a flaw in this technique (as opposed to the examination of the 1003 group codes)?

Know a better way? Please share.
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Layers frozen in viewports ...
« Reply #1 on: February 11, 2005, 02:38:13 PM »
MP,
Here is a routine I started yesterday. Did not get far as play time is short
for me these next few weeks. It is VERY rough, but the forzen layers seems to work
well.

Code: [Select]
(defun c:get_frozen (/ ent)
  (setq ent (vp_sel))
  (setq vp_frz_lst (vpfrzlays ent))
  (princ)
)

(defun c:put_frozen (/ ent)
  (setq ent (car (entsel "\nselect a viewport to updte:")))
  ;;  not here yet.
)

;;=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-==-=-=-=-=-=-=-=-=-=-=-=
;; CAB modified code from Jason Piercey
;; Argument: Ename, viewport ename
;; Return: list frozen layer name or nil
;;=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-==-=-=-=-=-=-=-=-=-=-=-=
(defun vpfrzlays (ename) ; by Jason Piercey
  (mapcar '(lambda (y) (cdr (assoc 2 (entget y))))
          (massoc 341 (entget ename))
  )
)


(defun massoc (key alist / x nlist) ; by Jaysen Long
  (foreach x alist
    (if (eq key (car x))
      (setq nlist (cons (cdr x) nlist))
    )
  )
  (reverse nlist)
)
;;=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-==-=-=-=-=-=-=-=-=-=-=-=

;;  CAB select VP routine
;;  will work on VP made from other entities
(defun vp_sel (/ vpflag sel-vport entvport vptest)
  (if (= (getvar "TileMode") 1) ; in model space
    ;;------------------------------------------------
    (progn
      (alert "****  You must be in Paper Space to run this routine.  ****")
    )
    ;;------------------------------------------------
    (progn ;else in a layout
      (if (/= (getvar "cvport") 1)
        (command "_pspace") ; close the view port
      )
      (setq vpflag (getvar "cvport")) ; get viewport #
      (while (= vpflag 1) ; No active viewport, Loop until one is picked
        (setq sel-vport (car (entsel "\nSelect view port: ")))
        (if (= sel-vport nil)
          (alert
            "You must select a viewport\r\n    --=<  Try again!  >=--"
          )
          (progn
            (setq entvport (entget sel-vport))
            (if (and ;; not all vp objects are LWPolylines
                     ;;(= (cdr (assoc 0 entvport)) "LWPOLYLINE")
                     (setq vptest (member '(102 . "{ACAD_REACTORS") entvport))
                     (setq vptest (member '(102 . "}") (reverse vptest)))
                     (assoc 330 vptest)
                )
              (setq entvport (entget (cdr (assoc 330 vptest))))
            )

            ;;  Make VP active
            (if (= (cdr (assoc 0 entvport)) "VIEWPORT")
              (progn
                (setq vpflag (cdr (assoc 69 entvport)))
            ;;    (command "mspace") ; activate the last vp active
            ;;    (setvar "cvport" vpflag) ; switch to this vp
              )    
            );  endif  viewport
          )
        ) ;  endif cond  sel-vport
      ) ;endwhile (= vpFlag 1)
    )
  )
  ;;  return the ename of vp or nil
  (cond (entvport (cdr(assoc -1 entvport))))
)
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Layers frozen in viewports ...
« Reply #2 on: February 11, 2005, 02:48:53 PM »
Thank you CAB, I'll look at that.

Just a heads up, but I believe Jaysen Long wrote massoc. Credit is a good thing.
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Layers frozen in viewports ...
« Reply #3 on: February 11, 2005, 03:04:26 PM »
I may have jumped to conclusion as to the author.
link
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Layers frozen in viewports ...
« Reply #4 on: February 11, 2005, 03:11:10 PM »
Jason wrote VpFrzLays; Jaysen wrote Massoc. :)

I was going to say Jason's doesn't work, but then I checked ... dumb stuff Autodesk ... AutoCAD 2002 uses dxf group 341, AutoCAD 2004 uses dxf group 331.

That's as brainiac as needing to check an instance of an xref to find the xref's path (if using the object model).

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

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Layers frozen in viewports ...
« Reply #5 on: February 11, 2005, 03:15:50 PM »
You have quite a memory, I'm sure it serves you well.
Just envious.  :)
I updated the credits.
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Layers frozen in viewports ...
« Reply #6 on: February 11, 2005, 03:17:17 PM »
Remind me what a memory is 'kay?

:)

(Good on ya btw).
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.
Layers frozen in viewports ...
« Reply #7 on: February 11, 2005, 08:57:40 PM »
Screw it, I'm reverting back to my old way (slight mod) as it works back several releases and performs well enough.

Code: [Select]
(defun GetLayersFrozenInViewport ( viewportEname )
    ;;  © 1999-2005 Michael Puckett
    (   (lambda ( data / pair result )
            (while (setq pair (assoc 1003 data))
                (setq
                    data (cdr (member pair data))
                    result (cons (cdr pair) result)
                )
            )
        )
        (cdadr
            (assoc -3
                (entget
                    viewportEname
                   '("acad")
                )
            )
        )
    )    
)
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

Mark

  • Custom Title
  • Seagull
  • Posts: 28762
Layers frozen in viewports ...
« Reply #8 on: February 11, 2005, 09:03:52 PM »
How the heck do you do that. :D
TheSwamp.org  (serving the CAD community since 2003)

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Layers frozen in viewports ...
« Reply #9 on: February 11, 2005, 09:04:34 PM »
Eh?
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

Mark

  • Custom Title
  • Seagull
  • Posts: 28762
Layers frozen in viewports ...
« Reply #10 on: February 11, 2005, 09:07:54 PM »
Your code, it's like reading a book from the middle out. :D

it inspires me!!
TheSwamp.org  (serving the CAD community since 2003)

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Layers frozen in viewports ...
« Reply #11 on: February 11, 2005, 09:09:01 PM »
Neat-o  thanks for sharing. :)
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Layers frozen in viewports ...
« Reply #12 on: February 11, 2005, 09:18:41 PM »
I don't know what to say besides thanks! :)

<Shrugging.mpg>
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.
Layers frozen in viewports ...
« Reply #13 on: February 11, 2005, 09:20:22 PM »
Hey, thanks CAB. :)
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Layers frozen in viewports ...
« Reply #14 on: February 11, 2005, 11:26:28 PM »
MP
In an effort to understand the function I need some explanation of some of the parts.
First it appears as though you left out the MapCar. The parentheses are there.
Is it understood somehow?
Code: [Select]
(   (lambda ( data / pair result )
 ............... ^^^  Mapcar??
 
  Then the list used in the lambda is generated by this
 
Code: [Select]
      (cdadr
            (assoc -3
                (entget  
                    viewportEname
                   '("acad")
                )
            )
        )


Second question, Why is this required
Code: [Select]
'("acad")
and what does it do?

PS I like the new emoticons Mark. :)
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Layers frozen in viewports ...
« Reply #15 on: February 12, 2005, 12:46:18 AM »
Ok Charles, I'm going to take a stab at this. Bear with me, I'm not the best teacher.

Ahhh lambda --

Quote from: AutoLISP Refrence
Use the lambda function when the overhead of defining a new function is not justified. It also makes the programmer's intention more apparent by laying out the function at the spot where it is to be used. This function returns the value of its last expr, and is often used in conjunction with apply and/or mapcar to perform a function on a list.

Trouble with the reference is that it shows only two examples, one for apply, and one for mapcar. It implies that's the only way it can be used.

As such, the calling syntax is the same for both, insomuch as you call lambda quoted --

Code: [Select]
(apply
   '(lambda (x y z)
        (* x (+ y z))
    )
   '(5 6 7)
)

=> 65

(mapcar
   '(lambda (x)
        (* x 25)
    )
   '(2 -4 6 -8 10)
)

=> (50 -100 150 -200 250)

Now think about this for a moment. If you were not using lambda how might you use apply and mapcar?

Couple examples --

Code: [Select]
(apply
   '+
   '(1 2 3)
)

=> 6

(mapcar
   'itoa
   '(1 2 3)
)

=> ("1" "2" "3")

Anything kind of leap off the page?

The functions passed to apply and mapcar were quoted.

<voice=BasilFawlty>Ahhh, fascinating, completely rivoting. And salient perhaps ummm, how?</voice>

When you use apply and mapcar you need to quote the functions passed to it. Subtitle: You can use lambda unquoted where a function would be used unquoted.

Consider this --

Code: [Select]
(defun squarex (x)
    (* x x)
)

To use --

(squarex 5)

=> 25

What if we didn't want the overhead of defining a function, but our code's readability (and maintainability) would be improved if we could define and use functionality precisely where we need it? Sounds like "lambda to the rescue" to me --

Code: [Select]
(   (lambda (x)
        (* x x)
    )
    5
)

=> 25

Perhaps laying out both horizontally will be more illuminating --

Code: [Select]
(squarex 5)
((lambda (x) (* x x)) 5)

Ding! Ding! Ding!

Another one to illustrate argument passing --

Code: [Select]
(   (lambda (a b c)
        (/ (+ a b) (float c))
    )
    1
    2
    4
)
=> 0.75

Did this help? Do you see now that I am not calling mapcar, explicitly or implicitly in my GetLayersFrozenInViewport function?


Perhaps you will view this function in a new light. :)

Oh, just about forgot --

Code: [Select]
(entget
    ename
   '("acad") ;; <= this tells entget to retrieve the
             ;;    extended entity data for registered
             ;;    application name "acad". This is
             ;;    where the frozen layer information
             ;;    is stored and why we wants it. :)
)

:) (my, my these are big assed icons)
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

JohnK

  • Administrator
  • Seagull
  • Posts: 10648
Layers frozen in viewports ...
« Reply #16 on: February 12, 2005, 09:39:36 AM »
Very nice explination MP! But if i may also add my "look here" statment.

Have a look HERE and get some more information. (Note: Scheme syntax looks a bit weird but just read the descriptions and you will understand.)
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Layers frozen in viewports ...
« Reply #17 on: February 12, 2005, 09:44:45 AM »
Thanks Se7en. Very similar to Pages 62 ... in "Structure and Interprettation of Computer Programs", Abelson / Sussman.

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

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Layers frozen in viewports ...
« Reply #18 on: February 12, 2005, 09:56:22 AM »
Quote from: MP
Ok Charles, I'm going to take a stab at this. Bear with me, I'm not the best teacher.

But you do a good job in my opinion.


OK, Taking it very slow for you know who.  :) [ME]

Apply passes the list as a list expecting the number of variables matches the number of items in the list.
Code: [Select]
(apply
   '(lambda (x y z) ; the 3 variables
        (* x (+ y z))
    )
   '(5 6 7) ; the list of 3 items
)

;returns one result
=> 65



Mapcar passes the list one item at a time collecting the result before passing the next item.
Then returns the individual results in a list.
Code: [Select]
(mapcar
   '(lambda (x) ; one variable 'x'
        (* x 25)
    )
   '(2 -4 6 -8 10) ; the list of 5 items
)

; returns a list of results
=> ("1" "2" "3")



In the Help the syntax given for Lambda is
Code: [Select]
(lambda arguments expr...)
but should have been ???
Code: [Select]
( [optionsl function] (lambda arguments expr...) list)
unless there is another way to use it, you must pass it something so you always need
the extra parens.

In your example
Code: [Select]
( (lambda (x) ; one variable
    (* x x)
  )
  5   ; one item, one variable
)

=> 25; one result


Could also have multiple items, but would need variables to match

Code: [Select]

((lambda (x y) ; two variables
   (* x y)
  )
  5 2   ; two items
)


So this is like the Apply in that the number of items has to match the number of variables
But the items are not in a list as ther are with Apply.


How did I do Teach?
Great graphic by the way, highlighting the data. What program did you use to produce it?

As for:
Code: [Select]
(entget
    ename
   '("acad") ;; <= this tells entget to retrieve the
             ;;    extended entity data for registered
             ;;    application name "acad". This is
             ;;    where the frozen layer information
             ;;    is stored and why we wants it. :)
)

You can use it any time you need to retrieve the x-data of an entity?
So much to learn too little time. :)
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

whdjr

  • Guest
Layers frozen in viewports ...
« Reply #19 on: February 14, 2005, 03:30:56 PM »
MP,

I really have this love for vl-remove-if-not, so I modified your code :) .  Do you like?
Code: [Select]
(defun GetLayersFrozenInViewport (viewportEname)
  (mapcar 'cdr
 (vl-remove-if-not
   '(lambda (x)
      (= (car x) 1003)
    )
   (cdadr
     (assoc -3
    (entget
      viewportEname
      '("acad")
    )
     )
   )
 )
  )
)

I hope I don't get in trouble with the copyright police.  :shock:

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Layers frozen in viewports ...
« Reply #20 on: February 14, 2005, 04:03:27 PM »
Do I like? Not really, it executes about 30% slower than going my route.

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

whdjr

  • Guest
Layers frozen in viewports ...
« Reply #21 on: February 14, 2005, 04:22:02 PM »
Using your tool this is what I get:
Code: [Select]
Benchmarking ......................Elapsed milliseconds / relative speed for 524288 iteration(s):

    GETLAYERSFROZENINVIEWPORT.....1921 / 1.00 <fastest>
    TEST..........................1922 / 1.00 <slowest>

Where is the 30%?

MP (not logged in)

  • Guest
Layers frozen in viewports ...
« Reply #22 on: February 14, 2005, 04:28:44 PM »
Apparently mileage varies.

Code: [Select]
Elapsed milliseconds / relative speed for 4096 iteration(s):

    (GETLAYERSFROZENINVIEWPORT1 ENAME).....1219 / 1.36 <fastest>
    (GETLAYERSFROZENINVIEWPORT2 ENAME).....1656 / 1.00 <slowest>

Subtitle: Probably better to use vl-remove-if/not unless hand rolled code can definately out perform native functions.

MP (not logged in)

  • Guest
Layers frozen in viewports ...
« Reply #23 on: February 14, 2005, 04:29:59 PM »
524288 iterations?

Yeow, what are you running, a CRAY?

whdjr

  • Guest
Layers frozen in viewports ...
« Reply #24 on: February 14, 2005, 04:33:05 PM »
I ran it a few more times:

Code: [Select]
Elapsed milliseconds / relative speed for 524288 iteration(s):

    GETLAYERSFROZENINVIEWPORT.....1921 / 1.00 <fastest>
    TEST..........................1922 / 1.00 <slowest>

Elapsed milliseconds / relative speed for 131072 iteration(s):

    GETLAYERSFROZENINVIEWPORT.....1266 / 1.10 <fastest>
    TEST..........................1391 / 1.00 <slowest>

Elapsed milliseconds / relative speed for 131072 iteration(s):

    TEST..........................1281 / 1.00 <fastest>
    GETLAYERSFROZENINVIEWPORT.....1282 / 1.00 <slowest>

Elapsed milliseconds / relative speed for 131072 iteration(s):

    TEST..........................1078 / 1.06 <fastest>
    GETLAYERSFROZENINVIEWPORT.....1140 / 1.00 <slowest>

Elapsed milliseconds / relative speed for 131072 iteration(s):

    GETLAYERSFROZENINVIEWPORT.....1500 / 1.06 <fastest>
    TEST..........................1594 / 1.00 <slowest>

Seems like your won all the way around.


hmm...maybe the benchmark tool is weighted....?  :?

whdjr

  • Guest
Layers frozen in viewports ...
« Reply #25 on: February 14, 2005, 04:36:39 PM »
Quote from: MP (not logged in)
524288 iterations?

Yeow, what are you running, a CRAY?


Just the tool I copied from here

MP (not logged in)

  • Guest
Layers frozen in viewports ...
« Reply #26 on: February 14, 2005, 05:23:09 PM »
Guess I should have mentioned the bias coded into BenchMark. :)