Change Base Insertion Point of Viewport

Change Base Insertion Point of Viewport

Anonymous
Not applicable
2,545 Views
8 Replies
Message 1 of 9

Change Base Insertion Point of Viewport

Anonymous
Not applicable

Hello,

 

I am new to creating AutoCAD LISPs. I found the lisp below to help create viewports in paper space from limits set in model space. I would like to have the basepoint of the viewport be the Top-right corner when I insert it into paper space, instead of being centered as it is now.

 

I am using this to create windows in paper space that focus on a particular rows of a master table. (cable schedule) I can then select the rows of  cables I want to be shown in Paper Space.

 

Note: When using LISP below, First point must be bottom-right corner, and second point must be top-left or it won't work.

 

Thanks,

 

Paul

 

 (defun c:NV (/ *error* _RestoreView p1 p2 doc ct vs vc tmp mp sc ll res vpdoc vpp vp ans)
      (defun *error* (Msg)
        (princ "Error: ")
        (princ Msg)
        (if ct (_RestoreView))
        (princ)
      )
      (defun _RestoreView ()
        (setvar "ctab" ct)
        (vla-ZoomCenter (vlax-Get-Acad-Object) (vlax-3d-Point (trans vc 1 0)) vs)
      )
      (vl-load-com)
      (if (/= (getvar "cvport") 1)
        (if
          (and
            (setq p1 (getpoint "\nSelect first point of view: "))
            (setq p2 (getcorner p1 "\nSelect second point of view: "))
          )
          (progn
            (setq doc (vla-get-ActiveDocument (vlax-get-acad-object))
                  ct (getvar "ctab")
                  vs (getvar "viewsize")
                  vc (getvar "viewctr")
                  sc (cond
                       ( (getint
                           (strcat
                             "\nWhat is Viewport Scale 1: <"
                             (itoa (setq sc (cond (sc) (1))))
                             ">: "
                           )
                         )
                       )
                       ( sc )
                     )
            )
            (setq ll
              (vlax-for % (vla-get-layouts doc)
                (setq res
                  (cons
                    (list
                      %
                      (vla-get-TabOrder %)
                    )
                    res
                  )
                )
              )
            )
            (vla-put-ActiveLayout doc
              (caar
                (vl-sort ll
                 '(lambda (a b)
                    (> (cadr a) (cadr b))
                  )
                )
              )
            )
            (vla-put-MSpace doc :vlax-false)
            (if (setq vpp (getpoint "\nSelect Point for Viewport: "))
              (progn
                (if
                  (<
                    (car (trans p2 1 0))
                    (car (trans p1 1 0))
                  )
                  (setq tmp p1 p1 p2 p2 tmp)
                )
                (setq mp
                  (list
                     (/ (+ (car p1) (car p2)) 2)
                     (/ (+ (cadr p1) (cadr p2)) 2)
                     0.0
                  )
                )
                (setq vpdoc (vla-get-PaperSpace doc)
                      vp (vla-AddPViewport
                           vpdoc
                           (vlax-3d-point vpp)
                           (/ (- (car p2) (car p1)) sc)
                           (/ (- (cadr p2) (cadr p1)) sc)
                         )
                )
                (vla-display vp :vlax-true)
                (vla-put-MSpace doc :vlax-true)
                (vla-put-ActivePViewport doc vp)
                (vla-ZoomCenter
                  (vlax-get-acad-object)
                  (vlax-3d-point mp)
                  1.0
                )
                (vla-put-CustomScale vp (/ 1. sc))
                (vla-put-MSpace doc :vlax-false)
                (vla-put-DisplayLocked vp :vlax-true)
                (initget "Yes No")
                (setq ans
                  (cond
                    ( (getkword "\nBack to model space [Yes/No] <No>: ") )
                    ( "No" )
                  )
                )
                (if (= ans "Yes") (_RestoreView))
              )
              (progn
                (princ "\n** Invalid Point ** ")
                (if ct (_RestoreView))
              )
            )
          )
          (princ "\n** Invalid Point ** ")
        )
        (princ "\nStart Program in Model Space ")
      )
      (princ)
    )
    (princ "\n Type NV to Invoke ")
    (princ)

0 Likes
2,546 Views
8 Replies
Replies (8)
Message 2 of 9

Kent1Cooper
Consultant
Consultant

@Anonymous_92 wrote:

.... I would like to have the basepoint of the viewport be the Top-right corner when I insert it into paper space, instead of being centered as it is now.

....

Note: When using LISP below, First point must be bottom-right corner, and second point must be top-left or it won't work.

....


The need for picking particular corners is probably because this part:

 

                (if
                  (<
                    (car (trans p2 1 0))
                    (car (trans p1 1 0))
                  )
                  (setq tmp p1 p1 p2 p2 tmp)
                )

 

compares only X coordinates  to decide whether to swap corners, without accounting for the Y coordinates.  That could be dealt with this way, in place of  that (if) function:

 

  (setq

    LL (mapcar 'min p1 p2); = Lower Left

    UR (mapcar 'max p1 p2); = Upper Right

    p1 LL p2 UR

  )

 

and it would end up with p1 at lower left and p2 at upper right [which is what it looks like its other calculations want -- I hope I'm reading that right], regardless of which two opposite corners were picked, or in which order.  [Of course, it could just use LL & UR in place of p1 & p2 in later calculations, and not bother to reset p1 & p2.]

 

That way of making a Viewport wants to be given a center, a width and a height.  You would want to calculate where the center is from your picked upper right corner, something like changing this:

 

                (setq vpdoc (vla-get-PaperSpace doc)
                      vp (vla-AddPViewport
                           vpdoc
                           (vlax-3d-point vpp)
                           (/ (- (car p2) (car p1)) sc)
                           (/ (- (cadr p2) (cadr p1)) sc)
                         )
                )

 

to this [untested]:

 

                (setq vpdoc (vla-get-PaperSpace doc)

                      vpwid (/ (- (car p2) (car p1)) sc); set to variables earlier, to use twice

                      vpht (/ (- (cadr p2) (cadr p1)) sc)
                      vp (vla-AddPViewport
                           vpdoc
                           (vlax-3d-point (mapcar '- vpp (list (/ vpwid 2) (/ vpht 2) 0)); back off left & down by half of each dimension
                           vpwid
                           vpht
                         )
                )

 

 

Kent Cooper, AIA
Message 3 of 9

Anonymous
Not applicable

Thanks Kent,

 

Now a view port can be created with any order and orientation of selected points.

 

I am still unable to change the base point from which the viewport is created. I would like to have my cursor attached to one of the top corners when the view port is created.

 

Thanks,

 

Paul

 

 

0 Likes
Message 4 of 9

Kent1Cooper
Consultant
Consultant

@Anonymous_92 wrote:

.... 

I am still unable to change the base point from which the viewport is created. I would like to have my cursor attached to one of the top corners when the view port is created.

.... 


I don't quite understand what you're asking.  Do you want to force the User to pick a top-edge corner first, for some reason?  If so, the first prompt can be specific about that, and the two picked corners can be compared in their Y coordinates, and if the second is greater, it could scold the User and have them try again.  But if it matters which corners are which, that can all be figured from the p1 p2 LL UR settings already discussed, without requiring the User to pick on a certain edge first.  And would either top corner do just as well as the other?

 

Or is what you mean something else?  Can you describe the desired steps in detail, or post a diagram or screencast, or something?

Kent Cooper, AIA
0 Likes
Message 5 of 9

Anonymous
Not applicable

Hi Kent,

 

Attached are two screenshots of what I am looking at.

 

The first, the viewport is centered around the cursor when it is created. (as the LSP works now)

 

The second (what i want to happen) when the viewport is created, the cursor is gripped to the top right corner.

 

Hope this clarifies the functionality I'm trying to get from this lisp.

 

Thanks,

 

Paul

0 Likes
Message 6 of 9

Kent1Cooper
Consultant
Consultant

So you want to make the upper-right-corner grip "hot" as if you had picked on it, but programmatically so you don't have to pick on it?  I don't know of a way to do that, but a Search for "make grip hot" reveals that the question has come up before -- I didn't dig very deep, but didn't see a way, though who knows? -- there may be a way in one of those threads.  The ones I looked at were mostly about dynamic Blocks or making multiple grips hot, but look around -- there might be something more like your situation.

Kent Cooper, AIA
0 Likes
Message 7 of 9

Anonymous
Not applicable

Hi Kent,

 

I've posted two more photos. I may not have explained correctly.

 

The first Photo (BAD) is how it works now. Since the cursor is centered in the view port, the VP cannot be easily aligned with other object.

 

The second photo (good), is how I would like it. The top right corner is the base point, and view port can be aligned with objects.

 

Thanks,

 

Paul

0 Likes
Message 8 of 9

Anonymous
Not applicable

I don't know if this will help or not but I just use mview to create viewports in paperspace.

     (setvar 'clayer "Viewports")
     (setq mvpt1 (list 5.5 4.25))
     (setq mvpt2 (list 7.0 5.75))
     (command "mview" mvpt1 mvpt2)
     (command "mspace")
     (command "-view" "_TOP")
     (setq vpzm1 (list 121.0 75.0))
     (setq vpzm2 (list 136.0 90.0))
     (command "zoom" "W" vpzm1 vpzm2)
     (command "grid" "off")
     (command "pspace")
0 Likes
Message 9 of 9

Kent1Cooper
Consultant
Consultant

It's still not clear to me.  Are you wanting to Move the Viewport immediately after creating it, with the routine establishing the point from which to Move it?  Your "good" image shows the middle of the right edge, not the top corner as previously described -- which is it?  A Move command can easily be built in after the Viewport is created, calculating the start point for it.  If you really meant the top right corner:

 

(command "_.move" (entlast) "" p2 pause)

Kent Cooper, AIA
0 Likes