TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: HasanCAD on November 28, 2016, 06:31:46 AM

Title: Wblock to export a block and its layer
Post by: HasanCAD 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.   )
Title: Re: Wblock to export a block and its layer
Post by: ChrisCarlson 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".
Title: Re: Wblock to export a block and its layer
Post by: Marc'Antonio Alessi 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...
Title: Re: Wblock to export a block and its layer
Post by: danallen 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).
Title: Re: Wblock to export a block and its layer
Post by: HasanCAD 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.   )
Title: Re: Wblock to export a block and its layer
Post by: ronjonp 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 ).
Title: Re: Wblock to export a block and its layer
Post by: Tharwat 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.
Title: Re: Wblock to export a block and its layer
Post by: Grrr1337 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.
Title: Re: Wblock to export a block and its layer
Post by: HasanCAD on December 03, 2016, 04:33:41 AM
Thanks all I'll follow
Title: Re: Wblock to export a block and its layer
Post by: HasanCAD 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?
Title: Re: Wblock to export a block and its layer
Post by: ronjonp on December 05, 2016, 10:02:10 AM
Iterate the block table.
Title: Re: Wblock to export a block and its layer
Post by: ChrisCarlson 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