convert / export layout to / as a block definition

convert / export layout to / as a block definition

jtm2020hyo
Collaborator Collaborator
1,081 Views
7 Replies
Message 1 of 8

convert / export layout to / as a block definition

jtm2020hyo
Collaborator
Collaborator

is there any AutoLISP to convert / export layout to / as a block definition?

0 Likes
Accepted solutions (3)
1,082 Views
7 Replies
Replies (7)
Message 2 of 8

Moshe-A
Mentor
Mentor
Accepted solution

@jtm2020hyo  hi,

 

hope this still actual? 😀

 

Attached EXPLAY (Export to Layout) command to export the current layout to file and than bring it in as standard block.

you must be in layout in order the command will work.

the block name will be the layout name and if the block is already exist, it will update it automatically.

 

is this what you had in mind?

 

enjoy

Moshe

 

 

; Export to layout than bring it as standard block

(vl-load-com)

(defun c:EXPLAY (/ savAttreq savAttdia fname bname)
 (setvar "cmdecho" 0)
 (command "._undo" "_begin")

 (setq savAttreq (getvar "attreq"))
 (setq savAttdia (getvar "attdia"))
  
 (setvar "attreq" 0) 
 (setvar "attdia" 0)

 (cond
  ((= (getvar "tilemode") 1)
   (vlr-beep-reaction)
   (princ "\nthis command only work from layout.")
  ); case
  ((setq fname (getfiled "Export Layout to Model Space Drawing" (strcat (getvar "dwgprefix") (getvar "ctab")) "dwg" 1))
   (command "._exportlayout" fname)
   (setq bname (vl-filename-base fname))
   (setvar "expert" 5)
   (command "._-insert" (strcat bname "=" bname))
   (command); escape
   (setvar "expert" 0)
   (princ (strcat "\n" bname " is now standard block."))
  ); case
 ); cond

 (setvar "attdia" savAttdia) 
 (setvar "attreq" savAttreq) 
  
 (command "._undo" "_end")
 (setvar "cmdecho" 1)

 (princ)
); explay
Message 3 of 8

jtm2020hyo
Collaborator
Collaborator

thanks a lot, MR. @Moshe-A , you saved me again.

 

image.pngimage.png

0 Likes
Message 4 of 8

jtm2020hyo
Collaborator
Collaborator

is it possible that it can be automatically saved to the block list?

0 Likes
Message 5 of 8

Moshe-A
Mentor
Mentor

@jtm2020hyo ,

 


@jtm2020hyo wrote:

is it possible that it can be automatically saved to the block list?


the lisp insert the layout block as a standard block and it shows in block list

 

 

Message 6 of 8

jtm2020hyo
Collaborator
Collaborator

I mean without user intervention. something like just leave to the user use the command and automatically add to block list with the Layout name adding (1) or (2) if repeat.

0 Likes
Message 7 of 8

Moshe-A
Mentor
Mentor
Accepted solution

@jtm2020hyo ,

 

Attached the fix.

 

About blocks name:

EXPLAY adds (nnn) as suffix to block name as basic format, example:

you have "ground plan" layout, the block would be "ground plan(1)" if "ground plan(1)" block is not already exist.

if you have "ground plan(1)" layout, the block would be "ground plan(1)" again if "ground plan(1)" block is not already exist. if it is? the block would be "ground plan(2)" ....etc.

 

the program search Layout name for the parentheses and the number inside and use it as index to increment. the max number is limited to 2 power 7 (up to 128) to prevent infinite loop.

this declares in code as:

 

; declare constants
(setq UPTO (expt 2 7)) ; (= 128)

 

if you find this as limitation, you can set a higher power value like (expt 2 😎  increase it to 256 😀

the program will exit if suffix is not a numeric value. rename the layout to resolve this.

 

enjoy

Moshe

 

 

 

(vl-load-com) ; load ActiveX support

; Export to layout than bring it as standard block

(defun c:EXPLAY (/ bname_generator ; local function
                   UPTO ERRMSG savAttreq savAttdia fname bname)

 ; block name generator
 (defun bname_generator (LayoutName / is_numeric gen_bn ; local functions
                                      p0 p1 suff)

  (defun is_numeric (suf)
   (vl-every
    '(lambda (c)
      (and (>= c 48) (<= c 57))
     )
    (vl-string->list suf)
   ); vl-every
  ); is_numeric
    
  (defun gen_bn (layN j / bn)
   (while (and
            (< j UPTO)
            (tblsearch "block" (setq bn (strcat layN "(" (itoa j) ")")))
          )
    (setq j (1+ j))
   ); while

   (if (< j UPTO)
     bn
   ) 
  ); gen_bn
   
  (cond 
   ((and
     (setq p0 (vl-string-position (ascii ")") LayoutName 0 t))
     (setq p1 (vl-string-position (ascii "(") LayoutName 0 t))
    )
    (setq suff (substr LayoutName (+ p1 2) (- p0 p1 1)))

    (if (not (is_numeric suff))
     ERRMSG 
     (gen_bn (vl-string-right-trim (strcat "(" suff ")") LayoutName) (1+ (atoi suff)))
    ); if
   ); case
   ( t
    (gen_bn LayoutName 1)
   )
  ); cond
 ); bname_generator

  
 ; here start EXPLAY command 
 (setvar "cmdecho" 0)
 (command "._undo" "_begin")

 ; declare constants
 (setq UPTO (expt 2 7)) ; (= 128)
 (setq ERRMSG "illegal value found inside layout name parentheses.")
  
 (setq savAttreq (getvar "attreq"))
 (setq savAttdia (getvar "attdia"))
  
 (setvar "attreq" 0) 
 (setvar "attdia" 0)

 (cond
  ((= (getvar "tilemode") 1)
   (vlr-beep-reaction)
   (princ "\nthis command only work from layout.")
  ); case
  ((not (setq bname (bname_generator (getvar "ctab"))))
   (vlr-beep-reaction)
   (princ (strcat "\nReached suffix limits, upto " (itoa UPTO) " is allowed, block is not created"))
  ); case
  ( t
   (if (eq bname ERRMSG)
    (progn
     (vlr-beep-reaction)
     (princ ERRMSG)
    ); progn
    (progn
     ; depending on layout 'volume' and computer speed
     ; next code lines may suspend autocad process
     ; for a while, without echoing to your screen - patience...
     (setq fname (strcat (getvar "dwgprefix") bname))
     (command "._exportlayout" fname)
     (setq bname (vl-filename-base fname))
     (setvar "expert" 5)
     (command "._-insert" bname)
     (command); escape
     (vl-file-delete (strcat fname ".dwg"))
     (setvar "expert" 0)
     (princ (strcat "\n" bname " is now standard block."))
    ); progn
   ); if 
  ); case
 ); cond

 (setvar "attdia" savAttdia) 
 (setvar "attreq" savAttreq) 
  
 (command "._undo" "_end")
 (setvar "cmdecho" 1)

 (princ)
); explay

 

 

 

Message 8 of 8

Moshe-A
Mentor
Mentor
Accepted solution

@jtm2020hyo 

 

attached is an update after fine tuning. the external block is now written to the temp folder preventing from overwritten an existing file on you working directory.

 

and more...it turns out  that  EXPORTLAYOUT is not so clean and may get AutoCAD to stuck. if you see explay suspend for long, go to task manager and 'kill' AutoCAD than do a good clean to the file and try again.

 

cheers,

Moshe