Author Topic: [Community Project] Clean Drawing Program  (Read 19899 times)

0 Members and 1 Guest are viewing this topic.

Lee Mac

  • Seagull
  • Posts: 12913
  • London, England
[Community Project] Clean Drawing Program
« on: December 09, 2012, 11:00:01 AM »
Inspired by this recent thread, I thought it would be beneficial to the community for us to construct a general program to clean drawings which are bloated by hundreds or thousands of what may be deemed as unnecessary objects, or common causes of drawings with ostensibly inexplicably huge file sizes.

To begin, I have put together the following program:

Code - Auto/Visual Lisp: [Select]
  1. (defun c:cleandwg ( / *error* _effectivename bkc cmd doc ext flg lck lst lyc )
  2.  
  3.     ;; Error Handler
  4.     (defun *error* ( m )
  5.         ;; Relock Layers
  6.         (foreach lay lck
  7.             (if (not (vlax-erased-p lay))
  8.                 (vla-put-lock lay :vlax-true)
  9.             )
  10.         )
  11.         ;; Reset CMDECHO
  12.         (if (= 'int (type cmd))
  13.             (setvar 'cmdecho cmd)
  14.         )
  15.         ;; Print critical errors
  16.         (if (not (wcmatch (strcase m t) "*break,*cancel*,*exit*"))
  17.             (princ (strcat "\nError: " m))
  18.         )
  19.         (princ)
  20.     )
  21.  
  22.     ;; Function to return block effective name
  23.     (defun _effectivename ( obj )
  24.         (if (vlax-property-available-p obj 'effectivename)
  25.             (vla-get-effectivename obj)
  26.             (vla-get-name obj)
  27.         )
  28.     )
  29.  
  30.           lyc (vla-get-layers doc)
  31.           bkc (vla-get-blocks doc)
  32.           cmd (getvar 'cmdecho)
  33.     )
  34.     ;; Unlock all layers
  35.     (vlax-for lay lyc
  36.         (if (= :vlax-true (vla-get-lock lay))
  37.             (progn
  38.                 (setq lck (cons lay lck))
  39.                 (vla-put-lock lay :vlax-false)
  40.             )
  41.         )
  42.     )
  43.  
  44.     ;; Remove Anonymous Groups
  45.     (vlax-for grp (vla-get-groups doc)
  46.         (if (wcmatch (vla-get-name grp) "`**")
  47.             (vl-catch-all-apply 'vla-delete (list grp))
  48.         )
  49.     )
  50.  
  51.     ;; Detect empty block definitions
  52.     (vlax-for blk bkc
  53.         (if
  54.             (and
  55.                 (= :vlax-false (vla-get-isxref blk))
  56.                 (= :vlax-false (vla-get-islayout blk))
  57.             )
  58.             (progn
  59.                 (vlax-for obj blk (setq flg t))
  60.                 (if (null flg)
  61.                     (setq lst (cons (strcase (vla-get-name blk)) lst))
  62.                 )
  63.                 (setq flg nil)
  64.             )
  65.         )
  66.     )
  67.     ;; Remove all references of empty definitions
  68.     (vlax-for blk bkc
  69.         (vlax-for obj blk
  70.             (if
  71.                 (and
  72.                     (= "AcDbBlockReference" (vla-get-objectname obj))
  73.                     (member (strcase (_effectivename obj)) lst)
  74.                 )
  75.                 (vl-catch-all-apply 'vla-delete (list obj))
  76.             )
  77.         )
  78.     )
  79.     ;; Remove empty block definitions
  80.     (foreach blk lst
  81.         (vl-catch-all-apply 'vla-delete (list (vla-item bkc blk)))
  82.     )
  83.  
  84.     ;; Remove Layer Property & Group Filters
  85.     (if
  86.         (and
  87.             (= :vlax-true (vla-get-hasextensiondictionary lyc))
  88.             (setq ext (vla-getextensiondictionary lyc))
  89.         )
  90.         (foreach dic '("ACLYDICTIONARY" "ACAD_LAYERFILTERS")
  91.              (vl-catch-all-apply 'vla-remove (list ext dic))
  92.         )
  93.     )  
  94.  
  95.     ;; Turn off command echo
  96.     (setvar 'cmdecho 0)
  97.    
  98.     ;; Remove unused scales
  99.     (command "_.-scalelistedit" "_D" "*" "_E")
  100.  
  101.     ;; Purge All
  102.     (repeat 3 (command "_.-purge" "_A" "*" "_N"))
  103.  
  104.     ;; Reset command echo
  105.     (setvar 'cmdecho cmd)
  106.  
  107.     ;; Relock Layers
  108.     (foreach lay lck
  109.         (if (not (vlax-erased-p lay))
  110.             (vla-put-lock lay :vlax-true)
  111.         )
  112.     )
  113.  
  114.     ;; Exit cleanly
  115.     (princ)
  116. )

As indicated by the thread title, I welcome others to contribute & improve this program, so that we may create a robust and all-purpose drawing cleaner for the community to use.

Ideas & suggestions are welcome!


Latest Version attached:
« Last Edit: December 10, 2012, 09:31:00 AM by Lee Mac »

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: [Community Project] Clean Drawing Program
« Reply #1 on: December 09, 2012, 11:05:39 AM »
Good idea Lee.
Off to play tennis but will take a look this afternoon.  :-)
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.

Lee Mac

  • Seagull
  • Posts: 12913
  • London, England
Re: [Community Project] Clean Drawing Program
« Reply #2 on: December 09, 2012, 11:07:51 AM »
Good idea Lee.
Off to play tennis but will take a look this afternoon.  :-)

Thanks Alan - have a great game!  8-)

Tharwat

  • Swamp Rat
  • Posts: 710
  • Hypersensitive
Re: [Community Project] Clean Drawing Program
« Reply #3 on: December 09, 2012, 12:13:20 PM »
It looks very useful tool and clear to read .

Is not it good idea to use vla-purgeall function instead of the command call ?

Thanks for your great efforts .  :-)

Lee Mac

  • Seagull
  • Posts: 12913
  • London, England
Re: [Community Project] Clean Drawing Program
« Reply #4 on: December 09, 2012, 01:09:20 PM »
It looks very useful tool and clear to read .

Thanks for your great efforts .  :-)

Thank you Tharwat  :-)

Is not it good idea to use vla-purgeall function instead of the command call ?

Although the PurgeAll method may be generally faster than the command call, it will not purge Multileader Styles, Empty Text objects, Zero-Length geometry, etc. etc.

roy_043

  • Water Moccasin
  • Posts: 1895
  • BricsCAD 18
Re: [Community Project] Clean Drawing Program
« Reply #5 on: December 09, 2012, 05:38:36 PM »
Some quick thoughts:

Your line 60 etc:
Code - Auto/Visual Lisp: [Select]
  1. (vlax-for obj blk (setq flg t))
Could become
Code - Auto/Visual Lisp: [Select]
  1. (if (zerop (vla-get-count blk)) ...

Why (only) remove anon groups? They can be as useful/useless as named groups.

Redundant groups:
Code - Auto/Visual Lisp: [Select]
  1. ;;; Purpose:       Get groups containing 1 or zero objects.
  2. ;;; Arguments:     groupObjectList - list of group objects
  3. ;;; Return value:  List of group objects or nil.
  4. (defun _GetRedundantGroups (groupObjectList)
  5.   (vl-remove-if
  6.     '(lambda (a) (> (vla-get-count a) 1))
  7.     groupObjectList
  8.   )
  9. )

Some portions of the program would have to be run repeatedly. Block defs can become empty if block refs of empty blocks have been deleted. Modularize?

Perhaps (also) start with purging. That way the program will potentially have to analyse/do less.

kruuger

  • Swamp Rat
  • Posts: 633
Re: [Community Project] Clean Drawing Program
« Reply #6 on: December 09, 2012, 05:46:51 PM »
really great idea Lee. my suggestions are:
- create dcl (on the fly) to select what to delete/clean and what not
- maybe multilanguage option like
Code: [Select]
(nth *l (list "Utwórz blok anonimowy\n" "Make annonymous block\n"))- maybe option to delete some dictionary (only for experts)

kruuger

PrinceLISPalot

  • Newt
  • Posts: 35
  • perfectionist trapped inside the mind of an idiot.
Re: [Community Project] Clean Drawing Program
« Reply #7 on: December 09, 2012, 07:10:19 PM »
You might like to consider issues coming from older Legacy drawings, or other cad apps that generate .dwg. An example of this is empty text strings. These used to be created in AutoCAD by editing some TEXT/MTEXT and deleting all the text in the dialogue, which resulted in an empty text string. This was fixed in later versions of AutoCAD, but is still possible in BricsCAD, and your likely to strike if your working on older drawings. Created the following routine to deal with.

Code - Auto/Visual Lisp: [Select]
  1. ; ****************************NOTXT.LSP*********************************
  2.  
  3. ;  Deletes empty TEXT strings from your drawing
  4. ;  Rev A
  5. ;  12/06/93
  6. ;  Jason Bourhill
  7. ; **********************************************************************
  8.  
  9. ;  Rev B
  10. ;  Updated to check for empty MTEXT strings as well.
  11. ;  10/09/99
  12. ;  Jason Bourhill
  13. ; **********************************************************************
  14.  
  15. ;  Rev C
  16. ;  Change string checking part to use wcmatch. quicker?
  17. ;  30/11/99
  18. ;  Jason Bourhill
  19. ; **********************************************************************
  20.  
  21. (defun C:NOTXT ( / searchtxt sset ans nfix)
  22.  
  23. ;-------------------------------SEARCHTXT-------------------------------
  24. (defun searchtxt (sset rstr / num txtfnd entnum ent strcomp stn)
  25. (setq num 0 txtfnd 0)
  26.  (princ "\nWorking ")
  27.  (repeat (sslength sset)
  28.         (setq entnum (ssname sset num))
  29.          (setq ent (entget entnum))
  30.          (setq stn (strlen (cdr (assoc 1 ent))))
  31.           (if (= 0 stn)(setq strcomp "")(setq strcomp " "))
  32. ;          (if (/= 0 stn) (repeat stn (setq strcomp (strcat strcomp " "))))
  33. ;           (cond ((= strcomp (cdr (assoc 1 ent)))
  34.             (cond ((wcmatch (cdr (assoc 1 ent)) strcomp)
  35.                         (if rstr (progn
  36.                                         (setq ent
  37.                                                 (subst (cons 1 rstr)
  38.                                                        (assoc 1 ent)
  39.                                                         ent
  40.                                                 )
  41.                                         )
  42.                                         (entmod ent)
  43.                                         (entupd entnum)
  44.                                         (setq txtfnd (1+ txtfnd))
  45.                                  )
  46.                                  (progn (entdel entnum) (setq txtfnd (1+ txtfnd)))
  47.                         )))
  48.         (princ ">")
  49.         (setq num (1+ num))
  50.  )
  51. txtfnd
  52. )
  53.  
  54. ;-------------------------------MAIN PROG-------------------------------
  55.  
  56. (princ "\nSearching For Text Please Wait")
  57. (setq sset
  58.  (ssget "x" (list(cons -4 "<or")
  59.                     (cons 0 "TEXT")
  60.                     (cons 0 "MTEXT")
  61.                  (cons -4 "or>")
  62.             )
  63.   )
  64.  )
  65.  (if sset (progn
  66.                 (initget "Delete Replace")
  67.                 (setq ans (getkword "\nDo you want to Delete or Replace empty text strings <Delete/Replace>: "))
  68.                 (cond ((= ans "Delete")(setq rstr nil))
  69.                       ((= ans "Replace")(setq rstr (getstring T "\nEnter replacement string: ")))
  70.                 )
  71.                 (setq nfix (searchtxt sset rstr))
  72.                 (princ (strcat "\nFound and Fixed " (itoa nfix) " Empty text strings"))
  73.          )
  74.          (princ "\nNo TEXT found in Drawing Data Base")
  75.  )
  76. )
  77.  

I've also seen bad implementation of valid objects on drawings. such as drawing a property boundary using the letter "X" instead of using an appropriate linetype. Maybe your routine needs to Audit first, provide the user with a list of potential issues, and allow them to select which ones they wish to act on.

I haven't checked, but you used to have issues with purging empty layers if you had set viewport related properties from within a paperspace viewport.


Lee Mac

  • Seagull
  • Posts: 12913
  • London, England
Re: [Community Project] Clean Drawing Program
« Reply #8 on: December 10, 2012, 08:34:36 AM »
Your line 60 etc:
Code - Auto/Visual Lisp: [Select]
  1. (vlax-for obj blk (setq flg t))
Could become
Code - Auto/Visual Lisp: [Select]
  1. (if (zerop (vla-get-count blk)) ...

Good catch roy - noted.

Why (only) remove anon groups? They can be as useful/useless as named groups.

My logic was that anonymous groups are more likely to be automatically generated by another program, or inadvertently generated by an unsuspecting user; whereas, named groups are more likely to have been created purposefully.

Redundant groups...

Good idea!

Some portions of the program would have to be run repeatedly. Block defs can become empty if block refs of empty blocks have been deleted. Modularize?

Another great idea, I had overlooked the case of nested empty block definitions whose reference is the only object within the parent definition - an unlikely scenario, but certainly should be included for completeness.

Perhaps (also) start with purging. That way the program will potentially have to analyse/do less.

Good shout - I'll change this also.

really great idea Lee.

Thanks Kruuger!  :-)

- create dcl (on the fly) to select what to delete/clean and what not

Nice idea Kruuger, I shall certainly look to implement this  :-)

- maybe multilanguage option like
Code: [Select]
(nth *l (list "Utwórz blok anonimowy\n" "Make annonymous block\n"))

Since this will be a general program for all to use, multilanguage support will be a necessity - though, I'll certainly need your's (& Google's) help with this!

- maybe option to delete some dictionary (only for experts)

The program currently removes the Layer Filter dictionaries, however, I was unsure as to which other dictionaries are unnecessary and can be safely removed in general - perhaps the AEC* dictionaries when opening a drawing in standard AutoCAD?

You might like to consider issues coming from older Legacy drawings, or other cad apps that generate .dwg. An example of this is empty text strings. These used to be created in AutoCAD by editing some TEXT/MTEXT and deleting all the text in the dialogue, which resulted in an empty text string. This was fixed in later versions of AutoCAD, but is still possible in BricsCAD, and your likely to strike if your working on older drawings.

Newer versions of AutoCAD include the option to purge Empty Text Objects using the in-built Purge command, though, I agree that it would be useful to create a separate function to remove these objects to account for versions in which the option is not present - thanks!

I've also seen bad implementation of valid objects on drawings. such as drawing a property boundary using the letter "X" instead of using an appropriate linetype.

These types of bad drafting practices will most probably be difficult to detect, and furthermore, many users may hold differing opinions over what is considered a good or bad drafting practice; for this program, I was aiming more to remove general redundancies which may be applicable across all industries of drafting.

Maybe your routine needs to Audit first, provide the user with a list of potential issues, and allow them to select which ones they wish to act on.

A great idea, but I see this being potentially difficult to implement, since some operations performed by the program (such as the Purge All operation) provide no programmatic output as to the objects available to be purged by the program.

I haven't checked, but you used to have issues with purging empty layers if you had set viewport related properties from within a paperspace viewport.

Aah, viewport overrides! Another can of worms!  :lol:

hermanm

  • Guest
Re: [Community Project] Clean Drawing Program
« Reply #9 on: December 10, 2012, 09:05:00 AM »
Quote
My logic was that anonymous groups are more likely to be automatically generated by another program, or inadvertently generated by an unsuspecting user;

I have written programs which generate "anonymous" groups, by design.
I use also a third party application which has a use for one entity groups, because it associates additional information with them.

Suggest (as others have) that you provide options as to what gets "cleaned up."

Lee Mac

  • Seagull
  • Posts: 12913
  • London, England
Re: [Community Project] Clean Drawing Program
« Reply #10 on: December 10, 2012, 09:12:25 AM »
Quote
My logic was that anonymous groups are more likely to be automatically generated by another program, or inadvertently generated by an unsuspecting user;

I have written programs which generate "anonymous" groups, by design.
I use also a third party application which has a use for one entity groups, because it associates additional information with them.

Suggest (as others have) that you provide options as to what gets "cleaned up."

Agreed  :-)

Lee Mac

  • Seagull
  • Posts: 12913
  • London, England
Re: [Community Project] Clean Drawing Program
« Reply #11 on: December 10, 2012, 09:29:57 AM »
I have attached an updated version of the program to the first post of the thread, incorporating a number of comments:
  • Program has been 'modularised' into separate functions, with documentation for each function.
  • Program now accounts for nested empty block definitions.
  • 'Purge All' operation now moved to start of program.
  • Groups with fewer than 2 objects now removed.
Still to do:
  • Write program description for code header
  • Create dialog to allow user to choose items to be 'cleaned'
  • Create function to remove empty Text & MText objects
  • Multilanguage support for dialog
« Last Edit: December 10, 2012, 09:37:33 AM by Lee Mac »

Patrick_35

  • Guest
Re: [Community Project] Clean Drawing Program
« Reply #12 on: December 10, 2012, 11:05:47 AM »
Hi

If you want, I did kind of program.
You can pick ideas here
It remains only to translate it into English

@+

Lee Mac

  • Seagull
  • Posts: 12913
  • London, England
Re: [Community Project] Clean Drawing Program
« Reply #13 on: December 10, 2012, 11:13:11 AM »
If you want, I did kind of program.
You can pick ideas here
It remains only to translate it into English

Wow - that is a very thorough and comprehensive program Patrick - thank you for sharing your efforts  :-)

Patrick_35

  • Guest
Re: [Community Project] Clean Drawing Program
« Reply #14 on: December 10, 2012, 11:27:56 AM »
Yes, and all the others are too detailed (on the same blog)
The only problem is that I have not mastered enough English to do the translation.
And as I have online, I was wondering what software did you animate your pictures to better explain your lisps

@+

Lee Mac

  • Seagull
  • Posts: 12913
  • London, England
Re: [Community Project] Clean Drawing Program
« Reply #15 on: December 10, 2012, 11:36:03 AM »
The only problem is that I have not mastered enough English to do the translation.

Your English is far better than my French! (and I studied French for 7 years  :lmao: )

And as I have online, I was wondering what software did you animate your pictures to better explain your lisps

I use Camtasia  :-)


Krushert

  • Seagull
  • Posts: 13679
  • FREE BEER Tomorrow!!
Re: [Community Project] Clean Drawing Program
« Reply #16 on: December 10, 2012, 11:45:40 AM »
Lee, or whomever: 

With a quick glance, I did not see where regapps were getting purged.  IN the past I have had to specifically call the purging of those but with the new fancy code it might be already taken care of.

**EDIT**
maybe throw in a file save?   :| :|
« Last Edit: December 10, 2012, 12:08:14 PM by Krushert »
I + XI = X is true ...  ... if you change your perspective.

I no longer CAD or Model, I just hang out here picking up the empties beer cans

Lee Mac

  • Seagull
  • Posts: 12913
  • London, England
Re: [Community Project] Clean Drawing Program
« Reply #17 on: December 10, 2012, 12:17:44 PM »
With a quick glance, I did not see where regapps were getting purged.  IN the past I have had to specifically call the purging of those but with the new fancy code it might be already taken care of.

Currently, I believe the regapps are being purged by the call to the Purge command with the 'All' option selected; however, you may be correct in that this option does not include regapps.

However, I intend to write separate functions for removing these items when incorporating the ability for the user to select the groups of items to be 'cleaned', rather than using the in-built Purge command.

**EDIT**
maybe throw in a file save?   :| :|

Risky should something important be accidentally removed...  :|

VovKa

  • Water Moccasin
  • Posts: 1629
  • Ukraine

kruuger

  • Swamp Rat
  • Posts: 633
Re: [Community Project] Clean Drawing Program
« Reply #19 on: December 10, 2012, 12:53:53 PM »
Since this will be a general program for all to use, multilanguage support will be a necessity - though, I'll certainly need your's (& Google's) help with this!
of course will be happy to help with polish translation.

i even wrote (community program) some translator. if someone is interesting please see attached sample file:
- makeblock.lsp - program with comments to translate
- tooltranslator.lsp
- library function - cadpack

more here -> http://forum.cad.pl/cadpl-pack-v1-lsp-t78161.html
kruuger

cmwade77

  • Swamp Rat
  • Posts: 1443
Re: [Community Project] Clean Drawing Program
« Reply #20 on: December 10, 2012, 04:08:28 PM »
I vote for removal of AEC Dictionaries in regular AutoCAD. If we could automate exploding AEC objects to blocks in regular AutoCAD, that would be even better.

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: [Community Project] Clean Drawing Program
« Reply #21 on: December 10, 2012, 04:11:34 PM »
Frustrating thread is frustrating. Subtitle: <I believe> there's sooooo much I could add to this thread, but I cannot under my current terms of employ. Having said that, there is tons of talent herein so my lack of participation is only a consequence for me. Kudos ya'll.
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

Pepe

  • Newt
  • Posts: 87
Re: [Community Project] Clean Drawing Program
« Reply #22 on: December 10, 2012, 06:19:15 PM »
Hi Lee Mac...

Sorry to say that '(command "_.-scalelistedit" "_D" "*" "_E")' doesn't work (in my version, at least...). It spits something like (traslating...) 'The scale "*" does not exists.'

I've tried via Dictionaries. I've used sometimes simply '(dictremove (namedobjdict) "ACAD_SCALELIST")' and file size decreased quite a lot. But, I've gone a little bit further, and I've tried this now:

Code - Auto/Visual Lisp: [Select]
  1. (setq scls (dictsearch (namedobjdict) "ACAD_SCALELIST"))
  2. (foreach n scls
  3. (if (= (car n) 350) (setq lscl (cons (cadr n) lscl)))
  4. )
  5. (mapcar 'entdel lscl)
  6.  

'SCALE' entities disappear, but if you call the command "_scalelistedit", scales are still there... :-o... :ugly:

Well. Curious, isn't it?

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: [Community Project] Clean Drawing Program
« Reply #23 on: December 11, 2012, 08:21:57 AM »
I've tried via Dictionaries. I've used sometimes simply '(dictremove (namedobjdict) "ACAD_SCALELIST")' and file size decreased quite a lot. But, I've gone a little bit further, and I've tried this now:
I'd be extremely careful about simply erasing the scales dictionary or any one scale from that. And you will keep on seeing them in the scales list because ACad tries to fix this possible crash situation of having no scales to display.

You need at least one scale left in the DWG. And you actually shouldn't delete scales which has been applied to something - that tends to crash acad. Erasing all scales' dictionaries do not test for scales being applied before deleting them. Anyhow thus use the Reset option in the scalelistedit command - that's a lot safer. Otherwise you'll need to step through everything in the DWG to see which scales can be erased.
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

Pepe

  • Newt
  • Posts: 87
Re: [Community Project] Clean Drawing Program
« Reply #24 on: December 11, 2012, 12:10:49 PM »
I've tried via Dictionaries. I've used sometimes simply '(dictremove (namedobjdict) "ACAD_SCALELIST")' and file size decreased quite a lot. But, I've gone a little bit further, and I've tried this now:
I'd be extremely careful about simply erasing the scales dictionary or any one scale from that. And you will keep on seeing them in the scales list because ACad tries to fix this possible crash situation of having no scales to display.

You need at least one scale left in the DWG. And you actually shouldn't delete scales which has been applied to something - that tends to crash acad. Erasing all scales' dictionaries do not test for scales being applied before deleting them. Anyhow thus use the Reset option in the scalelistedit command - that's a lot safer. Otherwise you'll need to step through everything in the DWG to see which scales can be erased.

Irneb, thank you very much for your advices!

My problems with scales came from a DWG file I recieved from a partner, used as a XREF in a project, in which I detected 6832 different scales (I suppose inherited from other drawings). Every drawing with that XREF attached took about 3 minutes to open ( :realmad: !!!). After I detected the problem, I just deleted the dictionary (the way I've posted), and saved... and it worked: default scales list were re-built as the file was opened without any other problem, 300kb saved in HD and few seconds to open drawings (sometimes, I go breathless, I swear  :doa:).

Just another curiosity 'to whom it may concern': I've used "scalelistedit" (NOT "-scalelistedit") against those 6832 scales, and it appears an error message saying "Matrix index out of range"... :?  Of course, "-scalelistedit", option "Reset", works properly.

Regards.

Jeff H

  • Needs a day job
  • Posts: 6150
Re: [Community Project] Clean Drawing Program
« Reply #25 on: December 11, 2012, 12:28:03 PM »
Do you guys want to match it up against a .NET version?
 
I was just going to Wblock the entire drawing then save it back to same file.

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: [Community Project] Clean Drawing Program
« Reply #26 on: December 12, 2012, 01:33:06 AM »
My problems with scales came from a DWG file I recieved from a partner, used as a XREF in a project, in which I detected 6832 different scales (I suppose inherited from other drawings). Every drawing with that XREF attached took about 3 minutes to open ( :realmad: !!!).
This is why I'm saying ADesk are idiots with the scales list. They don't have a clue about what a scale-name is supposed to be. They should never have written the extra code to affix that _XREF to the scale's name when loaded from a xref - there's no benefit to it and it causes this exponential growth in scales.

Just another curiosity 'to whom it may concern': I've used "scalelistedit" (NOT "-scalelistedit") against those 6832 scales, and it appears an error message saying "Matrix index out of range"... :?  Of course, "-scalelistedit", option "Reset", works properly.
That's because it's trying to load all 6832 names into the list for display in the dialog. And that listbox has a maximum length.

BTW, older acads popped up with that message due to the same list being displayed in the status bar - so you got that message simply by opening a dwg. Newer acads have an option to "Hide XRef Scales" so these are then not displayed in that dialog / status bar list. Though it only solves the issue of this error message, the _XREF scales are still there causing the usual slowdowns, they're just "out-of-sight-out-of-mind".
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

Pepe

  • Newt
  • Posts: 87
Re: [Community Project] Clean Drawing Program
« Reply #27 on: December 12, 2012, 03:33:19 AM »
Irneb, thank you very much, again, for your advices...  8-)!

In Spain, we have a funny and rough way to say that someone has been taught. In direct translation from spanish... he or she has been dis-donkeyed (yes, from donkey, that also means in spanish, dunce). So, I've been dis-donkeyed a lot about dealing with scales!

Regards.

Peter2

  • Swamp Rat
  • Posts: 650
Re: [Community Project] Clean Drawing Program
« Reply #28 on: March 23, 2015, 05:11:25 PM »
AutoCAD >= 2012 has a standard-purge-command for "groups". Is the code here different from the "internal-purge"?
Peter

AutoCAD Map 3D 2023 German (so some technical terms will be badly retranslated to English)
BricsCAD V23

ur_naz

  • Newt
  • Posts: 68
  • Made in Ukraine
Re: [Community Project] Clean Drawing Program
« Reply #29 on: March 25, 2015, 02:17:09 PM »
Think it is not a good idea to put all purge routines in single command because lisp is too slow even if compiled. On huge drawings it will freeze or even crash autocad

ronjonp

  • Needs a day job
  • Posts: 7527
Re: [Community Project] Clean Drawing Program
« Reply #30 on: March 25, 2015, 02:50:56 PM »
Think it is not a good idea to put all purge routines in single command because lisp is too slow even if compiled. On huge drawings it will freeze or even crash autocad


If you just use a simple wblock it's not slow at all.

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: [Community Project] Clean Drawing Program
« Reply #31 on: March 25, 2015, 03:02:02 PM »
There are a number of precautions one need take when using wblock * (or -ExportToAutoCAD yada) to ensure things don't FUBAR on you. This includes making sure the WCS is active in all paper-space resident viewports and model-space before performing the export. Failure to ensure will sometimes produce wack-a-doodle, undersirable results ...

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

ronjonp

  • Needs a day job
  • Posts: 7527
Re: [Community Project] Clean Drawing Program
« Reply #32 on: March 25, 2015, 04:33:06 PM »
There are a number of precautions one need take when using wblock * (or -ExportToAutoCAD yada) to ensure things don't FUBAR on you. This includes making sure the WCS is active in all paper-space resident viewports and model-space before performing the export. Failure to ensure will sometimes produce wack-a-doodle, undersirable results ...

/Red Sector A


Funny you say that .. noticed that many moons back when I wblocked some files with twisted UCS set to view and shite was out in space :)


Fixed it with:
Code - Auto/Visual Lisp: [Select]
  1. (setvar 'tilemode 1)
  2. (command "_.ucs" "_World")
  3. (command "._-wblock" file "*")

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: [Community Project] Clean Drawing Program
« Reply #33 on: March 25, 2015, 05:02:25 PM »
Yep ... and post processing is required to fix other wblock | exporttoautocad | yada aberrations. For example: wipeouts.
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

HasanCAD

  • Swamp Rat
  • Posts: 1421
Re: [Community Project] Clean Drawing Program
« Reply #34 on: March 29, 2015, 06:25:41 AM »
I belive this part of code ahould be considered (copied from another thread)
Code: [Select]
  ((lambda (dict)
     (if (dictsearch (namedobjdict) dict)
       (progn
(dictremove (namedobjdict) dict)
(princ "\nDGN Line Styles Removed")
       )
       (princ "\nDGN Line Styles not found")
     )
   )
    "ACAD_DGNLINESTYLECOMP"
  ) ;Delete DGN

HasanCAD

  • Swamp Rat
  • Posts: 1421
Re: [Community Project] Clean Drawing Program
« Reply #35 on: March 29, 2015, 06:28:14 AM »
This too
Code: [Select]
(vl-load-com)
(setq doc (vla-get-ActiveDocument (setq *acad (vlax-get-Acad-Object))))

(progn
  (vlax-for layer (vla-get-Layers doc)
    (vl-catch-all-apply
      'vla-Remove
      (list (vla-GetExtensionDictionary layer)
    "ADSK_XREC_LAYER_RECONCILED"
      )
    ) ;(vl-catch-all-apply 'vla-Delete (list (vla-GetExtensionDictionary layer)))
  )
  (princ "\nRECONCILED Layers Removed")
) ; Delete reconciled filter

ur_naz

  • Newt
  • Posts: 68
  • Made in Ukraine
Re: [Community Project] Clean Drawing Program
« Reply #36 on: March 29, 2015, 12:48:36 PM »
This code deletes all non-acad dictionaries.
Code - Auto/Visual Lisp: [Select]
  1.             (or (/= 3 (car &))
  2.                 (wcmatch (strcase (cdr &)) "ACAD_*")
  3.                 (wcmatch (cdr &) "AcDb*")
  4.                 (dictremove (namedobjdict) (cdr &))
  5.             )
  6.           )

Peter2

  • Swamp Rat
  • Posts: 650
Re: [Community Project] Clean Drawing Program
« Reply #37 on: March 29, 2015, 12:59:36 PM »
This code deletes all non-acad dictionaries. ...
I would say it is important to make differences between "Purging" (not used) things and deleting things.
Peter

AutoCAD Map 3D 2023 German (so some technical terms will be badly retranslated to English)
BricsCAD V23

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: [Community Project] Clean Drawing Program
« Reply #38 on: March 29, 2015, 02:44:02 PM »
This code deletes all non-acad dictionaries...

I HIGHLY advise against the sledge hammer approach. For example, if you delete "Ks_ShapeRefDictionary" you are in for a world of pain and quite possibly a lynching. Not all non acad dictionaries are evil and many of them are essential. In the case of "Ks_ShapeRefDictionary" it's essential for proper loading/display even if you are a mere consumer of a model (rather than the owner/maintainer).

tl:dr: Don't be the guy who knows enough to be dangerous.
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

ur_naz

  • Newt
  • Posts: 68
  • Made in Ukraine
Re: [Community Project] Clean Drawing Program
« Reply #39 on: March 29, 2015, 03:54:16 PM »
This code deletes all non-acad dictionaries...

I HIGHLY advise against the sledge hammer approach. For example, if you delete "Ks_ShapeRefDictionary" you are in for a world of pain and quite possibly a lynching. Not all non acad dictionaries are evil and many of them are essential. In the case of "Ks_ShapeRefDictionary" it's essential for proper loading/display even if you are a mere consumer of a model (rather than the owner/maintainer).

tl:dr: Don't be the guy who knows enough to be dangerous.
Sometimes we have no choise to be or not to be a chicken. Life is also dangerous, it cause death.
At least noone said you cannot add filter mask for exceptions.

I had purged drawings deleting dictionaries 6mb->250kb, 150mb->20mb. Sure, all that MBs was very useful.

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: [Community Project] Clean Drawing Program
« Reply #40 on: March 29, 2015, 04:05:13 PM »
This isn't about being a chicken. It's about judicious use of technique. As for espousing the merits of cleaning files you're preaching to the choir. :)
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

ur_naz

  • Newt
  • Posts: 68
  • Made in Ukraine
Re: [Community Project] Clean Drawing Program
« Reply #41 on: March 29, 2015, 05:20:40 PM »
This isn't about being a chicken. It's about judicious use of technique. As for espousing the merits of cleaning files you're preaching to the choir. :)
Some drawings like a boat floating from hands to hands. When drawing I recieve freezes my autocad I extremely need to clean all damn garbage to do my work.
I also need s/pline optimization including processing in blocks or/and to restore exploded text from lines and arcs. And of course I need to explode all proxy. So don't say me about danger. I have no idea on how to be careful  :-P

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: [Community Project] Clean Drawing Program
« Reply #42 on: March 29, 2015, 05:27:09 PM »
< .. >  And of course I need to explode all proxy. So don't say me about danger. I have no idea on how to be careful  :P




With all due respect .. In this case you will deserve all the pain that will be heaped upon your head.

kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • Superior Stupidity at its best
Re: [Community Project] Clean Drawing Program
« Reply #43 on: March 29, 2015, 05:36:18 PM »
I applaud the use of these techniques to clean up bloat in drawings. My average drawing is under 200k and I regularly purge everything. If it isn't in needed "right now", it gets purged ... I also flatten and overkill often just to make dang sure there isn't anything floating about in space or overlapping unnecessarily.

I only do 2d drawings so flattening it doesn't matter .. and I don't share with others so being aggressive cleaning it up doesn't matter either.

Make sure to clean up xrefs that aren't loading and images that are no longer being loaded.
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Find me on https://parler.com @kblackie

ur_naz

  • Newt
  • Posts: 68
  • Made in Ukraine
Re: [Community Project] Clean Drawing Program
« Reply #44 on: March 29, 2015, 05:46:19 PM »
Make sure to clean up xrefs that aren't loading and images that are no longer being loaded.

This is under construction for me
Below is VVA's version

Code - Auto/Visual Lisp: [Select]
  1. (defun C:DUXI (/ *error* file tmpObj retxref retimg retimgobj pat)
  2. ;;; Delete Unfound Xref and Image
  3. ;;; posted VVA http://forum.dwg.ru/showthread.php?t=36574
  4.   (defun *error* (msg)
  5.     (princ msg)
  6.     (mip:layer-status-restore)
  7.     (princ)
  8.   ) ;_ end of defun
  9.   (defun mip:layer-status-restore ()
  10.     (foreach item *MIP_LAYER_LST*
  11.       (if (not (vlax-erased-p (car item)))
  12.         (vl-catch-all-apply
  13.           '(lambda ()
  14.              (vla-put-lock
  15.                (car item)
  16.                (cdr (assoc "lock" (cdr item)))
  17.              ) ;_ end of vla-put-lock
  18.              (vla-put-freeze
  19.                (car item)
  20.                (cdr (assoc "freeze" (cdr item)))
  21.              ) ;_ end of vla-put-freeze
  22.            ) ;_ end of lambda
  23.         ) ;_ end of vl-catch-all-apply
  24.       ) ;_ end of if
  25.     ) ;_ end of foreach
  26.     (setq *MIP_LAYER_LST* nil)
  27.   ) ;_ end of defun
  28.  
  29.   (defun mip:layer-status-save ()
  30.     (setq *MIP_LAYER_LST* nil)
  31.     (vlax-for item
  32.                    (vla-get-layers
  33.                      (vla-get-activedocument (vlax-get-acad-object))
  34.                    ) ;_ end of vla-get-layers
  35.       (setq *MIP_LAYER_LST*
  36.              (cons (list item
  37.                          (cons "freeze" (vla-get-freeze item))
  38.                          (cons "lock" (vla-get-lock item))
  39.                    ) ;_ end of cons
  40.                    *MIP_LAYER_LST*
  41.              ) ;_ end of cons
  42.       ) ;_ end of setq
  43.       (vla-put-lock item :vlax-false)
  44.       (if (= (vla-get-freeze item) :vlax-true)
  45.         (vl-catch-all-apply
  46.           '(lambda () (vla-put-freeze item :vlax-false))
  47.         ) ;_ end of vl-catch-all-apply
  48.       ) ;_ end of if
  49.     ) ;_ end of vlax-for
  50.   ) ;_ end of defun
  51.  
  52.   (defun DetachImage (ImgName)
  53.     (vl-catch-all-apply
  54.       '(lambda ()
  55.          (vla-delete
  56.            (vla-item
  57.              (vla-item
  58.                (vla-get-dictionaries
  59.                  (vla-get-activedocument (vlax-get-acad-object))
  60.                ) ;_ end of vla-get-dictionaries
  61.                "ACAD_IMAGE_DICT"
  62.              ) ;_ end of vla-Item
  63.              ImgName
  64.            ) ;_ end of vla-Item
  65.          ) ;_ end of vla-Delete
  66.        ) ;_ end of lambda
  67.     ) ;_ end of vl-catch-all-apply
  68.   ) ;_ end of defun
  69.   (setq pat '("AcDbRasterImage" "AcDbBlockReference"))
  70.                   (vla-get-activedocument (vlax-get-acad-object))
  71.                 ) ;_ end of vla-get-Layouts
  72.     (princ "\nWait... the search for rasters and xrefs in ")(princ (vla-get-name lay))
  73.     (vlax-for item (vla-get-block lay)
  74.       (cond
  75.         ((and
  76.            (member (vla-get-objectname item) pat)
  77.            (eq (vla-get-objectname item) "AcDbBlockReference")
  78.            (vlax-property-available-p item 'Path)
  79.            (setq file (vla-get-path item))
  80.            (setq tmpObj
  81.                   (vla-item
  82.                     (vla-get-blocks
  83.                       (vla-get-activedocument (vlax-get-acad-object))
  84.                     ) ;_ end of vla-get-Blocks
  85.                     (vla-get-name item)
  86.                   ) ;_ end of vla-Item
  87.            ) ;_ end of setq
  88.          ) ;_ end of and
  89.          (if
  90.            (not (or (findfile file)
  91.                     (findfile (strcat (vl-filename-base file)
  92.                                       (vl-filename-extension file)
  93.                               ) ;_ end of strcat
  94.                     ) ;_ end of findfile
  95.                 ) ;_ end of or
  96.            ) ;_ end of not
  97.            (setq retxref (cons tmpobj retxref))
  98.          ) ;_ end of if
  99.         )
  100.         ((and
  101.            (member (vla-get-objectname item) pat)
  102.            (eq (vla-get-objectname item) "AcDbRasterImage")
  103.            (setq file (vla-get-imagefile item))
  104.          ) ;_ end of and
  105.          (if
  106.            (not (or (findfile file)
  107.                     (findfile (strcat (vl-filename-base file)
  108.                                       (vl-filename-extension file)
  109.                               ) ;_ end of strcat
  110.                     ) ;_ end of findfile
  111.                 ) ;_ end of or
  112.            ) ;_ end of not
  113.            (progn
  114.              (if (not (member (vla-get-name item) retimg))
  115.              (setq retimg (cons (vla-get-name item) retimg)))
  116.              (setq retimgobj (cons item retimgobj))
  117.            )
  118.          ) ;_ end of if
  119.         )
  120.         (t nil)
  121.       ) ;_ end of cond
  122.     ) ;_ end of vlax-for
  123.     (princ " ...OK")
  124.   ) ;_ end of vlax-for
  125.   (princ "\nTotal: ")
  126.   (princ (length retimg))(princ " unfound rasters and ")
  127.   (princ (length retxref))(princ " unfound xrefs ")
  128.   (if (or retimg retxref)
  129.     (progn
  130.   (initget "Image Xref Both")
  131.   (setq file (getkword "Delete unfound [Image/Xref/Both] <both>: "))
  132.   (cond ((= file "Image")
  133.          (setq pat '("AcDbRasterImage"))
  134.          )
  135.         ((= file "Xref")
  136.          (setq pat '("AcDbBlockReference"))
  137.          )
  138.         (t (setq pat '("AcDbRasterImage" "AcDbBlockReference")))
  139.         )
  140.   (mip:layer-status-save)
  141.   (if (member "AcDbRasterImage" pat)
  142.     (progn
  143.       (mapcar '(lambda(x)
  144.                  (vl-catch-all-apply 'vla-delete (list x))
  145.                  )
  146.               retimgobj
  147.               )
  148.        (mapcar '(lambda(x)
  149.                  (vl-catch-all-apply 'DetachImage (list x))
  150.                  )
  151.               retimg
  152.               )
  153.       )
  154.     )
  155.    (if (member "AcDbBlockReference" pat)
  156.     (progn
  157.        (mapcar '(lambda(x)
  158.                   (vl-catch-all-apply 'vla-detach (list x))
  159.                   )
  160.                retxref
  161.                )
  162.       )
  163.     )
  164.  (mip:layer-status-restore)
  165.   )
  166.     )
  167.   (princ)
  168. )

VVA

  • Newt
  • Posts: 166
Re: [Community Project] Clean Drawing Program
« Reply #45 on: March 29, 2015, 05:50:06 PM »
Some more offers
1.Remove DATALINK, DGNLINESTYLECOMP, VBA dictionary
Code: [Select]
(VL-CATCH-ALL-APPLY
   '(lambda()
 (mapcar '(lambda(x)
      (dictremove (namedobjdict) x)
           )
        '(
        "ACAD_DATALINK" 
        "ACAD_DGNLINESTYLECOMP" 
         "ACAD_VBA"
          )
        )
      )
   )
2. Removes the history from 3D solid primitives (_brep _all)
get from kpblc
Code: [Select]
(vlax-for blk_def (vla-get-blocks adoc)
        (if (equal (vla-get-isxref blk_def) :vlax-false)
          (vlax-for subent blk_def
            (if (= (vla-get-objectname subent) "AcDb3dSolid")
              (setq ent_lst (cons (vlax-vla-object->ename subent) ent_lst))
              ) ;_ end of if
            ) ;_ end of vlax-for
          ) ;_ end of if
        ) ;_ end of vlax-for
      (foreach ent ent_lst
        (if (and (cdr (assoc 350 (entget ent)))
                 (not (vl-catch-all-error-p
                        (vl-catch-all-apply
                          (function
                            (lambda () (entget (cdr (assoc 350 (entget ent)))))
                            ) ;_ end of function
                          ) ;_ end of vl-catch-all-apply
                        ) ;_ end of vl-catch-all-error-p
                      ) ;_ end of not
                 (entget (cdr (assoc 350 (entget ent))))
                 ) ;_ end of and
          (entdel (cdr (assoc 350 (entget ent))))
          ) ;_ end of if
        ) ;_ end of foreach

ronjonp

  • Needs a day job
  • Posts: 7527
Re: [Community Project] Clean Drawing Program
« Reply #46 on: March 29, 2015, 07:42:54 PM »
My take is if you're cleaning up files for internal use ( background ) .. Blast the smithereens out of them. ;) .  if you're sharing use caution.

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • Superior Stupidity at its best
Re: [Community Project] Clean Drawing Program
« Reply #47 on: March 30, 2015, 01:16:30 PM »
My take is if you're cleaning up files for internal use ( background ) .. Blast the smithereens out of them. ;) .  if you're sharing use caution.

I agree 82.5% mainly because it really depends upon why you are sharing the drawings.

Again, everything I do is 2D plan view ... I remove everything that isn't absolutely necessary for the user to actually see the layout of the plans.
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Find me on https://parler.com @kblackie