Visual LISP, AutoLISP and General Customization
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Auto Update of XY coord in Model Space onto Paper Space (Layout Tab) block

24 REPLIES 24
SOLVED
Reply
Message 1 of 25
skintsubby
4043 Views, 24 Replies

Auto Update of XY coord in Model Space onto Paper Space (Layout Tab) block

 

Hi Folks.

 

I've tried doing a block that automatically updates the X and Y coordinates from its moveable leader. I can get it to work in model space or paper space. But my problem is I want it to work in PS, but update with the MS coordinates through the viewport.

 

I can get it to update, by LISP, by picking a point and passing the points to the blocks attributes. At the moment I have a LISP when actioned it locks the viewports, activates the viewport, and asks you to select a point. Once selected it de-activates the viewport (going back into layout tab) then asks which block you want to update. Where you now select the block and it updates the coordinates.  I.e. manually pick the point in MS, switch to PS and then manually pick the block. Then I have to manually modify the leader to the correct location.

 

I'm hoping that I can (In PS) move the end of the leader to snap to a position in MS and the coord updates to reflect the correct MS coordinates. I'm thinking that maybe this can't be done automatically and that I maybe can move the leader but I'm not sure how I can do it without running a LISP to update the block?

 

I'd be happy moving the leader then just running a single command and the leader coordinates updating without having to select the points and the block again.

  

I've attached the block showing the auto update of the attributes (fields).

 

I've also posted this on the Dynamic Blocks forum to see if it can be done through dynamic route.

 

I've also posted this on the Dynamic Blocks Forum, to see if it's possible through that option only.

 

Any thoughts?

 

Thanks

 

Mark

24 REPLIES 24
Message 2 of 25
bhull1985
in reply to: skintsubby

Interesting problem, and thanks for taking the time to properly outline your issue and what you're wanting done. Saying a sentence like that makes me wish I had the solution outright to give to you but I don't. I would spend some time with it this morning though in order to give a hand in solving. One request though, and it's just for clarification purposes.....would you mind to post sample dwg or images that illustrate your lisp problem?

 

I see that you've posted a sample dwg that has the block in question as per your description of the attachment, but I run acad 2012 and being as you're saving your .dwgs in a newer version of acad I, nor anyone using software older than autocad 2012, are able to open and view your sample dwg! 😕

 

If you go into the autocad options and select the "Open and Save" tab in the options screen, there at the top-left of the open and save tab is where you would change your autocad to save as, say, (and this is quite typical) autocad 2007 format. This allows just about everyone to view your dwgs. Just a suggestion, as I'm pretty sure there's a reason you're saving them 2013+. Just wanted to let you know and provide the workaround....just images or the older save would both be very helpful in attempting to fix your routine

Cheers

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Please use code tags and credit where credit is due. Accept as solution, if solved. Let's keep it trim people!
Message 3 of 25
skintsubby
in reply to: bhull1985

bhull

 

Thanks for taking the time to read... and fair enough comment.

 

Saved as 2007 release format.

 

Mark

Message 4 of 25
bhull1985
in reply to: skintsubby

And I was able to open it!

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Please use code tags and credit where credit is due. Accept as solution, if solved. Let's keep it trim people!
Message 5 of 25
bhull1985
in reply to: bhull1985

Looking at your block....and your post....

you mentioned that you have a lisp routine that does much of the necessary work already.

 

If you, when choosing the new point that you are moving the leader to in paperspace that you want to retrieve the model space coordinate from....you could, create a point at the new selected point. As in, create an object that you can snap to and preform the "chspace" command onto. If that would be allowable, you could write a conditional that places a "point" object at the point where you've moved your leader to, then execute the "chspace" command onto it, and then once it has been moved into modelspace, I.D. that point and save it to a variable, then pass that variable back to the text portions of your mleader?

 

I'm sure there are more experienced coders who would have a better way to accomplish this but at first and second glances this seems like it may be a decent way to accomplish what you're attempting?

Let me know what you think and perhaps we can proceede or if that won't work, why? For when someone more experienced comes around , so that they will have more information to go on.

Cheers 

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Please use code tags and credit where credit is due. Accept as solution, if solved. Let's keep it trim people!
Message 6 of 25
skintsubby
in reply to: bhull1985

bhull

 

My thoughts were to use either CHSPACE or TRANS. My problem is getting the coordinates.... No matter how it's done... Say I end up with the model space X & Y as a variable called mscoords.. I can transfer that into the block. But it's getting the coordinate thats my problem. My thoughts were I wanted to just grip the leader on the block and move it to wherever. and either the block automatically changes the coordinates. Or I then run a command (let's say it's called "updatecoord") and the blocks auto updated. 

 

My routine is a lot more complicated and the blocks we use are different, hence why I never posted them. I also didn't want to give anyone a starter. As that leads to people going down one route, whereas I'm happy / open minded on any solution, also there are coders with far greater experience and may know a simpler way. If I can get this working I can easily incorporate it into my main routine.

 

Again thanks for taking the time to reply.

 

Mark

Message 7 of 25
bhull1985
in reply to: skintsubby

Take a look at this thread and see if there is an acceptable method of obtaining your modelspace point from paperspace.

 

http://www.cadtutor.net/forum/showthread.php?21820-Modelspace-coordinates-from-paperspace-viewports

 

Let us know!

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Please use code tags and credit where credit is due. Accept as solution, if solved. Let's keep it trim people!
Message 8 of 25
bhull1985
in reply to: bhull1985

This may also be of use:

 

http://docs.autodesk.com/ACD/2013/ENU/index.html?url=files/GUID-0A1B6F6E-B686-4816-A8E0-618A197C49A5...

 

and this:

http://www.theswamp.org/index.php?topic=42503.0

 

and this:

http://forums.cadalyst.com/showthread.php?t=5885

 

Hopefully there is a method within these pages that suffices for your needs!

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Please use code tags and credit where credit is due. Accept as solution, if solved. Let's keep it trim people!
Message 9 of 25
skintsubby
in reply to: bhull1985

bhull.

 

That doesn't really help. I know how to get my viewport limits etc... It's  linking it to the block that's the problem.

 

Mark

Message 10 of 25
Ajilal.Vijayan
in reply to: skintsubby

I think the problem is with the Original block Insertion Point.

 

I modified your block and  with the below code I was able to update the MS coord.

 

I did following changes to the block.[Please find the attached file]

 

1.Deleted the constraints.

2.Moved the insertion point of block to 0,0,0

3.Added a Basepoint parameter.

4.Included the base point parameter to the stretch action.

5.Reinserted the block again.

6. Run the below command.

 

(vl-load-com)
(Defun C:PSMS (/ Obj PScrd MScrd EOBJ )


(setq Obj (entsel))
(setq PScrd (cdr(assoc 10 (entget (car Obj)))))
(setq MScrd (trans PScrd 3 2))

(setq EOBJ (vlax-ename->vla-object (car Obj)))
(if (= (vla-get-hasattributes EOBJ) :vlax-true)
 	(progn
 	  (foreach N (vlax-safearray->list
 		   (variant-value
 			 (vla-getattributes EOBJ)
 		   )
 		 )
	    (if (= (vla-get-tagstring N) "X")
	      (vla-put-textstring N (car MScrd)))
             
            (if (= (vla-get-tagstring N) "Y")
	      (vla-put-textstring N (cadr MScrd))
             )
);for each
	  );progn
     );if

(princ)

);defun
Message 11 of 25
skintsubby
in reply to: Ajilal.Vijayan

ajilal Fantastic. That is excellent... exactly what I was after You're biggest help was changing the insertion point / adding the basepoint and the way you linked it to the TRANS function. Thats what I was having trouble with. I appreciate the time you've spent on it... and thank you. (also thanks to bhull for his input) Mark
Message 12 of 25
bhull1985
in reply to: skintsubby

Great! Congrats on resolving your issue.

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Please use code tags and credit where credit is due. Accept as solution, if solved. Let's keep it trim people!
Message 13 of 25
skintsubby
in reply to: Ajilal.Vijayan

ajilal

 

It works great. However the one problem I have is when I have a rotated viewport or a rotated UCS in the viewport. I always need to report WCS values

 

I thought the line  (setq MScrd (trans PScrd 3 2)) was transfering the pioint PScrd from 3 (paper space) to 2 (the display DCS) which is set at WCS    ( from --> (Trans pt from to [disp]) )

 

However it doesn't return the WCS values?yet when I change it to (setq MScrd (trans PScrd 3 0)) 0 being WCS I get bad arguement type: consp nil

 

So my question is ..  Am i miss-reading the TRANS function? Or doing something else wrong?

 

Mark

Message 14 of 25
Lee_Mac
in reply to: skintsubby

For PCS to WCS (with an active viewport) you will need to use:

 

(trans (trans <point> 3 2) 2 0)

 

Message 15 of 25
skintsubby
in reply to: Lee_Mac

Hi Lee

 

Thanks for replying

 

With (setq MScrd (trans (trans PScrd 3 2) 2 0)) I still get different values than the model space WCS values

 

Your line "With an active viewport" confused me.. for instance. I'm in Papersapce. I have one viewport (which is rotated) the block is in paperspace as well. There are no active viewports at the moment. When I run the lisp I get the wrong XY's yet if the viewport isn't rotated everthing is fine?

 

 

I think I've probably explained that poorly. You'll see what I mean if you open the drawing ajilal posted. Just rotate the viewport and you get different results to what the grid should be.

 

Thanks

 

Mark

Message 16 of 25
Lee_Mac
in reply to: skintsubby


@skintsubby wrote:

Your line "Within an active viewport" confused me.. for instance. I'm in Papersapce. I have one viewport (which is rotated) the block is in paperspace as well. There are no active viewports at the moment.


 

Mark,

 

Since we are transforming the coordinates from PCS (3) (aka PSDCS) to the DCS (2) of the active viewport, and then from DCS (2) to WCS (0), the viewport in question needs to be active to ensure that the correct DCS is used by the trans function. With no viewport active, the trans function is translating from PCS to WCS (since the Paperspace Layout viewport is the active viewport), and hence not accounting for the viewport configuration.

 

For transformation with an inactive viewport, I would suggest using gile's PCS2WCS function in conjunction with the viewport entity - here is a very quick draft:

 

(defun c:test ( / br ip vp )
    (if
        (and
            (setq br (LM:ssget "\nSelect block: " '("_+.:E:S:L" ((0 . "INSERT") (66 . 1)))))
            (setq vp (LM:ssget "\nSelect vport: " '("_+.:E:S"   ((0 . "VIEWPORT")))))
        )
        (progn
            (setq br (vlax-ename->vla-object (ssname br 0))
                  ip (PCS2WCS (vlax-get br 'insertionpoint) (ssname vp 0))
            )
            (foreach a (vlax-invoke br 'getattributes)
                (if (= "X" (strcase (vla-get-tagstring a)))
                    (vla-put-textstring a (rtos (car  ip) 2 0))
                    (vla-put-textstring a (rtos (cadr ip) 2 0))
                )
            )
        )
    )
    (princ)
)

;; PCS2WCS (gile)
;; Translates a PCS point to WCS based on the supplied Viewport
;;
;; (PCS2WCS pt vp) is the same as (trans (trans pt 3 2) 2 0) when vp is active
;;
;; pnt : PCS point
;; ent : Viewport ename

(defun PCS2WCS ( pnt ent / ang enx mat nor scl )
    (setq pnt (trans pnt 0 0)
          enx (entget ent)
          ang (- (cdr (assoc 51 enx)))
          nor (cdr (assoc 16 enx))
          scl (/ (cdr (assoc 45 enx)) (cdr (assoc 41 enx)))
          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 pnt scl)
                (vxs (cdr (assoc 10 enx)) (- scl))
                (cdr (assoc 12 enx))
            )
        )
        (cdr (assoc 17 enx))
    )
)

;; Matrix Transpose  -  Doug Wilson
;; Args: m - nxn matrix

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

;; Matrix x Matrix  -  Vladimir Nesterovsky
;; Args: m,n - nxn matrices

(defun mxm ( m n )
    ((lambda ( a ) (mapcar '(lambda ( r ) (mxv a r)) m)) (trp n))
)

;; Matrix x Vector  -  Vladimir Nesterovsky
;; Args: m - nxn matrix, v - vector in R^n

(defun mxv ( m v )
    (mapcar '(lambda ( r ) (apply '+ (mapcar '* r v))) m)
)

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

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

;; ssget  -  Lee Mac
;; A wrapper for the ssget function to permit the use of a custom selection prompt
;; msg - selection prompt
;; arg - list of ssget arguments

(defun LM:ssget ( msg arg / sel )
    (princ msg)
    (setvar 'nomutt 1)
    (setq sel (vl-catch-all-apply 'ssget arg))
    (setvar 'nomutt 0)
    (if (not (vl-catch-all-error-p sel)) sel)
)

(vl-load-com) (princ)
Message 17 of 25
Ajilal.Vijayan
in reply to: Lee_Mac

@ Lee,

Like always Excellent code Lee.

 

Mark,

Have you checked Lee's code ?

I slightly modified Lee's code to avoid asking to select Viewport, as you said you are working on single viewport.

 

Full code in here.

Spoiler
(defun c:test ( / br ip vp )


;;Blackbox
(defun cvprt()
    (setq ss
           (ssget "_x"
                  (list '(0 . "VIEWPORT")
                        (cons 410 (getvar 'ctab))
                        '(-4 . "<NOT")
                        '(69 . 1)   ; <-- Ignore pviewport                              
                        '(-4 . "NOT>")
                        )
                  )
          )
);cvprt


    (if
        (and
            (setq br (LM:ssget "\nSelect block: " '("_+.:E:S:L" ((0 . "INSERT") (66 . 1)))))
            ;(setq vp (LM:ssget "\nSelect vport: " '("_+.:E:S"   ((0 . "VIEWPORT")))))
			(setq vp (cvprt))
        )
        (progn
            (setq br (vlax-ename->vla-object (ssname br 0))
                  ip (PCS2WCS (vlax-get br 'insertionpoint) (ssname vp 0))
            )
            (foreach a (vlax-invoke br 'getattributes)
                (if (= "X" (strcase (vla-get-tagstring a)))
                    (vla-put-textstring a (rtos (car  ip) 2 0))
                    (vla-put-textstring a (rtos (cadr ip) 2 0))
                )
            )
        )
    )
    (princ)
)

;; PCS2WCS (gile)
;; Translates a PCS point to WCS based on the supplied Viewport
;;
;; (PCS2WCS pt vp) is the same as (trans (trans pt 3 2) 2 0) when vp is active
;;
;; pnt : PCS point
;; ent : Viewport ename

(defun PCS2WCS ( pnt ent / ang enx mat nor scl )
    (setq pnt (trans pnt 0 0)
          enx (entget ent)
          ang (- (cdr (assoc 51 enx)))
          nor (cdr (assoc 16 enx))
          scl (/ (cdr (assoc 45 enx)) (cdr (assoc 41 enx)))
          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 pnt scl)
                (vxs (cdr (assoc 10 enx)) (- scl))
                (cdr (assoc 12 enx))
            )
        )
        (cdr (assoc 17 enx))
    )
)

;; Matrix Transpose  -  Doug Wilson
;; Args: m - nxn matrix

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

;; Matrix x Matrix  -  Vladimir Nesterovsky
;; Args: m,n - nxn matrices

(defun mxm ( m n )
    ((lambda ( a ) (mapcar '(lambda ( r ) (mxv a r)) m)) (trp n))
)

;; Matrix x Vector  -  Vladimir Nesterovsky
;; Args: m - nxn matrix, v - vector in R^n

(defun mxv ( m v )
    (mapcar '(lambda ( r ) (apply '+ (mapcar '* r v))) m)
)

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

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

;; ssget  -  Lee Mac
;; A wrapper for the ssget function to permit the use of a custom selection prompt
;; msg - selection prompt
;; arg - list of ssget arguments

(defun LM:ssget ( msg arg / sel )
    (princ msg)
    (setvar 'nomutt 1)
    (setq sel (vl-catch-all-apply 'ssget arg))
    (setvar 'nomutt 0)
    (if (not (vl-catch-all-error-p sel)) sel)
)

(vl-load-com) (princ)
Message 18 of 25
skintsubby
in reply to: Ajilal.Vijayan

Lee

 

Apoligies for not replying yesterday.

 

I've spent this morning incorporating your code into mine (which is a bit of a fudged version of Ajilal's). and didn't want to reply unti lit I had it all working perfectly. which it now does.

 

Ajilal.

 

Thanks for your update. Can Blackbox's routine be modified to just select the last active viewport? ie. there is the odd drawing we have a couple of viewports on,,, ie the main viewport and a keyplan...  I liked how your original code worked that way. purely bacause it avoids one step of selecting the viewport. No problem if it doesn't.. I'm pretty happy with what I now have.

 

I have to say that there is no way I'd have managed this without the help and advice / time taken from both of you. I've pretty much done a cut and paste for the main part of my routine. I can understand parts of the routines, and some of it (mainly gile's part) I'd never have managed. So thanks for the help and pointing me in the correct direction.

 

Mark

 

 

 

Message 19 of 25
Lee_Mac
in reply to: Ajilal.Vijayan


@Ajilal.Vijayan wrote:

@ Lee,

Like always Excellent code Lee.

 


 

Thank you Ajilal Smiley Happy

 


@skintsubby wrote:

Lee

 

Apoligies for not replying yesterday.

 

I've spent this morning incorporating your code into mine (which is a bit of a fudged version of Ajilal's). and didn't want to reply until I had it all working perfectly. which it now does.

 


 

Excellent, you're welcome Mark.

Message 20 of 25
jamesdcc
in reply to: Lee_Mac

Hi all, hope this gets someone. I was looking for this exact thing and it does work fine. But.......... it would appear that the coordinates are wrong. Any ideas why this would be and how to fix? Thanks for any feedback

 

James

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report

”Boost