Is it possible to select elements within polyline or polygon in AutoCAD?

Is it possible to select elements within polyline or polygon in AutoCAD?

BriamR
Advocate Advocate
2,099 Views
17 Replies
Message 1 of 18

Is it possible to select elements within polyline or polygon in AutoCAD?

BriamR
Advocate
Advocate

Hi!

I am looking for an app or a lisp that allows me to select all the elements (blocks, hatch and polylines) that are inside a polyline or polygon, is this procedure possible? regards

0 Likes
Accepted solutions (4)
2,100 Views
17 Replies
Replies (17)
Message 2 of 18

Kent1Cooper
Consultant
Consultant

Do a little Searching.  There are multiple topics in this Forum, and maybe also in the basic AutoCAD Forum, about just this subject.

Kent Cooper, AIA
Message 3 of 18

devitg
Advisor
Advisor

Get all vertex and do a sdge wp

Message 4 of 18

devitg
Advisor
Advisor

Ssget

Message 5 of 18

BriamR
Advocate
Advocate
Accepted solution

@Kent1Cooper @devitg Thank you for your interest in the theme, find this lisp, it is very practical and useful, it resolves the post well. 

 thanks to @bc.chiquitofor sharing. https://forums.autodesk.com/t5/autocad-todos-los-productos/calcular-objetos-que-estan-dentro-de-un-p...

 

It would be great if there was a routine that could do the same but in addition to this that would select only the desired layers.

0 Likes
Message 6 of 18

hak_vz
Advisor
Advisor
Accepted solution
(defun c:sip( / e eo take drop_first_n points2d nthcdr coords) 
(defun take (amount lst / ret) (repeat amount (setq ret (cons (car lst) (take (1- amount) (cdr lst))))))
(defun drop_first_n (n alist) (nthcdr n alist))
(defun points2d (lst / ret) (while lst (setq ret (cons (take 2 lst) ret) lst (drop_first_n 2 lst))) (reverse ret))
(defun nthcdr (arg lst / ret) (setq ret lst) (if (> arg 0)(repeat arg (setq ret (cdr ret)))))
	(setq e (car (entsel "\nSelect selection set bounding polyline >")))
	(cond 
		((and e)
			(setq eo (vlax-ename->vla-object e))
			(cond 
				((= (vlax-get eo 'ObjectName) "AcDbPolyline")
					(setq coords (points2d (vlax-get eo 'Coordinates)))
					(setq ss (ssget "_wp" coords))
					
					(cond 
						((and coords)
							(sssetfirst nil (ssget "_wp" coords))
						)
					)
				)
			)
		)
	)
	(princ)
)

Miljenko Hatlak

EESignature

Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
Message 7 of 18

BriamR
Advocate
Advocate

@hak_vz Great, what a great routine, it works perfect, thanks!

0 Likes
Message 8 of 18

Kent1Cooper
Consultant
Consultant

Just be aware of the potential for incorrect results if the Polyline contains any arc segments.  Depending on the configuration, the routines here so far could see some things that are not within the Polyline, and/or miss some that are within it, because the Window-Polygon that it builds goes straight between all vertices.  The results of Searching will contain some topics in which there is some accounting for the possibility.

Kent Cooper, AIA
Message 9 of 18

hak_vz
Advisor
Advisor

Selected object are stored to global variable ss, so you can use them when needed (if not overwritten or erased).

command : Move

!ss

 

 

Miljenko Hatlak

EESignature

Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
Message 10 of 18

Kent1Cooper
Consultant
Consultant
Accepted solution

@BriamR wrote:

....

It would be great if there was a routine that could do the same but in addition to this that would select only the desired layers.


You can add a Layer filter in the (ssget) function, with multiple Layer names comma-separated, for example in @hak_vz 's suggestion:

....

 

(setq ss (ssget "_wp" coords '((8 . "Your,Desired,Layer,Names"))))

 

....

Kent Cooper, AIA
Message 11 of 18

BriamR
Advocate
Advocate


@Kent1Cooper Apparently I am doing something wrong since it does not select the specific layer that I include in the lines in this case "SEN_BASE_DEM".
Note: The selection of the layer can be variable? that is, when you run the lisp it asks which layers it should select within the polyline. @hak_vz 

0 Likes
Message 12 of 18

hak_vz
Advisor
Advisor

@BriamR wrote:


@Kent1Cooper Apparently I am doing something wrong since it does not select the specific layer that I include in the lines in this case "SEN_BASE_DEM".
Note: The selection of the layer can be variable? that is, when you run the lisp it asks which layers it should select within the polyline. @hak_vz 


 

Miljenko Hatlak

EESignature

Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
Message 13 of 18

hak_vz
Advisor
Advisor
Accepted solution

@hak_vz wrote:

@BriamR wrote:


@Kent1Cooper Apparently I am doing something wrong since it does not select the specific layer that I include in the lines in this case "SEN_BASE_DEM".



This select all entities inside bounding polyline in layer SEN_BASE_DEM

 

 

(defun c:sips( / e eo take drop_first_n points2d nthcdr coords) 
(defun take (amount lst / ret) (repeat amount (setq ret (cons (car lst) (take (1- amount) (cdr lst))))))
(defun drop_first_n (n alist) (nthcdr n alist))
(defun points2d (lst / ret) (while lst (setq ret (cons (take 2 lst) ret) lst (drop_first_n 2 lst))) (reverse ret))
(defun nthcdr (arg lst / ret) (setq ret lst) (if (> arg 0)(repeat arg (setq ret (cdr ret)))))
	(setq e (car (entsel "\nSelect selection set bounding polyline >")))
	(cond 
		((and e)
			(setq eo (vlax-ename->vla-object e))
			(cond 
				((= (vlax-get eo 'ObjectName) "AcDbPolyline")
					(setq coords (points2d (vlax-get eo 'Coordinates)))
					(setq ss (ssget "_wp" coords '((8 . "SEN_BASE_DEM"))))
					(cond 
						((and coords)
							(sssetfirst nil (ssget "_wp" coords '((8 . "SEN_BASE_DEM"))))
						)
					)
				)
			)
		)
	)
	(princ)
)

 

 

This version enables to select one or more layer so be included in creating selection set

 

(defun c:sipp( / e eo take drop_first_n points2d nthcdr coords collectLayerObjects getlayernames laylist LM:listbox LM:lst->str) 
(defun take (amount lst / ret) (repeat amount (setq ret (cons (car lst) (take (1- amount) (cdr lst))))))
(defun drop_first_n (n alist) (nthcdr n alist))
(defun points2d (lst / ret) (while lst (setq ret (cons (take 2 lst) ret) lst (drop_first_n 2 lst))) (reverse ret))
(defun nthcdr (arg lst / ret) (setq ret lst) (if (> arg 0)(repeat arg (setq ret (cdr ret)))))
(defun collectLayerObjects ( / ret) (reverse (vlax-for lay (vlax-get (vla-get-activedocument (vlax-get-acad-object)) 'Layers) (setq ret (cons (cons (vlax-get lay 'Name) lay)ret)))))
(defun getlayernames nil (mapcar 'car (collectLayerObjects)))
;; List Box  -  Lee Mac
;; Displays a DCL list box allowing the user to make a selection from the supplied data.
;; msg - [str] Dialog label
;; lst - [lst] List of strings to display
;; bit - [int] 1=allow multiple; 2=return indexes
;; Returns: [lst] List of selected items/indexes, else nil

(defun LM:listbox ( msg lst bit / dch des tmp rtn )
    (cond
        (   (not
                (and
                    (setq tmp (vl-filename-mktemp nil nil ".dcl"))
                    (setq des (open tmp "w"))
                    (write-line
                        (strcat "listbox:dialog{label=\"" msg "\";spacer;:list_box{key=\"list\";multiple_select="
                            (if (= 1 (logand 1 bit)) "true" "false") ";width=50;height=15;}spacer;ok_cancel;}"
                        )
                        des
                    )
                    (not (close des))
                    (< 0 (setq dch (load_dialog tmp)))
                    (new_dialog "listbox" dch)
                )
            )
            (prompt "\nError Loading List Box Dialog.")
        )
        (   t     
            (start_list "list")
            (foreach itm lst (add_list itm))
            (end_list)
            (setq rtn (set_tile "list" "0"))
            (action_tile "list" "(setq rtn $value)")
            (setq rtn
                (if (= 1 (start_dialog))
                    (if (= 2 (logand 2 bit))
                        (read (strcat "(" rtn ")"))
                        (mapcar '(lambda ( x ) (nth x lst)) (read (strcat "(" rtn ")")))
                    )
                )
            )
        )
    )
    (if (< 0 dch)
        (unload_dialog dch)
    )
    (if (and tmp (setq tmp (findfile tmp)))
        (vl-file-delete tmp)
    )
    rtn
)

	;; List to String  -  Lee Mac
	;; Concatenates each string in a supplied list, separated by a given delimiter
	;; lst - [lst] List of strings to concatenate
	;; del - [str] Delimiter string to separate each item

	(defun LM:lst->str ( lst del )
		(if (cdr lst)
			(strcat (car lst) del (LM:lst->str (cdr lst) del))
			(car lst)
		)
	)

	(setq e (car (entsel "\nSelect selection set bounding polyline >")))
	(setq laylist (LM:lst->str (LM:listbox "Select one or more layers to include in selection" (getlayernames) 1) ","))
	(cond 
		((and e)
			(setq eo (vlax-ename->vla-object e))
			(cond 
				((= (vlax-get eo 'ObjectName) "AcDbPolyline")
					(setq coords (points2d (vlax-get eo 'Coordinates)))
					(setq ss (ssget "_wp" coords (list (cons 8 laylist))))
					(cond 
						((and coords)
							(sssetfirst nil (setq ss (ssget "_wp" coords (list (cons 8 laylist)))))
						)
					)
				)
			)
		)
	)
	(princ)
)

 

 

 

Miljenko Hatlak

EESignature

Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
Message 14 of 18

Kent1Cooper
Consultant
Consultant

@BriamR wrote:

.... Apparently I am doing something wrong since it does not select the specific layer that I include in the lines in this case "SEN_BASE_DEM".
Note: The selection of the layer can be variable? that is, when you run the lisp it asks which layers it should select within the polyline.


Assuming you've double-checked spelling and that isn't the issue, show us how you incorporated the Layer name.  For example, did you perhaps lose the space on one side or the other of the period [it needs both]?

 

For variable ones, you would need something to ask the User earlier, for example:

 

  (setq laynames (getstring "\nLayer name(s) to select, comma-separated if multiple: "))

 

and then in the routine, the "quoted" list with the apostrophe prefix and the dotted-pair of 8 with names can't be used, but you need the explicit (list) and (cons) functions:

 

  (setq ss (ssget "_wp" coords (list (cons 8 laynames))))

Kent Cooper, AIA
Message 15 of 18

BriamR
Advocate
Advocate

@Kent1Cooper  The error was mine, I was just adding the line

 

(setq ss (ssget "_wp" coords '((8 . "Your,Desired,Layer,Names"))))

 

, without realizing that I was missing this

 

(sssetfirst nil (ssget "_wp" coords '((8 . "Your,Desired,Layer,Names"))))

 

, thank you very much.

 

@hak_vz  wow, a great routine, I perfectly solve what I needed, I hope it serves many more of this community.I only have one concern, and that is because when I execute the routine it selects only what is inside the polyline and does not select the blocks that intercept with the polyline, below I show an example video. Thanks!

 

0 Likes
Message 16 of 18

hak_vz
Advisor
Advisor

@BriamR 

 

Try to replace "_wp"  with "_cp" switch in my function . I guess it will work correctly. I'll test it tomorrow to see if it works.

Miljenko Hatlak

EESignature

Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
Message 17 of 18

BriamR
Advocate
Advocate
@hak_vz
It worked well, I solved my concern, thank you very much! regards
Message 18 of 18

АлексЮстасу
Advisor
Advisor

Hi,

 

I am now faced with the fact that the select of standard AutoCAD, lisp tools has an precision no better than 0.001-0.0007.
The precision of the SELECT command, and in lisp ssget and vla-selectbypolygon are the same.
It is also known that these tools can work erratically if not the entire selection polygon is visible on the screen.
I also noticed that the results may depend on the duration of the session in AutoCAD - with long work, the results may be worse.
Such low precision, in my opinion, makes the select unacceptably unreliable. Requires constant manual control. Which is almost impossible, for example, if the selection is deleted. Etc.

The precision of the select should be no worse than the maximum LUPREC, i.e. > 8 digits.


Maybe there are already alternative select programs relative to the polyline or polygon?

 


-- Alexander, private person, pacifist, english only with translator 🙂 --

Object-modeling _ odclass-odedit.com _ Help

0 Likes