Author Topic: Write Block to DXF  (Read 2149 times)

0 Members and 1 Guest are viewing this topic.

rhino85

  • Mosquito
  • Posts: 6
Write Block to DXF
« on: March 18, 2020, 03:27:04 PM »
Hi All,

I found this code online at the following link:
https://lispbox.wordpress.com/2014/12/15/export-wblock-all-blocks-in-drawing-in-one-click/
which works really well.

Code: [Select]

;;; --------------------------------------------------------;;
;;; MWBLOCK.LSP (Multiple WBlock)
;;; English translated
;;; --------------------------------------------------------;;

(defun DXF (C E / )(cdr (assoc C E)))
(defun BLOCK_LIST ( / DBL LBL)
  (while
    (setq DBL (tblnext "BLOCK" (not DBL)))
    (if
      (/= "*" (substr (DXF 2 DBL) 1 1))
      (setq LBL
     (append LBL (list (DXF 2 DBL)))
    )
      )
    )
  (if LBL
    (setq LBL (acad_strlsort LBL))
    (alert "You don't have any blocks in the current drawing ")
    )

  (if LBL LBL NIL)
  )


(defun C:MBL ( / FIRSTNAME LBL PTH X CMD INC)
  (setq INC 0
LBL (BLOCK_LIST)
)
  (if
    (= NIL LBL)
    (princ "\nDone")
    (progn
      (setq FIRSTNAME
     (getfiled "Select Export Directory" (car LBL) "dwg" 15)
    )
      (if FIRSTNAME
(progn
  (setq PTH (car (fnsplitl FIRSTNAME)))
  (setq CMD (getvar "CMDECHO"))
  (setvar "CMDECHO" 0)
  (foreach X LBL
    (if
      (findfile (strcat PTH X ".dwg"))
      (command "_.WBLOCK" (strcat PTH X) "_N")
      (progn
(command "_.WBLOCK" (strcat PTH X) "=")
(princ ".")
(setq INC (1+ INC))
)
      )
    )
  (setvar "CMDECHO" CMD)
  )
(princ "\nDone")
)
      )
    )

  (if PTH
    (alert
      (strcat "\nSaved "
      (itoa INC)
      " blocks in selected directory "
      (strcase PTH)
      )
      )
    )
  (princ)
  )

(princ)
(princ "\n Export All blocks in Drawing - MBL.")
(princ)

(c:mbl)


how can this code be modified to save the files as .dxf instead? I think it needs to use dxfout?

Thanks in advance :)
« Last Edit: March 18, 2020, 03:32:40 PM by rhino85 »

rhino85

  • Mosquito
  • Posts: 6
Re: Write Block to DXF
« Reply #1 on: March 19, 2020, 06:36:55 AM »
I tried to add in a save as function...

but it doesn't work the same as WBLOCK:

Quote
Code: [Select]
;;; --------------------------------------------------------;;
;;; MWBLOCK.LSP (Multiple WBlock)
;;; English translated
;;; --------------------------------------------------------;;


(defun BLOCK_LIST (/ DBL LBL)
  (vl-load-com)
  (while
    (setq DBL (tblnext "BLOCK" (not DBL)))
     (if
       (/= "*" (substr (DXF 2 DBL) 1 1))
        (setq LBL
               (append LBL (list (DXF 2 DBL)))
        ) ;_ end of setq
     ) ;_ end of if
  ) ;_ end of while
  (if LBL
    (setq LBL (acad_strlsort LBL))
    (alert "You have not any blocks in a drawing ")
  ) ;_ end of if
  (if LBL
    LBL
    NIL
  ) ;_ end of if
) ;_ end of defun
;;;
;;;
;;;

(defun C:MBL (/ FIRSTNAME LBL PTH itm CMD INC)
  (setq INC 0
        LBL (BLOCK_LIST)
  ) ;_ end of setq
  (if
    (= NIL LBL)
     (princ "\nNothing to do. ")
     (progn
       (setq FIRSTNAME
              (getfiled "Select Export Directory" (car LBL) "dxf" 15)
       ) ;_ end of setq
       (if FIRSTNAME
         (progn
           (setq PTH (car (fnsplitl FIRSTNAME)))
           (setq CMD (getvar "CMDECHO"))
           (setvar "CMDECHO" 0)
   (foreach itm LBL ;; For every 'itm' in the list given by 'LBL'
     (vla-saveas
       (vla-get-activedocument (vlax-get-acad-object))
       (strcat PTH itm ".dxf") ac2010_dxf
       )
     (setq INC (1+ INC)
   )
     ) ;; end foreach
   (setvar "CMDECHO" CMD)
   )
(princ "\nDone")
)
       )
    )

  (if PTH
    (alert
      (strcat "\nSaved "
      (itoa INC)
      " blocks in selected directory "
      (strcase PTH)
      )
      )
    )
  (princ)
  ) ;_ end of defun

(princ)
(princ "\n Export All blocks in Drawing - MBL.")
(princ)

(c:mbl)


The above gives me the same dxf file containing all the blocks as opposed to individual dxf files with one block only... hilarious :2funny:
« Last Edit: March 19, 2020, 06:41:48 AM by rhino85 »

VovKa

  • Water Moccasin
  • Posts: 1628
  • Ukraine
Re: Write Block to DXF
« Reply #2 on: March 19, 2020, 11:09:20 AM »
how can this code be modified to save the files as .dxf instead?
substitute "dwg" with "dxf" and add "" after (strcat PTH X) in both "-wblock" calls

rhino85

  • Mosquito
  • Posts: 6
Re: Write Block to DXF
« Reply #3 on: March 19, 2020, 11:39:58 AM »
It still outputs the files as .dwg...

Code - Auto/Visual Lisp: [Select]
  1. (defun C:MBL (/ FIRSTNAME LBL PTH X CMD INC)
  2.   (setq INC 0
  3.         LBL (BLOCK_LIST)
  4.   ) ;_ end of setq
  5.   (if
  6.     (= NIL LBL)
  7.      (princ "\nDone. ")
  8.      (progn
  9.        (setq FIRSTNAME
  10.               (getfiled "Select Export Directory" (car LBL) "dxf" 15)
  11.        ) ;_ end of setq
  12.        (if FIRSTNAME
  13.          (progn
  14.            (setq PTH (car (fnsplitl FIRSTNAME)))
  15.            (setq CMD (getvar "CMDECHO"))
  16.            (setvar "CMDECHO" 0)
  17.            (foreach X LBL
  18.              (if
  19.                (findfile (strcat PTH X ".dxf"))
  20.                 (command "_.WBLOCK" (strcat PTH X) "_N" "")
  21.                 (progn
  22.                   (command "_.WBLOCK" (strcat PTH X) "=" "")
  23.                   (princ ".")
  24.                   (setq INC (1+ INC))
  25.                 ) ;_ end of progn
  26.              ) ;_ end of if
  27.            ) ;_ end of foreach
  28.            (setvar "CMDECHO" CMD)
  29.          ) ;_ end of progn
  30.          (princ "\nDone. ")
  31.        ) ;_ end of if
  32.      ) ;_ end of progn
  33.   ) ;_ end of if
  34.   (if PTH
  35.     (alert
  36.       (strcat "\nWriten "
  37.               (itoa INC)
  38.               " blocks in cirectory "
  39.               (strcase PTH)
  40.       ) ;_ end of strcat
  41.     ) ;_ end of alert
  42.   ) ;_ end of if
  43.   (princ)
  44. ) ;_ end of defun
  45.  

VovKa

  • Water Moccasin
  • Posts: 1628
  • Ukraine
Re: Write Block to DXF
« Reply #4 on: March 19, 2020, 12:51:24 PM »
Code: [Select]
(command "_.WBLOCK" (strcat PTH X ".dxf") "" "=")

rhino85

  • Mosquito
  • Posts: 6
Re: Write Block to DXF
« Reply #5 on: March 19, 2020, 01:39:48 PM »
Code: [Select]
(command "_.WBLOCK" (strcat PTH X ".dxf") "" "=")

Awesome!! that worked!
Thank you very much!