Area from polyline round up

Area from polyline round up

bcddss
Enthusiast Enthusiast
1,862 Views
17 Replies
Message 1 of 18

Area from polyline round up

bcddss
Enthusiast
Enthusiast

Hi, can you help me with a lisp to calculate area from vertices of a polyline, but the vertices coordinates to be round up at 3 decimală. Thank you

Ahags
0 Likes
Accepted solutions (1)
1,863 Views
17 Replies
Replies (17)
Message 2 of 18

ВeekeeCZ
Consultant
Consultant

Would you give us some drawing that explains why this makes any sense?

0 Likes
Message 3 of 18

fatal.error
Advocate
Advocate

The rtos function returns a string that is the representation of number according to the settings of mode, precision, and the system variables UNITMODE, DIMZIN, LUNITS, and LUPREC.

 

Signature:

(rtos number [mode [precision]])

 

Grab any routine that fit your needs and set rtos preccision to 3 like below:

(defun c:LL ( / a b c)
 (if (setq a (ssget '((0 . "*polyline"))))
   (progn
     (setq b 0)
     (repeat (setq i (sslength a))
(setq b
       (+ (vla-get-area
	       (vlax-ename->vla-object (ssname a (setq i (1- i))))
	     )
	     b
       )
)
     )
     (setq c (strcat "\nPolyline Area: " (rtos b 2 3)))
     (alert c)
     (prompt c)
   )
 )
 (princ)
)

 

 

Further info:

rtos Function

atof Function

 

 

0 Likes
Message 4 of 18

LyleHardin
Advisor
Advisor

The AREA that AutoCAD naturally reports doesn't work for you? 

0 Likes
Message 5 of 18

LyleHardin
Advisor
Advisor

Something like this, maybe?

But it's not calculating the area from the rounded verticie points if that's what you are wanting. It's just taking the AREA value from the selected polyline and rounding that to 3 decmials.

 

;;; pick polyline for area ... IDAREATEXT
(defun c:IDAREATEXT ()
(command "area" "E" PAUSE)
(setq b (getvar "area"))
(setq b (rtos b 2 3)) ;convert area to a decimal number with 5 decimal point precision.
(setq p (getpoint "Pick text midpoint.. "))
(command "text" "m" p "" "0" B)
)
;; defun

 

0 Likes
Message 6 of 18

ВeekeeCZ
Consultant
Consultant

The routine keeps a re-drawn polyline with rounded coordinates in drawing just to visually see from which geometry is calculated. You can remove a semicolon to erase rounded pl from a drawing.

 

(defun c:AreaOddlyRouded ( /  LM:roundup e)
  
  ;; Round Up  -  Lee Mac ;; Rounds 'n' up to the nearest 'm'
  (defun LM:roundup ( n m ) ((lambda ( r ) (cond ((equal 0.0 r 1e-8) n) ((< n 0) (- n r)) ((+ n (- m r))))) (rem n m)))
  
  (and (setq e (car (entsel "Select polyline: ")))
       (setq e (entget e))
       (= "LWPOLYLINE" (cdr (assoc 0 e)))
       (setq e (entmakex (mapcar '(lambda (x) (if (= 10 (car x)) (list 10 (LM:roundup (cadr x) 0.001) (LM:roundup (last x) 0.001)) x)) e)))
       (princ (strcat "Area: " (rtos (getpropertyvalue e "Area"))))
       ;(entdel e)  ; remove the initial semicolon
       )
  (princ)
  )

 

0 Likes
Message 7 of 18

john.uhden
Mentor
Mentor

@ВeekeeCZ 

(defun JU:roundup ()
  (princ "\nYee Hah!\nGit along little dogie!\nYee Hah!")
  (princ)
)

John F. Uhden

0 Likes
Message 8 of 18

bcddss
Enthusiast
Enthusiast

I know how to round up an area. But I want to calculate an area with the polyline vertices only with 3 decimal places, not 6 like autocad. So I find a lisp that move the polyline vertices on the grid "fixoffgrid" then use any lisp who make area label or calculator. I searched heron formula, but I don't find anything simple to only show or label an area of rounded up polyline. Thank you

Ahags
0 Likes
Message 9 of 18

bcddss
Enthusiast
Enthusiast

I will try this function. Thank you

Ahags
0 Likes
Message 10 of 18

bcddss
Enthusiast
Enthusiast

i tested the code but not working. 

Ahags
0 Likes
Message 11 of 18

bcddss
Enthusiast
Enthusiast

i will use fixoffgrid for area calculation. thank you for help

Ahags
0 Likes
Message 12 of 18

ВeekeeCZ
Consultant
Consultant

@bcddss wrote:

i tested the code but not working. 


 

It does. But it's hard to help if someone is so poor in explanation and doesn't know about it.

0 Likes
Message 13 of 18

bcddss
Enthusiast
Enthusiast

I want only to know area from rounded coordinates at 3 decimals of a polyline who have vertices with more decimals, because I need to know differences between this area with 3 decimals and that area with more decimals (autocad area). I have another gis software and that calculates area only with 3 decimal for polyline coordinates. However, I use fixoffgrid lisp to create 3 decimal area in autocad, and I think that is ok, solve my problem. Anyway thanks for trouble!

Ahags
0 Likes
Message 14 of 18

john.uhden
Mentor
Mentor

@bcddss 

Why you would reduce the precision of known locations I can't understand.

You're not going to find any simple formula for computing the area of a polyline with bulged segments (or even without any bulges), but you don't have to because AutoCAD does it for you, or don't you trust their calculations?

John F. Uhden

0 Likes
Message 15 of 18

bcddss
Enthusiast
Enthusiast

I have another software that calculates areas with coordinates (Heron formula or another trick) and there is a difference between that area and autocad area. I want to know directly area value of the rounded area and try to move any of polyline autocad vertices to match this area. I don't know if you understand. Thank you anyway!

Ahags
0 Likes
Message 16 of 18

john.uhden
Mentor
Mentor

@bcddss 

I see that Heron's formula is for a triangle.  Do you seriously intend to programmatically cut up a polygon into triangles, perform Heron's formula on each, and add them up?  I'd put my money on AutoCAD.

And then you want to alter the vertices of a polyline to achieve a specific area?  What's your plan on how to choose which vertices to move in what direction?  Will you impose any constraints?

I remember that Land Desktop (or maybe it was its forerunner Softdesk, or both) had a lot (parcel) sizing routine.  Don't know if it's still there in Civil 3D.

John F. Uhden

0 Likes
Message 17 of 18

hak_vz
Advisor
Advisor
Accepted solution

@bcddss  Try this:

Works with straight segment polylines. Area calculated using cross method.

If you need rounded coordinates uncomment i.e. remove leftmost  ;  from lines at the bottom.

;(princ "\nRounded coordinates > ") ;uncomment to see poly coordinates
;(princ lst) ;uncomment to see poly coordinates

 

(defun c:a3dec ( / pick_poly take take2 pointlist2d eo lst i suma sumb)
	(vl-load-com)
	;Author:  hak_vz 
	;Saturday, March 5, 2022
	;https://forums.autodesk.com/t5/user/viewprofilepage/user-id/5530556
	;Posted at 
	;https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/area-from-polyline-round-up/td-p/10984310
	;Calculates area of closed polyline with coordinates rounded to 3 decimal places
	(defun pick_poly (msg)
		(setq e (car(entsel msg)))
		(if (and (not e) (= (getvar 'Errno) 7)) (pick_poly msg) e)
	)
	(defun take (amount lst / ret)(repeat amount (setq ret (cons (car lst) (take (1- amount) (cdr lst))))))
	(defun take2 (lst) (take 2 lst))
	(defun pointlist2d (lst / ret) (while lst (setq	ret (cons (take 2 lst) ret) lst (cddr lst))) (reverse ret))
	(setq eo (vlax-ename->vla-object (pick_poly "\nSelect polyline >")))
    (cond 
		((= (vla-get-Objectname eo) "AcDbPolyline")
			(setq lst (pointlist2d (mapcar '(lambda (x) (atof(rtos x 2 3))) (vlax-get eo 'Coordinates))))
			(setq lst (append lst (list (car lst))))
			(setq i -1)
			(setq lst (append lst (list (car lst))))
				(setq i -1 suma 0 sumb 0)
				(while (< (setq i (1+ i)) (-(length lst) 1))
					(setq suma (+ suma (* (car (nth i lst))(cadr (nth (+ i 1) lst)))))
					(setq sumb (+ sumb (* (cadr (nth i lst))(car (nth (+ i 1) lst)))))
				)
			;(princ "\nRounded coordinates > ") ;uncomment to see poly coordinates
			;(princ lst) ;uncomment to see poly coordinates
			(princ (strcat "\nArea = " (rtos (abs(* 0.5 (- suma sumb))) 2 2)))
		)
	)
   (princ)
)

 

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.
Message 18 of 18

bcddss
Enthusiast
Enthusiast

Thank you!

Ahags