Author Topic: Copying Objects from Modelspace to Paperspace  (Read 3976 times)

0 Members and 1 Guest are viewing this topic.

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Copying Objects from Modelspace to Paperspace
« on: February 09, 2014, 04:32:20 PM »
Inspired by the request in this thread at CADTutor, I accepted the challenge of writing a program allowing the user to copy a selection of objects from modelspace to paperspace such that the objects will appear exactly as they are displayed through the active viewport.

I realise that it is already possible to transfer objects between modelspace/paperspace using the CHSPACE command, however, I was interested in the challenge of performing the various matrix transformations required to display the objects correctly.

I have written the following program as an initial draft of this idea, however, the program currently only performs correctly with viewports for which the viewtwist is zero:

Code - Auto/Visual Lisp: [Select]
  1. ;; Modelspace to Paperspace  -  Lee Mac
  2.  
  3. (defun c:ms2ps ( / doc idx lst mat org sel )
  4.     (cond
  5.         (   (= 1 (getvar 'tilemode))
  6.             (prompt "\nCommand only available in Paperspace.")
  7.         )
  8.         (   (= 1 (getvar 'cvport))
  9.             (prompt "\nPlease activate a viewport.")
  10.         )
  11.         (   (setq sel (ssget '((410 . "Model"))))
  12.             (repeat (setq idx (sslength sel))
  13.                 (setq lst (cons (vlax-ename->vla-object (ssname sel (setq idx (1- idx)))) lst))
  14.             )
  15.             (setq mat
  16.                 (vlax-tmatrix
  17.                     (append
  18.                         (mapcar
  19.                            '(lambda ( a b )
  20.                                 (append (mapcar '- (trans (trans a 0 2) 2 3) org) (list b))
  21.                             )
  22.                            '(
  23.                                 (1.0 0.0 0.0)
  24.                                 (0.0 1.0 0.0)
  25.                                 (0.0 0.0 1.0)
  26.                             )
  27.                             (setq org (trans (trans '(0.0 0.0 0.0) 0 2) 2 3))
  28.                         )
  29.                        '((0.0 0.0 0.0 1.0))
  30.                     )
  31.                 )
  32.             )
  33.             (foreach obj
  34.                 (vlax-invoke (setq doc (vla-get-activedocument (vlax-get-acad-object))) 'copyobjects lst
  35.                     (vla-get-block
  36.                         (vla-item
  37.                             (vla-get-layouts doc)
  38.                             (getvar 'ctab)
  39.                         )
  40.                     )
  41.                 )
  42.                 (vla-transformby obj mat)
  43.             )
  44.         )
  45.     )
  46.     (princ)
  47. )

I wondered if anyone could offer an insight into what I am overlooking in my calculations?
Or perhaps offer a better approach?

Many thanks,

Lee

kruuger

  • Swamp Rat
  • Posts: 637
Re: Copying Objects from Modelspace to Paperspace
« Reply #1 on: February 09, 2014, 04:54:45 PM »
Inspired by the request in this thread at CADTutor, I accepted the challenge of writing a program allowing the user to copy a selection of objects from modelspace to paperspace such that the objects will appear exactly as they are displayed through the active viewport.

I realise that it is already possible to transfer objects between modelspace/paperspace using the CHSPACE command, however, I was interested in the challenge of performing the various matrix transformations required to display the objects correctly.

I have written the following program as an initial draft of this idea, however, the program currently only performs correctly with viewports for which the viewtwist is zero:

Code - Auto/Visual Lisp: [Select]
  1. ;; Modelspace to Paperspace  -  Lee Mac
  2.  
  3. (defun c:ms2ps ( / doc idx lst mat org sel )
  4.     (cond
  5.         (   (= 1 (getvar 'tilemode))
  6.             (prompt "\nCommand only available in Paperspace.")
  7.         )
  8.         (   (= 1 (getvar 'cvport))
  9.             (prompt "\nPlease activate a viewport.")
  10.         )
  11.         (   (setq sel (ssget '((410 . "Model"))))
  12.             (repeat (setq idx (sslength sel))
  13.                 (setq lst (cons (vlax-ename->vla-object (ssname sel (setq idx (1- idx)))) lst))
  14.             )
  15.             (setq mat
  16.                 (vlax-tmatrix
  17.                     (append
  18.                         (mapcar
  19.                            '(lambda ( a b )
  20.                                 (append (mapcar '- (trans (trans a 0 2) 2 3) org) (list b))
  21.                             )
  22.                            '(
  23.                                 (1.0 0.0 0.0)
  24.                                 (0.0 1.0 0.0)
  25.                                 (0.0 0.0 1.0)
  26.                             )
  27.                             (setq org (trans (trans '(0.0 0.0 0.0) 0 2) 2 3))
  28.                         )
  29.                        '((0.0 0.0 0.0 1.0))
  30.                     )
  31.                 )
  32.             )
  33.             (foreach obj
  34.                 (vlax-invoke (setq doc (vla-get-activedocument (vlax-get-acad-object))) 'copyobjects lst
  35.                     (vla-get-block
  36.                         (vla-item
  37.                             (vla-get-layouts doc)
  38.                             (getvar 'ctab)
  39.                         )
  40.                     )
  41.                 )
  42.                 (vla-transformby obj mat)
  43.             )
  44.         )
  45.     )
  46.     (princ)
  47. )

I wondered if anyone could offer an insight into what I am overlooking in my calculations?
Or perhaps offer a better approach?

Many thanks,

Lee
really good start :)
first thing i checked it was qleader. chspace detach leader from text :(
your program works fine but leader scale is not correct after transformation. same with dims. maybe something else. i will test more.
i wish to see twist  :D
k.

Bhull1985

  • Guest
Re: Copying Objects from Modelspace to Paperspace
« Reply #2 on: February 10, 2014, 07:31:49 AM »
Wow sweet, definitely going to give this a whirl.

Bhull1985

  • Guest
Re: Copying Objects from Modelspace to Paperspace
« Reply #3 on: February 10, 2014, 07:39:21 AM »
Program is not working for this dwg ...perhaps i'm using it incorrectly?


Or just wasn't aware that the program completion copies the objects to clipboard.
A simple ctrl+v let me paste in the model, so I think it did work correctly.
Just so used to seeing additional prompts or whatnot esp from something Lee's created :D
« Last Edit: February 10, 2014, 07:47:17 AM by Bhull1985 »

trogg

  • Bull Frog
  • Posts: 255

cwake

  • Guest
Re: Copying Objects from Modelspace to Paperspace
« Reply #5 on: February 11, 2014, 03:52:55 AM »
You've inspired me to review transformation matrices when I get some time Lee. I'm afraid I've become quite rusty on matrix math - it's been >25 years.  :-(

I have in my library a copy of the old Express Tool version of CHSPACE (obviously written in LISP).
It uses the following approach:

From within the viewport...
1 - Set the UCS to "view" - that of the viewport.
2 - Record a ratio 'c' of the viewport height (group code 41) to the system variable "viewsize" to use later for scaling.
3 - Create a base point for a block definition by translating 0,0,0 from world to the current view UCS.
Code: [Select]
(setq d '(0.0 0.0 0.0)
      d (trans d 0 1)
      )
4 - Create a block of the selection set using the base point 'd' as calculated in step 3.
5 - Translate 'd' twice more to make it suitable as a block insert point in paperspace.
Code: [Select]
(setq d (trans d 1 2)
      d (trans d 2 3)
      )
6 - Insert the block prefixed with "*" to invoke exploding, in paperspace (with UCS world), at the translated insert point 'd', scale 'c' and rotation 0.0.
7 - If any of the entities were DIMENSIONS or LEADERS it processes them further, modifying their dimscale and dimlfac using the scale factor 'c' and if appropriate, values extracted from each entity's "ACAD" extended data.
8 - Purge the block definition.

It's the translation steps that intrigue me. Having become rusty on matrix maths, I do wonder if there are any shortcuts to the translation steps?
And I'd like to think that there is a way to calculate a transformation matrix based on extracting what the current UCS values are, instead of changing the UCS and using "Command" like the extended tool version does? But my matrix math needs a fair bit of revision.

If you didn't have access to the old Express Tool function, perhaps the process described above may give you a brainwave?
« Last Edit: February 13, 2014, 03:38:19 AM by cwake »

Bhull1985

  • Guest
Re: Copying Objects from Modelspace to Paperspace
« Reply #6 on: February 12, 2014, 03:01:25 PM »

It's the translation steps that intrigue me. Having become rusty on matrix maths, I do wonder if there are any shortcuts to the translation steps?

Here's a link to Lee's matrix transformation functions:
http://www.lee-mac.com/matrixtransformationfunctions.html
HTH

cwake

  • Guest
Re: Copying Objects from Modelspace to Paperspace
« Reply #7 on: February 13, 2014, 03:36:51 AM »
Thanks for the link BHull1985, but I was referring to the maths behind the programming, not the programming itself. You can't really write (or apply) a function that deals with this problem unless you understand the maths first. While I studied civil engineering and have excelled in that field, I haven't touched matrix maths in a very long time. Just need some spare time, and to dig up some old textbooks. LOL.

Bhull1985

  • Guest
Re: Copying Objects from Modelspace to Paperspace
« Reply #8 on: February 13, 2014, 07:01:53 AM »
Apologies, I figured with the functions on display the math would be discernible but i'm not surprised that it's not.
Lee has his knowledge...

cwake

  • Guest
Re: Copying Objects from Modelspace to Paperspace
« Reply #9 on: February 15, 2014, 01:25:58 PM »
This seems to work Lee... cross fingers.

The trans from WCS to DCS (0 - 2) was observing the viewtwist but it was creating a transformation matrix that was rotating in the wrong direction.

Code: [Select]
;; Modelspace to Paperspace  -  Lee Mac

(defun c:ms2ps ( / ang doc idx lst mat org sel )
  (cond
    (   (= 1 (getvar 'tilemode))
     (prompt "\nCommand only available in Paperspace.")
     )
    (   (= 1 (getvar 'cvport))
     (prompt "\nPlease activate a viewport.")
     )
    (   (setq sel (ssget '((410 . "Model"))))
     (repeat (setq idx (sslength sel))
       (setq lst (cons (vlax-ename->vla-object (ssname sel (setq idx (1- idx)))) lst))
       )
     (setq ang (getvar "viewtwist")
           mat
            (vlax-tmatrix
              (append
                (mapcar
                  '(lambda ( a b )
                     (append (mapcar '- (trans a 2 3) org) (list b))
                     )
                  (list (list (cos ang) (- (sin ang)) 0.0)
                        (list (sin ang) (cos ang) 0.0)
                        '(0.0 0.0 1.0)
                        )
                 
                  (setq org (trans (trans '(0.0 0.0 0.0) 0 2) 2 3))
                  )
                '((0.0 0.0 0.0 1.0))
                )
              )
           )
     (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)
       )
     )
    )
  (princ)
  )
(vl-load-com) (princ)
« Last Edit: February 15, 2014, 01:30:22 PM by cwake »

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: Copying Objects from Modelspace to Paperspace
« Reply #10 on: February 15, 2014, 03:16:20 PM »
Thanks Clint -
I haven't had much time lately to check back with this thread, but I shall look over your suggestion this weekend  :-)

cwake

  • Guest
Re: Copying Objects from Modelspace to Paperspace
« Reply #11 on: February 16, 2014, 01:57:21 AM »
I've been thinking about this some more... I didn't know (and still don't know) all of the inward workings of the TRANS function, but I wondered if since (trans a 0 2) was returning vectors that represented an additional twist whether (trans a 2 0) would create a matrix for rotating in the other direction, effectively the same matrix that I created. And it does. So here is a simpler version than the one I posted earlier.

Code: [Select]
;; Modelspace to Paperspace  -  Lee Mac

(defun c:ms2ps ( / doc idx lst mat org sel )
  (cond
    (   (= 1 (getvar 'tilemode))
     (prompt "\nCommand only available in Paperspace.")
     )
    (   (= 1 (getvar 'cvport))
     (prompt "\nPlease activate a viewport.")
     )
    (   (setq sel (ssget '((410 . "Model"))))
     (repeat (setq idx (sslength sel))
       (setq lst (cons (vlax-ename->vla-object (ssname sel (setq idx (1- idx)))) lst))
       )
     (setq mat
            (vlax-tmatrix
              (append
                (mapcar
                  '(lambda ( a b )
                     (append (mapcar '- (trans (trans a 2 0 T) 2 3) org) (list b))
                     )
                  '(
                    (1.0 0.0 0.0)
                    (0.0 1.0 0.0)
                    (0.0 0.0 1.0)
                    )
                  (setq org (trans (trans '(0.0 0.0 0.0) 0 2) 2 3))
                  )
                '((0.0 0.0 0.0 1.0))
                )
              )
           )
     (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)
       )
     )
    )
  (princ)
  )
(vl-load-com) (princ)


I have also wondered whether doing it that way would overcome any problems that may occur where the view had been altered with DVIEW -  like a 3D view. I hardly ever have reason to venture into that area of AutoCAD.

In the end, I'm not sure it matters? I think it currently does what most people would want of it in moving entities from modelspace to paperspace...? But presently it doesn't cater for DVIEW alterations, and the alternative is probably a whole lot more maths!! I came across some work by GILE which does do a lot more maths in transforming a coordinate, and I think that it would cover that situation, but it also does the rotation and translation in separate steps and returns a point co-ordinate, not a transformation matrix suitable for use with vla-transformby.

Whatever comes out of this, it's been an engaging exercise, and I now think I could create many transformation matrices from scratch, which I had no hope of doing before.
« Last Edit: February 16, 2014, 02:52:57 AM by cwake »

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: Copying Objects from Modelspace to Paperspace
« Reply #12 on: February 16, 2014, 02:40:09 PM »
Hi Clint,

Thank you for your continued interest in this thread, I'm pleased to hear that you have enjoyed working on the program.  :-)

Unfortunately, the matrix generated by your proposed solution is encoding a non-uniform scaling operation which cannot be applied to the selected set of objects (only uniformly scaling matrix operations can be applied using the ActiveX transformby method).

Therefore, I decided to start from scratch with the matrix transformations and, after studying gile's excellent WCS2PCS function, I have adapted some of his code for use in this program.

I have published the first release of the completed program to my site here: Modelspace to Paperspace.
The program appears to perform successfully in my limited testing.

Thank you to everyone who contributed to this thread, this has been an interesting program to tackle.

Lee

cwake

  • Guest
Re: Copying Objects from Modelspace to Paperspace
« Reply #13 on: February 16, 2014, 03:46:55 PM »
Awesome! I'm not surprised you went that way. Like you, the challenge for me was in the matrix math, which in my case needed some solid revision. I look forward to seeing what you've done. I'm now inspired to examine the extra matrix multiplication required to transform a nested object. If you do it first, and if you post it online, please post with ***SPOILER ALERT!!*** so I don't look? Haha. I'd like to work that one out by myself.