Select rectangle and draw 2 diagonals

Select rectangle and draw 2 diagonals

a.tawkXHKN6
Contributor Contributor
802 Views
11 Replies
Message 1 of 12

Select rectangle and draw 2 diagonals

a.tawkXHKN6
Contributor
Contributor

Is there AutoLisp that select rectangle or square and automatically draw 2 diagonal polylines? 

0 Likes
803 Views
11 Replies
Replies (11)
Message 2 of 12

ВeekeeCZ
Consultant
Consultant

Never heard of Google? It's just a matter of seconds to find plenty of solutions. eg THIS 

0 Likes
Message 3 of 12

a.tawkXHKN6
Contributor
Contributor

it doesn't accept rectangle selection. only polyline selection works here. 

0 Likes
Message 4 of 12

ВeekeeCZ
Consultant
Consultant

Post a dwg of what you mean.

0 Likes
Message 5 of 12

Kent1Cooper
Consultant
Consultant

@a.tawkXHKN6 wrote:

it doesn't accept rectangle selection. only polyline selection works here. 


A reasonable assumption, since what the RECTANG command makes is a Polyline.  What kind(s) of object(s) are your rectangles?

 

>Here< is another, but also one that wants only closed four-segment Polylines.

 

EDIT:  If they are, for example, Blocks whose contents are rectangular, and when they are orthogonally oriented, the X-ing could be done easily enough based on their bounding boxes.  But not when rotated at non-orthogonal angles.

 

Why diagonal Polylines?  The XQ command in my link, and at least some of those in @ВeekeeCZ 's link, draw Lines -- is there some reason that Lines would not do?  It's easy enough to make them Polylines instead, but that would use more memory, so there should be a good reason for it.

Kent Cooper, AIA
0 Likes
Message 6 of 12

komondormrex
Mentor
Mentor

Hey,

Just for 4 vertices rectangle or square lw-plines.

 

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

;	komondormrex, feb 2023

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

(defun c:draw_rectangle_diagonals (/ ename_index ename_sset object)
	(vla-startundomark (vla-get-activedocument (vlax-get-acad-object)))
	(repeat (sslength (setq ename_index -1
							ignore_empty_sset (while (null (setq ename_sset (vl-catch-all-apply 'ssget (list '((0 . "lwpolyline") (-4 . "&=") (70 . 1) (90 . 4))))
														   )
													  )
											   )
							ename_sset (cond
												(
													(vl-catch-all-error-p ename_sset)
														(princ "\nCommand cancelled")
														(ssadd)
												)
												(
													t
														ename_sset
												)
									   )
					)
			)
				(setq object (vlax-ename->vla-object (ssname ename_sset (setq ename_index (1+ ename_index)))))
				(if
					(and
						 (= "AcDbPolyline" (vla-get-objectname object))
						 (equal (distance (vlax-curve-getpointatparam object 0) (vlax-curve-getpointatparam object 2))
								(distance (vlax-curve-getpointatparam object 1) (vlax-curve-getpointatparam object 3))
								1e-8
						 )
						 (equal (angle (vlax-curve-getpointatparam object 0) (vlax-curve-getpointatparam object 1))
								(angle (vlax-curve-getpointatparam object 3) (vlax-curve-getpointatparam object 2))
								1e-8
						 )
						 (equal (angle (vlax-curve-getpointatparam object 1) (vlax-curve-getpointatparam object 2))
								(angle (vlax-curve-getpointatparam object 0) (vlax-curve-getpointatparam object 3))
								1e-8
						 )
						 (zerop (apply '+ (mapcar 'abs (mapcar 'cdr (vl-remove-if-not '(lambda (group) (= 42 (car group))) (entget (ssname ename_sset ename_index)))))))
					)
						(progn
							(vla-addline (vla-get-block (vla-get-activelayout (vla-get-activedocument (vlax-get-acad-object))))
										 (vlax-3d-point (vlax-curve-getpointatparam object 0))
										 (vlax-3d-point (vlax-curve-getpointatparam object 2))
							)
							(vla-addline (vla-get-block (vla-get-activelayout (vla-get-activedocument (vlax-get-acad-object))))
										 (vlax-3d-point (vlax-curve-getpointatparam object 1))
										 (vlax-3d-point (vlax-curve-getpointatparam object 3))
							)
						)
				)
				(princ)
	)
	(vla-endaundomark (vla-get-activedocument (vlax-get-acad-object)))
	(princ)
)

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

 

0 Likes
Message 7 of 12

pendean
Community Legend
Community Legend

@a.tawkXHKN6 wrote:

it doesn't accept rectangle selection. only polyline selection works here. 


Perhaps you are unaware that RECTANG command creates a closed PLINE? There is no RECTANG object type in AutoCAD.

PLUS what you are looking to do is for an object in your file only has four LINE objects instead? So not LISP designed for PLINEs is going to work.

 

Share with us a portion of your DWG file with this "rectangle" in it please.  Or just use a basic LISP like this that requires two point selection https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/simple-lisp-for-cross-lines-between-... (and others further down that same thread).

 

0 Likes
Message 8 of 12

a.tawkXHKN6
Contributor
Contributor

please find the attached drawing

0 Likes
Message 9 of 12

Kent1Cooper
Consultant
Consultant

So it's a Polyline after all.  But it has an extra vertex in the left edge.  That's going to spoil routines that expect 4-vertex/4-segment Polylines.  Being orthogonally oriented, this one could be X-ed in using its bounding box, but that won't do at other orientations.  Better to fix the extra vertex situation, and use one of the routines that doesn't depend on an orthogonal orientation.

Kent Cooper, AIA
0 Likes
Message 10 of 12

pendean
Community Legend
Community Legend

@a.tawkXHKN6 wrote:

please find the attached drawing


You have a 5-sided 'rectangle'

pendean_0-1677514244837.png

 

Fix it

pendean_1-1677514274725.png

 

pendean_2-1677514296856.png

 

0 Likes
Message 11 of 12

ВeekeeCZ
Consultant
Consultant

If you have thousands of them, use OVERKILL to remove extra vertices.

0 Likes
Message 12 of 12

Sea-Haven
Mentor
Mentor

My $0.05 like the others just draw it correct.

SeaHaven_0-1677565103926.png

 

(if (not AH:getvalsm)(load "Multi Getvals.lsp"))
(setq ans (AH:getvalsm (list "Enter values " "Length " 5 4 "6" "width" 5 4 "1")))
(setq L1 (atof (car ans)) L2 (atof (cadr ans)))

 

0 Likes