LISP routine to move block to model space by block name

LISP routine to move block to model space by block name

james_martinSPK3G
Community Visitor Community Visitor
545 Views
7 Replies
Message 1 of 8

LISP routine to move block to model space by block name

james_martinSPK3G
Community Visitor
Community Visitor

Can anyone help me set up a LISP to select a block by block name and move it to model space?

My coding/LISP skills are woeful. 

 

Thanks

0 Likes
546 Views
7 Replies
Replies (7)
Message 2 of 8

ec-cad
Collaborator
Collaborator

Try this one on a test file.

 

ECCAD

;; Move_Block_to_model_space
;;
 (defun C:MBMS ( )
  (prompt "\nPick a Block{s} to move into Model Space:")
  (setq ss (ssget))
   (if ss
    (progn
     (command "_CHSPACE" ss "")
    ); progn
   ); if
;;
 (setq ct (getvar "ctab"))
 (setvar "ctab" "Model")
 (command "_zoom" "E")
 (setq x (getpoint (strcat "\nPick any Point to return to Layout " ct)))
 (setvar "ctab" ct)
 (princ)
 ); function C:MBMS
0 Likes
Message 3 of 8

Sea-Haven
Mentor
Mentor

Chspace is the key as already mentioned, a few questions "Block by name", do you intend to type it in ? Or pick a block and get block name then change all blocks with same name to model space. So basis of question is 1 block or multiples ? 

 

Maybe answer is you were not aware of the "Chspace" command.

0 Likes
Message 4 of 8

james_martinSPK3G
Community Visitor
Community Visitor

Rather than having to pick the block, block name is known and ideally inserted in the lisp. So it will only change that specific block from paper space to model space with no selection necessary. 

Thanks

0 Likes
Message 5 of 8

james_martinSPK3G
Community Visitor
Community Visitor

Thanks. I will try that one.

0 Likes
Message 6 of 8

Kent1Cooper
Consultant
Consultant

@james_martinSPK3G wrote:

Rather than having to pick the block, block name is known and ideally inserted in the lisp. So it will only change that specific block from paper space to model space with no selection necessary. 


So presumably the same Block exists in more than one Layout.  It's not hard to have a routine find all such Blocks, provided they are not dynamic and it can filter by the Block name of the insertions, but in addition to filtering by object type and name, it should also restrict the selection to those not already in Model space.  Then to use the CHSPACE command, it would need to step through the Blocks, get into the Layout each is in, and do its thing with it.  But would there ever be more than one Viewport within a Layout?  How would you want to account for the CHSPACE command asking you to designate through which Viewport the Block gets moved to Model space?  If a routine just accepts what's offered, you would sometimes end up with a Block in a very different place in Model space than you expect.

Kent Cooper, AIA
0 Likes
Message 7 of 8

Sea-Haven
Mentor
Mentor

Provided the viewports don't overlap this seemed to work ok as an example. Where pt would be say insertion point. It must be located within viewport boundary. Please confirm wether there is one or more viewport in a layout as requested by Kent. 

 

(command "chspace" blkent "" pt "")

 

 

 

Need a real dwg to test on.

 

 

0 Likes
Message 8 of 8

ronjonp
Advisor
Advisor

Another for fun. Lightly tested.

 

(defun c:foo (/ bn el p s)
  ;; RJP » 2024-06-12
  ;; Move all non attributed paperspace blocks to modelspace, does not account for viewport scale
  (setq bn "*")
  (cond
    ((setq s (ssget "_X" (list '(0 . "INSERT") '(410 . "~Model") (cons 2 bn) '(66 . 0))))
     (foreach e
	      (vl-sort (mapcar 'cadr (ssnamex s))
		       '(lambda (r j) (< (cdr (assoc 410 (entget r))) (cdr (assoc 410 (entget r)))))
	      )
       (setq el (entget e))
       (setvar 'ctab (cdr (assoc 410 el)))
       (cond ((and (setq p (trans (cdr (assoc 10 el)) 3 2)) (setq p (trans p 2 0)))
	      (if (setq el (append (subst '(410 . "Model") (assoc 410 el) el) (list (cons 10 p))))
		(progn (entmakex el) (entdel e))
	      )
	     )
	     ((print (strcat "\n" (getvar 'ctab) " had problems..")))
       )
     )
    )
  )
  (princ)
)

 

 

 

0 Likes