Abrasive Posted September 27, 2022 Posted September 27, 2022 I looked but can't find a routine to get the TOTAL width and height of an irregular polygon? That value can be put Into variable I can then use it. I would also like to sore the X & Y locations of the extremity points. Looking for the simplest routine...so I can understand it...lol Any help would be great! Thanks Quote
mhupp Posted September 27, 2022 Posted September 27, 2022 This will give you the length and width of any selected entity (defun C:foo (/ poly LL UR L&W) ;if you want to keeps these points for future use remove LL UR from this line. (setq poly (vlax-ename->vla-object (car (entsel "\nSelect Polyline")))) ;select entity and convert to vla-object (vla-GetBoundingBox poly 'LL 'UR) ;use bounding box function to get lower left and upper right points (smallest horizontal rectangle) (setq LL (vlax-safearray->list LL) ;convert to point UR (vlax-safearray->list UR) ;LR (list (car UR) (cadr LL)) ;if you need the other corners uncomment ;UL (list (car LL) (cadr UR)) L&W (mapcar '- UR LL) ;subtract UR from LL to get overall length and width of entity. ) (prompt (strcat "\nPoly Lenght:" (rtos (car L&W) 2 3) " Width:" (rtos (cadr L&W) 2 3))) ;displays lenght and width (princ) ) Example: picked blue rectangle the white is representing the bounding box. If you want accurate dimensions make sure things are square. Quote
Abrasive Posted September 27, 2022 Author Posted September 27, 2022 Is there a way to put all the X&Y points in a list that I can use? Quote
mhupp Posted September 27, 2022 Posted September 27, 2022 What are you trying to do? (setq ptslst (list LL UR)) Quote
BIGAL Posted September 27, 2022 Posted September 27, 2022 (edited) Had a think about this and what if I am trying to fit on a rectang sheet of wood ? So pick a side then UCS OB this would be used as sheet edge. This code snippet gives world and current ucs points, can then work out X&Y max min. which in turn can get length and width. (defun test () (setq plent (entsel "\nPick pline")) (setq co-ord (mapcar 'cdr (vl-remove-if-not '(lambda (x) (= (car x) 10)) (entget (car plent))))) (setq co-ord2 '()) (foreach pt co-ord (setq co-ord2 (cons (trans pt 0 1) co-ord2)) ) (setq co-ord2 (reverse co-ord2)) (setq lstx '() lsty '()) (foreach xy co-ord2 (setq lstx (cons (car xy) lstx)) (setq lsty (cons (cadr xy) lsty)) ) (setq lstx (vl-sort lstx '(lambda (x y) (< x y)))) (setq lsty (vl-sort lsty '(lambda (x y) (< x y)))) (setq minx (car lstx) maxx (last lstx)) (setq miny (car lsty) maxy (last lsty)) (command "line" (list minx miny) (list maxx miny) (list maxx maxy) (list minx maxy) (list minx miny) "") ) (test) Pick pline((0.4 8.0) (-10.1196005246636 -1.23785715420501) (-5.3487836362203 -15.0363826630028) (17.0484759810924 -15.3867562274954) (18.5241801323081 -3.07487995870171) (8.58914071535494 -1.93690290111795)) Pick pline((-5.29326847576615 12.9607603497426 0.0) (0.0 0.0 0.0) (14.6 0.0 0.0) (22.2498536304372 21.0532595916377 0.0) (11.0960574339204 26.4710848385949 0.0) (6.77409446849257 17.4532893138854 0.0)) Edited September 29, 2022 by BIGAL Quote
Abrasive Posted September 29, 2022 Author Posted September 29, 2022 @BIGAL I like it! works perfectly. I've tried to find how to get min and max values from the co-ord2 list but can't. There doesn't seem to be a simple way to find just the min/max X values? What I plan on doing is dividing the overall width by a given/entered number then draw vertical lines over the polygon at those graduations, starting from the left most point. I've learned enough to get that last part done, but the min/max I can't figure out any help would be great, Thanks Quote
BIGAL Posted September 29, 2022 Posted September 29, 2022 You need to look at the co-ords X & Y check is lowest is biggest. I updated code above. Quote
Jonathan Handojo Posted September 29, 2022 Posted September 29, 2022 (defun c:foo ( / enx inv maxx maxy minx miny n pl pt) (while (progn (setvar "errno" 0) ;; Reset errno (initget "Exit") ;; Initialise user input with the option "Exit" (setq pl (entsel "\nSelect polyline [Exit] <exit>: ")) ;; Prompt the user to select polyline (cond ;; If user misses selection, print "Nothing selected" and start loop over. ( (= (getvar "errno") 7) (princ "\nNothing selected.")) ;; If user specifies "Exit" or presses Enter/Space, end the while loop and the command ends. ( (member pl '("Exit" nil)) nil) ;; If the user selects any other object than a polyline, prompt to select again and start loop over. ( (not (wcmatch (cdr (assoc 0 (entget (setq pl (car pl))))) "LWPOLYLINE")) (princ "\nObject is not a polyline") ) ( t ;; Extract details of the polyline (setq enx (entget pl)) ;; We will store the min x-value in variable 'minx', and the max in 'maxx' ;; Similarly, the same for the y-values. ;; At the start, let's set then to nil. (setq minx nil maxx nil miny nil maxy nil) ;; Loop through the polyline detail. (foreach x enx ;; Polyline Coordinates have the DXF code 10. ;; Let's check for this: (if (= (car x) 10) (progn (setq pt (cdr x)) ;| If 'minx' is nil, no coordinates have been processed yet, therefore we will immediately set 'minx' to the current x-value. After the first loop. 'minx' cannot be nil anymore. If 'minx' is not nil, then the second and remaining coordinates need to be compared with 'minx' to ensure that the criteria match. We can use the OR statement to compare the x-value of the current coordinate with 'minx'. If the value of the current coordinate is less than 'minx', update the variable 'minx' with this new value. After finishing the 'foreach' loop, the variable 'minx' shall then hold the smallest x-value. |; (if (or (not minx) (< (car pt) minx)) (setq minx (car pt))) ;; Use the same logic for the remaining variables: (if (or (not maxx) (> (car pt) maxx)) (setq maxx (car pt))) (if (or (not miny) (< (cadr pt) miny)) (setq miny (cadr pt))) (if (or (not maxy) (> (cadr pt) maxy)) (setq maxy (cadr pt))) ) ) ;; end if ) ;; end foreach ;; At this point, 'minx', 'miny', 'maxx' and 'maxy' should contain the ;; bounding points. ;; ---------------------- Draw Vertical Lines (Below) -------------------------- ;; (initget 6) (if (setq n (getint "\nSpecify number of segments <exit>: ")) (progn (setq inv (/ (- maxx minx) n)) (repeat (1+ n) (entmake (list '(0 . "LINE") (list 10 minx miny 0.0) (list 11 minx maxy 0.0) ) ) (setq minx (+ minx inv)) ) ) ) ;; ---------------------- Draw Vertical Lines (Above) -------------------------- ;; n ) ) ) ) (princ) ) (vl-load-com) 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.