Author Topic: Lisp to Create viewport  (Read 36552 times)

0 Members and 1 Guest are viewing this topic.

HasanCAD

  • Swamp Rat
  • Posts: 1421
Lisp to Create viewport
« on: July 01, 2012, 02:58:27 AM »
This is final release for lisp to create a viewport for selected area
Your comments are welcome
Code: [Select]
;|------------ Viewport Creator ----------------
    q_|_|| _\|| q_|| _\|

  - Select area
  - ask for viewport scale
  - Go to last layout (first at right)
  - Ask for center point of viewport
  - Create a viewport
  - view the selected area with the viewport
  Create stations for Prestresed tendon

------------------------------------------------
  Author: Hasan M. Asous, 2010
ALL RIGHT RESERVED TO ALL
  Contact: HasanCAD @ TheSwamp.org,
           asos2000 @ CADTutor.net
           HasanCAD@gmail.com
------------------------------------------------
  Version: 1      2012 07 01
________________________________________________
      |;

;     q_|_|| _\|| q_|| _\|     ;
;       Mainroutine Start      ;

(defun c:NV (/       doc      p1 p2 temp   mp
     scl      SC       cd:DWG_LayoutsList   res
     a       b        vpp vpdoc vp
    )

  (vl-load-com)
  (setq Doc (vla-get-ActiveDocument (vlax-get-Acad-Object)))
  (setq p1 (getpoint "\Select objects to view:"))
  (setq p2 (getcorner p1))
  (if
    (< (car (trans p2 1 0)) (car (trans p1 1 0)))
     (setq tmp p1
   p1  p2
   p2  tmp
     )
     T
  )
  (setq mp (list (/ (+ (car p1) (car p2)) 2)
(/ (+ (cadr p1) (cadr p2)) 2)
0.0
   )
  )

  (setq sc (cond ((getint (strcat "\nWhat is Viewport Scale 1: <"
  (itoa (setq sc (cond (sc)
       (50)
)
)
  )
  ">: "
  )
  )
)
(sc)
   )
  )
  (setq cd:DWG_LayoutsList
(vlax-for % (vla-get-layouts Doc)
   (setq res (cons (list (vla-get-name %)
(vla-get-TabOrder %)
%
   )
   res
     )
   )
)
  )
  (setvar "CTab"
  (caar (vl-sort cd:DWG_LayoutsList
'(lambda (a b) (> (cadr a) (cadr b)))
)
  )
  )

  (setq VPDoc (vla-get-PaperSpace doc))
  (setq VPp (vlax-3D-point (getpoint "\nSelect Point for Viewport")))
  (setq VP (vla-AddPViewport
     VPDoc
     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
  )
  (vl-cmdf "_.zoom" (strcat (RTOS (/ 1.0 SC)) "xp"))
  (vla-regen (vla-get-activedocument (vlax-get-acad-object))
     acActiveViewport
  )
  (vla-put-mspace doc :vlax-FALSE)
  (VLA-PUT-DisplayLocked VP :vlax-true)
)


;     q_|_|| _\|| q_|| _\|     ;
;       Mainroutine End        ;

(princ "\n Type  NV  to Invoke")
(princ)

Updated
« Last Edit: February 27, 2013, 02:59:44 AM by HasanCAD »

kruuger

  • Swamp Rat
  • Posts: 633
Re: Lisp to Create viewport
« Reply #1 on: July 01, 2012, 03:25:35 AM »
hi HasanCAD

lisp not working. there are missing data for VPP variable (center of viewport in paper space).

kruuger

HasanCAD

  • Swamp Rat
  • Posts: 1421
Re: Lisp to Create viewport
« Reply #2 on: July 01, 2012, 04:11:17 AM »
Updated

kruuger

  • Swamp Rat
  • Posts: 633
Re: Lisp to Create viewport
« Reply #3 on: July 01, 2012, 04:28:08 PM »
Updated
if you don't mind. Probably still not perfect:
Code - Auto/Visual Lisp: [Select]
  1. (defun c:NV (/ *error* _RestoreView p1 p2 doc ct vs vc tmp mp sc ll res vpdoc vpp vp ans)
  2.   (defun *error* (Msg)
  3.     (princ "Error: ")
  4.     (princ Msg)
  5.     (if ct (_RestoreView))
  6.     (princ)
  7.   )
  8.   (defun _RestoreView ()
  9.     (setvar "ctab" ct)
  10.   )
  11.   (if (/= (getvar "cvport") 1)
  12.     (if
  13.       (and
  14.         (setq p1 (getpoint "\nSelect first point of view: "))
  15.         (setq p2 (getcorner p1 "\nSelect second point of view: "))
  16.       )
  17.       (progn
  18.               ct (getvar "ctab")
  19.               vs (getvar "viewsize")
  20.               vc (getvar "viewctr")
  21.               sc (cond
  22.                    ( (getint
  23.                        (strcat
  24.                          "\nWhat is Viewport Scale 1: <"
  25.                          (itoa (setq sc (cond (sc) (50))))
  26.                          ">: "
  27.                        )
  28.                      )
  29.                    )
  30.                    ( sc )
  31.                  )
  32.         )
  33.         (setq ll
  34.           (vlax-for % (vla-get-layouts doc)
  35.             (setq res
  36.               (cons
  37.                 (list
  38.                   %
  39.                   (vla-get-TabOrder %)
  40.                 )
  41.                 res
  42.               )
  43.             )
  44.           )
  45.         )
  46.         (vla-put-ActiveLayout doc
  47.           (caar
  48.             (vl-sort ll
  49.              '(lambda (a b)
  50.                 (> (cadr a) (cadr b))
  51.               )
  52.             )
  53.           )
  54.         )
  55.         (vla-put-MSpace doc :vlax-false)
  56.         (if (setq vpp (getpoint "\nSelect Point for Viewport: "))
  57.           (progn
  58.             (if
  59.               (<
  60.                 (car (trans p2 1 0))
  61.                 (car (trans p1 1 0))
  62.               )
  63.               (setq tmp p1 p1 p2 p2 tmp)
  64.             )
  65.             (setq mp
  66.               (list
  67.                  (/ (+ (car p1) (car p2)) 2)
  68.                  (/ (+ (cadr p1) (cadr p2)) 2)
  69.                  0.0
  70.               )
  71.             )
  72.             (setq vpdoc (vla-get-PaperSpace doc)
  73.                   vp (vla-AddPViewport
  74.                        vpdoc
  75.                        (vlax-3d-point vpp)
  76.                        (/ (- (car p2) (car p1)) sc)
  77.                        (/ (- (cadr p2) (cadr p1)) sc)
  78.                      )
  79.             )
  80.             (vla-display vp :vlax-true)
  81.             (vla-put-MSpace doc :vlax-true)
  82.             (vla-put-ActivePViewport doc vp)
  83.             (vla-ZoomCenter
  84.               (vlax-get-acad-object)
  85.               (vlax-3d-point mp)
  86.               1.0
  87.             )
  88.             (vla-put-CustomScale vp (/ 1. sc))
  89.             (vla-put-MSpace doc :vlax-false)
  90.             (vla-put-DisplayLocked vp :vlax-true)
  91.             (initget "Yes No")
  92.             (setq ans
  93.               (cond
  94.                 ( (getkword "\nBack to model space [Yes/No] <No>: ") )
  95.                 ( "No" )
  96.               )
  97.             )
  98.             (if (= ans "Yes") (_RestoreView))
  99.           )
  100.           (progn
  101.             (princ "\n** Invalid Point ** ")
  102.             (if ct (_RestoreView))
  103.           )
  104.         )
  105.       )
  106.       (princ "\n** Invalid Point ** ")
  107.     )
  108.     (princ "\nStart Program in Model Space ")
  109.   )
  110.   (princ)
  111. )
  112. (princ "\n Type NV to Invoke ")
kruuger

KewlToyZ

  • Guest
Re: Lisp to Create viewport
« Reply #4 on: July 01, 2012, 05:24:03 PM »
My routines below:
Code - Auto/Visual Lisp: [Select]
  1. (defun c:vpt()
  2. ;===================Turn off command line responses==============
  3. (command "CMDECHO" 0);DO NOT CHANGE THIS LINE
  4. ;=======================================================       
  5.        
  6.         (setq userlayer (getvar "clayer"))
  7.         (setq ds2 (getvar "dimscale"))
  8.         (setq userview (getvar "ctab"))
  9.         (setq myTile (getvar "TILEMODE"))
  10.         (autoload "LRG" '("LRG"))
  11.         (autoload "MPL" '("MPL"))
  12.        
  13.         (if (> 0 myTile)
  14.                         (prompt "\n   Switching view tab......")
  15.                         (command "tilemode" 0)
  16.         )
  17.  
  18.         (c:LRG)
  19.         (setvar "CLAYER" "G-VIEW-PORT")        
  20.        
  21.         (princ "\n")
  22.         (command "osmode" "523")
  23.         (princ "\n")
  24.         (command "mview")
  25.         (princ "\n")
  26.         (command (setq p1 (getpoint "\n   Select first corner of window opening: ")))
  27.         (prompt "\n ")
  28.         (command (getcorner p1 "\n   Select opposite corner of window opening: "))
  29.         (prompt "\n ")
  30.         (command "mspace")
  31.        
  32.         ; moved MPL for set scale to invoke in mspace only
  33.         (c:MPL)
  34.        
  35.         (princ "\n")
  36.         (command "zoom" "e")
  37.         (princ "\n")
  38.         (command "zoom" "c")
  39.         (princ "\n")
  40.         (command (getpoint "\n   Select center of view:"))
  41.         (princ "\n")
  42.         (command (strcat "1/" (rtos (getvar "dimscale") 2 0) "xp"))
  43.         (princ "\n")
  44.         (command "pspace")
  45.         (princ "\n")
  46.         (setvar "psltscale" 0)
  47.         (princ "\n")
  48.         (prompt "\n   DIMSCALE returning to original setting. ")
  49.         (princ "\n")
  50.         (setvar "dimscale" ds2)
  51.        
  52. ;===================Turn on command line responses==============
  53. (command "CMDECHO" 1);DO NOT CHANGE THIS LINE
  54. ;======================================================
  55.         (prompt "\n   Returning view and layer.....")
  56.         (command "ctab" userview)
  57.         (command "clayer" userlayer)
  58.         (prompt "\n   View and layer returned!")
  59.         (princ)
  60. )
  61.  
« Last Edit: July 02, 2012, 03:56:02 PM by KewlToyZ »

martinle

  • Newt
  • Posts: 22
Re: Lisp to Create viewport
« Reply #5 on: July 01, 2012, 11:58:54 PM »
This is final release for lisp to create a viewport for selected area
Your comments are welcome
Code: [Select]
;|------------ Viewport vreator ----------------
    q_|_|| _\|| q_|| _\|

  - Select area
  - ask for viewport scale
  - Go to last layout (first at right)
  - Ask for center point of viewport
  - Create a viewport
  - view the selected area with the viewport
  Create stations for Prestresed tendon

------------------------------------------------
  Author: Hasan M. Asous, 2010
ALL RIGHT RESERVED TO ALL
  Contact: HasanCAD @ TheSwamp.org,
           asos2000 @ CADTutor.net
           HasanCAD@gmail.com
------------------------------------------------
  Version: 1      2012 07 01
________________________________________________
      |;

;     q_|_|| _\|| q_|| _\|     ;
;       Mainroutine Start      ;

(defun c:NV (/       doc      p1 p2 temp   mp
     scl      SC       cd:DWG_LayoutsList   res
     a       b        vpp vpdoc vp
    )

  (vl-load-com)
  (setq Doc (vla-get-ActiveDocument (vlax-get-Acad-Object)))
  (setq p1 (getpoint "\Select objects to view:"))
  (setq p2 (getcorner p1))
  (if
    (< (car (trans p2 1 0)) (car (trans p1 1 0)))
     (setq tmp p1
   p1  p2
   p2  tmp
     )
     T
  )
  (setq mp (list (/ (+ (car p1) (car p2)) 2)
(/ (+ (cadr p1) (cadr p2)) 2)
0.0
   )
  )

  (setq sc (cond ((getint (strcat "\nWhat is Viewport Scale 1: <"
  (itoa (setq sc (cond (sc)
       (50)
)
)
  )
  ">: "
  )
  )
)
(sc)
   )
  )
  (setq cd:DWG_LayoutsList
(vlax-for % (vla-get-layouts Doc)
   (setq res (cons (list (vla-get-name %)
(vla-get-TabOrder %)
%
   )
   res
     )
   )
)
  )
  (setvar "CTab"
  (caar (vl-sort cd:DWG_LayoutsList
'(lambda (a b) (> (cadr a) (cadr b)))
)
  )
  )

  (setq VPDoc (vla-get-PaperSpace doc))
  (setq VPp (vlax-3D-point (getpoint "\nSelect Point for Viewport")))
  (setq VP (vla-AddPViewport
     VPDoc
     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
  )
  (vl-cmdf "_.zoom" (strcat (RTOS (/ 1.0 SC)) "xp"))
  (vla-regen (vla-get-activedocument (vlax-get-acad-object))
     acActiveViewport
  )
  (vla-put-mspace doc :vlax-FALSE)
  (VLA-PUT-DisplayLocked VP :vlax-true)
)


;     q_|_|| _\|| q_|| _\|     ;
;       Mainroutine End        ;

(princ "\n Type  NV  to Invoke")
(princ)

Updated

Hello HasanCAD

The Lisp works very well. It would be great if you could choose the layout where you want to create the view window.
Would this be feasible?

Martin

HasanCAD

  • Swamp Rat
  • Posts: 1421
Re: Lisp to Create viewport
« Reply #6 on: July 02, 2012, 03:15:59 AM »
Updated
if you don't mind. Probably still not perfect:
...
kruuger

Of course i would not mind, but give me a time to study yours, compare with mine and see what is missing in mine.
Thanks Krugger for development.
« Last Edit: February 27, 2013, 03:01:05 AM by HasanCAD »

HasanCAD

  • Swamp Rat
  • Posts: 1421
Re: Lisp to Create viewport
« Reply #7 on: July 02, 2012, 03:25:00 AM »
My routines below:
...
Thanks for new idea
but gives error
Code: [Select]
Initializing...
      Scale list being loaded, Please be patient......
bad argument type: numberp: nil

KewlToyZ

  • Guest
Re: Lisp to Create viewport
« Reply #8 on: July 02, 2012, 12:22:26 PM »
My routines below:
...
Thanks for new idea
but gives error
Code: [Select]
Initializing...
      Scale list being loaded, Please be patient......
bad argument type: numberp: nil

I have heard that before. I use it with my own settings and I am not sure what others are missing. I got it working in versions 2010 through 2013 on my end, but I have a reactor built into the startup from here for ScaleListEdit. I'm just wondering what variables I am missing for it.

kruuger

  • Swamp Rat
  • Posts: 633
Re: Lisp to Create viewport
« Reply #9 on: July 02, 2012, 12:44:47 PM »
My routines below:
Code: [Select]
(defun c:vpt()
;=============================================================================Turn off command line responses
(command "CMDECHO" 0);DO NOT CHANGE THIS LINE
;=============================================================================

(setq userlayer (getvar "clayer"))
(setq ds2 (getvar "dimscale"))
(setq userview (getvar "ctab"))
(setq myTile (getvar "TILEMODE"))
(autoload "LRG" '("LRG"))
;(autoload "setviewscale" '("setviewscale"))
(autoload "MPL" '("MPL"))
;(setq vptLayer ("G-VIEW-PORT"))

;(> <NUMB1> <NUMB2>)                      Returns T if <NUMB1>is greater than <NUMB2>.
(if (> 0 myTile)
(prompt "\n   Switching view tab......")
(command "tilemode" 0)
)

(c:LRG)
(setvar "CLAYER" "G-VIEW-PORT")   

(princ "\n")
(command "osmode" "523")
(princ "\n")
(command "mview")
(princ "\n")
(command (setq p1 (getpoint "\n   Select first corner of window opening: ")))
(prompt "\n ")
(command (getcorner p1 "\n   Select opposite corner of window opening: "))
(prompt "\n ")
(command "mspace")

; moved setscale to invoke in mspace only
;(c:setviewscale)
(c:MPL)

(princ "\n")
(command "zoom" "e")
(princ "\n")
(command "zoom" "c")
(princ "\n")
(command (getpoint "\n   Select center of view:"))
(princ "\n")
(command (strcat "1/" (rtos (getvar "dimscale") 2 0) "xp"))
(princ "\n")
    (command "pspace")
    (princ "\n")
(setvar "psltscale" 0)
(princ "\n")
(prompt "\n   DIMSCALE returning to original setting. ")
(princ "\n")
(setvar "dimscale" ds2)

;=============================================================================Turn off command line responses
(command "CMDECHO" 1);DO NOT CHANGE THIS LINE
;=============================================================================
(prompt "\n   Returning view and layer.....")
(command "ctab" userview)
(command "clayer" userlayer)
(prompt "\n   View and layer returned!")
(princ)
)
i saw that you are using some subroutine form here: http://web2.airmail.net/terrycad/AutoLISP-Code.htm (like set_tile_list, maybe more)
without loading some additional function program will not work.
kruuger

KewlToyZ

  • Guest
Re: Lisp to Create viewport
« Reply #10 on: July 02, 2012, 01:14:15 PM »
My routines below:
Code - Auto/Visual Lisp: [Select]
  1. (defun c:vpt()
  2. ;=============================================================================Turn off command line responses
  3. (command "CMDECHO" 0);DO NOT CHANGE THIS LINE
  4. ;============================================================================= 
  5.        
  6.         (setq userlayer (getvar "clayer"))
  7.         (setq ds2 (getvar "dimscale"))
  8.         (setq userview (getvar "ctab"))
  9.         (setq myTile (getvar "TILEMODE"))
  10.         (autoload "LRG" '("LRG"))
  11.         (autoload "MPL" '("MPL"))
  12.        
  13.         (if (> 0 myTile)
  14.                         (prompt "\n   Switching view tab......")
  15.                         (command "tilemode" 0)
  16.         )
  17.  
  18.         (c:LRG)
  19.         (setvar "CLAYER" "G-VIEW-PORT")        
  20.        
  21.         (princ "\n")
  22.         (command "osmode" "523")
  23.         (princ "\n")
  24.         (command "mview")
  25.         (princ "\n")
  26.         (command (setq p1 (getpoint "\n   Select first corner of window opening: ")))
  27.         (prompt "\n ")
  28.         (command (getcorner p1 "\n   Select opposite corner of window opening: "))
  29.         (prompt "\n ")
  30.         (command "mspace")
  31.        
  32.         ; moved MPL set scale to invoke in mspace only
  33.         (c:MPL)
  34.        
  35.         (princ "\n")
  36.         (command "zoom" "e")
  37.         (princ "\n")
  38.         (command "zoom" "c")
  39.         (princ "\n")
  40.         (command (getpoint "\n   Select center of view:"))
  41.         (princ "\n")
  42.         (command (strcat "1/" (rtos (getvar "dimscale") 2 0) "xp"))
  43.         (princ "\n")
  44.         (command "pspace")
  45.         (princ "\n")
  46.         (setvar "psltscale" 0)
  47.         (princ "\n")
  48.         (prompt "\n   DIMSCALE returning to original setting. ")
  49.         (princ "\n")
  50.         (setvar "dimscale" ds2)
  51.        
  52. ;=============================================================================Turn off command line responses
  53. (command "CMDECHO" 1);DO NOT CHANGE THIS LINE
  54. ;============================================================================= 
  55.         (prompt "\n   Returning view and layer.....")
  56.         (command "ctab" userview)
  57.         (command "clayer" userlayer)
  58.         (prompt "\n   View and layer returned!")
  59.         (princ)
  60. )
  61.  
i saw that you are using some subroutine form here: http://web2.airmail.net/terrycad/AutoLISP-Code.htm (like set_tile_list, maybe more)
without loading some additional function program will not work.
kruuger
Yes I included the additional routines in my zip file?
One is a layer insert routine, the other is a scale setting routine.
« Last Edit: July 02, 2012, 03:52:11 PM by KewlToyZ »

antistar

  • Guest
Re: Lisp to Create viewport
« Reply #11 on: July 05, 2012, 09:52:36 AM »
HasanCAD,
Congratulations, this routine is very helpful to me.
However, in some designs, it returns the following error:

Select Point for Viewport
ERROR: Automation Error. Invalid arqument Height in AddPViewport method

I work in centimeters.

Now a suggestion:
I work with many layouts in drawings.
There are how to select layout that I want to creat the viewport?

Thanks in advance
« Last Edit: July 05, 2012, 10:08:13 AM by antistar »

kruuger

  • Swamp Rat
  • Posts: 633
Re: Lisp to Create viewport
« Reply #12 on: July 09, 2012, 06:14:10 AM »
Now a suggestion:
I work with many layouts in drawings.
There are how to select layout that I want to creat the viewport?

The Lisp works very well. It would be great if you could choose the layout where you want to create the view window.
Would this be feasible?

you can try this one.
kruuger

martinle

  • Newt
  • Posts: 22
Re: Lisp to Create viewport
« Reply #13 on: July 10, 2012, 12:05:05 AM »
Hello Kruuger!

Unfortunately I get an error message:


command:
Select Point for Viewport: Error: Automation error Invalid argument
Height in methodRegeneriert AddPViewport model.

Martin

kruuger

  • Swamp Rat
  • Posts: 633
Re: Lisp to Create viewport
« Reply #14 on: July 10, 2012, 06:53:20 AM »
Hello Kruuger!

Unfortunately I get an error message:


command:
Select Point for Viewport: Error: Automation error Invalid argument
Height in methodRegeneriert AddPViewport model.

Martin
hmm, this is very weird.
sometimes is Invalid Height, sometimes Error setting current viewportRestoring cached viewports. and sometimes is working.
i will try to investigate this. maybe replace with (command "vports" ...)
kruuger