;;-------------------=={ Modelspace to Paperspace }==-------------------;;
;;                                                                      ;;
;;  This program allows the user to copy a selection of objects from    ;;
;;  Modelspace to Paperspace through an active viewport.                ;;
;;                                                                      ;;
;;  This functionality is similar to the standard CHSPACE command,      ;;
;;  however, this program will copy the selection of objects as         ;;
;;  opposed to moving the objects from Modelspace to Paperspace.        ;;
;;                                                                      ;;
;;  Upon issuing the command 'ms2ps' at the AutoCAD command-line, the   ;;
;;  user is prompted to make a selection of objects to copy.            ;;
;;                                                                      ;;
;;  Following a valid selection, the program will copy the objects to   ;;
;;  the active Paperspace layout, before performing the necessary       ;;
;;  matrix transformations to maintain the visual appearance of the     ;;
;;  objects as displayed through the active viewport.                   ;;
;;                                                                      ;;
;;  The program is compatible with rectangular & polygonal viewports,   ;;
;;  under all UCS & view settings, and with objects constructed in      ;;
;;  any UCS construction plane.                                         ;;
;;                                                                      ;;
;;  Note that the command is only available when a Paperspace Layout    ;;
;;  is set current, with a viewport active.                             ;;
;;                                                                      ;;
;;----------------------------------------------------------------------;;
;;  Author:  Lee Mac, Copyright ?2014  -  www.lee-mac.com              ;;
;;----------------------------------------------------------------------;;
;;  Version 1.0    -    2014-02-16                                      ;;
;;                                                                      ;;
;;  First release.                                                      ;;
;;----------------------------------------------------------------------;;
;实体由模型空间转到图纸空间[lm_ms2ps]
(defun c:lm_ms2ps ( / old_cmd ang doc enx lst mat nor scl sel obj )


    (mini_start nil)
    (mai_last_com "lm_ms2ps")
    (cond
        (   (= 1 (getvar 'tilemode))
            (prompt "\n命令只能在图纸空间下使用.")
        )
        (   (= 1 (getvar 'cvport))
            (prompt "\n请先激活一个视口.")
        )
        (   (setq sel (mai_select_first '((410 . "Model")) "选择要转到图纸空间的实体:" "没有找到实体."))
            (setq lst (mapcar 'vlax-ename->vla-object (mai_ssget_2_list sel))
                  enx (entget (ssname (ssget "_X" (list '(0 . "VIEWPORT") (cons 69 (getvar 'cvport)))) 0))
                  ang (mai_dxf 51 enx)
                  nor (mai_dxf 16 enx)
                  scl (/ (mai_dxf 41 enx) (mai_dxf 45 enx)) 
                  mat ;; The following is adapted from gile's WCS2PCS function:
                      (mai_mxm
					        (mai_trp
								(mapcar '(lambda(v) (mai_v_scale (trans v nor 0 t) scl))
									'( (1. 0. 0.) (0. 1. 0.) (0. 0. 1.))
								)
                            )
					        (mai_m_rotate_z ang)
                      )
                  mat (vlax-tmatrix
                        (append
                          (mapcar '(lambda(a b) (append a (list b)))
                             mat
                            (mapcar '+
                                (mai_mxv mat (mapcar '- (mai_dxf 17 enx)))
                                (mai_v_scale (mai_dxf 12 enx) (- scl))
                                (mai_dxf 10 enx)
                            )
                          )
                          (list (list 0. 0. 0. 1.))
                         )
                      )
            )
			(setq doc (vlax-get (vlax-get-acad-object) 'activedocument))
            (foreach obj
                (vlax-invoke doc 'copyobjects lst
                     (vlax-get (vla-item (vlax-get doc 'layouts) (getvar 'ctab)) 'block)
                )
                (vla-transformby obj mat)
            )
        )
    )
    (mai_end)
)
;;----------------------------------------------------------------------;;
;;                             End of File                              ;;
;;----------------------------------------------------------------------;;
