Author Topic: How to PURGE All, Except the Named Groups?  (Read 4279 times)

0 Members and 1 Guest are viewing this topic.

ranjanarju

  • Guest
How to PURGE All, Except the Named Groups?
« on: September 27, 2016, 02:28:10 AM »
Dear Experts,

Greetings! This is my first post here.

I want to PURGE my .dwg files. I have some named group objects. Some of these group objects are used, some are not used. But I need to keep all these named groups even after I use PURGE command. When I use PURGE command, it removes my unused named groups too.

Need your help and suggestions on this.

Thank You in advance!

With regards,
Ranjan
BJIT Ltd.
« Last Edit: September 29, 2016, 01:11:22 AM by ranjanarju »

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1451
  • Marco
Re: How to PURGE all except the named groups?
« Reply #1 on: September 27, 2016, 02:51:30 AM »
Try this:
Code: [Select]
(defun ALE_Utl_PurgeGroupsNoName (DbxDoc / VlaObj Countr)
  (setq Countr 0)
  (vlax-map-Collection
    (setq VlaObj (vla-get-groups DbxDoc))
   '(lambda (LmbDat)
      (or
        (< 1 (vla-get-count LmbDat))
        (/= "*A" (substr (vla-get-Name LmbDat) 1 2))
        (progn
          (vla-delete LmbDat)
          (setq Countr (1+ Countr))
        )
      )
    )
  )
  (or
    (zerop Countr)
    (princ (strcat "\n" (itoa Countr) " unreferenced  groups purged. "))
  )
  (vlax-release-object VlaObj)
)

(ALE_Utl_PurgeGroupsNoName (vla-get-ActiveDocument (vlax-get-acad-object)))
Code: [Select]
; Function: ALE_DeleteGroups
;
; Version 1.00 - 29/09/2004
;
; Arguments: WcMNms - see WcMatch
;
; Return Values: none
;
; Examples:
;  (ALE_DeleteGroups "`*A*")          > Delete anonimous groups
;  (ALE_DeleteGroups "GroupA,GroupB") > Delete GroupA GroupB
;  (ALE_DeleteGroups "Group*")        > Delete all Group*
;  (ALE_DeleteGroups "*")             > Delete all Groups
;
(defun ALE_DeleteGroups (WcMNms / DctDat GrpNms EntNms Dxf__3)
  (and
    (setq
      DctDat (dictsearch (namedobjdict) "ACAD_GROUP")
      GrpNms (member (assoc 3 DctDat) DctDat)
      DctDat (cdar DctDat)
    )
    (while (setq Dxf__3 (cdar GrpNms))
      (and (wcmatch Dxf__3 WcMNms) (entdel (dictremove DctDat Dxf__3)))
      (setq GrpNms (cddr GrpNms))
    )
  )
)

ranjanarju

  • Guest
Re: How to PURGE all except the named groups?
« Reply #2 on: September 29, 2016, 12:47:02 AM »
Dear Marc'Antonio Alessi,

Thank you so much for your time and efforts!

I am really very sorry to say that I am not able to use these codes. I am comparatively very new in AutoLisp/Scripting world!

How to use these codes actually? I meant the methodology to use these codes.

I copied these codes to a text file.... then changed the extension of the .txt as .lsp... loaded the file in AutoCAD using APPLOAD. But I did not find any command to invoke these codes. I tried-  i) "ALE_Utl_PurgeGroupsNoName" to invoke one .lsp; and ii)  "ALE_DeleteGroups" to invoke another .lsp  . But it did not work. I know, this is not the way. I searched, but did not able to find the methodology.

Should I copy these codes to ACADDOC.lsp and then invoke the command ? What is the command actually then?

Or Should I change these codes to [e.g...(defun c:abc()...] type, so that I can invoke the command as 'abc' in command line? If so, how to integrate your codes to [...(defun c:abc()...] format?

Please help! You may also share some links, where these methodologies are described.

Thank you.

Bests,
Ranjan
« Last Edit: September 29, 2016, 12:52:31 AM by ranjanarju »

danallen

  • Guest
Re: How to PURGE all except the named groups?
« Reply #3 on: September 29, 2016, 12:58:59 AM »
Ranjanarju,

Can you clarify your groups and how they are purged? I'm on Bricscad which is pretty much AutoCAD v2005, and groups are not purged when using the PURGE command. Neither named nor unnamed (starting with *A). Even after I erase all objects in a drawing, the groups still exist until drawing is saved & reopened.

Unless I'm mistaken, Marc'Antonio Alessi's code will delete groups which is the opposite of what you want.

Dan

ranjanarju

  • Guest
Re: How to PURGE all...Except the Named Groups?
« Reply #4 on: September 29, 2016, 01:46:34 AM »
Dear Dan,

Thank you for your comments!

The unused groups are named as: SIZE_[Size]_[Position Coordinates]_[Drawing ID]_...etc (For example: SIZE_42.5_ 15_715.665_620.558_M-663321_0118_M_1). Here [42.5, 15] is the size; [715.665,620.558] is the coordinates; M-663321 is the drawing id.

These are named groups, but these are empty and unused. When PURGE command is used, all these empty and unused groups are removed. But we need to keep these groups, even after PURGE command is used (but we need to keep the data size of our .dwg files as minimum as possible too). These unused groups are necessary when other designers want to add/delete groups later when necessary.

Need a solution for this!

Attached some images for more clarifications:
1. Before PURGE
2. After PURGE
3. Used Groups



Thanks.

Br,
Ranjan
« Last Edit: September 29, 2016, 01:50:41 AM by ranjanarju »

lamarn

  • Swamp Rat
  • Posts: 636
Re: How to PURGE All, Except the Named Groups?
« Reply #5 on: September 29, 2016, 03:36:35 AM »
Did you dive into this code for some possibilities..?
I made a couple of 'my layers' unpurgable using it..

https://www.theswamp.org/index.php?topic=44094.msg493432#msg493432
Design is something you should do with both hands. My 2d hand , my 3d hand ..

ranjanarju

  • Guest
Re: How to PURGE All, Except the Named Groups?
« Reply #6 on: September 29, 2016, 07:59:30 AM »
Dear Dan,

The second code of Marc'Antonio Alessi actually worked for me! And it is to be used as (ALE_DeleteGroups "`*A*") in command line. I am still an infant to this AutoLISP programming world!
Thanks to Marc'Antonio !


Dear lamarn,
The method gile described in other thread actually matches very well with my requirements. I want to use PURGE, but do not want to remove the named groups.

But then, how to deploy these AutoLISP codes (gc:SetUnpurgeable) for referencing in the Group Table Extension Dictionary?

Thanks.

BR,
Ranjan

danallen

  • Guest
Re: How to PURGE All, Except the Named Groups?
« Reply #7 on: September 29, 2016, 12:04:08 PM »
Bricscad v14 doesn't have the option to purge groups. Regarding Gile's code to make things unpurgeable, I would think twice about that, I hate the idea of creating items that can't easily be purged with regular CAD commands.

You can create custom commands with your own purge options using wildcards. The grave accent character (top-left under tilde ~), will treat the next asterisk * as that character, not as a wildcard. The second asterisk * is treated as a wilcard, thus the command purges any group name starting with *A
Code - Auto/Visual Lisp: [Select]
  1. (defun c:myGroupPurge ()
  2.   (command "-purge" "group" "`*A*" "n")
  3.   (princ)
  4. )
  5.  

here is my general purge command with added option for purging unnamed groups, though command sequence tested on Bricscad, may need to be adjusted for current AutoCAD. (side note - can't believe I'm using code 14 years old!)
Code - Auto/Visual Lisp: [Select]
  1. ;;; purge all in order of preference
  2. (defun c:PA ()
  3.   (foreach x (list "BLOCK" "MLINESTYLE" "Tablestyles" "LAYER" "DIMSTYLE" "STYLE" "LTYPE" "PLOTSTYLES" "Visualstyles" "MATERIALS")
  4.     (SAA_PurgeAll x nil)
  5.   ) ;_foreach
  6.   (SAA_PurgeAll "GROUP" "`*A*")
  7.   (princ)
  8. )
  9.  
  10. ;;;============================================================================
  11. ;;; repeat purges passed in purge type
  12. ;;; nil = all
  13. ;;; use singular form, ex. "block" not "blocks"
  14. ;;; options: block, dimstyle, layer, ltype, plotstyle, shape, textstyle, mlinestyle, all
  15. ;;;
  16. ;;; modified from
  17. ;;; From: Cliff Middleton (cmiddleton@generalbroach.com)
  18. ;;; Subject: Re: Can Lisp determine if purge is required?
  19. ;;; Newsgroups: autodesk.autocad.customization
  20. ;;; Date: 2002-08-01 13:00:33 PST
  21. ;;;
  22. ;;; I like it, Mark.  You may have found the graille (SP?).  Here's my version.
  23. ;;;============================================================================
  24. (defun SAA_PurgeAll (ptype mask / tcnt cnt purgeall_aux)
  25.   (if (not mask)(setq mask "*"))
  26.   (if (= "ALL" ptype)
  27.     (command "-purge" "reg" mask "no")
  28.   )
  29.   (if (not ptype)(setq ptype "All"))  ;trap for purge type
  30.   (defun purgeall_aux (/ cnt)
  31.     (setq cnt 0)
  32.     (command "._PURGE" ptype mask "Yes")
  33. ;    (command "._PURGE" ptype "~*`**" "Yes") ;don't purge anonymous blocks
  34.     (while (= (getvar "CMDACTIVE") 1) (command "Yes") (setq cnt (1+ cnt)))
  35.     cnt
  36.   ) ;_ defun
  37.   (SETV "cmdecho" 0)
  38.   (command "._UNDO" "BEGIN")
  39.   (setq tcnt 0)
  40.   (while (> (setq cnt (purgeall_aux)) 0) (setq tcnt (+ tcnt cnt)))
  41.   (if (= 9999 tcnt)(progn (princ "\nQuitting at 9999 purges")(setq tcnt 0))) ;trap for endless loops
  42.   (command "._UNDO" "_END")
  43.   (RSETV "cmdecho")
  44.   (if (> tcnt 0)
  45.     (princ (strcat "\n("
  46.                    (itoa tcnt)
  47.                    ") "
  48.                    (if (= (strcase ptype) "ALL")
  49.                      "item"
  50.                      ptype
  51.                    )
  52.                    (if (/= 1 tcnt)
  53.                      "s"
  54.                      ""
  55.                    ) ;_ if
  56.                    " purged."
  57.            ) ;_ strcat
  58.     ) ;_ princ
  59.   )
  60.   (princ)
  61. ) ;_ defun
  62.  
  63. ;==========================================================
  64. ; SETV function saves setvar settings to be reset at end with RSETV
  65. ;     (setv "cmdecho" 0) set cmdecho off
  66. ;     (rsetv "cmdecho")  resets cmdecho (see below)
  67. ;   taken from Essential AutoLISP by Roy Harkow
  68. ;==========================================================
  69. (defun SETV (sysvar newval / cmdnam)
  70.   (setq cmdnam (read (strcat sysvar "1")))              ;Create   [savevar]1
  71.   (set cmdnam (getvar sysvar))                          ;Save     [savevar]'s value
  72.   (setvar sysvar newval)                                        ;Then set [savevar] to new value
  73. )
  74.  
  75. (defun RSETV (sysvar / )
  76.   (if (eval (read (strcat sysvar "1")))                 ;Only change if exists
  77.     (progn
  78.       (setq cmdnam (read (strcat sysvar "1")))          ;Create   [savevar]1
  79.       (setvar sysvar (eval cmdnam))                             ;Restore  [savevar]'s value
  80.       (set cmdnam nil)
  81.     ) ;end progn
  82.   ) ;end if
  83. )
  84.  

ranjanarju

  • Guest
Re: How to PURGE All, Except the Named Groups?
« Reply #8 on: September 30, 2016, 01:51:47 AM »
Hello Dan,

Thank you so much for your time and efforts!

The first program (c:myGroupPurge) keeps my named groups. Shows "No unreferenced groups found" notification in command line.

The second program (defun c:PA) opens up so many windows as expected. This needs user interaction a lot. This must be very useful when users preferences need to maintain. But might be little difficult to control, if user wants to run this program on many files as a batch.

BTW, someone might find your codes very much useful even after another 14 years...!!  8-)

Thank You!

BR,
Ranjan

lamarn

  • Swamp Rat
  • Posts: 636
Re: How to PURGE All, Except the Named Groups?
« Reply #9 on: September 30, 2016, 03:46:36 AM »
@dan allen
Quote "..I would think twice about that, I hate the idea of creating items that can't easily be purged with.."

Advice taken.
I am a user of 'Radical purge', addon by *edit*.. Gile (.. Hey.. The Same Guy !! :)
Great tool but it will kills all layers, no possiblity to filter out what i want keep otherwise
« Last Edit: September 30, 2016, 03:52:07 AM by lamarn »
Design is something you should do with both hands. My 2d hand , my 3d hand ..

danallen

  • Guest
Re: How to PURGE All, Except the Named Groups?
« Reply #10 on: September 30, 2016, 11:37:29 AM »
The first program (c:myGroupPurge) keeps my named groups. Shows "No unreferenced groups found" notification in command line.
I think "No unreferenced groups found" means no unnamed groups were purged. Trying running -group > ? to list groups before and after in a drawing where you have created an unnamed group and then deleted the objects to see if it works.

On the PA command, this is the intended output, no user input required. Likely the command prompts have changed and the lisp needs revision:
Code: [Select]
: pa
No un-referenced blocks.
(8) BLOCKs purged.
No un-referenced mline styles.
No un-referenced table styles.
No un-referenced layers.
(59) LAYERs purged.
No un-referenced dimension styles.
(15) DIMSTYLEs purged.
No un-referenced text styles.
(23) STYLEs purged.
No un-referenced linetypes.
(74) LTYPEs purged.
No un-referenced plot styles.
(16) PLOTSTYLESs purged.
No un-referenced visual styles.
No un-referenced materials.

ranjanarju

  • Guest
Re: How to PURGE All, Except the Named Groups?
« Reply #11 on: October 05, 2016, 08:54:17 AM »
Hello Dan,

Sorry for my late reply.

1. Is it possible for you to revise the AutoLisp code for (c:PA)? I myself can not do this. I am not good (at all) in AutoLisp programming :-( !

2. Also what is the command for purging 'Zero-lenth geometry' and 'Empty text objects' like you used in (c:myGroupPurge) as "group" for 'Groups'?

Thank You.

BR,
Ranjan

danallen

  • Guest
Re: How to PURGE All, Except the Named Groups?
« Reply #12 on: October 05, 2016, 11:45:31 AM »
try this simple version, if it has problems, change 1st (setvar "cmdecho" 0) from 0 to 1, then copy/paste command line history to show me where problem is. You'll have to repeat command to get full purge. Since I don't have acad, I can't test how to fix my c:PA command to work for you.

Code - Auto/Visual Lisp: [Select]
  1. (defun c:mypurge ( / )
  2.   (setvar "cmdecho" 0)
  3.   (command "._UNDO" "BEGIN")
  4.   (foreach x (list "ZERO" "EMPTY" "BLOCK" "MLINESTYLE" "TABLESTYLES" "LAYER" "DIMSTYLE" "STYLE" "LTYPE" "PLOTSTYLES" "VISUALSTYLES" "MATERIALS")
  5.     (command "-purge" x "*" "n")
  6.   ) ;_foreach
  7.   (command "-purge" "group" "`*A*" "n")
  8.   (command "._UNDO" "END")
  9.   (setvar "cmdecho" 1)
  10.   (princ)
  11. ) ;_ defun
  12.  

ranjanarju

  • Guest
Re: How to PURGE All, Except the Named Groups?
« Reply #13 on: October 06, 2016, 09:44:02 AM »
Dear Dan,

Here is the images as attached. First image is when- 'cmdecho' is 0. Second and third images are when- 'cmdecho' is 1.

Seems problem is for 'Zero' and 'Empty'

Thank you for your help.

BR,
Ranjan
« Last Edit: October 06, 2016, 09:51:25 AM by ranjanarju »

danallen

  • Guest
Re: How to PURGE All, Except the Named Groups?
« Reply #14 on: October 06, 2016, 10:17:27 AM »
Try changing "zero" to "z" and "empty" to "e". Next time just select the actual text history instead of a screen shot, easier on my eyes & less data on Mark's server

-Dan