Viewport with specific scale

Viewport with specific scale

Anonymous
Not applicable
4,177 Views
4 Replies
Message 1 of 5

Viewport with specific scale

Anonymous
Not applicable

Hi everyone, 

 

I am working in a bigger lisp that will create new layouts containing titleblock and viewport. I have the first part figure out, but I don't know how to proceed with the viewports. I want to make a new viewport that will fit in a rectangle in the paperspace and will have a specific scale determined by the drawing and will be center to fit in another rectangle in the model space. 

My idea so far is the following:

  1. Create a new viewport
  2. Origin and dimension of the rectangle are known (not a problem)
  3. Read an attribute in the drawing to know the scale (we use different templates for different scales)
  4. Take that scale and apply it to the viewport
  5. And somehow align the rectangle within the viewport (model space) with the rectangle of the viewport. 

I know how to do the first steps, but I am not sure about number 4 and completely lost with 5. 

Any ideas about how to solve it this way or with other approach?

 

 PS. I was thinking that I could make the rectangle in model space a block, and when I create the viewport I can zoom to object (and that should make the scale work)

0 Likes
Accepted solutions (1)
4,178 Views
4 Replies
Replies (4)
Message 2 of 5

paullimapa
Mentor
Mentor
Accepted solution

#4 is similar to what you would do manually. But I would do this step after #5. 

 

#5 to do what you suggested, use this command sequence:

(command"_.MSPACE""_.ZOOM""_OBJECT""_ALL""")

 

Once you've zoomed into the rectangular object, then to do #4 will depend on how your scale information is stored.  For example, if your scale is always stored in this pattern 1"=8'-0", then I would create a list with this pattern:

(setq scl-lst (list "1\"=8'-0\"" "1\"=4'-0\"" "1\"=2'-0\"" ))

Also create a matching list with the actual xp zoom factor:

(setq zm-lst (list "0.010416xp" "0.02083xp" "0.04166xp"))

So let's say you've retrieved the scale information from the attribute and stored it as a text in symbol scl-txt:

(if(setq match (member scl-txt scl-lst))

 (progn ; if matching scale pattern found

  (setq match-len (- (length scl-lst)(length match))) ; find position in the scale list

  (setq zmfac(nth match-len zm-lst)) ; get the matching zoom factor position from the zoom list

  ;;; Finally, use this lisp sequence to complete #4:

  (command"_.ZOOM" zmfac)

 )

 (princ"\nNo Matching Scale Found")

)

Area Object Link | Attribute Modifier | Dwg Setup | Feet-Inch Calculator
Layer Apps | List on Steroids | VP Zoom Scales | Exchange App Store


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
0 Likes
Message 3 of 5

Anonymous
Not applicable

Thank you Pli!
I will be trying out you suggestions. I will have to tweak the step to zoom in the rectangle in model space because we have a palette tool outside of the drawing area, and zoom all won't work.
I will come back once I have some time to give it another try, hopefully to post the solution.

0 Likes
Message 4 of 5

mgorecki
Collaborator
Collaborator

Hi, we have a similar process where I work.  Instead of a program creating the viewports, we have them defined in the templates.  That way they are always centered when you use the template.  Then it's just a matter of scaling the viewport.  Here is some code to find the viewport to change its scale.  We put our viewports on layer "Viewport", so that's what it looks for (there are usually 2 viewports that it will find, even if you only created one in your template).

We call our layouts "P1", "P2", ... so layoutNum is the tab.

(setvar "ctab" (vla-get-name layoutNum)) ;"P2", "P3", "P4"....
    ;--- Change Viewport Scale ---
    ;Create a selection set of the viewports on the current layout tab only
    (setq vpSS (ssget "X" (list '(0 . "VIEWPORT")(cons 410 (getvar "ctab")))))
    (setq vpCounter 0)
    (while (< vpCounter (sslength vpSS))
     ;Make sure you have the correct viewport (the one on layer "VIEWPORT")
     (setq vpLayer (cdr (assoc 8 (entget (ssname vpSS vpCounter)))))
     (if (= vpLayer "VIEWPORT")
      ;Set the scale of the viewport to 1:1
      (vla-put-customscale (vlax-ename->vla-object (ssname vpSS vpCounter)) 1.0)
     ) ;ends if
     (setq vpCounter (+ vpCounter 1))
    ) ;ends while

Best regards,

 

Mark

Message 5 of 5

Anonymous
Not applicable

Pli's solution was good! For future references I will paste my code for the whole sequence.

    (defun VP ();Main function
        ;;DECLARING SUB-FUNCTIONS

        ;GETATT finds the scale of the drawing
        (defun GETATT ()
            (setq att_tag "SCALE");attribute that defines scale
            (setq blkname "TITLE-L1");block that holds the attribute
            (if (and(/= blkname nil) (/= att_tag nil))
                (progn  
                    (ssget "x" (list '(0 . "INSERT") (cons 2 (strcat blkname ",`*U*")) '(66 . 1)));select block
                    (vlax-for item (vla-get-activeselectionset (vla-get-activedocument (vlax-get-acad-object)))
                        (if (eq (strcase blkname) (strcase (vla-get-effectivename item)));(dynamic blocks when modified change their names)
                            (foreach att (vlax-safearray->list (vlax-variant-value (vla-getattributes item)));look for attributes inside of block
                                (if (= (strcase att_tag) (vla-get-tagstring att)) (setq scl-txt (vla-get-textstring att)));save scale value from attribute
                            );;foreach
                        );;if
                    )
                );;progn
            );;if/=
        );End of GETATT
        ;;-------------------------------------------------------------------------


        ;ZOOMVP will zoom to the drawing area defined by the chosen paper size 
        (defun ZOOMVP ()

            (if (setq ss (ssget "_X" (list '(0 . "INSERT") (cons 2 PS))));select block PS (eg. 18x24)
                (progn
                    (COMMAND "ZOOM" "OBJECT" ss "");zoom to the object
                )
            )        
        );End of ZOOMVP
        ;;----------------------------------------------------------------

        (defun VPSC ();VPSC Sets up viewport scale with the values got in GETATT
        (if (= MS "IMP")
            (progn
                (setq scl-lst (list "1'-0\"=1'-0\"" "6\"=1'-0\"" "3\"=1'-0\"" "1 1/2\"=1'-0\"" "1\"=1'-0\"" "3/4\"=1'-0\"" "1/2\"=1'-0\"" "3/8\"=1'-0\"" "1/4" "3/16\"=1'-0\"" "1/8\"=1'-0\"" "3/32\"=1'-0\"" "1/16\"=1'-0\"" "1/32\"=1'-0\"" "1/64\"=1'-0\"" "1/128\"=1'-0\""))
                (setq zm-lst (list "1xp" "1/2xp" "1/4xp" "1/8xp" "1/12xp" "1/16xp" "1/24xp" "1/32xp" "1/48xp" "1/64xp" "1/96xp" "1/128xp" "1/192xp" "1/384xp" "1/768xp" "1/1536xp"))
            )
            (progn
                (setq scl-lst (list "1:1" "1:2" "1:5" "1:10" "1:20" "1:25" "1:50" "1:75" "1:100" "1:125" "1:150" "1:200" "1:250" "1:300" "1:500" "1:1000" ))
                (setq zm-lst (list "1xp" "1/2xp" "1/5xp" "1/10xp" "1/20xp" "1/25xp" "1/50xp" "1/75xp" "1/100xp" "1/125xp" "1/150xp" "1/200xp" "1/300xp" "1/500xp" "1/1000xp"))
            )
        )

        (if(setq match (member scl-txt scl-lst))
            (progn ; if matching scale pattern found
                (setq match-len (- (length scl-lst)(length match))) ; find position in the scale list
                (setq zmfac(nth match-len zm-lst)) ; get the matching zoom factor position from the zoom list
                (command"_.ZOOM" zmfac)
            )
            (princ"\nNo Matching Scale Found")
        )
        );end VPSC
        ;;--------------------------------------------------------------------


        ;MAIN FUNCTION
        (if (setq VPSELECT (ssget "_X" '((0 . "VIEWPORT"))));select viewport (already created in template)
           (progn
               (setq d (vla-get-activedocument (vlax-get-acad-object)))
               (vla-put-mspace d :vlax-true);step into viewport
            )
        )

        (GETATT)
        (ZOOMVP)
        (VPSC)
        (vla-put-mspace d :vlax-false);step out of viewport
        (COMMAND "ZOOM" "E")
        (command "mview" "l" "ON" VPSELECT "") ;lock viewport
        (princ)
    );end of VP

This code is part of a bigger script, so it it missing the

(defun C:VPSC ()
0 Likes