Jump to content

Recommended Posts

Posted

Hello,

 

I'm back to school in Autolisp discipline.

Your help will be greatly appreciated, as I'M DIGGING in this issue for last 48 hours without success.

 

Here is the code for selecting viewport and  by next step selecting only objects within that viewport.

 

I want to do some slight modifications to code. I want to iterate through viewports automatically in paper space in order to select all objects and ("do whatever I want with those objects" - that's the future plan).

For now I would like to pass the selection set to the command or function.(not clear to me due to the luck of experience and knowledge).

The selection set to pass to main code is "rrr". I read the description of the script. It didn't help me

       (setq aa (ssget "_x" (list '(0 . "VIEWPORT"))))
      (setq Ent1 (ssname aa 0))
	(setq b2 (car  (entget Ent1)))


          (setq sss  (ssadd))       ; Sets ss to a null selection set.

    (ssadd Ent1 sss)
      (setq rrr (ssadd Ent1 sss))

Main code

;;;---------------------------------------------------------------------------;
;;;
;;; vpsel.lsp
;;;
;;; By Jimmy Bergmark
;;; Copyright (C) 1997-2006 JTB World, All Rights Reserved
;;; Website: www.jtbworld.com
;;; E-mail: info@jtbworld.com
;;;
;;; 2000-04-14 - First release
;;; Tested on AutoCAD 2000
;;;
;;;---------------------------------------------------------------------------;
;;;  DESCRIPTION
;;;
;;;  Select all visible objects in selected or active paperspace viewport
;;;  Works transparently when in modelspace and for polygonal viewports too
;;;  Example1: ERASE ALL R 'VPC >>> Erase all in model except what is visible
;;;  Example2: (command "erase" "all" "r" (c:vpc) "")
;;;  Example3: VPC ERASE >>> VPC is run previous the command and the objects
;;;                            are also in previous selection set
;;;
;;;  c:vpc - select all visible objects with crossing in viewport
;;;  c:vpw - select all visible objects with window in viewport
;;;---------------------------------------------------------------------------;

(defun c:vpc ()
  (vpsel "C")
  (princ)
)

(defun c:vpw ()
  (vpsel "W")
  (princ)
)

(defun dxf (n ed) (cdr (assoc n ed)))

(defun vpsel (typ   /     ad    ss    ent   vpno  ok    vpbl  vpur
              msbl  msur  msul  mslr  ss1   pl    nlist x     n
             )
  (vl-load-com)
  (setq ok t)
  (if (= (getvar "tilemode") 0)
    (progn
      (setq ad (vla-get-activedocument (vlax-get-acad-object)))
      (if (= (getvar "cvport") 1)
        (if (and (= (getvar "cmdactive") 0)
                 (/= (setq ss (ssget ":E:S" '((0 . "VIEWPORT")))) nil)
            )
          (progn
            (setq ent (ssname ss 0))
            (setq vpno (dxf 69 (entget ent)))
            (vla-Display (vla-get-activepviewport ad) :vlax-true)
            (vla-put-mspace ad :vlax-true)
            (setvar "cvport" vpno)
          )
          (progn
            (setq ok nil)
          )
        )
        (setq ent (vlax-vla-object->ename (vla-get-activepviewport ad)))
      )
      (if (and ok (/= 1 (logand 1 (dxf 90 (setq ed (entget ent))))))
        (progn
          (if (= (vla-get-clipped (vlax-ename->vla-object ent))
                 :vlax-false
              )
            (progn
              (vla-getboundingbox
                (vla-get-activepviewport ad)
                'vpbl
                'vpur
              )
              (setq msbl (trans (vlax-safearray->list vpbl) 3 2))
              (setq msur (trans (vlax-safearray->list vpur) 3 2))
              (setq msul (list (car msbl) (cadr msur)))
              (setq mslr (list (car msur) (cadr msbl)))
              (setq ss1
                     (ssget (strcat typ "P") (list msbl msul msur mslr))
              )
            )
            (progn
              (setq pl (entget (dxf 340 (entget ent))))
              (setq nlist nil)
              (foreach x pl
                (if (eq 10 (car x))
                  (setq nlist (cons (trans (cdr x) 3 2) nlist))
                )
              )
              (setq ss1 (ssget (strcat typ "P") nlist))
            )
          )
          (sssetfirst nil ss1)
          (if ss1
            (setq n (sslength ss1))
            (setq n 0)
          )
          (princ n)
          (princ " found ")
          (if (and ss1 (= (getvar "cmdactive") 1))
            ss1
          )
        )
      )
    )
  )
  (setq ss nil ss1 nil)
)

(princ)

I'm relieved because I know I will get some help....Thanks in advance!

Posted

Hi,

This line ask user to select a viewport on screen. If you already have the viewport (iterate through your viewport selection set), you got to skip this step, and put your viewport directly in

(if (and (= (getvar "cmdactive") 0)
                 (/= (setq ss (ssget ":E:S" '((0 . "VIEWPORT")))) nil)
            )

Replace

;replace this: (setq ent (ssname ss 0))
;with this:
(setq ent yourviewporthere)

Also the vpsel function pass the result to the grip selection, then nullify the result at the end (setq ss nil ss1 nil)

While you want to get the result, so, you got to comment out that line, and take the ss1 result out of the function, merge it to your rrr selection

  • Like 2
Posted

Linh, Thank you a lot for the answer. It works half way. I forgot to mention that "rrr" selection is for other lisp that calls vsel and vsp. I don't want one lisp for my task I want two. I will take my time for figuring it out by your great tip. I just thought that,s its not nice to delay a "Thank you" for your analysis.

 

Posted (edited)

Trial and error...Crime and Punishment

Here is the updated code:

;;;---------------------------------------------------------------------------;
;;;
;;; vpsel.lsp
;;;
;;; By Jimmy Bergmark
;;; Copyright (C) 1997-2006 JTB World, All Rights Reserved
;;; Website: www.jtbworld.com
;;; E-mail: info@jtbworld.com
;;;
;;; 2000-04-14 - First release
;;; Tested on AutoCAD 2000
;;;
;;;---------------------------------------------------------------------------;
;;;  DESCRIPTION
;;;
;;;  Select all visible objects in selected or active paperspace viewport
;;;  Works transparently when in modelspace and for polygonal viewports too
;;;  Example1: ERASE ALL R 'VPC >>> Erase all in model except what is visible
;;;  Example2: (command "erase" "all" "r" (c:vpc) "")
;;;  Example3: VPC ERASE >>> VPC is run previous the command and the objects
;;;                            are also in previous selection set
;;;
;;;  c:vpc - select all visible objects with crossing in viewport
;;;  c:vpw - select all visible objects with window in viewport
;;;---------------------------------------------------------------------------;

(defun c:vpc ()
  (vpsel "C")
  (princ)
)

(defun c:vpw ()
  (vpsel "W")
  (princ)
)

(defun dxf (n ed) (cdr (assoc n ed)))

(defun vpsel (typ   /	  ad	ss    ent   vpno  ok	vpbl  vpur
	      msbl  msur  msul	mslr  ss1   pl	  nlist	x     n
	     )
  (vl-load-com)
  (setq ok t)
;;;  (if (= (getvar "tilemode") 0)
;;;    (progn
;;;      
;;;      (if (= (getvar "cvport") 1)
;;;        (if (and (= (getvar "cmdactive") 0)
;;;                 (/= (setq ss (ssget ":E:S" '((0 . "VIEWPORT")))) nil)
;;;            )
	(progn
	  (setq ad (vla-get-activedocument (vlax-get-acad-object)))
	  (setq aa (ssget "_x" (list '(0 . "VIEWPORT"))))
	  (setq ent (ssname aa 0))
;;;



;;;	  (setq ent rrr)
;;;	(setq ent (car  (entget Ent1)))
	  (setq vpno (dxf 69 (entget ent)))
	  (vla-Display (vla-get-activepviewport ad) :vlax-true)
	  (vla-put-mspace ad :vlax-true)
	  (setvar "cvport" vpno)
	)
	(progn
	  (setq ok nil)
	)
;;;        )
	(setq ent (vlax-vla-object->ename (vla-get-activepviewport ad)))
      
      (if (and ok (/= 1 (logand 1 (dxf 90 (setq ed (entget ent))))))
	(progn
	  (if (= (vla-get-clipped (vlax-ename->vla-object ent))
		 :vlax-false
	      )
	    (progn
	      (vla-getboundingbox
		(vla-get-activepviewport ad)
		'vpbl
		'vpur
	      )
	      (setq msbl (trans (vlax-safearray->list vpbl) 3 2))
	      (setq msur (trans (vlax-safearray->list vpur) 3 2))
	      (setq msul (list (car msbl) (cadr msur)))
	      (setq mslr (list (car msur) (cadr msbl)))
	      (setq ss1
		     (ssget (strcat typ "P") (list msbl msul msur mslr))
	      )
	    )
	    (progn
	      (setq pl (entget (dxf 340 (entget ent))))
	      (setq nlist nil)
	      (foreach x pl
		(if (eq 10 (car x))
		  (setq nlist (cons (trans (cdr x) 3 2) nlist))
		)
	      )
	      (setq ss1 (ssget (strcat typ "P") nlist))
	    )
	  )
	  (sssetfirst nil ss1)
	  (if ss1
	    (setq n (sslength ss1))
	    (setq n 0)
	  )
	  (princ n)
	  (princ " found ")
	  (if (and ss1 (= (getvar "cmdactive") 1))
	    ss1
	  )
	)
      )
    
  
;;;  (setq	ss  nil	ss1 nil)
;;;	  (setq sss (ssadd))
;;;	  (ssadd ent sss)
;;;	  (setq rrr (ssadd ss1 sss))
)

(princ)

I got stuck on this line: it produces an error

Please help. Basically looking for tutor. I will do my homework:) It highlight the viewport but doesn't select it. Going through each line and checking data. Error on this line:

(setq pl (entget (dxf 340 (entget ent))))
Edited by nzoro99
Posted

I think the topic name should be changed to something relate to "Select All Entities Within Viewport" or so.

I can't try debug your code but I see:

(setq aa (ssget "_x" (list '(0 . "VIEWPORT"))))
	  (setq ent (ssname aa 0))

You select all viewport in the layouts but instead of loop through each viewport, you set the ent to the first item in the selection set.

Firstly, better rename "ent" to "viewport" or "vp" to clarify the code.

Second, the aa can be empty so the (ssname aa 0) returns error.

(setq ent (vlax-vla-object->ename (vla-get-activepviewport ad)))

Few lines next, you drop the ent's viewport value and get the new value from activeviewport of the activedocument ad.

So the (setq aa (ssget ...)) lines are useless, all the retrieved values are thrown away.

In the original code, the programmer takes care of 2 cases:

1. is in a layout, paper space

2. is in a layout, but in mspace, (the cursor is within a viewport)

 

For the viewport (ent), it also has 2 cases:

+ normal viewport: its boundary is a rectangle

+ clipped viewport: its boundary is some polyline, that is what this code is all about:

(setq pl (entget (dxf 340 (entget ent))))

if error raises, try debug by put to watch, ent, (entget ent), (dxf 340 (entget ent)) to see anything is nil

Just curious, is there in the world a kind of homework in AutoLisp?

I am self taught (actually the web taught me), and I never know there is Autolisp study in any school :)

Posted

"Select All Entities Within Viewport" , I agree its a better name

Quote

You select all viewport in the layouts but instead of loop through each viewport, you set the ent to the first item in the selection set.

Yes the loop will be later, for now I want any of 2 viewports to be selected.

Quote

Firstly, better rename "ent" to "viewport" or "vp" to clarify the code.

Done.

 

 

Quote

Second, the "aa" can be empty

aa result

 

cadt.PNG.4ce4bed377be8ca86ccc42b6db2c92d1.PNG

 

(entget vp) - result is, ((-1 . <Entity name: 14d67bf58d0>) (0 . "VIEWPORT") (330 . <Entity name: 14d03befec0>) (5 . "335") (100 . "AcDbEntity") (67 . 1) (410 . "Layout1") (8 . "G-ANNO-NPLT") (100 . "AcDbViewport") (10 -306.971 81.0516 0.0) (40 . 155.262) (41 . 128.7) (68 . 2) (69 . 3) (12 21977.9 5012.98 0.0) (13 0.0 0.0 0.0) (14 0.0 0.0 0.0) (15 0.0 0.0 0.0) (16 0.0 0.0 1.0) (17 0.0 0.0 0.0) (42 . 0.0) (43 . 0.0) (44 . 0.0) (45 . 12870.0) (50 . 0.0) (51 . 0.0) (72 . 1000) (90 . 32768) (1 . "") (281 . 0) (71 . 1) (74 . 1) (110 0.0 0.0 0.0) (111 1.0 0.0 0.0) (112 0.0 1.0 0.0) (79 . 0) (146 . 0.0) (170 . 0) (61 . 5) (348 . <Entity name: 14d03befaf0>) (292 . 1) (282 . 1) (141 . 0.0) (142 . 0.0) (63 . 250) (421 . 3355443))

 

(dxf 340 (entget ent)) - result is, nil ( there is no dxf for 340)

 

;;;---------------------------------------------------------------------------;
;;;
;;; vpsel.lsp
;;;
;;; By Jimmy Bergmark
;;; Copyright (C) 1997-2006 JTB World, All Rights Reserved
;;; Website: www.jtbworld.com
;;; E-mail: info@jtbworld.com
;;;
;;; 2000-04-14 - First release
;;; Tested on AutoCAD 2000
;;;
;;;---------------------------------------------------------------------------;
;;;  DESCRIPTION
;;;
;;;  Select all visible objects in selected or active paperspace viewport
;;;  Works transparently when in modelspace and for polygonal viewports too
;;;  Example1: ERASE ALL R 'VPC >>> Erase all in model except what is visible
;;;  Example2: (command "erase" "all" "r" (c:vpc) "")
;;;  Example3: VPC ERASE >>> VPC is run previous the command and the objects
;;;                            are also in previous selection set
;;;
;;;  c:vpc - select all visible objects with crossing in viewport
;;;  c:vpw - select all visible objects with window in viewport
;;;---------------------------------------------------------------------------;

(defun c:vpc ()
  (vpsel "C")
  (princ)
)

(defun c:vpw ()
  (vpsel "W")
  (princ)
)

(defun dxf (n ed) (cdr (assoc n ed)))

(defun vpsel (typ   /	  ad	ss    ent   vpno  ok	vpbl  vpur
	      msbl  msur  msul	mslr  ss1   pl	  nlist	x     n
	     )
  (vl-load-com)
  (setq ok t)
;;;  (if (= (getvar "tilemode") 0)
;;;    (progn
;;;      
;;;      (if (= (getvar "cvport") 1)
;;;        (if (and (= (getvar "cmdactive") 0)
;;;                 (/= (setq ss (ssget ":E:S" '((0 . "VIEWPORT")))) nil)
;;;            )
	(progn
	  (setq ad (vla-get-activedocument (vlax-get-acad-object)))
	  (setq aa (ssget "_x" (list '(0 . "VIEWPORT"))))
	  (setq vp (ssname aa 0))
;;;



;;;	  (setq ent rrr)
;;;	(setq ent (car  (entget Ent1)))
	  (setq vpno (dxf 69 (entget vp)))
	  (vla-Display (vla-get-activepviewport ad) :vlax-true)
	  (vla-put-mspace ad :vlax-true)
	  (setvar "cvport" vpno)
	)
	(progn
	  (setq ok nil)
	)
;;;        )
	(setq vp (vlax-vla-object->ename (vla-get-activepviewport ad)))
      
      (if (and ok (/= 1 (logand 1 (dxf 90 (setq ed (entget vp))))))
	(progn
	  (if (= (vla-get-clipped (vlax-ename->vla-object vp))
		 :vlax-false
	      )
	    (progn
	      (vla-getboundingbox
		(vla-get-activepviewport ad)
		'vpbl
		'vpur
	      )
	      (setq msbl (trans (vlax-safearray->list vpbl) 3 2))
	      (setq msur (trans (vlax-safearray->list vpur) 3 2))
	      (setq msul (list (car msbl) (cadr msur)))
	      (setq mslr (list (car msur) (cadr msbl)))
	      (setq ss1
		     (ssget (strcat typ "P") (list msbl msul msur mslr))
	      )
	    )
	    (progn
	      (setq pl (entget (dxf 340 (entget vp))))
	      (setq nlist nil)
	      (foreach x pl
		(if (eq 10 (car x))
		  (setq nlist (cons (trans (cdr x) 3 2) nlist))
		)
	      )
	      (setq ss1 (ssget (strcat typ "P") nlist))
	    )
	  )
	  (sssetfirst nil ss1)
	  (if ss1
	    (setq n (sslength ss1))
	    (setq n 0)
	  )
	  (princ n)
	  (princ " found ")
	  (if (and ss1 (= (getvar "cmdactive") 1))
	    ss1
	  )
	)
      )
    
  
;;;  (setq	ss  nil	ss1 nil)
;;;	  (setq sss (ssadd))
;;;	  (ssadd ent sss)
;;;	  (setq rrr (ssadd ss1 sss))
)

(princ)

 

Thank you!

 

Posted (edited)

+ aa is not empty, but there is case your user has a drawing without any viewport, and aa is empty. Your software must take care all the cases.

+ if the 340 dxf is nil, the viewport is not clipped. It should fall into the first "if condition" above:

(if (= (vla-get-clipped (vlax-ename->vla-object vp))
		 :vlax-false
	      )

why it fell to the 2nd condition, i don't know

 

I also note that if you have 3 viewport, can you highlight entities in all 3 viewports? I think AutoCAD only allow 1.

Anyway, if your purpose is get the entities, not highlight them, then ok. You can get list of entities from everywhere in the database.

Edited by Linh
Posted (edited)

Linh thank you, It worked

 

;;;---------------------------------------------------------------------------;
;;;
;;; vpsel.lsp
;;;
;;; By Jimmy Bergmark
;;; Copyright (C) 1997-2006 JTB World, All Rights Reserved
;;; Website: www.jtbworld.com
;;; E-mail: info@jtbworld.com
;;;
;;; 2000-04-14 - First release
;;; Tested on AutoCAD 2000
;;;
;;;---------------------------------------------------------------------------;
;;;  DESCRIPTION
;;;
;;;  Select all visible objects in selected or active paperspace viewport
;;;  Works transparently when in modelspace and for polygonal viewports too
;;;  Example1: ERASE ALL R 'VPC >>> Erase all in model except what is visible
;;;  Example2: (command "erase" "all" "r" (c:vpc) "")
;;;  Example3: VPC ERASE >>> VPC is run previous the command and the objects
;;;                            are also in previous selection set
;;;
;;;  c:vpc - select all visible objects with crossing in viewport
;;;  c:vpw - select all visible objects with window in viewport
;;;---------------------------------------------------------------------------;

(defun c:vpttt ()
  (vpselttt "C")
  (princ)
)

(defun c:vpwttt ()
  (vpselttt "W")
  (princ)
)

(defun dxf (n ed) (cdr (assoc n ed)))

(defun vpselttt (typ   /     ad    ss    ent   vpno  ok    vpbl  vpur
              msbl  msur  msul  mslr  ss1   pl    nlist x     n
             )
  (vl-load-com)
  (setq ok t)
  (if (= (getvar "tilemode") 0)
    (progn
      (setq ad (vla-get-activedocument (vlax-get-acad-object)))
      (if (= (getvar "cvport") 1)
        (if (and (= (getvar "cmdactive") 0)
                (setq ss (ssget "_x" (list '(0 . "VIEWPORT")))) 
            )
          (progn
            (setq ent (ssname ss 0))
            (setq vpno (dxf 69 (entget ent)))
            (vla-Display (vla-get-activepviewport ad) :vlax-true)
            (vla-put-mspace ad :vlax-true)
            (setvar "cvport" vpno)
          )
          (progn
            (setq ok nil)
          )
        )
        (setq ent (vlax-vla-object->ename (vla-get-activepviewport ad)))
      )
      (if (and ok (/= 1 (logand 1 (dxf 90 (setq ed (entget ent))))))
        (progn
          (if (= (vla-get-clipped (vlax-ename->vla-object ent))
                 :vlax-false
              )
            (progn
              (vla-getboundingbox
                (vla-get-activepviewport ad)
                'vpbl
                'vpur
              )
              (setq msbl (trans (vlax-safearray->list vpbl) 3 2))
              (setq msur (trans (vlax-safearray->list vpur) 3 2))
              (setq msul (list (car msbl) (cadr msur)))
              (setq mslr (list (car msur) (cadr msbl)))
              (setq ss1
                     (ssget (strcat typ "P") (list msbl msul msur mslr))
              )
            )
           (progn
              (setq pl (entget (dxf 340 (entget ent))))
              (setq nlist nil)
              (foreach x pl
                (if (eq 10 (car x))
                  (setq nlist (cons (trans (cdr x) 3 2) nlist))
                )
              )
              (setq ss1 (ssget (strcat typ "P") nlist))
            )
          )
          (sssetfirst nil ss1)
          (if ss1
            (setq n (sslength ss1))
            (setq n 0)
          )
          (princ n)
          (princ " found ")
          (if (and ss1 (= (getvar "cmdactive") 1))
            ss1
          )
        )
      )
    )
  )
  (setq ss nil ss1 nil)
)

(princ)

The only thing is that, when I count my viewports it says 3 from 0-2, but I have only 2 in paper space under 0 and 2

We figure it out, but I didn't get how and why...

Thank you!!!

Edited by nzoro99
Posted

Well found. I haven't tested in Lisp, but coding in .NET I did also find 1 viewport in an empty layout, 2 viewports in 1 viewport layout.So I always skip the first viewport.

Not sure why, maybe that is something about the layout. I guess the layout itself is a special viewport.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...