Author Topic: Wblock to export a block and its layer  (Read 1262 times)

0 Members and 1 Guest are viewing this topic.

HasanCAD

  • Swamp Rat
  • Posts: 1323
Wblock to export a block and its layer
« on: November 28, 2016, 06:31:46 AM »
This lisp export blocks for files perfectly but the block is exploded
I am wondering is there a way to export blocks as it is and its layer?
Code - Auto/Visual Lisp: [Select]
  1. (defun c:bwblock (/ ss cntr bn)
  2.   (setq ss (ssget "x" '((0 . "INSERT"))))
  3.   (setq cntr 0)
  4.  
  5.   (mapcar 'setvar '(filedia cmddia) '(0 0))
  6.  
  7.   (if (not (zerop (sslength ss)))
  8.         (while (setq ent (ssname ss cntr))
  9.            (setq bn (cdr (assoc 2 (entget ent)))
  10.                          cntr (1+ cntr)
  11.                          )
  12.           ;; wblock each block in the current dwg prefixed with "BLOCK_"
  13.           ;; and sent to c:\temp which must exist
  14.           ;(command "_wblock" (strcat "c:\\temp\\BLOCK_" bn) bn "NO"); for Map
  15.           (command "_wblock" (strcat "c:\\temp\\" bn) bn)
  16.           )
  17.     (princ "no blocks found")
  18.         )
  19.   (mapcar 'setvar '(filedia cmddia) '(1 1))
  20.   )

Master_Shake

  • Swamp Rat
  • Posts: 1447
Re: Wblock to export a block and its layer
« Reply #1 on: November 28, 2016, 08:56:13 AM »
WBLOCK is working as it is intended. The program is initiated in drawing "A", the block is saved as drawing "B", upon inserting drawing "B" into drawing "C" the entities will be inserted as a block identical to the one in drawing "A".

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1058
  • Marco
Re: Wblock to export a block and its layer
« Reply #2 on: November 28, 2016, 01:24:35 PM »
You should not have a file that contains a block that has the same name, you may have the "circular reference" problems...

danallen

  • Guest
Re: Wblock to export a block and its layer
« Reply #3 on: November 28, 2016, 07:26:00 PM »
This lisp export blocks for files perfectly but the block is exploded
I am wondering is there a way to export blocks as it is and its layer?

I agree with previous comments, but to do what you want, revise code during WBLOCK command to select entities such as the already inserted block, instead of using blockname (bn).

HasanCAD

  • Swamp Rat
  • Posts: 1323
Re: Wblock to export a block and its layer
« Reply #4 on: November 29, 2016, 05:54:48 AM »
Thanks all for quick reply
this final (up to my knowladge)
Code - Auto/Visual Lisp: [Select]
  1. (defun c:WBlockTest (/ blk blkins blknm cntr dr ent ss )
  2.  
  3.   (if (and
  4.         (setq ss (ssget "_X" '((0 . "INSERT"))))
  5.         (setq cntr 0)
  6.         (mapcar 'setvar '(filedia cmddia) '(0 0))
  7.         (setq dr "c:\\TEMP") ;folder for extraction
  8.         (if (not (vl-file-directory-p dr)) (vl-mkdir dr)) ; if
  9.         ) ; and
  10.     (progn
  11.       (if (not (zerop (sslength ss)))
  12.         (while
  13.           (setq ent (ssname ss cntr))
  14.           (setq BlkIns (cdr (assoc 10 (entget ent))))
  15.           (setq BlkNm (vla-get-effectivename (vlax-ename->vla-object ent)))
  16.           (setq cntr (1+ cntr))
  17.           (setq blk (ssget "_X" (list '(0 . "INSERT") (cons 2 BlkNm))))
  18.           (vl-cmdf "_wblock" (strcat dr "\\" BlkNm)  "" BlkIns blk "")
  19.           ) ; while
  20.         (princ "no blocks found")
  21.         ) ;if
  22.       (mapcar 'setvar '(filedia cmddia) '(1 1))
  23.       ) ;progn
  24.     ) ;if
  25.   )

ronjonp

  • Needs a day job
  • Posts: 7111
Re: Wblock to export a block and its layer
« Reply #5 on: November 29, 2016, 10:33:10 AM »
FWIW, Your make directory within the AND statement will always return nil after the directory exists.
Maybe use: (or (findfile (setq dir "C:\\Temp")) (vl-mkdir dir))
Also, don't set variables until you actually get into your program ( PROGN ).

Windows 10 x64 - AutoCAD /C3D 2020

Custom Build PC

Tharwat

  • Swamp Rat
  • Posts: 524
  • Hypersensitive
Re: Wblock to export a block and its layer
« Reply #6 on: November 29, 2016, 03:25:19 PM »
* Dynamic Blocks can not be selected the same way that regular blocks being selected with ssget function.
* There is no need to test if the variable ss has selected objects tow times since it is already tested that in the first part with and function.

Grrr1337

  • Swamp Rat
  • Posts: 720
Re: Wblock to export a block and its layer
« Reply #7 on: November 29, 2016, 04:41:34 PM »
I think you could wrap the entire thing with a single and function, after following ronjonp and Tharwat's advices.
(apply ''((a b c)(a b c))
  '(
    (( f L ) (apply 'strcat (f L)))
    (( L ) (if L (cons (chr (car L)) (f (cdr L)))))
    (72 101 108 108 111 32 87 111 114 108 100)
  )
)

HasanCAD

  • Swamp Rat
  • Posts: 1323
Re: Wblock to export a block and its layer
« Reply #8 on: December 03, 2016, 04:33:41 AM »
Thanks all I'll follow

HasanCAD

  • Swamp Rat
  • Posts: 1323
Re: Wblock to export a block and its layer
« Reply #9 on: December 05, 2016, 06:50:25 AM »
In some cases There are blocks not inserted
So How to modify this lisp to insert all blocks even not inserted?

ronjonp

  • Needs a day job
  • Posts: 7111
Re: Wblock to export a block and its layer
« Reply #10 on: December 05, 2016, 10:02:10 AM »
Iterate the block table.

Windows 10 x64 - AutoCAD /C3D 2020

Custom Build PC

Master_Shake

  • Swamp Rat
  • Posts: 1447
Re: Wblock to export a block and its layer
« Reply #11 on: December 05, 2016, 10:28:32 AM »
Ron wrote this years ago

Code - Auto/Visual Lisp: [Select]
  1. (defun c:exportblocks (/ blk blks x dir)
  2. ;;Ronjonp 5/20/14
  3. ;;https://www.theswamp.org/index.php?topic=47103.msg521360#msg521360
  4.     (progn (setvar 'filedia 0)
  5.            (setq dir (strcat (getvar 'dwgprefix) "_ExportedBlocks\\"))
  6.            (vl-mkdir dir)
  7.            (vlax-for blk blks
  8.              (if (and (not (wcmatch (setq x (vla-get-name blk)) "*|*,*`**"))
  9.                       (= (vla-get-isxref blk) :vlax-false)
  10.                  )
  11.                (command "._-wblock" (strcat dir x) x)
  12.              )
  13.            )
  14.            (setvar 'filedia 1)
  15.     )
  16.   )
  17.   (princ)
  18. )

Still use it