Viewport outline draw with change space combination issue

Viewport outline draw with change space combination issue

danglar
Advocate Advocate
3,394 Views
18 Replies
Message 1 of 19

Viewport outline draw with change space combination issue

danglar
Advocate
Advocate

This lisp is my combination of 2 subroutines first one - can create viewport outline in model space.

Second one use selection set from the first one in order to copy it to model space

;;; VPLIMITS.lsp draws the limits of a Paperspace Viewport Boundary in
;;; Modelspace
;;; Based on VPLIM.lsp written by Murray Clack, February 18, 1999
;;;
;;; Modified by Stephen Bindon for Circlular Viewports
;;;
;;; Modified by Tod Winn to stop ALERT STATEMENT.
;;; Modified to place on a non plotting XLINE layer.
;;; Modified to add error coding.
;;; Modified to redefined variables
;;; Modified again to place on a non plotting G-ANNO-NPLT layer.

;;; |vp| - Get ViewPort object
;;; |ht| - Get Outside HeighT of Viewport
;;; |wd| - Get Oustide WiDth of Viewport
;;; |vn| - Get Viewport Number
;;; |ctr| - Get CenTeR of Viewport
;;; |ctrx| - Calculate X of Viewport CenTeR
;;; |ctry| - Caluculate Y of Viewport CenTeR
;;; |vs| - Get View Size of viewport
;;; |xp| - Calculate XP factor of viewport
;;; |iw| - Calculate Width of viewport
;;; |bl| - Calculate Bottom Left corner of viewport
;;; |br| - Calculate Bottom Right corner of viewport
;;; |tr| - Calculate Top Right corner of viewport
;;; |tl| - Calculate Top Left corner of viewport
;;; |plinewid| - Save Pline Width
;;; |osmode| - Save OSmode

;;; VPL error code
(defun |vplerror| (|msg|)
  (if (or (= |msg| "Function cancelled")
	  (= |msg| "quit / exit abort")
      )
    (princ (strcat "\nError: " |msg|))
  )
  (command ".ucs" "p")			;Restore UCS back
  (setvar "clayer" |clayer|)		;Restore old Current Layer
  (setvar "cmdecho" |cmdecho|)		;Restore old CMDECHO
  (setvar "osmode" |osmode|)		;Restore osmode
  (setq *error* |olderror|)		;Restore old Error
  (command ".pspace")			;Go Back To Papserspace
  (command "undo" "end")		;End UNDO
  (princ)
)

;;; alert statement
;;; (alert "\nMake sure DEFPOINTS layer is On and Thawed! ")

;;; start function and define variables
(defun c:vpl (/		|vp|	  |ht|	    |wd|      |vn|
	      |ctr|	|ctrx|	  |ctry|    |vs|      |xp|
	      |iw|	|bl|	  |br|	    |tr|      |tl|
	      |osmode|	|clayer|  |cmdecho| |olderror|
	      |vplerror|
	     )
  (command "undo" "begin")		;Set UNDO 
  (setq |olderror| *error*)		;Get current Error
  (setq *error* |vplerror|)		;Set Error 
  (setq |clayer| (getvar "clayer"))	;Get current Layer
  (setq |cmdecho| (getvar "cmdecho"))	;Get CMDECHO
  (setq |osmode| (getvar "osmode"))	;Get OSMODE
  (setvar "cmdecho" 0)			;Turn off command echoing
  (command ".pspace")			;Enter pspace
  (if (tblsearch "layer" "defpoints")
    (command "_.-layer"	"_thaw"	"defpoints" "_on" "defpoints" "")
  )
  
  (setq ss (ssget "_:L"))
  
  (setq	|vp| (entget			;Select viewport boundary
	       (car (entsel "\nSelect Viewport to Draw Boundary in "))
	     )
  )
  (setq |ht| (cdr (assoc 41 |vp|)))	;Get Viewport height with
  (setq |wd| (cdr (assoc 40 |vp|)))	;Get Viewport width with
  (setq |vn| (cdr (assoc 69 |vp|)))	;Get Viewport Number
  (command ".mspace")			;enter mspace
  (setvar "cvport" |vn|)		;Set correct viewport
  (command ".ucs" "v")			;Set UCS to View
  (setq |ctr| (getvar "viewctr"))	;Get VIEWCTR store as CTR
  (setq |ctrx| (car |ctr|))		;Get X of CTR
  (setq |ctry| (cadr |ctr|))		;Get Y of CTR
  (setq |vs| (getvar "viewsize"))	;Get inside Viewport height
  (setq |xp| (/ |ht| |vs|))		;Get XP Factor with HeighT /
					;View Size
  (setq |iw| (* (/ |vs| |ht|) |wd|))	;Get inside width of Viewport by
  (setq |bl| (list (- |ctrx| (/ |iw| 2)) (- |ctry| (/ |vs| 2))))
					;Find four corners of Viewport
  (setq |br| (list (+ |ctrx| (/ |iw| 2)) (- |ctry| (/ |vs| 2))))
  (setq |tr| (list (+ |ctrx| (/ |iw| 2)) (+ |ctry| (/ |vs| 2))))
  (setq |tl| (list (- |ctrx| (/ |iw| 2)) (+ |ctry| (/ |vs| 2))))
  
  (setvar "osmode" 0)			;Turn off Osnaps
  (command ".pline" |bl| |br| |tr| |tl| "c") ;Draw pline inside border
  (command ".ucs" "p")			;Restore UCS back
  (setvar "clayer" |clayer|)		;Restore old Current Layer
  (setvar "cmdecho" |cmdecho|)		;Restore old CMDECHO
  (setvar "osmode" |osmode|)		;Restore osmode
  (setq *error* |olderror|)		;Restore old Error
  (command ".pspace")			;Go Back To Papserspace
  (command "undo" "end")		;End UNDO
  (princ)				;Clean up command prompt
)

;;; |vp| - Get ViewPort object
;;; |vn| - Get Viewport Number
;;; |ctr| - Get CenTeR of Viewport
;;; |vs| - Get View Size of viewport
;;; |plinewid| - Save PlineWid
;;; |osmode| - Save OSmode

;;; VPC error code
(defun |vpcerror| (|msg|)
  (if (or (= |msg| "Function cancelled")
	  (= |msg| "quit / exit abort")
      )
    (princ (strcat "\nError: " |msg|))
  )
  (command ".ucs" "p")			;Restore UCS back
  (setvar "clayer" |clayer|)		;Restore old Current Layer
  (setvar "cmdecho" |cmdecho|)		;Restore old CMDECHO
  (setvar "osmode" |osmode|)		;Restore osmode
  (setq *error* |olderror|)		;Restore old Error
  (command ".pspace")			;Go Back To Papserspace
  (princ)
)

(defun c:CSC (/ ss2 ss i)
 
(vl-load-com)

(setq ss (ssget "P"))
 
(if (setq ss2 (ssadd)
 
ss (ssget "P")
 
)
 
(progn
 
(repeat (setq i (sslength ss))
 
(ssadd
 
(vlax-vla-object->ename (vla-copy (vlax-ename->vla-object (ssname ss (setq i (1- i))))))
 
ss2
 
)
 
)
 
(vl-cmdf "_.chspace" ss2 "" "")
 (command "._pspace")
)
 
)
 
(princ)
 
)



(defun c:CGC (/ ss2 ss i)

(c:vpl)
(c:csc)

)
;(c:cgc)

I have  2 problems:

 

1. Program working properly on rectangular viewports only

but I need it on polygonal viewports too.

2. Now program need two selections: one - for viewport in order to draw outline

and second one - for change space

Is it possible to use one selection only (for PS entities and viewport)?

Any help will be very appreciated

0 Likes
Accepted solutions (3)
3,395 Views
18 Replies
Replies (18)
Message 2 of 19

ВeekeeCZ
Consultant
Consultant

And THIS Lee's VP Outline does not fit to you?

0 Likes
Message 3 of 19

Moshe-A
Mentor
Mentor

@danglar hi,

 

here is my fix, replace the following code lines in code

actually to achieve what you want, you do not need the part code bellow (c:vpl) command

in respond to Select Objects do a crossing on the viewport (to grab the viewport + the polyline in case of non rectangular viewport)

 

limitation: in case of non rectangular viewport the layout can have only 1 viewport.

 

enjoy

moshe

 

;; start function and define variables
(defun c:vpl (/		|vp|	  |ht|	    |wd|      |vn|
	      |ctr|	|ctrx|	  |ctry|    |vs|      |xp|
	      |iw|	|bl|	  |br|	    |tr|      |tl|
	      |osmode|	|clayer|  |cmdecho| |olderror|
	      |vplerror|
	      ent0 ent1 ent2 ent3 AcDbPline0 AcDbPline1
	     )
  (command "undo" "begin")		;Set UNDO 
  (setq |olderror| *error*)		;Get current Error
  (setq *error* |vplerror|)		;Set Error 
  (setq |clayer| (getvar "clayer"))	;Get current Layer
  (setq |cmdecho| (getvar "cmdecho"))	;Get CMDECHO
  (setq |osmode| (getvar "osmode"))	;Get OSMODE
  (setvar "cmdecho" 0)			;Turn off command echoing
  (command ".pspace")			;Enter pspace
  (if (tblsearch "layer" "defpoints")
    (command "_.-layer"	"_thaw"	"defpoints" "_on" "defpoints" "")
  )
  
  (setq ss (ssget "_:L" '((0 . "lwpolyline,viewport"))))

  (cond
   ((= (sslength ss) 1)
    (setq |vp| (entget (ssname ss 0)))
    (setq |ht| (cdr (assoc 41 |vp|)))	;Get Viewport height with
    (setq |wd| (cdr (assoc 40 |vp|)))	;Get Viewport width with
    (setq |vn| (cdr (assoc 69 |vp|)))	;Get Viewport Number
    (command ".mspace")			;enter mspace
    (setvar "cvport" |vn|)		;Set correct viewport
    (command ".ucs" "v")		;Set UCS to View
    (setq |ctr| (getvar "viewctr"))	;Get VIEWCTR store as CTR
    (setq |ctrx| (car |ctr|))		;Get X of CTR
    (setq |ctry| (cadr |ctr|))		;Get Y of CTR
    (setq |vs| (getvar "viewsize"))	;Get inside Viewport height
    (setq |xp| (/ |ht| |vs|))		;Get XP Factor with HeighT /
					;View Size
    (setq |iw| (* (/ |vs| |ht|) |wd|))	;Get inside width of Viewport by
    (setq |bl| (list (- |ctrx| (/ |iw| 2)) (- |ctry| (/ |vs| 2))))
					;Find four corners of Viewport
    (setq |br| (list (+ |ctrx| (/ |iw| 2)) (- |ctry| (/ |vs| 2))))
    (setq |tr| (list (+ |ctrx| (/ |iw| 2)) (+ |ctry| (/ |vs| 2))))
    (setq |tl| (list (- |ctrx| (/ |iw| 2)) (+ |ctry| (/ |vs| 2))))
  
    (setvar "osmode" 0)			;Turn off Osnaps
    (command ".pline" |bl| |br| |tr| |tl| "c") ;Draw pline inside border
    (command ".ucs" "p")			;Restore UCS back
   ); case
   ((= (sslength ss) 2)
    (vl-load-com)
    (setq ent1 (ssname ss 0) ent2 (ssname ss 1))
    (if (eq (cdr (assoc '0 (entget ent1))) "LWPOLYLINE")
     (setq ent0 ent1)
     (setq ent0 ent2)
    )

    (setq AcDbPline0 (vlax-ename->vla-object ent0))
    (setq AcDbPline1 (vla-copy AcDbPline0))
    (setq ent3 (vlax-vla-object->ename AcDbPline1))
    (command ".chspace" "si" ent3)
   ); case
  ); cond

  
  (setvar "clayer" |clayer|)		;Restore old Current Layer
  (setvar "cmdecho" |cmdecho|)		;Restore old CMDECHO
  (setvar "osmode" |osmode|)		;Restore osmode
  (setq *error* |olderror|)		;Restore old Error
  (command ".pspace")			;Go Back To Papserspace
  (command "undo" "end")		;End UNDO
  (princ)				;Clean up command prompt
)

 

0 Likes
Message 4 of 19

danglar
Advocate
Advocate

First of all thank you all respondents

Lee Mak routine don't fit to my needs because it can select viewports only and when I change a selection filter routine returns error in most cases although working somehow 

Moshe-A fit is better but works on polylines and viewports

 

(setq ss (ssget "_:L" '((0 . "lwpolyline,viewport"))))

I can fix it by myself

but limitation on single viewport.. it's understood why but unfortunately unacceptable

because non-rectangular viewport comes over many other viewports as boundary

Is it possible to do something? 

 

0 Likes
Message 5 of 19

danglar
Advocate
Advocate

I did some modifications (see attached lisp)

Now routine working on single rectangular or polygonal viewport

program working properly but remans one question

Is it possible to make selection for viewport and rest entities in "one shot"?

0 Likes
Message 6 of 19

dbhunia
Advisor
Advisor

I also think @Lee_Mac "VPOutlineV1-2.lsp" is fit for you as @ВeekeeCZ said.... Because this works on any shapes of viewports......

 

For the below requirements.....

 

............................

 

but limitation on single viewport.. it's understood why but unfortunately unacceptable

because non-rectangular viewport comes over many other viewports as boundary

Is it possible to do something? 


 

I made some modifications in @Lee_Mac code (To get in loop) and also combined your "C:CSC" & "C:CGC" code in it .......

 

Try it possibly it will fulfill your requirements........

 

Only bug in the modified code is that.... whenever you select the viewports it only highlights the Rectangular/Square shaped viewports...... (But internally it selects all those you selected).......

 

Hopefully you can manage this bug and the rest....

 

 


Debashis Bhunia
Co-Founder of Geometrifying Trigonometry(C)
________________________________________________
Walking is the First step of Running, Technique comes Next....
0 Likes
Message 7 of 19

danglar
Advocate
Advocate

Thank you dbhunia for your quick response

Your modification working properly but have a same problems as my previous attempt

1. We need to make 2 selection sets one - for PS objects, and the second one - for viewport

(I think about optimization of two these steps to one. is it possible?)

2. If PS have more than one viewports we need option to select needful viewport for change space command..

Your response is very important for me

0 Likes
Message 8 of 19

ВeekeeCZ
Consultant
Consultant

Ok, my try. 

0 Likes
Message 9 of 19

dbhunia
Advisor
Advisor

@danglar wrote:

 

...................................

1. We need to make 2 selection sets one - for PS objects, and the second one - for viewport

(I think about optimization of two these steps to one. is it possible?)

2. If PS have more than one viewports we need option to select needful viewport for change space command..

..............................


 

Yes it is possible .......

 

You just create one selection set (say SS) by selecting those you want........

 

Then filter for viewports (whose have the properties "(0 . "VIEWPORT")") and with those make a new selection set (SS1 as in code) ...... and with the rest make another selection set (SS3 as in code) .......

 

And put these in your C:CGC portion.....

 

For your 2nd requirement I am unable to understand totally...... But whatever i get ..... you can achieve to your requirement by putting conditions in C:CGC portion.....

 


Debashis Bhunia
Co-Founder of Geometrifying Trigonometry(C)
________________________________________________
Walking is the First step of Running, Technique comes Next....
0 Likes
Message 10 of 19

danglar
Advocate
Advocate

Thank you for your advice.

You just create one selection set (say SS) by selecting those you want........

 

Then filter for viewports (whose have the properties "(0 . "VIEWPORT")") and with those make a new selection set (SS1 as in code) ...... and with the rest make another selection set (SS3 as in code) .......

 

And put these in your C:CGC portion.....

 

Can you translate it to AutoLisp code?

Really, I don't know how to do this..

How can I filter it from common data?

0 Likes
Message 11 of 19

dbhunia
Advisor
Advisor

Ok shall do it in couple hour.....now I am way to home ..... operating from mobile....


Debashis Bhunia
Co-Founder of Geometrifying Trigonometry(C)
________________________________________________
Walking is the First step of Running, Technique comes Next....
0 Likes
Message 12 of 19

danglar
Advocate
Advocate

OK. I'm waiting for your response. Thank you

0 Likes
Message 13 of 19

ВeekeeCZ
Consultant
Consultant
Accepted solution

@ВeekeeCZ wrote:

Ok, my try. 


 

Sorry, I attached unfinished version. This should be better.

 

Anyway. Just you to learn something. Easy ss filtering or re-filtering.

(setq ss (ssget))

(setq svp (ssget "_P" '((0 . "VIEWPORT")))) ; ss containing VPs only - very easy, unfortunatelly possible just once (sssetfirst nil ss) ; highlight selection again

(setq ssr (ssget "_I" '((0 . "~VIEWPORT"))) ; all the rest but VPs

 

Unfortunately, this case wasn't that easy because non-rectangle VP are 2 entities VP+PL and that PL has to be filtered you too.

Message 14 of 19

Moshe-A
Mentor
Mentor
Accepted solution

@danglar

 

ok fix it, support for more than 1 viewports in same layout

also notice when selecting a non rectangular viewport by pick, autocad (ssget) returns both the vp and the pline so you do not have to do crossing

 

enjoy

moshe

 

 

(defun c:vpl (/		|vp|	  |ht|	    |wd|      |vn|
	      |ctr|	|ctrx|	  |ctry|    |vs|      |xp|
	      |iw|	|bl|	  |br|	    |tr|      |tl|
	      |osmode|	|clayer|  |cmdecho| |olderror|
	      |vplerror|
	      ent0 ent1 ent2 ent3 AcDbPline0 AcDbPline1 AcDbPline2 AcDbRegion centroid
	     )
  (setq |cmdecho| (getvar "cmdecho"))	;Get CMDECHO
  (setvar "cmdecho" 0)			;Turn off command echoing
  (command "undo" "begin")		;Set UNDO 
  (setq |olderror| *error*)		;Get current Error
  (setq *error* |vplerror|)		;Set Error 
  (setq |clayer| (getvar "clayer"))	;Get current Layer
  (setq |osmode| (getvar "osmode"))	;Get OSMODE
  (command ".pspace")			;Enter pspace
  (if (tblsearch "layer" "defpoints")
    (command "_.-layer"	"_thaw"	"defpoints" "_on" "defpoints" "")
  )
  
  (setq ss (ssget "_:L" '((0 . "lwpolyline,viewport"))))

  (cond
   ((= (sslength ss) 1)
    (setq |vp| (entget (ssname ss 0)))
    (setq |ht| (cdr (assoc 41 |vp|)))	;Get Viewport height with
    (setq |wd| (cdr (assoc 40 |vp|)))	;Get Viewport width with
    (setq |vn| (cdr (assoc 69 |vp|)))	;Get Viewport Number
    (command ".mspace")			;enter mspace
    (setvar "cvport" |vn|)		;Set correct viewport
    (command ".ucs" "v")		;Set UCS to View
    (setq |ctr| (getvar "viewctr"))	;Get VIEWCTR store as CTR
    (setq |ctrx| (car |ctr|))		;Get X of CTR
    (setq |ctry| (cadr |ctr|))		;Get Y of CTR
    (setq |vs| (getvar "viewsize"))	;Get inside Viewport height
    (setq |xp| (/ |ht| |vs|))		;Get XP Factor with HeighT /
					;View Size
    (setq |iw| (* (/ |vs| |ht|) |wd|))	;Get inside width of Viewport by
    (setq |bl| (list (- |ctrx| (/ |iw| 2)) (- |ctry| (/ |vs| 2))))
					;Find four corners of Viewport
    (setq |br| (list (+ |ctrx| (/ |iw| 2)) (- |ctry| (/ |vs| 2))))
    (setq |tr| (list (+ |ctrx| (/ |iw| 2)) (+ |ctry| (/ |vs| 2))))
    (setq |tl| (list (- |ctrx| (/ |iw| 2)) (+ |ctry| (/ |vs| 2))))
  
    (setvar "osmode" 0)			;Turn off Osnaps
    (command ".pline" |bl| |br| |tr| |tl| "c") ;Draw pline inside border
    (command ".ucs" "p")			;Restore UCS back
   ); case
   ((= (sslength ss) 2)
    (vl-load-com)
    (setq ent1 (ssname ss 0) ent2 (ssname ss 1))
    (if (eq (cdr (assoc '0 (entget ent1))) "LWPOLYLINE")
     (setq ent0 ent1)
     (setq ent0 ent2)
    )

    (setq AcDbPline0 (vlax-ename->vla-object ent0))
    (setq AcDbPline1 (vla-copy AcDbPline0))
    (setq ent3 (vlax-vla-object->ename AcDbPline1))

    (if (setq ss1 (ssget "x" (list '(0 . "viewport") (cons '410 (getvar "ctab")))))
     (cond
      ((= (sslength ss1) 2)
       (command ".chspace" "si" ent3)
      ); case
      ( t
       (setq AcDbPline2 (vla-copy AcDbPline0))
       (command ".region" "si" (entlast))
       (setq AcDbRegion (vlax-ename->vla-object (entlast)))
       (setq centroid (vlax-safearray->list (vlax-variant-value (vla-get-centroid AcDbRegion))))
       (vla-delete AcDbRegion)
       (vlax-release-object AcDbRegion)
       (vlax-release-object AcDbPline2)
       (setq centroid (list (car centroid) (cadr centroid) 0.0))
       (command ".chspace" "si" ent3 "" centroid)
      ); case
     ); cond
    ); if  
   
    (vlax-release-object AcDbPline1)
    (vlax-release-object AcDbPline0)
   ); case
  ); cond
  
  (setvar "clayer" |clayer|)		;Restore old Current Layer
  (setvar "cmdecho" |cmdecho|)		;Restore old CMDECHO
  (setvar "osmode" |osmode|)		;Restore osmode
  (setq *error* |olderror|)		;Restore old Error
  (command ".pspace")			;Go Back To Papserspace
  (command "undo" "end")		;End UNDO
  (princ)				;Clean up command prompt
)

 

Message 15 of 19

danglar
Advocate
Advocate

Thank you        and      Moshe-A                             

Your solution is very good and realy fit to my needs. With some little modifications I can put it to a real working process

Aseptically thanks to you      for easy lesson of filtering and re filtering data from selection  set. It helps me in a future to resolve the same problems

0 Likes
Message 16 of 19

danglar
Advocate
Advocate

..Just a little modification in order to return objects transferred to MS (change space with copy option). In this case selected objects copied to model space (see attached lisp)

Message 17 of 19

dbhunia
Advisor
Advisor
Accepted solution

@danglar sorry for late ....... Actually I was little busy ....... 

 

For.....

 


@danglar wrote:

 

.....................................

Can you translate it to AutoLisp code?

Really, I don't know how to do this..

How can I filter it from common data?


 

Check in the code.....

 

.....................
(setq ss (ssget))
(setq ss1 (ssadd))
(setq ss3 (ssadd))
(repeat (setq N (sslength ss))
	(setq ent1 (entget (setq ent_name (ssname ss (setq N (- N 1))))))
	   (if (= "VIEWPORT" (cdr (assoc 0 ent1)))
		(ssadd ent_name ss1)
		   (if (/= "{ACAD_REACTORS" (cdr (assoc 102 ent1)))
			(ssadd ent_name ss3)
		   )
	   )
)
.........................

 

You can also use "ssdel" ....... that is just create a new selection set and remove those entity (using "ssdel") from main selection set........


Debashis Bhunia
Co-Founder of Geometrifying Trigonometry(C)
________________________________________________
Walking is the First step of Running, Technique comes Next....
Message 18 of 19

danglar
Advocate
Advocate

Thank you Bhunia

Your approach working good. Your help is really appreciated by me

0 Likes
Message 19 of 19

TomBeauford
Advisor
Advisor

Late to the discussion but I've used Jimmy Bergmark's https://jtbworld.com/autocad-vp-outline-lsp for 20+ years. You only have to pick the viewport if you're in Paper Space, if you're in the viewport Model Space it adds the viewport boundary instantly. Works with any viewport even circular ones. Seen many others but none as simple as his.

64bit AutoCAD Map & Civil 3D 2023
Architecture Engineering & Construction Collection
2023
Windows 10 Dell i7-12850HX 2.1 Ghz 12GB NVIDIA RTX A3000 12GB Graphics Adapter
0 Likes