Entmake LWPOLYLINE using 2 points

Entmake LWPOLYLINE using 2 points

rpajounia
Advocate Advocate
758 Views
10 Replies
Message 1 of 11

Entmake LWPOLYLINE using 2 points

rpajounia
Advocate
Advocate

Is there a way i can make a rectangle using entmake. I can provide bottom left corner points and top right corner points

0 Likes
759 Views
10 Replies
Replies (10)
Message 2 of 11

hak_vz
Advisor
Advisor

 

 

 

(defun _rect (p1 p2 / lt point2d x1 y1 x2 y2 pts) 
	(defun lt (pt) (trans pt 1 0))
	(defun point2d (pt) (list (car pt) (cadr pt)))
        ;(command "_.ucs" "w")
	(setq
		x1 (apply 'min (mapcar 'car (list p1 p2)))
		y1 (apply 'min (mapcar 'cadr (list p1 p2)))
		x2 (apply 'max (mapcar 'car (list p1 p2)))
		y2 (apply 'max (mapcar 'cadr (list p1 p2)))
	    pts (mapcar 'point2d(mapcar 'lt (list (list x1 y1) (list x2 y1) (list x2 y2) (list x1 y2))))
	)
	(cond 
		((and pts)
			(entmake
				(apply 'append
					(cons
					  (list
						'(0 . "LWPOLYLINE")
						'(100 . "AcDbEntity")
						'(100 . "AcDbPolyline")
						'(410 . "Model")
						'(8 . "0")
						'(38 . 0)
						'(62 . 256)
						'(67 . 0)
						(cons 90 (length pts))
						'(70 . 1)
					  )
					  (mapcar 'list (mapcar '(lambda (a) (cons 10 a)) pts))
					) 
				)
			)
		)
	)
	(princ)
)

 

 

 

 

 

 

Usage:
(setq p1 '(200 200) p2 '(100 100))
(_rect p1 p2)

 

 

 

This will create rectangle in current UCS starting from first point in lower left corner and drawn  in counter-clockwise direction as a closed polyline.

If you work in World UCS remove ; in front (command "_.ucs" "w") in above code

I hope you know how to add it to your code

 

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.
0 Likes
Message 3 of 11

ronjonp
Mentor
Mentor

@hak_vz 

FWIW, I had to put the closed flag 70 after code 90 for this to work.

				      (cons 90 (length pts))
				      '(70 . 1)
0 Likes
Message 4 of 11

hak_vz
Advisor
Advisor

@ronjonp wrote:

@hak_vz 

FWIW, I had to put the closed flag 70 after code 90 for this to work.

				      (cons 90 (length pts))
				      '(70 . 1)

Hi @ronjonp 

I've written this code without testing from my second computer that don't have Autocad installed. After testing I have to admit your correction is needed for closing poyline. I have made this changes to code above.

Thanks!

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.
0 Likes
Message 5 of 11

Kent1Cooper
Consultant
Consultant

@ronjonp wrote:

 

... the closed flag 70 after code 90 for this to work.
				      ....
				      '(70 . 1)

Or '(70 . 129) if a non-continuous linetype is involved and you want linetype generation enabled.

Kent Cooper, AIA
0 Likes
Message 6 of 11

ronjonp
Mentor
Mentor

You could also check the PLINEGEN setting:

(cons 70
      (if (= 0 (getvar 'plinegen))
	1
	129
      )
)

 

0 Likes
Message 7 of 11

Kent1Cooper
Consultant
Consultant

Why, if I may ask, do you want to do it with (entmake)?  You may have a good reason, but this is a good example of how very complex that can be in comparison with just using a command that takes care of all that complexity for you [in less than 1/24 as many code characters ignoring spaces]:

(command "_.rectang" p1 p2)

Kent Cooper, AIA
0 Likes
Message 8 of 11

rpajounia
Advocate
Advocate

entmake returns the entity id of the new created poly line 🙂

0 Likes
Message 9 of 11

ronjonp
Mentor
Mentor

@rpajounia wrote:

entmake returns the entity id of the new created poly line 🙂


entmakeX returns the ename ... but really you could use a command route if necessary and call (entlast) after it's created.

0 Likes
Message 10 of 11

rpajounia
Advocate
Advocate

*edited* nvm had set and not setq

0 Likes
Message 11 of 11

ronjonp
Mentor
Mentor

Change entmake to entmakeX. Or set the the entmake to 'e' and call this: (setq e (cdr (assoc 0 (entget e))))

0 Likes