Nikon Posted November 14, 2025 Posted November 14, 2025 (edited) Hello everyone, can you tell me how to get the coordinates of the upper-left point of the trapezoid correctly? And the angle of 30 degrees does not work... (defun c:DrawTrapez30 (/ baseHeight height angle radian topBase pt1 pt2 pt3 pt4) (setq baseHeight (getreal "\nEnter the size of the bottom base: ")) (setq height (getreal "\nEnter the height of the trapezoid: ")) ;; Angle in radians (setq angle 30) (setq radian (* angle (/ pi 180.0))) ;; Calculating the upper base (setq topBase (* baseHeight (cos radian))) ;; Determining the starting point (setq pt1 (getpoint "\nEnter the starting point of the lower base: ")) ;; Calculating the coordinates of the vertices of a trapezoid??? (setq pt2 (list (car pt1) (+ (cadr pt1) height))) ; upper left point (setq pt3 (list (+ (car pt1) baseHeight) (cadr pt1))) ; lower right point (setq pt4 (list (+ (car pt1) baseHeight (* (sin radian) height)) (+ (cadr pt1) height ))) ; upper right point ;; Drawing a trapezoid (command "_.pline" pt1 pt2 pt4 pt3 "_C") (princ)) Edited November 14, 2025 by Nikon Quote
marko_ribar Posted November 14, 2025 Posted November 14, 2025 (edited) Untested, but it should work IMHO... (defun c:DrawTrapez30 (/ baseHeight height ang radian topBase pt1 pt2 pt3 pt4) (initget 7) (setq baseHeight (getreal "\nEnter the size of the bottom base: ")) (initget 7) (setq height (getreal "\nEnter the height of the trapezoid: ")) ;; Angle in radians (setq ang 30) (setq radian (* ang (/ pi 180.0))) ;; Determining the starting point (setq pt1 (mapcar (function +) (list 0.0 0.0) (getpoint "\nEnter the starting point of the lower base: "))) ;; Calculating the coordinates of the vertices of a trapezoid (setq pt2 (polar pt1 0.0 baseHeight)) (setq pt3 (polar pt2 (- (* 0.5 pi) radian) (/ height (cos radian)))) (setq pt4 (polar pt1 (+ (* 0.5 pi) radian) (/ height (cos radian)))) ;; Calculating the upper base (setq topBase (distance pt3 pt4)) ;; Drawing a trapezoid (command "_.pline" "_non" pt1 "_non" pt2 "_non" pt3 "_non" pt4 "_C") (princ) ) Edited November 14, 2025 by marko_ribar Changed "angle" variable to "ang" and added "_non" osnaps into "_.pline" command line... 1 Quote
GLAVCVS Posted November 14, 2025 Posted November 14, 2025 1 hour ago, Nikon said: Hello everyone, can you tell me how to get the coordinates of the upper-left point of the trapezoid correctly? And the angle of 30 degrees does not work... (defun c:DrawTrapez30 (/ baseHeight height angle radian topBase pt1 pt2 pt3 pt4) (setq baseHeight (getreal "\nEnter the size of the bottom base: ")) (setq height (getreal "\nEnter the height of the trapezoid: ")) ;; Angle in radians (setq angle 30) (setq radian (* angle (/ pi 180.0))) ;; Calculating the upper base (setq topBase (* baseHeight (cos radian))) ;; Determining the starting point (setq pt1 (getpoint "\nEnter the starting point of the lower base: ")) ;; Calculating the coordinates of the vertices of a trapezoid??? (setq pt2 (list (car pt1) (+ (cadr pt1) height))) ; upper left point (setq pt3 (list (+ (car pt1) baseHeight) (cadr pt1))) ; lower right point (setq pt4 (list (+ (car pt1) baseHeight (* (sin radian) height)) (+ (cadr pt1) height ))) ; upper right point ;; Drawing a trapezoid (command "_.pline" pt1 pt2 pt4 pt3 "_C") (princ)) I think you should rename the variable 'angle': 'angle' is a language symbol, that is, a function. Try changing it to 'ang', for example. 1 1 Quote
pkenewell Posted November 14, 2025 Posted November 14, 2025 Here's my quick version: (defun c:trapezoid (/ bw p0 p1 p2 p3 p4 ra sa th) (if (and (setq bw (getreal "\nEnter the width of the Base: ")) (setq th (getreal "\nEnter the Height: ")) (setq sa (getreal "\nEnter the side angles: ")) (setq p0 (getpoint "\nSelect the insertion point: ")) ) (progn (setq ra (* pi (/ sa 180.0)) p1 (list (- (car p0) (/ bw 2)) (cadr p0) (caddr p0)) p2 (list (+ (car p1) bw) (cadr p0) (caddr p0)) p3 (list (+ (car p2) (* (/ th (cos ra)) (sin ra))) (+ (cadr p0) th) (caddr p0)) p4 (list (- (car p1) (* (/ th (cos ra)) (sin ra))) (+ (cadr p0) th) (caddr p0)) ) (command-s "._pline" "_non" p1 "_non" p2 "_non" p3 "_non" p4 "_c") ) ) ) 1 1 Quote
Nikon Posted November 14, 2025 Author Posted November 14, 2025 (edited) 1 hour ago, marko_ribar said: Untested, but it should work IMHO... (defun c:DrawTrapez30 (/ baseHeight height ang radian topBase pt1 pt2 pt3 pt4) @marko_ribar thanks, but the code gives an error: error: invalid argument type: numberp: #<SUBR @000001f24dbceca0 ANGLE> I replaced it in this line "angle" на "ang" and the code now works. (setq radian (* angle (/ pi 180.0))) → (setq radian (* ang (/ pi 180.0))) Edited November 14, 2025 by Nikon Quote
Nikon Posted November 14, 2025 Author Posted November 14, 2025 (edited) 58 minutes ago, pkenewell said: Here's my quick version: (defun c:trapezoid (/ bw p0 p1 p2 p3 p4 ra sa th) @pkenewell Thank you, your code works perfectly! And thank you for the possibility of choice the angle. Edited November 14, 2025 by Nikon 1 Quote
mhupp Posted November 14, 2025 Posted November 14, 2025 I always try to avoid using command when I can. entmakex is faster and doesn't output to the command line. wrapping with setq you can even save the entity or add to selection set. (setq pts (list p1 p2 p3 p4)) (setq trap (entmakex (append (list '(0 . "LWPOLYLINE") '(100 . "AcDbEntity") '(100 . "AcDbPolyline") '(90 . 4) '(70 . 1)) (mapcar '(lambda (p) (cons 10 p)) pts) ) ) ) (sssetfirst nil (ssadd trap)) 2 1 Quote
Nikon Posted November 14, 2025 Author Posted November 14, 2025 3 minutes ago, mhupp said: I always try to avoid using command when I can. entmakex is faster and doesn't output to the command line. wrapping with setq you can even save the entity or add to selection set. @mhupp thanks, but I don't understand how this can be used in the code... Quote
mhupp Posted November 14, 2025 Posted November 14, 2025 Replace (command-s line with what i posted. the (sssetfirst line isn't need just to show as an example. 1 Quote
Steven P Posted November 14, 2025 Posted November 14, 2025 1 hour ago, mhupp said: I always try to avoid using command when I can. entmakex is faster and doesn't output to the command line. wrapping with setq you can even save the entity or add to selection set. (setq pts (list p1 p2 p3 p4)) (setq trap (entmakex (append (list '(0 . "LWPOLYLINE") '(100 . "AcDbEntity") '(100 . "AcDbPolyline") '(90 . 4) '(70 . 1)) (mapcar '(lambda (p) (cons 10 p)) pts) ) ) ) (sssetfirst nil (ssadd trap)) and entmake isn't affected by snaps in case you forget to bypass them as necessary. Far better I think - It is all I ever do when creating entities Quote
Nikon Posted November 14, 2025 Author Posted November 14, 2025 (edited) 58 minutes ago, mhupp said: Replace (command-s line with what i posted. the (sssetfirst line isn't need just to show as an example. (command-s line with what i posted. the (sssetfirst line isn't need just to show as an example. I replaced in the code @pkenewell (command-s "._pline" "_non" p1 "_non" p2 "_non" p3 "_non" p4 "_c") with (entmakex… It works too. ;; pkenewell 14.11.2025 (defun c:trapezoid-pk-mh (/ bw p0 p1 p2 p3 p4 ra sa th) (if (and (setq bw (getreal "\nEnter the width of the Base: ")) (setq th (getreal "\nEnter the Height: ")) (setq sa (getreal "\nEnter the side angles: ")) (setq p0 (getpoint "\nSelect the insertion point: ")) ) (progn (setq ra (* pi (/ sa 180.0)) p1 (list (- (car p0) (/ bw 2)) (cadr p0) (caddr p0)) p2 (list (+ (car p1) bw) (cadr p0) (caddr p0)) p3 (list (+ (car p2) (* (/ th (cos ra)) (sin ra))) (+ (cadr p0) th) (caddr p0)) p4 (list (- (car p1) (* (/ th (cos ra)) (sin ra))) (+ (cadr p0) th) (caddr p0)) ) ; (command-s "._pline" "_non" p1 "_non" p2 "_non" p3 "_non" p4 "_c") ; pkenewell (setq pts (list p1 p2 p3 p4)) ; mhupp (setq trap (entmakex (append (list '(0 . "LWPOLYLINE") '(100 . "AcDbEntity") '(100 . "AcDbPolyline") '(90 . 4) '(70 . 1)) (mapcar '(lambda (p) (cons 10 p)) pts) ) ) ) ) ) (princ) ) Edited November 14, 2025 by Nikon 1 Quote
pkenewell Posted November 14, 2025 Posted November 14, 2025 2 hours ago, mhupp said: I always try to avoid using command when I can. entmakex is faster and doesn't output to the command line. wrapping with setq you can even save the entity or add to selection set. (setq pts (list p1 p2 p3 p4)) (setq trap (entmakex (append (list '(0 . "LWPOLYLINE") '(100 . "AcDbEntity") '(100 . "AcDbPolyline") '(90 . 4) '(70 . 1)) (mapcar '(lambda (p) (cons 10 p)) pts) ) ) ) (sssetfirst nil (ssadd trap)) @mhupp that works fine. I personally think the benefits are extremely tiny on such a simple code form. For short routines, I tend to just use the command sequence. On more intensive stuff I use the ActiveX entity creation more often then using entmake with DXF codes. 1 1 Quote
pkenewell Posted November 14, 2025 Posted November 14, 2025 (edited) 1 hour ago, Nikon said: I replaced in the code @pkenewell (command-s "._pline" "_non" p1 "_non" p2 "_non" p3 "_non" p4 "_c") with (entmakex… It works too. ;; pkenewell 14.11.2025 (defun c:trapezoid-pk-mh (/ bw p0 p1 p2 p3 p4 ra sa th) (if (and (setq bw (getreal "\nEnter the width of the Base: ")) (setq th (getreal "\nEnter the Height: ")) (setq sa (getreal "\nEnter the side angles: ")) (setq p0 (getpoint "\nSelect the insertion point: ")) ) (progn (setq ra (* pi (/ sa 180.0)) p1 (list (- (car p0) (/ bw 2)) (cadr p0) (caddr p0)) p2 (list (+ (car p1) bw) (cadr p0) (caddr p0)) p3 (list (+ (car p2) (* (/ th (cos ra)) (sin ra))) (+ (cadr p0) th) (caddr p0)) p4 (list (- (car p1) (* (/ th (cos ra)) (sin ra))) (+ (cadr p0) th) (caddr p0)) ) ; (command-s "._pline" "_non" p1 "_non" p2 "_non" p3 "_non" p4 "_c") ; pkenewell (setq pts (list p1 p2 p3 p4)) ; mhupp (setq trap (entmakex (append (list '(0 . "LWPOLYLINE") '(100 . "AcDbEntity") '(100 . "AcDbPolyline") '(90 . 4) '(70 . 1)) (mapcar '(lambda (p) (cons 10 p)) pts) ) ) ) ) ) (princ) ) @Nikon IMHO - the extra variables that mhupp referenced in his example are unnecessary, and you didn't localize them. I'd recommend you simplify to this: (defun c:trapezoid (/ bw p0 p1 p2 p3 p4 ra sa th) (if (and (setq bw (getreal "\nEnter the width of the Base: ")) (setq th (getreal "\nEnter the Height: ")) (setq sa (getreal "\nEnter the side angles: ")) (setq p0 (getpoint "\nSelect the insertion point: ")) ) (progn (setq ra (* pi (/ sa 180.0)) p1 (list (- (car p0) (/ bw 2)) (cadr p0) (caddr p0)) p2 (list (+ (car p1) bw) (cadr p0) (caddr p0)) p3 (list (+ (car p2) (* (/ th (cos ra)) (sin ra))) (+ (cadr p0) th) (caddr p0)) p4 (list (- (car p1) (* (/ th (cos ra)) (sin ra))) (+ (cadr p0) th) (caddr p0)) ) (entmakex (append (list '(0 . "LWPOLYLINE") '(100 . "AcDbEntity") '(100 . "AcDbPolyline") '(90 . 4) '(70 . 1)) (mapcar '(lambda (x) (cons 10 x)) (list p1 p2 p3 p4)) ) ) ) ) ) Edited November 14, 2025 by pkenewell 2 Quote
mhupp Posted November 14, 2025 Posted November 14, 2025 I was just showing a different way to do things. In AutoCAD do whatever your comfortable with. Yes with simple code you will never see a difference in time. 1 Quote
pkenewell Posted November 14, 2025 Posted November 14, 2025 23 minutes ago, mhupp said: I was just showing a different way to do things. In AutoCAD do whatever your comfortable with. Yes with simple code you will never see a difference in time. @mhupp No problem my friend. I agree wholeheartedly that it's good to show alternative ways to do the code - helps folks to learn the way it works. Cheers 1 Quote
Lee Mac Posted November 14, 2025 Posted November 14, 2025 Another for fun - should work in all UCS/Views: (defun c:itsatrap ( / hgt len ocs off pt1 pt2 ) (if (and (setq pt1 (getpoint "\nInsertion point: ")) (setq len (getdist "\nLength of base: " pt1)) (setq hgt (getdist "\nHeight: " pt1)) (setq ocs (trans '(0 0 1) 1 0 t) pt2 (cons (+ (car pt1) len) (cdr pt1)) off (* hgt (/ (sqrt 3) 3)) ) ) (entmake (list '(0 . "LWPOLYLINE") '(100 . "AcDbEntity") '(100 . "AcDbPolyline") '(90 . 4) '(70 . 1) (cons 10 (trans pt1 1 ocs)) (cons 10 (trans pt2 1 ocs)) (cons 10 (trans (list (+ (car pt2) off) (+ (cadr pt2) hgt) (caddr pt2)) 1 ocs)) (cons 10 (trans (list (- (car pt1) off) (+ (cadr pt1) hgt) (caddr pt1)) 1 ocs)) (cons 210 ocs) ) ) ) (princ) ) 3 1 Quote
BIGAL Posted November 14, 2025 Posted November 14, 2025 (edited) If you want 30 degree use (* (/ 1.0 6.0) pi) My $0.05 the front end can preset values but change as required. (if (not AH:getvalsm)(load "Multi Getvals.lsp")) (setq ans (AH:getvalsm (list "Enter Values" "Base Width " 5 4 "100" "Height " 5 4 "100" "Angle" 5 4 "30" ))) (setq Wid (atof (nth 0 ans)) ht (atof (nth 1 ans)) ang (atof (nth 2 ans))) (setq ang (* pi (/ ang 180.0))) Multi GETVALS.lsp Edited November 14, 2025 by BIGAL 1 1 Quote
Nikon Posted November 15, 2025 Author Posted November 15, 2025 (edited) Here is another version of the code ;; Draw of a trapezoid based on the lower base, height and angle of the lateral lines ;; insertion beyond the lower left point of the base line (defun c:DrawTrapWHAng (/ baseWidth height ang radian dx pt1 pt2 pt3 pt4) (setq baseWidth (getreal "Enter the size of the bottom base: ")) (setq height (getreal "Enter the height of the trapezoid: ")) ;; The angle between the vertical and the lateral side (in degrees) ; (setq ang 30.0) ; The angle is set to 30 ;; You can set any angle manually (in degrees) (setq ang (getreal "Enter the angle between the vertical and the side (in degrees): ")) ;; Converting the angle to radians ;; If you need a fixed angle of 30 degrees (or other), delete the ";" in front of one of the lines below. ; (setq radian (* ang (/ pi 180.0))) ; (setq radian (* (/ 1.0 6.0) pi)) ; or *advice from BIGAL ; (setq radian (/ pi 6)) ; or *advice from Lee Mac (setq dx (* height (/ (sin radian) (cos radian)))) ;; The starting point of the lower base (bottom left) (setq pt1 (getpoint "Specify the starting point of the lower base: ")) ;; Lower right point (setq pt2 (list (+ (car pt1) baseWidth) (cadr pt1))) ;; Upper right point – move outward to the right (setq pt3 (list (+ (car pt1) baseWidth dx) (+ (cadr pt1) height))) ;; Upper left point – move outward to the left (setq pt4 (list (- (car pt1) dx) (+ (cadr pt1) height))) ;; Drawing a trapezoid (command "_.pline" pt1 pt2 pt3 pt4 "_C") (princ) ) Edited November 16, 2025 by Nikon ; (setq radian (/ pi 6)) ; (setq radian (* (/ 1.0 6.0) pi)) 1 Quote
Lee Mac Posted November 16, 2025 Posted November 16, 2025 On 11/14/2025 at 11:53 PM, BIGAL said: If you want 30 degree use (* (/ 1.0 6.0) pi) Or just (/ pi 6) Quote
ScottMC Posted November 16, 2025 Posted November 16, 2025 here's the 'BIGAL DCL version.. (defun c:trpz ;; https://www.cadtutor.net/forum/topic/98827-the-coordinates-of-the-trapezoid/ (/ *error* ang radian pt1 pt2 pt3 pt4) (princ "\n Trapezoid from Lower.Left..") (defun *error* ( msg ) (setvar 'cmdecho 0) ;; 5.28.24 (vla-endundomark (vla-get-activedocument (vlax-get-acad-object))) (if msg (prompt (strcat "\n" msg))) (setvar 'cmdecho 1) (princ "\n") (princ) ) (if (not AH:getvalsm) (load "Multi Getvals.lsp") ) ;_ end of if (setvar 'cmdecho 0) (setq ans (AH:getvalsm (list "Enter Values" ;; title "Base Width " 5 4 "10" "Height " 5 4 "10" "Angle" 5 4 "30" ) ;_ end of list ) ;_ end of AH:getvalsm ) ;_ end of setq (setq Wid (atof (nth 0 ans)) ht (atof (nth 1 ans)) ang (atof (nth 2 ans)) ) ;_ end of setq (if (= ang 90)(c:trpz)) ;; restart if ang 90.. etc (setq radian (* pi (/ ang 180.0))) ;; Determining the starting point (setq pt1 (mapcar (function +) (list 0.0 0.0) (getpoint "\r Specify the Lower.Left Basepoint: ") ;; \r overrights values from Multi.. ) ;_ end of mapcar ) ;_ end of setq ;; Calculating the coordinates of the vertices of a trapezoid (setq pt2 (polar pt1 0.0 wid)) (setq pt3 (polar pt2 (- (* 0.5 pi) radian) (/ ht (cos radian)))) (setq pt4 (polar pt1 (+ (* 0.5 pi) radian) (/ ht (cos radian)))) ;; Drawing a trapezoid (command "_.pline" "_non" pt1 "_non" pt2 "_non" pt3 "_non" pt4 "_C") ;_ end of command (setvar 'cmdecho 1) (*error* nil) (princ) ) ;_ end of defun 1 Quote
Recommended Posts
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.