Author Topic: purge everything but layers  (Read 1948 times)

0 Members and 1 Guest are viewing this topic.

andrew_nao

  • Guest
purge everything but layers
« on: September 25, 2012, 11:23:45 AM »
in a lisp im using this line to purge everything
Code: [Select]
(vla-purgeall (vla-get-activedocument (vlax-get-acad-object)))its is in a file that is loaded on startup and (not thinking) this seems to be effecting my dwg templates by deleting my layers.
i would like to change this around and have it purge everything except my layers and im having a bit of a brain fart.
can anyone suggest a better way?

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: purge everything but layers
« Reply #1 on: September 25, 2012, 11:42:38 AM »
The purgeall method is unfortunately an 'all-or-nothing' approach - you cannot control which collections will be purged. As an aside, note that the purgeall method will also skip Multileader Styles when purging a drawing - this is a known bug.

Since you don't wish to purge your template, how about testing whether the drawing has been saved before performing the purge, e.g.:
Code - Auto/Visual Lisp: [Select]
  1. (if (= 1 (getvar 'dwgtitled))
  2.     (progn
  3.         (repeat 3 (vla-purgeall doc))
  4.     )
  5. )

Alternatively, use the -purge command to purge specific colections.

Tharwat

  • Swamp Rat
  • Posts: 710
  • Hypersensitive
Re: purge everything but layers
« Reply #2 on: September 25, 2012, 11:56:34 AM »
This could be of a use .  :-D

Code - Auto/Visual Lisp: [Select]
  1. (defun c:test (/ l p)
  2.   (while
  3.     (setq l (tblnext "LAYER" (null l)))
  4.      (setq p (cons (entmakex (list '(0 . "POINT")
  5.                                    (list 10 0. 0. 0.)
  6.                                    (cons 8 (cdr (assoc 2 l)))
  7.                              )
  8.                    )
  9.                    p
  10.              )
  11.      )
  12.   )
  13.   (repeat 3
  14.     )
  15.   )
  16.   (foreach x p (entdel x))
  17.   (princ)
  18. )

ronjonp

  • Needs a day job
  • Posts: 7529
Re: purge everything but layers
« Reply #3 on: September 25, 2012, 12:13:02 PM »
You might want to check that the layer is unlocked before creating the points. Otherwise they will not get removed.

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: purge everything but layers
« Reply #4 on: September 25, 2012, 05:48:59 PM »
Another alternative way might be to simply save a list of layers into a variable, do the purgeall, then step through the list recreating any layers not found with their saved properties. Something like this:
Code - Auto/Visual Lisp: [Select]
  1. (defun PurgeAll-ButLayers  (doc / layers layer-collection new)
  2.   (vlax-for lay  (setq layer-collection (vla-get-Layers doc))
  3.     (setq layers
  4.            (cons (mapcar '(lambda (property) (cons property (vl-catch-all-apply 'vlax-get (list lay property))))
  5.                          '("Name"           "Description"    "Freeze"         "LayerOn"        "Linetype"
  6.                            "Lineweight"     "Lock"           "Material"       "PlotStyleName"  "Plottable"
  7.                            "TrueColor"      "ViewportDefault"))
  8.                  layers)))
  9.   (vla-PurgeAll doc)
  10.   (foreach lay  layers
  11.     (if (not (vl-catch-all-error-p (setq new (vl-catch-all-apply 'vla-add (list layer-collection (cdar lay))))))
  12.       (foreach property (cdr lay) (vl-catch-all-apply 'vlax-put (list new (car property) (cdr property)))))))
« Last Edit: September 25, 2012, 05:55:07 PM by irneb »
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.