Author Topic: delete all objects on frozen layers  (Read 3855 times)

0 Members and 1 Guest are viewing this topic.

ronjonp

  • Needs a day job
  • Posts: 7529
delete all objects on frozen layers
« on: April 26, 2006, 12:21:31 PM »
I know there are some routines out there already that do this but I'm trying to write my own feeble routine  :-D.

Why won't this code delete the objects in the SS?

Code: [Select]
(defun byebye (/ lays lay n ss index)
  (setq lays (vla-get-layers
       (vla-get-activedocument (vlax-get-acad-object))
     )
  )
  (vlax-for lay lays
    (if (or (eq (vla-get-freeze lay) :vlax-true)
    (eq (vla-get-layeron lay) :vlax-false)
)
      (progn
(setq n (vla-get-name lay))
(if (not (wcmatch n "*|*"))
  (progn
    (vla-put-freeze
      lay
      :vlax-false
    )
    (vla-put-layeron
      lay
      :vlax-true
    )
    (vla-put-lock
      lay
      :vlax-false
    )
    (vla-regen (vla-get-activedocument (vlax-get-acad-object))acactiveviewport)
    (if
      (setq ss (ssget "x" (list (cons 8 n))))
       (progn
(setq index (1- (sslength ss)))
(while (>= index 0)
   (vla-delete (vlax-ename->vla-object (ssname ss index)))
)
       )
       (princ (strcat "Nothing on layer " n))
    )
  )
  (princ (strcat "Frozen or off xref layer skipped - " n))
)
      )
    )
  )
  (princ)
)

Thanks,

Ron

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: delete all objects on frozen layers
« Reply #1 on: April 26, 2006, 12:37:32 PM »
You are not deincrementing your counter:
Also don't unlock,thaw unless you have to.
Two line you may not need, but test them.
Code: [Select]
(defun byebye (/ lays lay layname ss index lock_state)
  (setq lays (vla-get-layers
               (vla-get-activedocument (vlax-get-acad-object))
             )
  )
  (vlax-for lay lays
    (if (or (eq (vla-get-freeze lay) :vlax-true)
            (eq (vla-get-layeron lay) :vlax-false)
        )
      (progn
        (setq layname (vla-get-name lay))
        (if (not (wcmatch layname "*|*"))
          (progn
            (if
              (setq ss (ssget "_X" (list (cons 8 layname))))
               (progn
                 (vla-put-freeze lay :vlax-false)
                 ;; NOT NEEDED (vla-put-layeron lay :vlax-true)
                 (setq lock_state (vla-get-lock lay))
                 (vla-put-lock lay :vlax-false)
                 ;; NOT NEEDED (vla-regen (vla-get-activedocument (vlax-get-acad-object))acactiveviewport)
                 (setq index (sslength ss))
                 (while (>= (setq index (1- index)) 0)
                   (vla-delete (vlax-ename->vla-object (ssname ss index)))
                 )
                 (if (eq lock_state :vlax-true)
                   (vla-put-lock lay :vlax-true)
                 )
               )
               (princ (strcat "Nothing on layer " layname))
            )
          )
          (princ (strcat "Frozen or off xref layer skipped - " layname))
        )
      )
    )
  )
  (princ)
)
« Last Edit: April 26, 2006, 01:02:21 PM by CAB »
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.

T.Willey

  • Needs a day job
  • Posts: 5251
Re: delete all objects on frozen layers
« Reply #2 on: April 26, 2006, 12:43:36 PM »
Here is one I wrote a little while ago.

Code: [Select]
(defun c:EFrozeOff (/ ActDoc LayList LayNameList)
; Erases all objects on frozen/off layers, then purges drawing.

(setq ActDoc (vla-get-ActiveDocument (vlax-get-Acad-Object)))
(vlax-for Lay (vla-get-Layers ActDoc)
 (if
  (or
   (= (vla-get-LayerOn Lay) ':vlax-false)
   (= (vla-get-Freeze Lay) ':vlax-true)
  )
  (progn
   (vla-put-Lock Lay :vlax-false)
   (setq LayList (cons Lay LayList)
    LayNameList (cons (vla-get-Name Lay) LayNameList)
   )
  )
 )
)
(vla-StartUndoMark ActDoc)
(if LayList
 (progn
  (vlax-for Layout (vla-get-Layouts ActDoc)
   (vlax-for Obj (vla-get-Block Layout)
    (if (member (vla-get-Layer Obj) LayNameList)
     (vla-Delete Obj)
    )
   )
  )
  (vla-PurgeAll ActDoc)
 )
)
(vla-EndUndoMark ActDoc)
(princ)
)

Looks like Alan let you know what was wrong, so this is just another way of doing it.

Edit: Updated code to unlock froze of off layers per Alan's post.
« Last Edit: April 26, 2006, 01:23:14 PM by T.Willey »
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

ronjonp

  • Needs a day job
  • Posts: 7529
Re: delete all objects on frozen layers
« Reply #3 on: April 26, 2006, 12:50:40 PM »
Thanks for the replies guys. I always miss something!

CAB,

In the code you modified above:
(not (wcmatch n "*|*")) needs to be (not (wcmatch layname "*|*"))


Just incase someone else tries it out  :kewl:.

thanks again,

Ron

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: delete all objects on frozen layers
« Reply #4 on: April 26, 2006, 01:03:33 PM »
OK, changed the post, thanks.
But let us know how you make out with the routine.
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.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: delete all objects on frozen layers
« Reply #5 on: April 26, 2006, 01:04:45 PM »
Tim,
Two questions:
1. vla-delete honers locked layers or not?

2. No problem looping through the layout collections?
From the Help file I see this

Quote
vla-delete

Remarks
       
When you delete an object in a collection, all remaining items in the collection
are reassigned a new index based on the current count. You should therefore
avoid loops that delete an object while iterating through the collection.
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: 7529
Re: delete all objects on frozen layers
« Reply #6 on: April 26, 2006, 01:07:13 PM »
OK, changed the post, thanks.
But let us know how you make out with the routine.

It works great :).

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

T.Willey

  • Needs a day job
  • Posts: 5251
Re: delete all objects on frozen layers
« Reply #7 on: April 26, 2006, 01:15:26 PM »
Tim,
Two questions:
1. vla-delete honers locked layers or not?

2. No problem looping through the layout collections?
From the Help file I see this

Quote
vla-delete

Remarks
       
When you delete an object in a collection, all remaining items in the collection
are reassigned a new index based on the current count. You should therefore
avoid loops that delete an object while iterating through the collection.
1 - Will throw an error.  I never lock layers, so I never encountered that error.  Thanks for pointing that out.


2 - I haven't had any problems.  Good point though, and I don't remember reading that.  I guess if this is true, then I could make a list of all the objects, and then delete from the list.  Maybe when I get some free time I will check it out.

I just did a quick little test.  In a new drawing I created a layer, froze it and turned if off.  I then drew 6 lines.  I move lines 2, 4 and 6 to the new layer and ran the command.  It erased the objects, and purged the layer out of the drawing.  Don't know if it's a fluke or not.
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: delete all objects on frozen layers
« Reply #8 on: April 26, 2006, 01:35:34 PM »
 I didn't know. That's why I asked. 8-)
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.

Jeff_M

  • King Gator
  • Posts: 4096
  • C3D user & customizer
Re: delete all objects on frozen layers
« Reply #9 on: April 26, 2006, 02:56:40 PM »
2. No problem looping through the layout collections?
From the Help file I see this

Quote
vla-delete

Remarks
       
When you delete an object in a collection, all remaining items in the collection
are reassigned a new index based on the current count. You should therefore
avoid loops that delete an object while iterating through the collection.
CAB/Tim, This is true if you iterate the collection by the index, such as:
Code: [Select]
(setq i -1
         mdlspc (vla-get-modelspace doc)
 )
(while (< (setq i (1+ i)) (vla-get-count mdlspc))
  (vla-delete (vla-item mdlspc i))
)
But if you use For...Each (lisp equivilent is (vlax-for....)) you are not limited by the Index......which the Help file does point out.

T.Willey

  • Needs a day job
  • Posts: 5251
Re: delete all objects on frozen layers
« Reply #10 on: April 26, 2006, 03:10:24 PM »
2. No problem looping through the layout collections?
From the Help file I see this

Quote
vla-delete

Remarks
       
When you delete an object in a collection, all remaining items in the collection
are reassigned a new index based on the current count. You should therefore
avoid loops that delete an object while iterating through the collection.
CAB/Tim, This is true if you iterate the collection by the index, such as:
Code: [Select]
(setq i -1
         mdlspc (vla-get-modelspace doc)
 )
(while (< (setq i (1+ i)) (vla-get-count mdlspc))
  (vla-delete (vla-item mdlspc i))
)
But if you use For...Each (lisp equivilent is (vlax-for....)) you are not limited by the Index......which the Help file does point out.
Thanks for the clarification Jeff, which makes a lot of sense.
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: delete all objects on frozen layers
« Reply #11 on: April 26, 2006, 03:14:20 PM »
Thanks, Jeff, for clearing that up.
The vlax-for must act just like the foreach in that it creates a Private copy of the list  which is unaffected by anything happening within the foreach loop.
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.