Author Topic: Layer 0 attack  (Read 17183 times)

0 Members and 1 Guest are viewing this topic.

ronjonp

  • Needs a day job
  • Posts: 7526
Re: Layer 0 attack
« Reply #15 on: November 18, 2008, 08:53:02 AM »
That's exactly what you do  :-) If you are going to run this by odbx, you'd probably have to combine the two....but I could be wrong.

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

Notsober

  • Guest
Re: Layer 0 attack
« Reply #16 on: November 18, 2008, 09:56:12 AM »
That's exactly what you do  :-) If you are going to run this by odbx, you'd probably have to combine the two....but I could be wrong.

Okay. I opened the dwg that you *fixed* THANKS that's exactly what I wanted! but now i wish I understood the difference between odbx and lisp... cuz right now I'm still stuck on how to do what you did with the code.

help me get this right.

I save this code into a notepad and save it as *.lsp? I get the feeling I'm wrong there...

ronjonp

  • Needs a day job
  • Posts: 7526
Re: Layer 0 attack
« Reply #17 on: November 18, 2008, 10:13:29 AM »
Odbx is a way to process a bunch of drawings without actually "opening" them. If you have tons of these drawings to shred, that might be the route to take. As for the notepad and .lsp extension, that's exactly what you need to do.

Add this to your lisp file to call it with "allzero at the command line:

Code: [Select]
(defun c:allzero (/)
  (ChangeAllItemsToLayer0
    (vla-get-ActiveDocument (vlax-get-Acad-Object))
  )
)

Ron

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Layer 0 attack
« Reply #18 on: November 18, 2008, 10:22:41 AM »
Slight mod & some comments.
This worked for me, although a regen was needed for some objects to show properly.
Code: [Select]
(defun ChangeAllItemsToLayer0 (doc)
  ;; unlock all layers
  (vlax-for i (vla-get-Layers doc)
    (vla-put-Lock i :vlax-false)
  )
  ;; loop through all blocks
  (vlax-for blk (vla-get-Blocks doc)
    (vlax-for i blk
      (makesmecry i) ; go set properties of object
      ;;  if the object is a block look for attributes
      (if (= (vla-get-ObjectName i) "AcDbBlockReference")
        ;;  foreach attribute
(foreach j (append (vlax-invoke i 'GetAttributes)
   (vlax-invoke i 'GetConstantAttributes)
   )
  (makesmecry j) ; go set properties of object
)
      )
    )
  )
)

(defun makesmecry (obj / p)
  ;;  for each property
  (foreach p '(("Layer" "0")("Color" 256) ("Linetype" -1) ("Lineweight" "ByLayer"))
    ;;  if property is one of these
    (if ;(and (vlax-property-available-p obj (car p) t)
             (not (equal (vlax-get-property obj (car p)) (cadr p)));)
      (vl-catch-all-apply ; catch any error while
'vlax-put-property ; changing the property
(list obj (car p) (cadr p))
      )
    )
  )
  (princ)
)

(defun c:test()
  (ChangeAllItemsToLayer0 (vla-get-ActiveDocument (vlax-get-Acad-Object)))
  (princ)
)
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.

ronjonp

  • Needs a day job
  • Posts: 7526
Re: Layer 0 attack
« Reply #19 on: November 18, 2008, 10:32:04 AM »
What are the errors? Perhaps post the drawing that is errors on.

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

ronjonp

  • Needs a day job
  • Posts: 7526
Re: Layer 0 attack
« Reply #20 on: November 18, 2008, 10:38:37 AM »
CAB,

Your version of makesmecry does the opposite of what he needs.
If the property of the object is bylayer, then you need to grab the layer property and apply it byobject.

Ron

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Layer 0 attack
« Reply #21 on: November 18, 2008, 10:47:06 AM »
Oops, I missed that requirement. :oops:
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.

ronjonp

  • Needs a day job
  • Posts: 7526
Re: Layer 0 attack
« Reply #22 on: November 18, 2008, 10:48:33 AM »
Oops, I missed that requirement. :oops:

It happens to the best of us  :-)

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Layer 0 attack
« Reply #23 on: November 18, 2008, 11:00:03 AM »
I was thinking to myself, "Why is Ron doing that?"  8-)
I'm on my third cup, you'd think I'd be awake by now.
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.
Re: Layer 0 attack
« Reply #24 on: November 18, 2008, 11:19:55 AM »
This is what I can ante up now.

It forces all to layer "0" except entities that were on "frozen" or "off" layers. Rather than delete the offending entities I opted to force them to two new layers, "FROZEN" or "OFF", so in the end you will have a drawing with potentially three layers "0", "FROZEN" and "OFF", as it purges all other layers, though absolute purging is not guaranteed in this version. For example, layers frozen in view ports will not be purged as the view port's xdata will reference the layers, preventing a purge. While that can easily be dealt with, as well as a suite of other layer purge inhibiting conditions, I simply don't have the time.

All entities will end up with hard coded color, line type and line weights corresponding to the layer they originally resided on. However, to perform the job correctly any objects with color or linetype "ByBlock" should inherit their properties from their parent's block (or parent block's layer). The program is coded so it branches on "ByBlock" accordingly, but in this version "ByBlock" is treated the same as "ByLayer", that is properties are inherited from the immediate parent layer.

Copy all the code, paste to a new text file and save as ForeceLayerZero.lsp to a folder in your support path. To load it type (load "ForceLayerZero"). To run it after it's loaded just type ForceLayerZero. Read the inline comments if you want to know more.

While it may look intimidating and / or appear to be a lot of code, it's neither. I have a verbose, modular coding style that makes it appear more than it is. Fear not, it's easy, simple sh*t.

Code: [Select]
;;  Provided complete with errors and omissions, without warranty,
;;  implied or stated for any particular use. USE AT YOUR OWN RISK.

(defun C:ForceLayerZero ( / _ChangeProperties _ItemExists _AddItem _Main )

    (defun _ChangeProperties ( object layer_index frozen off / properties lineweight )
   
        (setq properties
            (cdr
                (assoc
                    (vla-get-layer object)
                    layer_index
                )
            )
        )

        ;;  color
       
        (cond
       
            ;;  bylayer

            (   (eq 256 (vla-get-color object))
                (vla-put-color
                    object
                    (cdr (assoc 'color properties))
                )
            )
       
            ;;  byblock -- to do this properly the color
            ;;  of the parent block (if there is one) should
            ;;  be used (or the parent block's layer color if
            ;;  the parent block is bylayer). Maybe in the
            ;;  next version, for now treat same as bylayer.
   
            (   (zerop (vla-get-color object))
                (vla-put-color
                    object
                    (cdr (assoc 'color properties))
                )
            )
        )   
       
        ;;  linetype
       
        (cond
       
            ;;  bylayer

            (   (eq "ByLayer" (vla-get-linetype object))
                (vla-put-linetype
                    object
                    (cdr (assoc 'linetype properties))
                )
            )

            ;;  byblock -- to do this properly the linetype
            ;;  of the parent block (if there is one) should
            ;;  be used (or the parent block's layer linetype
            ;;  if the parent block linetype is bylayer). Maybe
            ;;  in the next version, for now treat same as bylayer.
   
            (   (eq "ByBlock" (vla-get-linetype object))
                (vla-put-linetype
                    object
                    (cdr (assoc 'linetype properties))
                )
            )
           
        )
       
        ;;  lineweight
                   
        (if
            (/=
                (vla-get-lineweight object)
                (setq lineweight (cdr (assoc 'lineweight properties)))
            )
            (vla-put-lineweight object lineweight)
        )
       
        ;;  map the entity to layer "FROZEN", "OFF" or "0"
       
        (cond
            (   (minusp (cdr (assoc 'freeze properties)))
                (vla-put-layer object frozen)
            )
            (   (zerop (cdr (assoc 'layeron properties)))
                (vla-put-layer object off)
            )
            (   t
                (vla-put-layer object "0")
            )
        )
       
    )
   
    (defun _ItemExists ( collection key / result )
        (vl-catch-all-apply
           '(lambda ( )
                (vla-item collection key)
                (setq result t)
            )
        )
        result
    )
   
    (defun _AddItem ( collection prefix / key )
   
        (if (_ItemExists collection prefix)
            (setq key prefix)
            (   (lambda ( i )
                    (while
                        (_ItemExists collection
                            (setq key
                                (strcat
                                    prefix
                                    (itoa (setq i (1+ i)))
                                )       
                            )       
                        )
                    )
                )
                1000
            )               
        )
       
        (vla-add collection key)
   
    )

    (defun _Main ( document / layers layer_index frozen frozen_name off off_name )
   
        (if (< 1 (vla-get-count (setq layers (vla-get-Layers document))))
       
            (progn
   
                ;;  unlock all layers and create
                ;;  the layer property index
       
                (vlax-for layer (setq layers (vla-get-Layers document))
       
                    (vla-put-Lock layer :vlax-false)
       
                    (setq layer_index
                        (cons
                            (cons
                                (vla-get-name layer)
                                (mapcar
                                   '(lambda ( property )
                                        (cons
                                            property
                                            (vlax-get layer property)
                                        )
                                    )
                                   '(color linetype lineweight layeron freeze)
                                )
                            )
                            layer_index
                        )
                    )
                )
       
                ;;  make layer 0 active so we can
                ;;  nuke all the other layers later
               
                (   (lambda ( layer )
                        (vla-put-layeron layer :vlax-true)
                        (if (/= "0" (getvar "clayer"))
                            (vla-put-freeze  layer :vlax-false)
                        )                       
                    )
                    (vla-item layers "0")
                )
       
                (setvar "clayer" "0")
               
                ;;  create 2 new layers, one will be called "FROZEN" the
                ;;  other will be called "OFF". Place entities accordingly.
               
                (setq
                    frozen      (_AddItem layers "FROZEN")
                    frozen_name (vla-get-name frozen)
                    off         (_AddItem layers "OFF")
                    off_name    (vla-get-name off)
                )
               
                ;;  set the layer properties accordingly
               
                (vla-put-freeze frozen :vlax-true)
                (vla-put-layeron off :vlax-false)
       
                ;;  now abuse every object in the drawing
       
                (vlax-for block (vla-get-Blocks document)
                    (vlax-for object block
                        (_ChangeProperties object layer_index frozen_name off_name)
                        (if (eq "AcDbBlockReference" (vla-get-ObjectName object))
                            (foreach attrib
                                (append
                                    (vlax-invoke object 'GetAttributes)
                                    (vlax-invoke object 'GetConstantAttributes)
                                )
                                (_ChangeProperties object layer_index frozen_name off_name)
                            )
                        )
                    )
                )
       
                ;;  purge the bastard
       
                (repeat 5 (vla-purgeall document))
               
                ;;  if the frozen and off layers remain in the drawing
                ;;  rename them if they don't sport the names we want
               
                (if (null (vlax-erased-p frozen))
                    (if (/= "FROZEN" (strcase (vla-get-name frozen)))
                        (vla-put-name frozen "FROZEN")
                    )
                )     
       
                (if (null (vlax-erased-p off))
                    (if (/= "OFF" (strcase (vla-get-name off)))
                        (vla-put-name off "OFF")
                    )
                )
   
            )
           
        )                           
       
        (princ)       

    )
   
    ;;  do it, do it now

    (_Main (vla-get-activedocument (vlax-get-acad-object)))

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

Notsober

  • Guest
Re: Layer 0 attack
« Reply #25 on: November 18, 2008, 11:51:05 AM »
As much as I really appreciate everyone's efforts, I still have to say: It took alot of guts for some of you, and I really can't give you any good reasons why I asked for this routine. But when the boss says do this, I have no choice but to say, okay. Not because I'm a kissass, hardly the case. It's only because they don't know any better, and I'd be talking out of my ass when I go to explain to them why they still shouldn't do this. Basically a dead end road for me if I do that.

So, here is the dwg that I mutilated with your help! Go ahead, check it out. Leaving "0" ON, you can turn OFF BIFENTHRIN, turn ON FORTRESS and viola! You're now looking at the lines and valves that have to do with only that particular process. MOCAP is not yet completed.. so it's still the same as BIFENTHRIN I think.

Notsober

  • Guest
Re: Layer 0 attack
« Reply #26 on: November 18, 2008, 11:53:44 AM »
What are the errors? Perhaps post the drawing that is errors on.


Code: [Select]
allzero.lsp successfully loaded.
Command: ; error: syntax error
Command:
Command: allzero
; error: no function definition: CHANGEALLITEMSTOLAYER0
Command:
Command: appload
allzero.lsp successfully loaded.
Command: ; error: extra right paren on input
Command:
Command: appload
allzero.lsp successfully loaded.
Command: ; error: bad argument type: VLA-OBJECT nil

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Layer 0 attack
« Reply #27 on: November 18, 2008, 12:04:22 PM »
Change this
(defun ChangeAllItemsToLayer0 (vla-get-ActiveDocument (vlax-get-Acad-Object)))
to this
(defun ChangeAllItemsToLayer0 (doc)

but id didn't work for me
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.

Notsober

  • Guest
Re: Layer 0 attack
« Reply #28 on: November 18, 2008, 02:12:30 PM »
Change this
(defun ChangeAllItemsToLayer0 (vla-get-ActiveDocument (vlax-get-Acad-Object)))
to this
(defun ChangeAllItemsToLayer0 (doc)

but id didn't work for me

didn't work for me either... bummer.

I have the borders now, that some reason there are some leftover layers that won't purge! i cannot find where they are hiding! grrrr....

M-dub

  • Guest
Re: Layer 0 attack
« Reply #29 on: November 18, 2008, 02:20:59 PM »
Just explode everything a dozen times and THEN see if you can find it.  When / if you do, undo and go change it.