Author Topic: the relation of point in modelspace and paperspace  (Read 10938 times)

0 Members and 1 Guest are viewing this topic.

taner

  • Guest
the relation of point in modelspace and paperspace
« on: June 19, 2009, 09:03:46 PM »
Dear sirs,

A question have made me almost mad! Anyone can help me out?

What is he  relation of point in modelspace and paperspace? In other words, When Know the coordinates of one point in mode space, how can I get it's coordinates in paperspacer by using lisp code?

Thanks a lot

Lee Mac

  • Seagull
  • Posts: 12912
  • London, England
Re: the relation of point in modelspace and paperspace
« Reply #1 on: June 19, 2009, 09:22:50 PM »
Look into the TRANS function.

As quoted from ACAD help:

Quote
Translates a point (or a displacement) from one coordinate system to another

(trans pt from to [disp])
Arguments

pt
A list of three reals that can be interpreted as either a 3D point or a 3D displacement (vector).

from
An integer code, entity name, or 3D extrusion vector identifying the coordinate system in which pt is expressed. The integer code can be one of the following:

0 World (WCS)

1 User (current UCS)

2 If used with code 0 or 1, this indicates the Display Coordinate System (DCS) of the current viewport. When used with code 3, it indicates the DCS of the current model space viewport.

3 Paper space DCS (used only with code 2)

to
An integer code, entity name, or 3D extrusion vector identifying the coordinate system of the returned point. See the from argument for a list of valid integer codes.

disp
If present and is not nil, this argument specifies that pt is to be treated as a 3D displacement rather than as a point.


taner

  • Guest
Re: the relation of point in modelspace and paperspace
« Reply #2 on: June 19, 2009, 10:52:53 PM »
I can't express it clearly due to my poor english.

Please see the attached files.

test1 is the original file. It has a pagesetp config which name is "th-paper". I want to change the "th-paper" to it in test2 by using lisp code.
The problem is when I use (vla-SetWindowToPlot config ll ur),I do not know how to get ll ur in paperspace.

tks!

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: the relation of point in modelspace and paperspace
« Reply #3 on: June 20, 2009, 06:41:48 AM »
Hi,

Translations of coordinates between ModelSpace and PaperSpace (or inverse) can be done only about a specific viewport.

If a PaperSpace is active, you can use the trans function to translate pt coordinates :

From PaperSpace to ModelSpace WCS: (trans (trans pt 3 2) 2 0)

From ModelSpace to WCS PaperSpace: (trans (trans pt 0 2) 2 3)

Without activating a viewport, it can be done using these routines. They need a viewport pointer as argument (ENAME or VLA-OBJECT)

EDIT : added some missing routines
EDIT 2 : added one more missing routine


Code: [Select]
;; WCS2PCS (gile)
;; Translates a point WCS coordinates to the PaperSpace CS according to
;; the specified Viewport
;;
;; (WCS2PCS pt vp) is the same as (trans (trans pt 0 2) 2 3) when vp is active
;;
;; Arguments
;; pt : a point
;; vp : the viewport (ename or vla-object)

(defun WCS2PCS (pt vp / elst ang nor scl mat)
  (vl-load-com)
  (and (= (type vp) 'VLA-OBJECT)
       (setq vp (vlax-vla-object->ename vp))
  )
  (setq pt   (trans pt 0 0)
elst (entget vp)
ang  (cdr (assoc 51 elst))
nor  (cdr (assoc 16 elst))
scl  (/ (cdr (assoc 41 elst)) (cdr (assoc 45 elst)))
mat  (mxm
       (list (list (cos ang) (- (sin ang)) 0.0)
     (list (sin ang) (cos ang) 0.0)
     '(0.0 0.0 1.0)
       )
       (mapcar (function (lambda (v) (trans v nor 0 T)))
       '((1.0 0.0 0.0) (0.0 1.0 0.0) (0.0 0.0 1.0))
       )
     )
  )
  (mapcar '+
  (vxs (mxv mat (mapcar '- pt (cdr (assoc 17 elst)))) scl)
  (vxs (cdr (assoc 12 elst)) (- scl))
  (cdr (assoc 10 elst))
  )
)

;; PCS2WCS (gile)
;; Translates a point PaperSpace coordinates to WCS coordinates
;; according to the specified viewport
;;
;; (PCS2WCS pt vp) is the same as (trans (trans pt 3 2) 2 0) when vp is active
;;
;; Arguments
;; pt : a point
;; vp : the viewport (ename or vla-object)

(defun PCS2WCS (pt vp / ang nor scl mat)
  (vl-load-com)
  (and (= (type vp) 'VLA-OBJECT)
       (setq vp (vlax-vla-object->ename vp))
  )
  (setq pt   (trans pt 0 0)
elst (entget vp)
ang  (- (cdr (assoc 51 elst)))
nor  (cdr (assoc 16 elst))
scl  (/ (cdr (assoc 45 elst)) (cdr (assoc 41 elst)))
mat  (mxm
       (mapcar (function (lambda (v) (trans v 0 nor T)))
       '((1.0 0.0 0.0) (0.0 1.0 0.0) (0.0 0.0 1.0))
       )
       (list (list (cos ang) (- (sin ang)) 0.0)
     (list (sin ang) (cos ang) 0.0)
     '(0.0 0.0 1.0)
       )
     )
  )
  (mapcar '+
  (mxv mat
       (mapcar '+
       (vxs pt scl)
       (vxs (cdr (assoc 10 elst)) (- scl))
       (cdr (assoc 12 elst))
       )
  )
  (cdr (assoc 17 elst))
  )
)

;; VXS Multiply a vector by a scalar
;;
;; Arguments : a vector and a real

(defun vxs (v s) (mapcar (function (lambda (x) (* x s))) v))

;; VXV (gile)
;; Returns the dot product of two vectors (real)
;;
;; Arguments : two vectors
;; return : a real number

(defun vxv (v1 v2) (apply '+ (mapcar '* v1 v2)))

;; TRP
;; transposes a matrix -Doug Wilson-
;;
;; Argument : a matrix
;; return : a matrix

(defun trp (m) (apply 'mapcar (cons 'list m)))

;; MXV
;; Applies a transformation matrix to a vector  -Vladimir Nesterovsky-
;;
;; Arguments : une matrice et un vecteur
;; return : a vector

(defun mxv (m v)
  (mapcar '(lambda (r) (vxv r v)) m)
)

;; MXM
;; Multiplies (combinates) two matrices -Vladimir Nesterovsky-
;;
;; Arguments : deux matrices
;; return : a matrix

(defun mxm (m q)
  (mapcar '(lambda (r) (mxv (trp q) r)) m)
)
« Last Edit: June 22, 2009, 01:25:49 AM by gile »
Speaking English as a French Frog

taner

  • Guest
Re: the relation of point in modelspace and paperspace
« Reply #4 on: June 20, 2009, 07:12:38 AM »
Hi, Gile

Wonderful!You can always give me surprise!It is just what I want!

Thanks very much!

TanEr

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: the relation of point in modelspace and paperspace
« Reply #5 on: June 20, 2009, 07:19:02 AM »
Oopss !!!

I forgot to join some vector/matrix calculus routines needed by WCS2PCS and PCS2WCS functions.
The upper message is updated
Speaking English as a French Frog

litss

  • Guest
Re: the relation of point in modelspace and paperspace
« Reply #6 on: June 21, 2009, 09:15:39 PM »
seems missing a subroutine "vxs"

Lee Mac

  • Seagull
  • Posts: 12912
  • London, England
Re: the relation of point in modelspace and paperspace
« Reply #7 on: June 21, 2009, 10:00:49 PM »
My guess is that vxs goes something like this,...


Code: [Select]
(defun vxs (v s)
  (mapcar '(lambda (x) (* x s)) v))

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: the relation of point in modelspace and paperspace
« Reply #8 on: June 22, 2009, 12:01:02 AM »
Good guess Lee.

Code: [Select]
;; VXS Multiply a vector by a scalar
;;
;; Arguments : a vector and a real

(defun vxs (v s) (mapcar (function (lambda (x) (* x s))) v))
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: the relation of point in modelspace and paperspace
« Reply #9 on: June 22, 2009, 01:41:49 AM »
Lee, CAB, thanks dor compensating my absent-mindness.
Speaking English as a French Frog

Lupo76

  • Bull Frog
  • Posts: 343
Re: the relation of point in modelspace and paperspace
« Reply #10 on: August 26, 2015, 06:25:31 AM »
Hello Gile,
thank you very much for your code.
I'm using your function WCS2PCS, long time.
It works well, but now I have the need to use it with a VIEWPORT with its contents rotated 45 °.

Unfortunately the code of your function is too complicated to my knowledge.

Can anyone help me to implement it?
Thank you in advance

roy_043

  • Water Moccasin
  • Posts: 1895
  • BricsCAD 18
Re: the relation of point in modelspace and paperspace
« Reply #11 on: August 26, 2015, 04:17:55 PM »
It works well, but now I have the need to use it with a VIEWPORT with its contents rotated 45 °.
A quick test shows that the code works just fine in that situation.

Lupo76

  • Bull Frog
  • Posts: 343
Re: the relation of point in modelspace and paperspace
« Reply #12 on: August 27, 2015, 02:24:43 AM »
It works well, but now I have the need to use it with a VIEWPORT with its contents rotated 45 °.
A quick test shows that the code works just fine in that situation.

I'm using the .dwg file that I attached.
In the left window it is working properly, while the right window is not working.
Now I noticed that only works when you set the "Global UCS" (within the viewport), otherwise it is not working.

roy_043

  • Water Moccasin
  • Posts: 1895
  • BricsCAD 18
Re: the relation of point in modelspace and paperspace
« Reply #13 on: August 27, 2015, 03:52:00 AM »
The function (WCS2PCS) works properly I think. But, as the name says, it will translate a point from the WCS to the PCS.

To pick a point in the current UCS of a viewport the viewport must be active. Then using (trans) is the most obvious choice:
Code: [Select]
(trans (getpoint "\nPoint: ") 1 3)

roy_043

  • Water Moccasin
  • Posts: 1895
  • BricsCAD 18
Re: the relation of point in modelspace and paperspace
« Reply #14 on: August 27, 2015, 05:47:00 AM »
For AutoCAD you may need to use this:
Code: [Select]
(trans (trans (getpoint "\nPoint: ") 1 2) 2 3)See: http://docs.autodesk.com/ACD/2011/ENU/filesALR/WS1a9193826455f5ff1a32d8d10ebc6b7ccc-6905.htm

Lupo76

  • Bull Frog
  • Posts: 343
Re: the relation of point in modelspace and paperspace
« Reply #15 on: August 29, 2015, 12:09:48 PM »
Thank you all,
in fact the code of Gile, works properly.

I had made some mistakes in my code.
I apologize if I have wasted your time