Author Topic: Code for cleaning dwg's  (Read 1560 times)

0 Members and 1 Guest are viewing this topic.

w64bit

  • Newt
  • Posts: 78
Code for cleaning dwg's
« on: December 12, 2020, 12:02:58 PM »
Hi,
I have some code, which I use it to clean some bloated dwg files.
It seems to work but it would be better if I someone can have a look over it, only to see if there is any mistake.
Thank you

Code: [Select]
;; Load the Extended AutoLISP Functions
(vl-load-com)
;; ----------------------------------------------------------------------------
;; Delete Layer States
(setq ActDoc (vla-get-ActiveDocument (vlax-get-Acad-Object)))
(setq LayCol (vla-get-Layers ActDoc))
(setq LayDict (vla-GetExtensionDictionary LayCol))
(setq LayStDict (vl-catch-all-apply 'vla-Item (list LayDict "ACAD_LAYERSTATES")))
(vlax-for State LayStDict
(vla-Delete State))
;; ----------------------------------------------------------------------------
;; Delete Layer Filters
(lambda (_vla-remove dictionary)
(_vla-remove "ACLYDICTIONARY" dictionary)
(_vla-remove "ACAD_LAYERFILTERS" dictionary))
(lambda (item dictionary)
(vl-catch-all-apply 'vla-remove (list dictionary item)))
(vla-getextensiondictionary
(vla-get-layers (vla-get-activedocument (vlax-get-acad-object))))
;; ----------------------------------------------------------------------------
;; Delete Page Setups
(setq
curdwg   (vla-get-ActiveDocument (vlax-get-Acad-Object))
pslayout (vla-get-Layout (vla-get-PaperSpace curdwg)))
(vla-RefreshPlotDeviceInfo pslayout)
(vlax-for x (vla-get-Plotconfigurations curdwg)
(vla-delete x))
;; ----------------------------------------------------------------------------

« Last Edit: December 13, 2020, 08:22:42 AM by w64bit »

rayakmal

  • Newt
  • Posts: 49
Re: Code for cleaning dwg's
« Reply #1 on: December 13, 2020, 08:53:04 PM »
You may need to include this:

Code: [Select]
(defun c:DGNFix (/)
  ;; remove DGN style dictionary and purge drawing.
  ;; will fix bloated drawing issues (linetypes, text/dimstyles, etc.)
  ;; should also decrease file size significantly
  ;; Alan J. Thompson
  (if (dictremove (namedobjdict) "ACAD_DGNLINESTYLECOMP")
    (progn (command "_.-purge" "_all" "*" "_no")
           (alert "DGN Dictionary has been removed. Drawing should be fixed.")
    )
    (alert "No DGN dictionary found in drawing.")
  )
  (princ)
)



myloveflyer

  • Newt
  • Posts: 152
Re: Code for cleaning dwg's
« Reply #2 on: December 18, 2020, 04:33:15 AM »
Code: [Select]
(defun dicts (n / xdoc xdicts xi xc xii xlst xname xnl xhl myGetCount)

   (defun myGetCount ( xd xn / xitem errobj)
      (setq xitem (vla-item xd xn)  errobj (vl-catch-all-apply 'vla-get-count (list xitem)))
      (if (vl-catch-all-error-p errobj)
           "#n/a"
           (itoa errobj)
      )
   )  ; end of myerr()

   (vl-load-com)
   ;(textscr)
   (command "Undo" "c" "n")
   (setq xdoc (vla-get-ActiveDocument (vlax-get-Acad-Object))
         xdicts (vla-get-dictionaries xdoc)
         xdictlist (entget (namedobjdict))
         xi 0  xc (length xdictlist)  xii 0  xnl nil  xhl nil
   )
   (while (< xi xc)  ; for each element in xdictlist, the dictionary list
      (if (= (car (setq xlst (nth xi xdictlist))) 3)
          (progn
             (setq xii (1+ xii)  xi (1+ xi))
;(princ (strcat (itoa xii) ". \"" (setq xname (cdr xlst)) "\"  "  (myGetCount xdicts xname) "\n"))
             (setq xnl (cons xname xnl)  xhl (cons (cdr (nth xi xdictlist)) xhl))
          )  ; end of progn(it's a dictionary item)
      )  ; end of if(it's a dictionary item)
      (setq  xi (1+ xi))
   )  ; end of while(each element in dictionary list)
   ;(princ (strcat "\nActiveDocument.Dictionaries.Count=" (itoa (vla-get-count xdicts)) "\n"))
   (initget 6)  ; no zero or negative value allowed
   (setq  xnl (reverse xnl)  xhl (reverse xhl)  xi n)
   (if (and xi (<= xi xii)) (entdel (nth (1- xi) xhl)))
   (command "Undo" "a")
   (princ n)
)  ; end of (C:dicts)
(dicts 4)
(dicts 5)
(dictremove (namedobjdict) "ACAD_DGNLINESTYLECOMP")
(command "purge" "a" "" "n" "")
(princ)
Never give up !

ronjonp

  • Needs a day job
  • Posts: 7527
Re: Code for cleaning dwg's
« Reply #3 on: December 18, 2020, 01:18:54 PM »

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC