;;-------------------=={ 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 idx lst mat nor scl sel vxs )

;; Vector x Scalar  -  Lee Mac
;; Args: v - vector in R^n, s - real scalar

(defun vxs ( v s )
    (mapcar '(lambda ( n ) (* n s)) v)
)

    (mini_start nil)
    (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
                            (list
                                (list (cos ang) (- (sin ang)) 0.)
                                (list (sin ang)    (cos ang)  0.)
                                '(0. 0. 1.)
                            )
                            (mapcar '(lambda(v) (vxs (trans v nor 0 t) scl))
                                '( (1. 0. 0.) (0. 1. 0.) (0. 0. 1.))
                            )
                      )
                  mat (vlax-tmatrix
                        (append
                          (mapcar '(lambda(a b) (append a (list b)))
                             mat
                            (mapcar '+
                                (mai_mxv mat (mapcar '- (mai_dxf 17 enx)))
                                (vxs (mai_dxf 12 enx) (- scl))
                                (mai_dxf 10 enx)
                            )
                          )
                          '((0. 0. 0. 1.))
                         )
                      )
            )
            (foreach
                obj
                (vlax-invoke (setq doc (vla-get-activedocument (vlax-get-acad-object)))
                     'copyobjects lst
                     (vla-get-block (vla-item (vla-get-layouts doc) (getvar 'ctab)))
                )
                (vla-transformby obj mat)
            )
        )
    )
    (mai_end)
)
;;----------------------------------------------------------------------;;
;;                             End of File                              ;;
;;----------------------------------------------------------------------;;