Visual LISP, AutoLISP and General Customization
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

open / check for block / explode / purge / save

6 REPLIES 6
Reply
Message 1 of 7
bedros.j
1220 Views, 6 Replies

open / check for block / explode / purge / save

Hi,

 

i have list of drawings in folder:

 

  • i want to select the folder (dxf or dwg or both if possible)
  • it will open each drawing then check if there is any block inside
  • if yes explode, purge and save
  • if no it will open purge and save (and not explode anything else)

is this possible to lisp?

 

Many Thanks,

 

Bedros

6 REPLIES 6
Message 2 of 7
devitg
in reply to: bedros.j

Please Upload a few of such files , DXF or DWG 

Message 3 of 7
Lee_Mac
in reply to: bedros.j


@bedros.j wrote:

 

  • it will open each drawing then check if there is any block inside
  • if yes explode, purge and save

 Save the following code as expurge.lsp to an AutoCAD Support File Search Path:

 

(defun c:expurge ( ) (c:eab) (c:purgeall))

;; Purge All  -  Lee Mac

(defun c:purgeall ( )
    (command "_.-purge" "_R" "*" "_N")
    (repeat 3 (command "_.-purge" "_A" "*" "_N"))
    (princ)
)
    
;; Explode All Blocks  -  Lee Mac
;; Recursively explodes all primary & nested blocks (nested to any depth) in all drawing layouts.

(defun c:eab ( )
    (vlax-for blk (vla-get-blocks (vla-get-activedocument (vlax-get-acad-object)))
        (if (= :vlax-false (vla-get-isxref blk))
            (vlax-for obj blk (explodeblock obj))
        )
    )
    (princ)
)
(defun explodeblock ( obj / lst )
    (if
        (and
            (= "AcDbBlockReference" (vla-get-objectname obj))
            (vlax-method-applicable-p obj 'explode)
            (vlax-write-enabled-p obj)
        )
        (if
            (not
                (vl-catch-all-error-p
                    (setq lst
                        (vl-catch-all-apply 'vlax-invoke (list obj 'explode))
                    )
                )
            )
            (progn
                (vla-delete obj)
                (foreach obj lst (explodeblock obj))
            )
        )
    )
)
(vl-load-com) (princ)

 

Then download & run my Script Writer program (in a blank drawing), using the following as the 'Script Line':

 

_.open *file* (load "expurge.lsp" nil) (if c:expurge (c:expurge)) _.qsave _.close

(Note: underscores don't appear to display correctly in code tags on this forum!)

 

Let me know how you get on or if you have any questions.

 

Lee

Message 4 of 7
hmsilva
in reply to: Lee_Mac

Lee,
as the OP wanted to process dwg's and dxf's, and your Script Writer program don't process dxf's (not sure), I would suggest to add a save and close sub-routine to your expurge.lsp, perhaps something like this

(defun c:MyClose ()
  (if
    (= (strcase (vl-filename-extension (getvar 'DWGNAME))) ".DWG")
     (command "_.qsave" "_.close")
     (command "_.saveas" "DXF" "Version" "2010(LT2010)" "16" "" "Y" "_.close" "_N")
  );; if
  (princ)
  );; MyClose

And to create the script file, something like this...

(sorry, don't have the quality of your Script Writer) 🙂

(defun c:myscr (/ DPATH DWGLST FLSCR MSG NAME SCRFILE TYP)
  (vl-load-com)
  (setq msg "Open a Folder and click on Save")
  (initget "dWg dXf")
  (if (and (setq typ (getkword "\nEnter the File Type to Insert [dWg/dXf] <Exit>: "))
	   (setq dpath (getfiled "Browse for Folder" msg " " 1))
      );; and
    (progn
      (setq dpath  (substr dpath 1 (- (strlen dpath) (strlen msg)))
	    dpath  (vl-string-translate "\\" "/" dpath)
	    dwglst (vl-directory-files dpath (strcat "*." typ) 1)
	    flscr  (open (strcat dpath "MyScr.scr") "w")
      );; setq
      (foreach dwg dwglst
	(setq name (strcat "\"" dpath dwg "\""))
	(write-line "_.open" flscr)
	(write-line name flscr)
	(write-line (strcat "(load ""\"expurge.lsp""\" nil)") flscr)
	(write-line (strcat "(if c:expurge (c:expurge))") flscr)
      );; foreach
      (close flscr)
      (command "_.script" (strcat "\"" dpath "MyScr.scr" "\""))
    );; progn
  );; if
  (princ)
);; myscr

 

Just a thought...

 

Cheers

Henrique

EESignature

Message 5 of 7
bedros.j
in reply to: Lee_Mac

Thanks,

 

this worked perfectly with dwg files and kept the dxf ones 🙂

Message 6 of 7
bedros.j
in reply to: hmsilva

Henrique,

 

i see that you are adding some codes to merge it with Lee's to make the script work with both dxf and dwg,

 

will try to figure out codes and see if i can merge, or if you guys can do it faster than me that will be great,

 

thanks,

 

Bedros

Message 7 of 7
hmsilva
in reply to: bedros.j

Bedros, change the expurge function to

(defun c:expurge ( ) (c:eab) (c:purgeall) (c:MyClose))

and just before the (vl-load-com) (princ) add the myclose function.

For the script, you'll need to run the myscr routine.

 

Remember that the original code is from Lee, and it was just an idea to make the dxf's processing possible.

For sure Lee will have a different solution and you should wait for Lee's reply...

 

Henrique

EESignature

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report

”Boost