TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: pedroantonio on March 08, 2014, 02:43:49 PM

Title: Adjust the area of a polygon
Post by: pedroantonio on March 08, 2014, 02:43:49 PM
Good evening. I am looking for a lisp file that fixes the size polygon.
I designed the polygon knowing the lengths of sides and its area. But  at the end the area is not the same.
Look the attach *.dwg
Thanks
Title: Re: Adjust the area of a polygon
Post by: roy_043 on March 09, 2014, 03:44:48 AM
If I look at the green rectangle in your drawing I notice that the long sides are not exactly parallel and one is 165.90 long and the other is 165.93. Both short sides are 30.00. The conclusion must be that this is not an exact rectangle. The question is then: Why is 4977.00 the correct area?

Don't you typically need measurements for multiple diagonals to accurately draw these land parcels?

But in theory you can create a lisp that scales the polyline using this factor:
Code: [Select]
(sqrt (/ expectedArea currentArea)).
Title: Re: Adjust the area of a polygon
Post by: pedroantonio on March 09, 2014, 04:19:08 AM
the dimensions and the area of ​​the polygon I get from PLANNING.
Quote
But in theory you can create a lisp that scales the polyline using this factor:
Code: [Select]
(sqrt (/ expectedArea currentArea)).

Yes but if i scale the polygon the dimensions will be change.I don't want to change the dimensions.I need the polygon to have the dimension I give and the area I give.
For example a lisp
1) Select a close polyline
2) Give the area of the polygon
Then adjust the polygon (Keep the dimensions)
Something like mapcheck but with specific area
Title: Re: Adjust the area of a polygon
Post by: snownut2 on March 09, 2014, 10:04:13 AM
pedro,

You are trying to adjust area by changing polygon angles, with no reference angles to begin with, there can be multiple solutions to this....After looking at your drawing, you realize that by doing this you will be affecting the area of any adjoining lot, you will be opening up pandora's box...
Title: Re: Adjust the area of a polygon
Post by: pedroantonio on March 09, 2014, 12:35:06 PM
Yes snownut2 you are right but i was thinking a lisp

1) Select a close polyline
2) Give the area of the polygon
3)Select the point  (one or two points to change the angle)

Then adjust the polygon (Keep the dimensions)
Something like mapcheck but with specific area

What do you think ? I think this way is better .
Title: Re: Adjust the area of a polygon
Post by: snownut2 on March 09, 2014, 12:40:50 PM
Pedro,

Think about that, you need to adjust every single angle to maintain the line lengths, not just one or two.

Bruce
Title: Re: Adjust the area of a polygon
Post by: pedroantonio on March 09, 2014, 12:47:50 PM
Not exactly . If i want to change point 2 ,i need to select point 1 ,point2,point3
look the dwg
Title: Re: Adjust the area of a polygon
Post by: snownut2 on March 09, 2014, 01:22:12 PM
Maybe I was a little dramatic when I said every single angle, however I still stand that you need to adjust multiple (many) angles to maintain the line lengths.  (on lots with only a few lot lines it will be all angles).

In your sample drawing, the angle prior to pt1 along with pt1, pt2 & pt3 angles would all be affected by making any area change.

Affecting 4 out of 6 angles, and affecting the area of the abutting lot to the rear.

Title: Re: Adjust the area of a polygon
Post by: mailmaverick on March 09, 2014, 01:35:44 PM
Dear Friend,

I did some research and found that there are multiple solutions to your problem.

There can be multiple combinations of 6 sided polyons with length of sides given by you and area also as given by you.

As an example, I am attaching DWG file in which I have made three 6 sides polygons with each side as given by you and area also as given by you.

You can see that apart from these three, there are even more multiple solutions.

Title: Re: Adjust the area of a polygon
Post by: pedroantonio on March 09, 2014, 01:41:42 PM
yes but if i chose only  one angle to be change  like point1,point2,point3 not all the angles , it will change only the angle in the point 2
Title: Re: Adjust the area of a polygon
Post by: roy_043 on March 09, 2014, 02:02:06 PM
Isn't this really a case of rounding?
2517.12 ("wrong"); Rounded: 2517 ("correct").
4977.33 ("wrong"); Rounded: 4977 ("correct").
Title: Re: Adjust the area of a polygon
Post by: pedroantonio on March 09, 2014, 02:04:15 PM
yes but in same othe option you will have a bigger difference for example

2517.12 ---->2517.90 or 2519.30 etc
Title: Re: Adjust the area of a polygon
Post by: snownut2 on March 09, 2014, 02:06:57 PM
Pedro,

Why don't you try drawing a new lot configuration with a new area by adjusting the angle @ pt2 ONLY, keeping all line lengths are they are then post result here, that way we can understand exactly what it is you are after.

If as in Roy's post it is caused by rounding, instead of using an "=" sign in the comparison statement use  "equal" with a fuzz factor. 

Code - Auto/Visual Lisp: [Select]
  1. (if (equal area1 area2 0.1)
  2.    (carry on)
  3. )
  4.  


Bruce 
Title: Re: Adjust the area of a polygon
Post by: roy_043 on March 09, 2014, 02:12:30 PM
Code: [Select]
(setq differencePercent (* (/ (- 2517.90 2517.0) 2517.0) 100.0)) ; => 0.0357569 percentWhat are we talking about. Aren't these tolerances normal in topography?
Title: Re: Adjust the area of a polygon
Post by: pedroantonio on March 09, 2014, 02:33:12 PM
yes it is. just i ask for it.I understand that is not possible to make a lisp to fix it. Thank you gays..
Title: Re: Adjust the area of a polygon
Post by: MP on March 09, 2014, 03:46:12 PM
lol
Title: Re: Adjust the area of a polygon
Post by: snownut2 on March 09, 2014, 04:25:25 PM
lol

you're a sport... 8-)
Title: Re: Adjust the area of a polygon
Post by: vladgothe on March 10, 2014, 06:00:17 AM
Hello, this is my first post and I need your help. I am looking for a program in which the user selects a polyline, writes the desired area, then selects some vertices on that polyline and the program will stretch the selected vertices until the desired area is achieved. Can this be done?
Title: Re: Adjust the area of a polygon
Post by: mailmaverick on March 11, 2014, 03:06:45 AM
Hello Vladgothe

I dont think such a LISP is possible because there would be unlimited combinations of each vertex to be stretched.

Title: Re: Adjust the area of a polygon
Post by: ribarm on March 11, 2014, 05:31:26 AM
I would suggest rather simple code as Roy proposed - scaling instead of stretching...

Code: [Select]
(defun c:scf ( / ent ea len nlen cir ncir are nare vol nvol ch sf bp )
  (vl-load-com)
  (setq ent (car (entsel "\nPick entity for scaling")))
  (if (vlax-property-available-p (setq ea (vlax-ename->vla-object ent)) 'Length)
    (setq len (vla-get-length ea))
    (if (not (vl-catch-all-error-p (vl-catch-all-apply 'vlax-curve-getendparam (list ea)))) (setq len (- (vlax-curve-getdistatparam ea (vlax-curve-getendparam ea)) (vlax-curve-getdistatparam ea (vlax-curve-getstartparam ea)))))
  )
  (if (vlax-property-available-p ea 'Circumference)
    (setq cir (vla-get-circumference ea))
  )
  (if (vlax-property-available-p ea 'Area)
    (setq are (vla-get-area ea))
  )
  (if (vlax-property-available-p ea 'Volume)
    (setq vol (vla-get-volume ea))
  )
  (prompt "\nChoose current parameter to adjust while scaling : 1.Length = ") (princ len) (prompt "; 2.Circumference = ") (princ cir) (prompt "; 3.Area = ") (princ are) (prompt "; 4.Volume = ") (princ vol) (prompt " : ")
  (initget "1 2 3 4")
  (setq ch (getkword))
  (cond
    ( (eq ch "1")
      (initget 7)
      (setq nlen (getreal "\nSpecify desired Length : "))
      (setq sf (/ nlen len))
    )
    ( (eq ch "2")
      (initget 7)
      (setq ncir (getreal "\nSpecify desired Circumference : "))
      (setq sf (/ ncir cir))
    )
    ( (eq ch "3")
      (initget 7)
      (setq nare (getreal "\nSpecify desired Area : "))
      (setq sf (expt (/ nare are) (/ 1.0 2.0)))
    )
    ( (eq ch "4")
      (initget 7)
      (setq nvol (getreal "\nSpecify desired Volume : "))
      (setq sf (expt (/ nvol vol) (/ 1.0 3.0)))
    )
  )
  (setq bp (getpoint "\nPick base point for scale by factor for desired parameter : "))
  (vla-scaleentity ea (vlax-3d-point bp) sf)
  (princ)
)
Title: Re: Adjust the area of a polygon
Post by: vladgothe on March 11, 2014, 12:04:24 PM
Thank you ribarm. I already made a program which scales a polyline using the centroid, but this is good too.I tried to make myself a program, but it gives me an error and I don't know why. It will help me very much if you can help me to get rid of that error. Here is the code:
Code - Auto/Visual Lisp: [Select]
  1. (defun c:ms ()
  2. (setvar "CMDECHO" 0)
  3. (command "undo" "begin")
  4. (setq os_old (getvar "OSMODE"))
  5. (setvar "OSMODE" 1)
  6. (setq lista '())
  7. (setq obj (car (entsel "\nSelecteaza conturul: ")))
  8. (while (not obj)
  9. (setq obj (car (entsel "\nSelecteaza conturul: ")))
  10. )
  11. (if (wcmatch (cdr (assoc 0 (entget obj))) "*POLYLINE")
  12. ;;facem lista cu coordonatele
  13. (foreach x (setq e (entget obj))
  14.         (if (= 10 (car x)) (setq l (cons (cdr x) l)))
  15.     )
  16. (vla-put-closed (vlax-ename->vla-object obj) :vlax-true)       
  17. (setq arieinitiala (vla-get-Area (vlax-ename->vla-object obj)))
  18. (initget (+ 1 2 4))
  19. (setq ariedor (getreal (strcat "\nScrieti suprafata la care doriti sa ajungeti <" (rtos arieinitiala 2 2) "mp> ")))
  20. ;;daca aria dorita este egala cu aria initiala
  21. (if (= (rtos ariedor 2 0) (rtos arieinitiala 2 0))
  22. (alert "\nAti ales aceeasi arie cu cea a conturului!")
  23. (setq pt (getpoint "\nAlegeti punctele din care se modifica suprafata: "))
  24. (setq xpt (car pt))
  25. (setq ypt (cadr pt))
  26. (if (not (member (list xpt ypt) l))
  27. (alert "\nPunctul trebuie sa fie un vertex al conturului!")
  28. (setq lista (append (list (list xpt ypt)) lista))
  29. )
  30. )
  31. (setq lista (reverse lista))
  32. (setq lista (LI_MakeUnique lista))
  33. (mapcar '(lambda (x) (= 0.01 x)) '(offset1 offset2 offset3 offset4 offset5 offset6 offset7 offset8 offset9 offset10 offset11 offset12 offset13 offset14 offset15))
  34. (setq arieobj arieinitiala)
  35. ;;Cazul 1
  36. ;;Aria dorita este mai mica decat aria initiala
  37. ((< ariedor arieinitiala)
  38. (while (< ariedor arieinitiala)
  39. (foreach item lista
  40. (setq xprev (car (vlax-curve-getPointAtParam (vlax-ename->vla-object obj) (1- (vlax-curve-getParamAtPoint (vlax-ename->vla-object obj) item)))))
  41. (setq yprev (cadr (vlax-curve-getPointAtParam (vlax-ename->vla-object obj) (1- (vlax-curve-getParamAtPoint (vlax-ename->vla-object obj) item)))))
  42. (setq xnext (car (vlax-curve-getPointAtParam (vlax-ename->vla-object obj) (1+ (vlax-curve-getParamAtPoint (vlax-ename->vla-object obj) item)))))
  43. (setq ynext (cadr (vlax-curve-getPointAtParam (vlax-ename->vla-object obj) (1+ (vlax-curve-getParamAtPoint (vlax-ename->vla-object obj) item)))))
  44. ((and (or (<= xprev (car item))  (>= xprev (car item))) (>= xnext (car item)) (>= ynext (cadr item)) (<= yprev (cadr item)))
  45. (command "stretch" "c" item item "" item (list (+ (car item) offset1) (cadr item)))
  46. (setq offset1 (+ 0.01 offset1))
  47. )
  48. ((and (or (<= xprev (car item)) (>= xprev (car item))) (>= xnext (car item)) (>= ynext (cadr item))  (>= yprev (cadr item)))
  49. (command "stretch" "c" item item "" item (list (car item) (+ (cadr item) offset2)))
  50. (setq offset2 (+ 0.01 offset2))
  51. )
  52. ((and (>= xnext (car item)) (<= ynext (cadr item)) (<= xprev (car item)) (>= yprev (cadr item)))
  53. (command "stretch" "c" item item "" item (list (- (car item) offset3) (cadr item)))
  54. (setq offset3 (+ offset 0.01))
  55. )
  56. ((and (>= xnext (car item)) (<= ynext (cadr item)) (<= xprev (car item)) (<= yprev (cadr item)))
  57. (command "stretch" "c" item item "" item (list (car item) (- (cadr item) offset4)))
  58. (setq offset4 (+ offset4 0.01))
  59. )
  60. ((and (>= xnext (car item)) (<= ynext (cadr item)) (>= xprev (car item)) (<= yprev (cadr item)))
  61. (command "stretch" "c" item item "" item (list (+ (car item) offset5) (- (cadr item) offset5)))
  62. (setq offset5 (+ offset5 0.01))
  63. )
  64. ((and (>= xnext (car item)) (<= ynext (cadr item)) (>= xprev (car item)) (>= yprev (cadr item)))
  65. (command "stretch" "c" item item "" item (list (+ (car item) offset6) (cadr item)))
  66. (setq offset6 (+ offset7 0.01))
  67. )
  68. ((and (<= xnext (car item)) (>= ynext (cadr item)) (<= xprev (car item)) (<= yprev (cadr item)))
  69. (command "stretch" "c" item item "" item (list (- (car item) offset8) (cadr item)))
  70. (setq offset7 (+ offset7 0.01))
  71. )
  72. ((and (<= xnext (car item)) (>= ynext (cadr item)) (<= xprev (car item)) (>= yprev (cadr item)))
  73. (command "stretch" "c" item item "" item (list (- (car item) offset8) (+ (cadr item) offset8)))
  74. (setq offset8 (+ offset8 0.01))
  75. )
  76. ((and (<= xnext (car item)) (>= ynext (cadr item)) (>= xprev (car item)) (<= yprev (cadr item)))
  77. (command "stretch" "c" item item "" item (list (+ (car item) offset9) (+ (cadr item) offset9)))
  78. (setq offset9 (+ offset9 0.01))
  79. )
  80. ((and (<= xnext (car item)) (>= ynext (cadr item)) (>= xprev (car item)) (>= yprev (cadr item)))
  81. (command "stretch" "c" item item "" item (list (car item) (+ (cadr item) offset10)))
  82. (setq offset10 (+ offset10 0.01))
  83. )
  84. ((and (<= xnext (car item)) (<= ynext (cadr item)) (<= xprev (car item)) (<= yprev (cadr item)))
  85. (command "stretch" "c" item item "" item (list (- (car item) offset11) (- (cadr item) offset11)))
  86. (setq offset11 (+ offset11 0.01))
  87. )
  88. ((and (<= xnext (car item)) (<= ynext (cadr item)) (<= xprev (car item)) (>= yprev (cadr item)))
  89. (command "stretch" "c" item item "" item (list (+ (car item) offset12) (cadr item)))
  90. (setq offset12 (+ offset12 0.01))
  91. )
  92. ((and (<= xnext (car item)) (<= ynext (cadr item)) (>= xprev (car item)) (>= yprev (cadr item)))
  93. (command "stretch" "c" item item "" item (list (+ (car item) offset13) (- (cadr item) offset13)))
  94. (setq offset13 (+ offset13 0.01))
  95. )
  96. ((and (<= xnext (car item)) (<= ynext (cadr item)) (>= xprev (car item)) (<= yprev (cadr item)))
  97. (command "stretch" "c" item item "" item (list (car item) (- (cadr item) offset14)))
  98. (setq offset14 (+ offset14 0.01))
  99. )
  100. ((and (<= xnext (car item)) (<= ynext (cadr item)) (<= xprev (car item)) (<= yprev (cadr item)))
  101. (command "stretch" "c" item item "" item (list (- (car item) offset15) (- (cadr item) offset15)))
  102. (setq offset15 (+ offset15 0.01))
  103. )
  104. (T nil)
  105. )
  106. )
  107. )
  108. )
  109. ((> ariedor arieinitiala)
  110. (while (> ariedor arieinitiala)
  111. (foreach item lista
  112. ;;verificam daca e si pct de start selectat
  113. (setq xnext (car (vlax-curve-getPointAtParam (vlax-ename->vla-object obj) (1+ (vlax-curve-getParamAtPoint (vlax-ename->vla-object obj) item)))))
  114. (setq ynext (cadr (vlax-curve-getPointAtParam (vlax-ename->vla-object obj) (1+ (vlax-curve-getParamAtPoint (vlax-ename->vla-object obj) item)))))
  115. (setq xprev (car (vlax-curve-getPointAtParam (vlax-ename->vla-object obj) (1- (vlax-curve-getParamAtPoint (vlax-ename->vla-object obj) item)))))
  116. (setq yprev (cadr (vlax-curve-getPointAtParam (vlax-ename->vla-object obj) (1- (vlax-curve-getParamAtPoint (vlax-ename->vla-object obj) item)))))
  117. ((and (>= xnext (car item)) (>= ynext (cadr item)) (or (<= xprev (car item))  (>= xprev (car item))) (<= yprev (cadr item)))
  118. (command "stretch" "c" item item "" item (list (- (car item) offset1) (cadr item)))
  119. (setq offset1 (+ 0.01 offset1))
  120. )
  121. ((and (>= xnext (car item)) (>= ynext (cadr item)) (or (<= xprev (car item)) (>= xprev (car item))) (>= yprev (cadr item)))
  122. (command "stretch" "c" item item "" item (list (car item) (- (cadr item) offset2)))
  123. (setq offset2 (+ 0.01 offset2))
  124. )
  125. ((and (>= xnext (car item)) (<= ynext (cadr item)) (<= xprev (car item)) (>= yprev (cadr item)))
  126. (command "stretch" "c" item item "" item (list (+ (car item) offset3) (cadr item)))
  127. (setq offset3 (+ offset 0.01))
  128. )
  129. ((and (>= xnext (car item)) (<= ynext (cadr item)) (<= xprev (car item)) (<= yprev (cadr item)))
  130. (command "stretch" "c" item item "" item (list (car item) (+ (cadr item) offset4)))
  131. (setq offset4 (+ offset4 0.01))
  132. )
  133. ((and (>= xnext (car item)) (<= ynext (cadr item)) (>= xprev (car item)) (<= yprev (cadr item)))
  134. (command "stretch" "c" item item "" item (list (- (car item) offset5) (+ (cadr item) offset5)))
  135. (setq offset5 (+ offset5 0.01))
  136. )
  137. ((and (>= xnext (car item)) (<= ynext (cadr item)) (>= xprev (car item)) (>= yprev (cadr item)))
  138. (command "stretch" "c" item item "" item (list (- (car item) offset6) (cadr item)))
  139. (setq offset6 (+ offset7 0.01))
  140. )
  141. ((and (<= xnext (car item)) (>= ynext (cadr item)) (<= xprev (car item)) (<= yprev (cadr item)))
  142. (command "stretch" "c" item item "" item (list (+ (car item) offset8) (cadr item)))
  143. (setq offset7 (+ offset7 0.01))
  144. )
  145. ((and (<= xnext (car item)) (>= ynext (cadr item)) (<= xprev (car item)) (>= yprev (cadr item)))
  146. (command "stretch" "c" item item "" item (list (+ (car item) offset8) (+ (cadr item) offset8)))
  147. (setq offset8 (+ offset8 0.01))
  148. )
  149. ((and (<= xnext (car item)) (>= ynext (cadr item)) (>= xprev (car item)) (<= yprev (cadr item)))
  150. (command "stretch" "c" item item "" item (list (- (car item) 0.01) (- (cadr item) 0.01)))
  151. (alert "Asta e mo!")
  152. (setq offset9 (+ offset9 0.01))
  153. )
  154. ((and (<= xnext (car item)) (>= ynext (cadr item)) (>= xprev (car item)) (>= yprev (cadr item)))
  155. (command "stretch" "c" item item "" item (list (car item) (- (cadr item) offset10)))
  156. (setq offset10 (+ offset10 0.01))
  157. )
  158. ((and (<= xnext (car item)) (<= ynext (cadr item)) (<= xprev (car item)) (<= yprev (cadr item)))
  159. (command "stretch" "c" item item "" item (list (+ (car item) offset11) (+ (cadr item) offset11)))
  160. (setq offset11 (+ offset11 0.01))
  161. )
  162. ((and (<= xnext (car item)) (<= ynext (cadr item)) (<= xprev (car item)) (>= yprev (cadr item)))
  163. (command "stretch" "c" item item "" item (list (- (car item) offset12) (cadr item)))
  164. (setq offset12 (+ offset12 0.01))
  165. )
  166. ((and (<= xnext (car item)) (<= ynext (cadr item)) (>= xprev (car item)) (>= yprev (cadr item)))
  167. (command "stretch" "c" item item "" item (list (- (car item) offset13) (+ (cadr item) offset13)))
  168. (setq offset13 (+ offset13 0.01))
  169. )
  170. ((and (<= xnext (car item)) (<= ynext (cadr item)) (>= xprev (car item)) (<= yprev (cadr item)))
  171. (command "stretch" "c" item item "" item (list (car item) (+ (cadr item) offset14)))
  172. (setq offset14 (+ offset14 0.01))
  173. )
  174. ((and (<= xnext (car item)) (<= ynext (cadr item)) (<= xprev (car item)) (<= yprev (cadr item)))
  175. (command "stretch" "c" item item "" item (list (+ (car item) offset15) (+ (cadr item) offset15)))
  176. (setq offset15 (+ offset15 0.01))
  177. )
  178. (T nil)
  179. )
  180. )
  181. )
  182. )
  183. (T nil)
  184. )
  185. )
  186. )
  187. )
  188. )
  189. (command "undo" "end")
  190. (setvar "OSMODE" os_old)
  191. )
  192.  
  193. (defun LI_MakeUnique( Lst / Lst1 tmp )
  194.  
  195. (setq Lst1 '())
  196. (foreach tmp Lst
  197.         (if (not (member tmp Lst1))
  198.                 (setq Lst1 (cons tmp Lst1))
  199.         )
  200. )
  201. (reverse Lst1)
  202. )
Title: Re: Adjust the area of a polygon
Post by: ribarm on March 11, 2014, 12:21:36 PM
Polyline can have unlimited number of vertices... How do you determine which vertices do you want to stretch to achieve desired area? Beside that you defined 15 point stretches... I would define (while) loop to pick desired vertices and then define vector by picking 2 points that will determine direction of stretching... But then again routine will have to increment stretching by some small factor until relatively correct area is reached, but it'll take time and area would be not exactly correct...
Title: Re: Adjust the area of a polygon
Post by: CAB on March 11, 2014, 12:46:46 PM
My first look at the problem and looks like two adjacent vertices need to move & four angles would change to adjust the area. (Keeping all side the same length)
My guess is that you can move the two vertices in one of two general directions and likely get the desired area.

So you will need to pick two vertices to move & the direction to move them OR use the shortest distance to determine the direction.
Title: Re: Adjust the area of a polygon
Post by: CAB on March 11, 2014, 01:06:13 PM
Looking at another polygon it looks doable.
Pick points 1 and 2 & the segment 1-2 can be moved maintaining adjacent segment lengths.
haven't looked for an algorithm to accomplish this.
Title: Re: Adjust the area of a polygon
Post by: vladgothe on March 11, 2014, 01:14:23 PM
ribarm, the routine I posted does exactly what you said, but it's not working and until now, I don't know from where that error is coming.
Cab, I don't care how the vertices are stretched because for me both solutions are fine if the desired area is achieved.
My code should do the trick, but I get an bad argument type numberp nil error.
Title: Re: Adjust the area of a polygon
Post by: ribarm on March 11, 2014, 01:15:06 PM
Either points 1 or 2 can independently be rotated around centers of circles, so how do you know which one is right and if both are to be rotated how do you know direction of rotation?
Title: Re: Adjust the area of a polygon
Post by: ribarm on March 11, 2014, 01:17:12 PM
vladgothe, where in the code do you specify dirction vector for stretching? - it isn't what I described...
Title: Re: Adjust the area of a polygon
Post by: vladgothe on March 11, 2014, 01:22:49 PM
In the stretch command. I'm sorry, I forgot to translate in english language. In my code, the user selects the polyline, inputs the desired area, then selects the vertices to be stretched. The rules for stretching every vertex are defined in the cond statement.
I am a beginner in lisp, so I may be wrong and that's why I need your help. If you can, I would like you to run my code and see the cause of that error.
Cab, is not necessary to preserve adjacent segments lengths, they can be modified.
Title: Re: Adjust the area of a polygon
Post by: pedroantonio on March 11, 2014, 02:57:56 PM
nice job ribarm

you can add two parametres

1)select polyline
2)Specify desired Length  and Specify desired area
3)Pick two points to avoid the rotation
4)calculate the polygon
Title: Re: Adjust the area of a polygon
Post by: ribarm on March 11, 2014, 04:12:44 PM
vladgothe, here is my version... See if it can help you...

Code: [Select]
(defun c:ms ( / adoc ar d k nar osm p1 p2 pl pla pt ptlst ptt vf )
  (vl-load-com)
  (setq adoc (vla-get-activedocument (vlax-get-acad-object)))
  (setq osm (getvar 'osmode))
  (prompt "\nPick closed POLYLINE")
  (setq pl (ssname (ssget "_+.:E:S:L" '((0 . "*POLYLINE") (-4 . "&=") (70 . 1))) 0))
  (setq ar (vla-get-area (setq pla (vlax-ename->vla-object pl))))
  (prompt "\nSpecify new desired area value - old was : ") (princ (rtos ar 2 2)) (prompt " ; - : ")
  (initget 7)
  (setq nar (getreal))
  (setvar 'osmode 1)
  (setq pt (getpoint "\nPick start vertex on POLYLINE for stretching : "))
  (while pt
    (setq ptlst (cons pt ptlst))
    (setq pt (getpoint "\nPick next vertex on POLYLINE for stretching - ENTER to finish : "))
  )
  (setq p1 (getpoint "\nPick first-start point of stretch vector direction : "))
  (setq p2 (getpoint "\nPick second-end point of stretch vector direction : "))
  (setvar 'osmode 0)
  (vla-startundomark adoc)
  (foreach pt ptlst
    (command "_.stretch" "_C" pt pt "" pt (mapcar '+ pt (mapcar '- p2 p1)))
  )
  (if (and (> nar ar) (> (vla-get-area pla) ar)) (setq vf t))
  (if (and (> nar ar) (< (vla-get-area pla) ar)) (setq vf nil))
  (if (and (< nar ar) (< (vla-get-area pla) ar)) (setq vf t))
  (if (and (< nar ar) (> (vla-get-area pla) ar)) (setq vf nil))
  (if (and (> nar ar) vf)
    (progn
      (setq k 0.0)
      (setq ptt (mapcar '/ (mapcar '- p2 p1) (list (setq d (* (distance p1 p2) 100.0)) d d)))
      (while (> nar (vla-get-area pla))
        (command "_.undo" "_B")
        (foreach pt ptlst
          (command "_.stretch" "_C" pt pt "" pt (mapcar '+ pt (mapcar '* ptt (list (setq k (1+ k)) k k))))
        )
        (vla-update pla)
      )
    )
  )
  (if (and (> nar ar) (not vf))
    (progn
      (setq k 0.0)
      (setq ptt (mapcar '/ (mapcar '- p1 p2) (list (setq d (* (distance p1 p2) 100.0)) d d)))
      (while (> nar (vla-get-area pla))
        (command "_.undo" "_B")
        (foreach pt ptlst
          (command "_.stretch" "_C" pt pt "" pt (mapcar '+ pt (mapcar '* ptt (list (setq k (1+ k)) k k))))
        )
        (vla-update pla)
      )
    )
  )
  (if (and (< nar ar) vf)
    (progn
      (setq k 0.0)
      (setq ptt (mapcar '/ (mapcar '- p2 p1) (list (setq d (* (distance p1 p2) 100.0)) d d)))
      (while (< nar (vla-get-area pla))
        (command "_.undo" "_B")
        (foreach pt ptlst
          (command "_.stretch" "_C" pt pt "" pt (mapcar '+ pt (mapcar '* ptt (list (setq k (1+ k)) k k))))
        )
        (vla-update pla)
      )
    )
  )
  (if (and (< nar ar) (not vf))
    (progn
      (setq k 0.0)
      (setq ptt (mapcar '/ (mapcar '- p1 p2) (list (setq d (* (distance p1 p2) 100.0)) d d)))
      (while (< nar (vla-get-area pla))
        (command "_.undo" "_B")
        (foreach pt ptlst
          (command "_.stretch" "_C" pt pt "" pt (mapcar '+ pt (mapcar '* ptt (list (setq k (1+ k)) k k))))
        )
        (vla-update pla)
      )
    )
  )
  (vla-endundomark adoc)
  (setvar 'osmode osm)
  (princ)
)
Title: Re: Adjust the area of a polygon
Post by: vladgothe on March 11, 2014, 05:23:15 PM
Thank you ribarm, I will test the code tomorrow. I think I found the errors in my code and I will update it too. I usually don't write the codes so bad, but I wrote it when I was at work and I had very little time at my disposal.
Title: Re: Adjust the area of a polygon
Post by: pedroantonio on March 11, 2014, 06:00:44 PM
I try it but is not working well ,  i think that scf.lsp is better 

ribarm if you can do this changes

1)select polyline
2)Specify desired Length  and Specify desired area
3)Pick two points to avoid the rotation
4)calculate the polygon

it will be perfect
Title: Re: Adjust the area of a polygon
Post by: snownut2 on March 11, 2014, 06:21:16 PM
Pedro,

Why don't you attempt to make some adjustments on your own, you seem to be quite capable of spotting issues with the code.  It will show the other members here that you are at least willing to try.  Who knows maybe you will learn something in the process or at least gain a greater respect for those willing to share their knowledge.

Bruce
Title: Re: Adjust the area of a polygon
Post by: ribarm on March 12, 2014, 05:24:43 PM
For improved accuracy... Test this code :

M.R.

[EDIT : Found some mistake in amount of stretching when multiple points are selected - Tested on square and picked 2 side points to make rectangle - now distances of stretched sides of rectangle are the same - that wasn't the case, that's why I said to test the code... It was total of 2 downloads till I reattached file...]

Hope this is now all OK...
Title: Re: Adjust the area of a polygon
Post by: ribarm on March 13, 2014, 06:17:45 AM
Reattached ms.lsp - there was little mistake when multiple points are to be stretched...

M.R.
Title: Re: Adjust the area of a polygon
Post by: pedroantonio on March 13, 2014, 06:26:36 AM
when i run this lisp my autocad dont respond . why ? Can you give some instractions?
Title: Re: Adjust the area of a polygon
Post by: ribarm on March 13, 2014, 07:35:05 AM
Topographer, there was one more mistake I made while copying pieces of codes... Now is all OK; Redownload file attached now... And for instructions just follow prompts CAD asks you and be sure to pick initial vector that would be smaller for imaginary stretch than desired area (pick small vector in any case - 2 near points)...

M.R.
Title: Re: Adjust the area of a polygon
Post by: pedroantonio on March 13, 2014, 07:47:20 AM
I try again and my autocad crash again . I dont know
Title: Re: Adjust the area of a polygon
Post by: vladgothe on March 13, 2014, 10:06:23 AM
Marko, AutoCAD 2007 gives an error message: INTERNAL ERROR CMD UNDO 2 every time I run the lisp, then crashes. What could the problem be?
I started working on a new routine, but unfortunately I have very much work to do and I don't have time to finish it now. Maybe someone can help?
Here is the start of the routine:
Code - Auto/Visual Lisp: [Select]
  1. (defun c:ms (/ obj arieinitiala arieobj ariedor lista lista2)
  2. (setvar "CMDECHO" 0)
  3. (setq os_old (getvar "OSMODE"))
  4. (setvar "OSMODE" 1)
  5. (setq lista '())
  6. (setq lista2 '())
  7. (setq obj (car (entsel "\nSelect a closed polyline: ")))
  8. (while (not obj)
  9. (setq obj (car (entsel "\nSelect a closed polyline: ")))
  10. )
  11. (while (not (wcmatch (cdr (assoc 0 (entget obj))) "*POLYLINE"))
  12. (alert "The selected object is not a polyline!")
  13. (setq obj (car (entsel "\nSelect a closed polyline: ")))
  14. (while (not obj)
  15. (setq obj (car (entsel "\nSelect a closed polyline: ")))
  16. )
  17. )
  18. (setq pl (vlax-ename->vla-object obj))
  19. (if (zerop (cdr (assoc 70 (entget obj))))
  20. (vla-put-closed pl :vlax-true)
  21. )
  22. ;;we make the list with polyline vertices
  23. (foreach x (setq e (entget obj))
  24.         (if (= 10 (car x)) (setq l (cons (cdr x) l)))
  25.     )  
  26. (setq arieinitiala (vla-get-Area pl))
  27. (setq ariedor (getreal (strcat "\nWrite the area you want to achieve <" (rtos arieinitiala 2 0) " sq meters> ")))
  28. ;;daca aria dorita este egala cu aria initiala
  29. (if (= (rtos ariedor 2 0) (rtos arieinitiala 2 0))
  30. (alert "\nThe selected area is equal with polyline area!")
  31. (setq pt (getpoint "\nSelect the vertex you want to be stretched: "))
  32. (setq xpt (car pt))
  33. (setq ypt (cadr pt))
  34. (if (not (member (list xpt ypt) l))
  35. (alert "\nThe selected point must be a vertex on polyline!")
  36. (setq lista (append (list (list xpt ypt)) lista))
  37. (setq dir (getpoint "\nChoose the direction you want to stretch the vertex: "))
  38. (setq xdir (car dir))
  39. (setq ydir (cadr dir))
  40. (setq lista2 (append (list (list xdir ydir)) lista2))
  41. )
  42. )
  43. )
  44. (setq lista (reverse lista))
  45. (setq lista2 (reverse lista2))
  46. (setq lista (LI_MakeUnique lista))
  47. (setq lista2 (LI_MakeUnique lista2))
  48. (setq arieobj arieinitiala)
  49. (setq cont 0)
  50. (setq offset 0)
  51. ;;here goes the code to verify area and stretch every vertex on selected directions
  52. ;;.................
  53. ;;..................
  54. )
  55. )
  56. (setvar "OSMODE" os_old)
  57. )
  58.  
  59. (defun LI_MakeUnique( Lst / Lst1 tmp )
  60.  
  61. (setq Lst1 '())
  62. (foreach tmp Lst
  63.         (if (not (member tmp Lst1))
  64.                 (setq Lst1 (cons tmp Lst1))
  65.         )
  66. )
  67. (reverse Lst1)
  68. )
  69.  
  70.  
Title: Re: Adjust the area of a polygon
Post by: ribarm on March 13, 2014, 10:31:18 AM
vladgothe and Topographer,

It seems that you don't have luck with version of ACAD... I've tested on my machines and the oldest version I am using is A2008 on 32 bit system, and it works fine... Guess, you both should update your hardware and software... The newest version of ACAD can be found on www and it's A2014... Just remember that during installation avoid to download service pack, for you won't be able to activate it correctly...

M.R.
Title: Re: Adjust the area of a polygon
Post by: pedroantonio on March 13, 2014, 10:41:03 AM
i use Autocad 2012  64 bit and this lisp crash ... it starts  but  after few seconds autocad not respond
Title: Re: Adjust the area of a polygon
Post by: ribarm on March 13, 2014, 11:07:01 AM
You are driving me nuts... I've tested the code once again on A2014 and A2012 64 bit and it's working fine... Can someone else confirm that I am not laying... It's impossible that only my ACAD's work fine... Topographer, as I said - pick closed polyline; enter desired area; pick vertices on polyline - be careful to pick only vertices (osnap will be automatically turned on to END snap) and when done right click with mouse or ENTER; pick small vector in direction you want stretch to be processed - 2 near points (must be small vector)... And that's it - you have to be patient to be sure routine finish... No crashes on my computers and no endless loops - it finishes correctly...
Title: Re: Adjust the area of a polygon
Post by: pedroantonio on March 13, 2014, 11:16:00 AM
ribarm can you upload a gif to see the steps. Maybe i do something wrong.

Thanks
Title: Re: Adjust the area of a polygon
Post by: ribarm on March 13, 2014, 11:30:11 AM
Here is the MS.gif
Title: Re: Adjust the area of a polygon
Post by: pedroantonio on March 13, 2014, 12:08:59 PM
Now some some times work. Perhaps it's my fault .

thank you ribarm
Title: Re: Adjust the area of a polygon
Post by: vladgothe on March 14, 2014, 02:53:32 AM
In AutoCAD Map 3d 2014, the "UNDO" problem doesn't appear anymore. It seems that in ACAD 2007, some commands cannot be repeated for too many times. Undo and align are two of them. But compared to undo command, align can be used as a function. Some time ago, I had a lisp where I used (command "align" etc...) multiple times and AutoCAD 2007 gave the same error. I resolved the problem using (align etc....) instead of using (command "align" ....). But it's not the case for undo command. It's a software problem, not hardware, because I work on a last generation PC.
Marko, i tested your code. When the desired area is bigger than polyline area, the program works perfectly. The problem appears when the desired area is smaller than polyline area. I set the sysvar CMDECHO to 1 before running the program and I found out that, in this case, after some time, the stretch command finds 0 objects and repeats itself in an endless loop.
Can you confirm this? Maybe I am wrong.
Title: Re: Adjust the area of a polygon
Post by: vladgothe on March 14, 2014, 05:03:55 AM
I figured it out. On big polylines with many vertices or if the difference between areas is very big, the direction vector must be bigger. If you make a short distance vector, it will take forever to accomplish the task and this is why AutoCAD seems to hang. Topographer, set sysvar COORDS to 1 and when you think that AutoCAD hangs look at coordinates and you will see that they are changing. I made a few modifications on routine, but anyway, excellent work Marko! Thank you!
Title: Re: Adjust the area of a polygon
Post by: Fabricio28 on March 14, 2014, 10:03:11 AM
@ribarm
Excellent job!

That code is fantastic!  :-D

Quote
- you have to be patient to be sure routine finish... No crashes on my computers and no endless loops - it finishes correctly...
My computer crashes when I use the code for the area above 4.000mē. But to small area work perfect.

Title: Re: Adjust the area of a polygon
Post by: Kerry on March 14, 2014, 10:07:00 AM
< .. >  when I use the code for the area above 4.000mē. But to small area work perfect.

Is that four or four thousand ?
Title: Re: Adjust the area of a polygon
Post by: Fabricio28 on March 14, 2014, 11:31:00 AM
Area = 4.000
 :-D
Title: Re: Adjust the area of a polygon
Post by: Kerry on March 14, 2014, 11:39:05 AM
very amusing.
Title: Re: Adjust the area of a polygon
Post by: Fabricio28 on March 14, 2014, 12:44:52 PM
Did you try that, Kerry?
Title: Re: Adjust the area of a polygon
Post by: ribarm on March 14, 2014, 04:53:11 PM
FABRICIO28, sorry for inconvenience, try this new version... It works and for small area values and for large, just remember to input small starting vector...

M.R.
Title: Re: Adjust the area of a polygon
Post by: pedroantonio on March 14, 2014, 06:28:53 PM
riban i know that you are trying hard , but this lisp still crashes .Sorry  :embarrassed:
Is not possible to make a lisp

1) giving perimetre and area
2) select only one or two sides to stretch ??
Title: Re: Adjust the area of a polygon
Post by: ribarm on March 15, 2014, 01:20:42 AM
I've tested ms-new.lsp under A2014 x64 with Windows 7... And this lisp passed all cases - small area and large... Maybe when you update your software - hardware you'll find it very useful as now precision is even greater than it was... If you have problems and want to use it with your current computer software - hardware, try removing last pieces of code making lisp smaller and less precise... To make it more easy to see what you should do, I'll post smaller version, but I am not sure weather it'll not crash again with your current computer state...

M.R.
Title: Re: Adjust the area of a polygon
Post by: pedroantonio on March 15, 2014, 03:38:06 AM
Thank you riban i know that you are trying hard , but this lisp still crashes .Sorry  :embarrassed:

I have Autocad 2012 64bit  ,win7 ultimate SP1 with all last updates 64 bit

AMD Athlon (tm) II x4  640 Prossesor 3.00 GHz
RAM 8GB
VGA   PCI  Sapphire 5570 1 GB DDR3 HDMI

I am not laying ...

First I pick two vertex of the side i want to strech.
Second I pick to vertex on the smaller side for  the Direction
Then start working ....working....working and then crash

I try another way

First I pick two vertex of the side i want to strech.
Second I pick to points inside the polygon with very small lendth for the Direction
And then start working ....working....working and then crash again

I dont know what to do ....

The scf.lsp was working fine but it was only a scale . I am searching for a lisp to adjust the area but keeping the perimetric distances.

Imagine that you want to design a specific polygon with specific dimensions and specific  area

Thanks

Title: Re: Adjust the area of a polygon
Post by: pedroantonio on March 15, 2014, 05:14:09 AM
I Think that all the problem is in this two commands

Pick first-start point of stretch vector direction
Pick second-end point of stretch vector direction

I don't how possisble is to change this ,but if you can change it something like this

for direction

1) Pick a point (in the polygon) for smaller areas
2) Pick a point (out the polygon) for bigger areas

Thanks
Title: Re: Adjust the area of a polygon
Post by: ribarm on March 15, 2014, 07:57:10 AM
I think that problem doesn't lie in vector - it always have to be small and it's not important if it's inside or outside of polygon - it can even be on the polygon (I used this to stretch square to rectangle and picked vector on one side of square - just used nearest snap with 2 near points)... The problem is in functionality of your ACADs - on my netbook Windows XP 32bit A2008 and A2009, I had problem with endless looping when used ms-new.lsp with 40K, but when used ms-new.lsp I last posted it's all OK, just little more waiting to finish routine and result isn't so precise... But on my working PC Windows 7 64bit A2012 A2014 8GB RAM the best routine - ms-new.lsp 40K worked fine and really quick and result is almost exact error is on >10 decimal depends on picked vector and type of polygon routine is applied to... I don't know what to say, I am also not laying - you have to believe me, new gif is unneccessary, it'll already show already posted result only when (vl-load-com); (rtos (vla-get-area (vlax-ename->vla-object (car (entsel)))) 2 50) applied on resulting polygon it'll show how precise operation of stretching through routine actually was...
Title: Re: Adjust the area of a polygon
Post by: ribarm on March 15, 2014, 08:57:28 AM
As a matter a fact, here is the proof... Test was made under A2012 on system shown on SYSTEM.jpg...
Title: Re: Adjust the area of a polygon
Post by: pedroantonio on March 15, 2014, 11:56:10 AM
Working only for small polygons. I try to use 90 sqm  and works fine then i try to use 9000 sqm  or  60000 sqm and crash.
Title: Re: Adjust the area of a polygon
Post by: pedroantonio on March 15, 2014, 12:10:59 PM
I don't know if this help , but when the autocad crass write in the command line this

Quote
Must enter UNDO END to go back further
Title: Re: Adjust the area of a polygon
Post by: ribarm on March 15, 2014, 12:40:52 PM
It doesn't crash on my A2012... Make sure stretch is visible on screen...
Title: Re: Adjust the area of a polygon
Post by: snownut2 on March 15, 2014, 01:40:40 PM
pedro,

Not to sound stupid but is your polyline/polygon closed ie; are you ending the last line by typing "C", or are you trying to just click on the endpoint..
Title: Re: Adjust the area of a polygon
Post by: pedroantonio on March 15, 2014, 01:48:39 PM
if it is not close the lisp didnt start any way
Title: Re: Adjust the area of a polygon
Post by: pedroantonio on March 15, 2014, 01:58:41 PM
Here is the crash
Title: Re: Adjust the area of a polygon
Post by: pedroantonio on April 11, 2014, 11:43:24 AM
ribarm your lisp work perfect. My Autocad was the problem.I use autocad 2010 now and your lisp work fine
Title: Re: Adjust the area of a polygon
Post by: MORITZK on April 12, 2014, 05:19:42 AM
Hallo, Ribarm
your code unfortunately not works with bricscad V14. Itīs an endless loop with only the first stretch and without errors.
I think, bricscad not regen the screen before (vla-get-area ...).
Moritz