LISP to select a set object in alternate pattern or selection .

LISP to select a set object in alternate pattern or selection .

Kenn.O
Contributor Contributor
3,068 Views
22 Replies
Message 1 of 23

LISP to select a set object in alternate pattern or selection .

Kenn.O
Contributor
Contributor

Dear ALL,

new on this forum.

Good day.

 

Very much appreciate if some one can share/ help for a LISP. to select an alternate object in a set of object.

 

1) select set of object

KennO_0-1675046357079.png

2) select with alternate pattern ( also for Horizontal lines)

KennO_1-1675046388235.png

Best regards & thank you very much,

Kenneth

 

 

0 Likes
Accepted solutions (3)
3,069 Views
22 Replies
Replies (22)
Message 2 of 23

Sea-Haven
Mentor
Mentor

Can be done using (ssget "F" option but is 2 steps 1st select all via a drag over say 2 pts, then remove every second one or 3rd from selection and so on making a new desired selection set.

 

Maybe for 2 as step

 

 

(defun c:test ( / pt1 ss ss2 x )
(setq pt1 (getpoint "\nPick 1st point ") pt2 (getpoint pt1 "\nPick 2nd point "))
(setq ss (ssget "F" (list pt1 pt2)))
(setq ss2 (ssadd))
(setq x 0)
(while (< x (sslength ss))
(setq ss2 (ssadd (ssname ss x) ss2))
(setq x (+ x 2))
)
(princ)
)
(c:test)

 

 

 

Message 3 of 23

Kenn.O
Contributor
Contributor

Hi @Sea-Haven, thank you for the quick reply, but I need to know first how to create and use a LISP. 😐😏

very much appreciate expanding a bit more for a very interesting  AutoCAD command.

my simple understanding is "Select the Lines or Ogject", then run a command or script. .

i tried to copy and paste but not working.. 

anyway, thank a lot.

 

Regards,

Kenneth

 

 

0 Likes
Message 4 of 23

Sea-Haven
Mentor
Mentor

Ok 1st how to use, you copy and paste to notepad, then save as what you want to call it, but save as a .lsp not txt. Then can use appload or explorer drag and drop, I have changed code a bit to run as what I provided was for say inside a bigger piece of code.

SeaHaven_0-1675056310162.png

 

 

Now you asked for a selection method and that is what I provided, what you did not ask for is what your going to do once you have every second line, you need to provide the missing information, then a complete answer can be provided.

 

So many times here people only give a 1/2 request so suggest in future provide as much info as possible and what the final out come is. A before and after is always good.

0 Likes
Message 5 of 23

Kenn.O
Contributor
Contributor

Hi Sir/ ALL,

 

my apologies for not being very clear and missing information & question.

 

Our objective is to Select an "alternate" pattern from specified selected lines or objects.

 steps:

1) from the big dwgs. of lines with very close space to each other. (Vertically or sometimes horizontally)

2) basically we can Select by using windows or crossing lines.

3) once we have the Selected lines.

KennO_0-1675071790686.png

 

4) we need to further select  ONLY the alternate pattern. from step  3.

           - this is the part we need for some solution to speed up the alternate line selection, instead to select 1 at a time.

KennO_1-1675071810882.png

 

for the purpose of changing color, layers, isolate, and others.

 

hope the above is clear. 

 

Regards and thank you,

Kenneth

 

 

0 Likes
Message 6 of 23

Kent1Cooper
Consultant
Consultant

It really makes more sense for the selection to be done by Fence, rather than by Window/Crossing/Lasso, because the order of the objects in the selection set will be the order in which the Fence line crosses them.  That makes it infinitely easier to remove every other one from the selection, as compared to a Window/Crossing selection, which gives a selection set in drawn [or maybe reversed drawn] order, and that could be all jumbled, so a lot of calculation is required to figure out the order they're in.  Fence selection also means it doesn't matter whether the selected objects are horizontal or vertical or at any other angle, whereas with a WindowCrossing selection, some figuring of direction would need to be part of the figuring of the sequence for every-other determination.

 

Whether that selection is before or after starting the command -- either could be done, but if it's inside the command, the User could be prompted to select by Fence, or with two points that the routine will use for a Fence.

Kent Cooper, AIA
0 Likes
Message 7 of 23

Sea-Haven
Mentor
Mentor

Look at what happens with this command sequence so can make some lisp defuns that all use the ssget first then call which property to change.

 

 

Command: CHPROP
Select objects: !ss2
<Selection set: fa>
1 found

Select objects:

Enter property to change [Color/LAyer/LType/ltScale/LWeight/Thickness/TRansparency/Material/Annotative]: *Cancel*

 

 

Have a go in a lisp, if stuck post again a start (c:test)(command "chprop" ..........

 

Message 8 of 23

Kent1Cooper
Consultant
Consultant

Another question occurs to me:  The first images have an odd number of objects in the bigger selection, so both end objects end up selected.  But the images in Message 5 show an even number of objects in the bigger selection, and in the culled selection one of the original end objects [at left] is selected, and the other end object [at right] is left out.  Is there some criterion for which end object should be "first" and selected, from which the every-other sequence should begin?

Kent Cooper, AIA
0 Likes
Message 9 of 23

komondormrex
Mentor
Mentor

Hi, the routine sequentially makes alternate sset patterns out of selected enames.

 

;*****************************************************************************************************************************************************

(defun c:even_odd_alternate_pattern_sset ( / lines_sset lines_sset_list command_terminated odd_order line_angle testing_coordinate
											 alternate_sset alternate_index ename
										 )
	(if (setq lines_sset (cadr (ssgetfirst)))
		(sssetfirst)
		(setq lines_sset (ssget '((0 . "line"))))
	)
 	(setq lines_sset_list (vl-remove-if 'listp (mapcar 'cadr (ssnamex lines_sset))))
	(cond
		(
			(or
				(equal (* 1.5 pi) (setq line_angle (vla-get-angle (vlax-ename->vla-object (car lines_sset_list)))) 1e-2)
				(equal (* 0.5 pi) line_angle 1e-2)
			)
				(setq testing_coordinate 'car)		;	vertical
		)
		(
			(or
				(equal (* 0.0 pi) line_angle 1e-2)
				(equal (* 1.0 pi) line_angle 1e-2)
			)
				(setq testing_coordinate 'cadr) 	;	horizontal
		)
		(
			t
				(setq testing_coordinate 'skip)
		)
	)
	(if (not (= testing_coordinate 'skip))
		(setq lines_sset_list (vl-sort (mapcar '(lambda (line) (cons line (apply testing_coordinate (list (vlax-get (vlax-ename->vla-object line) 'startpoint)))))
												lines_sset_list
									   )
									   '(lambda (line_1 line_2) (< (cdr line_1) (cdr line_2)))
							  )
		)
	)
	(sssetfirst nil lines_sset)
	(prompt "\rPress <Space> to see alternate pattern, <Esc,Enter> to confirm")
	(while (not command_terminated)
			(setq error_occurred (if (vl-catch-all-error-p (setq grread_data (vl-catch-all-apply 'grread (list t 12 0)))) t nil))
	    	(cond
				(
					error_occurred                                                              ;	Esc
						(setq command_terminated t)
				)
				(
				 	(equal grread_data '(2 32))													;	Space
						(prompt "\rPress <Space> to see alternate pattern, <Esc,Enter> to confirm")
						(sssetfirst)
						(setq alternate_sset (ssadd)
							  alternate_index 0
						)
						(if odd_order
							(setq alternate_index 1)
							(setq alternate_index 0)
						)
					  	(setq odd_order (not odd_order))
						(while (setq ename (car (nth alternate_index lines_sset_list)))
							(ssadd ename alternate_sset)
							(setq alternate_index (+ 2 alternate_index))
						)
						(sssetfirst nil alternate_sset)
				)
				(
				 	(equal grread_data '(2 13))													;	Enter
						(setq command_terminated t)
				)
			)
	)
	(princ)
)

 

 

0 Likes
Message 10 of 23

Kenn.O
Contributor
Contributor

@komondormrex  and ALL,

look it is working as what I/we need. now to try for our group to use.

this is a very big help to us. 

 

Thank you very much @komondormrex 

Best regards,

Kenneth

 

0 Likes
Message 11 of 23

Kenn.O
Contributor
Contributor

@komondormrex 

 

Good day, & very much appreciate your LISP of the above.

i have an additional requirement.

1) Sometimes we need to use "Qselect", to filter the lines we need to be selected in alternate.

2) how to use your LISP, for example, we already have "Selected lines", then further select an alternate.

 

currently, I tried preselected lines, then using your LISP it will NOT select the line although some message on the command line.

KennO_0-1675412856543.png

Very much appreciate your help. and BTW, do you have references to share for the LISP coding? i am very interested to learn.

 

Thank you in advance,

Kenneth

 

0 Likes
Message 12 of 23

komondormrex
Mentor
Mentor
Accepted solution

hi)

1, 2 done.  Just run program while qs set is active.  I've updated program code above.

Sure, my references are 

"AutoLISP Reference Guide.pdf"

AutoLISP Developers Guide.pdf"

"AutoCAD 2014 DXF Reference.pdf"

0 Likes
Message 13 of 23

pankaj
Contributor
Contributor

Hello  komondormrex,
I was looking for the same requirement lisp but with lines with different angles.

I am attaching the files with my requirement.
Please help me out with an possible lisp solution.
Thanks in advance,
Pankaj.

0 Likes
Message 14 of 23

Kent1Cooper
Consultant
Consultant

@pankaj wrote:

Hello  komondormrex,
.... the same requirement lisp but with lines with different angles. ....


If they all aimed at the same center so that they could be ordered by their varying angles, or if they were positioned in drawn order, I could imagine some approaches.  But they don't -- many are parallel, and the drawn order is random.

 

Are you willing to be required to select them using Fence mode only?  Then the order in the selection would be the order in which the Fence crosses them, and it wouldn't be difficult to do what you need.  EDIT:

 

(defun C:SEO (/ n ss); = Select Every Other
  (setq
    sseo (ssadd); initially empty
    n -1
  ); setq
  (prompt "\nUsing Fence selection only,")
  (if (setq ss (ssget)); add entity type or Layer or... filter(s) as desired
    (while (< (setq n (+ n 2)) (sslength ss))
      (ssadd (ssname ss n) sseo)
    ); while
  ); if
  (sssetfirst nil sseo); select/grip/highlight
)

 

That leaves them in the sseo variable to do with what you will, if just using the Properties palette with them as selected isn't what you need.  It could be made to do something else directly with each rather than put them into another selection set.

 

As written, it picks the second, fourth, etc., but could be made to pick the first, third, etc., by changing the initial n variable to -2.  Or it could ask whether you want the odds or the evens.  It could even be made to ask for the interval -- select every third one? every seventh one?

 

Or, do they always have Multileader numbered identifiers pointing to them as in your drawing [i.e. those are not just for illustration]?

Kent Cooper, AIA
0 Likes
Message 15 of 23

Kenn.O
Contributor
Contributor

Hi sir and ALL,

 

Firstly, I very much appreciate the "alternate Lisp".

currently, I /we need to create arrows or multi-leader arrows. Currently, we created it as 1 by 1. Pick the lines 😀

 

1) Like, once I after select the "alternate lines" it will add an arrow or multi-leader arrows. 

 

KennO_1-1695347182021.png

 

similar to the below image.

Or, I may put the 1st multi-leader arrow, then "add leader" to other selected lines.

KennO_3-1695347203940.png

Again very much appreciate the big help.

 

Thank you,

Kenneth

0 Likes
Message 16 of 23

komondormrex
Mentor
Mentor

hey,

what these arrows are for? i mean to visualize the pattern only? at which level?

0 Likes
Message 17 of 23

Kenn.O
Contributor
Contributor

Hi good day,

thank you for the replay.

 

1) the arrows are used for labeling that particular line/rebar and can be any location.

    or, can it be put anywhere, and later on I/we will adjust the location

2) once we have the arrows - we will provide labels/ information near/ above the arrows.

     this is to know what label we will put in the paper space later on.

3) the starting of the arrows is for the final label at the paper space. - this is at later part.

 

as below image.

 

Thank you very much for the support,

Kenn

 

KennO_1-1695612697168.png

 

 

 

 

0 Likes
Message 18 of 23

komondormrex
Mentor
Mentor

hi,

see if this helps

 

 

 

;***************************************************************************************************************************************

(defun draw_mleader (testing_coordinate far_point vertices_list odd_order / points_array mleader )
	(setq points_array (vlax-make-safearray vlax-vbDouble '(0 . 5)))
	(if (equal 'car testing_coordinate) 
		(vlax-safearray-fill points_array (append (list (last vertices_list)) (cdr far_point) far_point))
		(vlax-safearray-fill points_array (append (list (car far_point)) (list (last vertices_list)) (list (caddr far_point)) far_point))
	)
	(setq mleader (vla-addmleader (vla-get-block (vla-get-activelayout (vla-get-activedocument (vlax-get-acad-object)))) points_array 0))
	(vla-put-textleftattachmenttype mleader 3)
	(vla-put-textrightattachmenttype mleader 3)
	(vla-put-textstring mleader (strcat (cdr (assoc odd_order '((nil . "ODD") (t . "EVEN") (all . "ALL")))) 
										(if (equal 'car testing_coordinate) " LEFT TO RIGHT" " TOP TO BOTTOM")
								)
	)
	(foreach vertex (cdr (reverse vertices_list))
		(if (equal 'car testing_coordinate) 
				(vlax-safearray-put-element points_array 0 vertex)
				(vlax-safearray-put-element points_array 1 vertex)
		)
		(vla-addleaderlineex mleader points_array)
	)
	mleader
)

;***************************************************************************************************************************************

(defun c:even_odd_alternate_pattern_sset ( / lines_sset lines_sset_list command_terminated odd_order line_angle testing_coordinate
											 alternate_sset alternate_index ename mleader sset_set
										 )
	(if (setq lines_sset (cadr (ssgetfirst)))
		(sssetfirst)
		(setq lines_sset (ssget '((0 . "line"))))
	)
 	(setq lines_sset_list (vl-remove-if 'listp (mapcar 'cadr (ssnamex lines_sset))))
	(cond
		(
			(or
				(equal (* 1.5 pi) (setq line_angle (vla-get-angle (vlax-ename->vla-object (car lines_sset_list)))) 1e-2)
				(equal (* 0.5 pi) line_angle 1e-2)
			)
				(setq testing_coordinate 'car)		;	vertical
		)
		(
			(or
				(equal (* 0.0 pi) line_angle 1e-2)
				(equal (* 1.0 pi) line_angle 1e-2)
			)
				(setq testing_coordinate 'cadr) 	;	horizontal
		)
		(
			t
				(setq testing_coordinate 'skip)
		)
	)
	(if (not (= testing_coordinate 'skip))
		(setq lines_sset_list (vl-sort (mapcar '(lambda (line) (cons line (apply testing_coordinate 
																				(list (vlax-get (vlax-ename->vla-object line) 'startpoint))
																		  )
															   )
												)
												lines_sset_list
									   )
									   '(lambda (line_1 line_2) (< (cdr line_1) (cdr line_2)))
							  )
		)
	)
	(sssetfirst nil lines_sset)
	(prompt "\rLeft click to mark/reposition marking MLeader for all selected, <Space> to see/mark alternate pattern, <Esc, Enter> to end")
	(while (not command_terminated)
			(setq error_occurred (if (vl-catch-all-error-p (setq grread_data (vl-catch-all-apply 'grread (list t 12 0)))) t nil))
	    	(cond
				(
					error_occurred                                                              ;	Esc
						(setq command_terminated t)
				)
				(
					(= 3 (car grread_data))				    							;	Mouse Left Click
						(if	sset_set
							(progn
								(setq far_point (cadr grread_data))
								(if (null mleader) 
									(setq mleader (draw_mleader testing_coordinate far_point mleader_vertices_list odd_order))
									(progn
										(vla-erase mleader)
										(setq mleader (draw_mleader testing_coordinate far_point mleader_vertices_list odd_order))
									)
								)
							)

							(progn
								(setq mleader_vertices_list (mapcar 'cdr lines_sset_list))
								(if (null mleader)
									(setq mleader (draw_mleader testing_coordinate far_point mleader_vertices_list 'all))
									(progn
										(vla-erase mleader)
										(setq mleader (draw_mleader testing_coordinate far_point mleader_vertices_list 'all))
									)
								)
							)
						)
				)
				(
			   		(= 5 (car grread_data))														;	Mouse Moving
					(setq far_point (cadr grread_data))
				)
				(
					(equal grread_data '(2 32))													;	Space
						(prompt "\rLeft click to reposition marking Mleader, <Space> to see/mark alternate pattern, <Esc, Enter> to end")
						(sssetfirst)
						(setq alternate_sset (ssadd)
							  alternate_index 0
						)
						(if odd_order
							(setq alternate_index 1)
							(setq alternate_index 0)
						)
					  	(setq odd_order (not odd_order)
							  mleader_vertices_list nil
						)
						(while (setq ename (car (nth alternate_index lines_sset_list)))
							(setq mleader_vertices_list (append mleader_vertices_list (list (cdr (nth alternate_index lines_sset_list)))))   
							(ssadd ename alternate_sset)
							(setq alternate_index (+ 2 alternate_index))
						)
						(sssetfirst nil alternate_sset)
						(if (null mleader) 
							(setq mleader (draw_mleader testing_coordinate far_point mleader_vertices_list odd_order))
							(progn
								(vla-erase mleader)
								(setq mleader (draw_mleader testing_coordinate far_point mleader_vertices_list odd_order))
							)
						)
						(if (null sset_set) (setq sset_set t))
				)
				(
				 	(equal grread_data '(2 13))													;	Enter
						(setq command_terminated t)
				)
			)
	)
	(princ)
)

;***************************************************************************************************************************************

 

 

updated

 

 

0 Likes
Message 19 of 23

ngoclinhbmt
Explorer
Explorer

please! convert lisp to user point or text, thanks Screenshot 2023-09-27 025150.png

 

0 Likes
Message 20 of 23

Kenn.O
Contributor
Contributor

Dear 

@komondormrex ,

Wow..

Firstly, thank you very much.

The Lisp itself can use its purpose. Currently, I just make it in 2 operations to create mLeader of the next alternate lines. Or copy if it has the same spacing.

 

Further, can use the word "request".😍

for a Lisp, to create the mLeader only.

meaning, after I have the selected lines, then invoke the mLeader Lisp, to create the mLeader- with this I can use it any time, whenever I select particular lines.

 

Unfortunately, I tried to edit the current Lisp, by removing the part or alternate ODD and EVEN selection.

But, it won't work.😚  . Not easy..

 

once again very much appreciate your help.

Thank you,

Kenneth

 

 

 

 

0 Likes