Jump to content

Recommended Posts

Posted

Hi everybody,

 

I have list representing the vertices of a closed polygon. How can I find the area of the polygon with an autolisp expression?.

 

Thanks in advance,

 

Aloy

Posted

Hi,

 

I am sure that I have seen this question on the swamp.org not from a quite long time but unfortunately I couldn't find it.

 

Anyway you can create a polyline of these vertices then get the area of the newly created polyline then delete the polyline.

Posted

Thanks Tharwat,

I did this more than twenty years ago while developing program for road cross section. I have forgotten that. There should be an easier way without having to do it manually. In fact the list is also prepared automatically.

 

Regards,

 

Aloy

Posted

I did not mean to do it manually but to have a function that draws the polyline then get the area then delete the polyline.

Posted (edited)

Try the following and be sure to have the current layer Unlocked to allow the function to delete the newly created polyline by the function.

 

(defun AreaOfVertices (l / a e)
 (and (setq e (entmakex
                (append (list '(0 . "LWPOLYLINE")
                              '(100 . "AcDbEntity")
                              '(100 . "AcDbPolyline")
                              (cons 90 (length l))
                              '(70 . 0)
                        )
                        (mapcar (function (lambda (p) (cons 10 p))) l)
                )
              )
      )
      (setq a (vlax-curve-getarea e))
      (entdel e)
 )
 a
)

 

Usage of the above function:

 

(AreaOfVertices lst)

Edited by Tharwat
typo of 'l' variable
Posted

Try the following:

 

(defun areaG (lst)
   (abs
       (/
           (apply '+
               (mapcar
                   '(lambda ( a b )
                        (- (* (car a) (cadr b)) (* (car b) (cadr a)))
                    )
                   lst
                   (cons (last lst) lst)
               )
           )
       2.0
       )
   )
) 

 

 

(setq Lv '((0.0 5.0 0.0) (3.0 5.0 0.0) (3.0 0.0 0.0) (0.0 0.0 0.0)))

((0.0 5.0 0.0) (3.0 5.0 0.0) (3.0 0.0 0.0) (0.0 0.0 0.0))

_$

 

(areaG Lv)

15.0

_$

Posted

Tharwat, GP_

 

Thanks for the reply. However what I am looking for is a way to respond to " area" command of autocad by giving points out of my list.

 

Aloy

Posted

(setq Lv '((0.0 5.0 0.0) (3.0 5.0 0.0) (3.0 0.0 0.0) (0.0 0.0 0.0)))

 

 

(command "area")

(apply 'command Lv)

(command "")

 

 

(getvar 'area)

15.0

_$

Posted

I don't understand why the OP would want to do it this way. But whenever you use (command ..) you should be aware that the OSMODE setting can affect the result.

Posted

Roy_043,

 

This means we have to set the "osmode" to "0" at the beginning and set it back to original at the end?.

 

Thanks for the advice.

 

Aloy

Posted
This means we have to set the "osmode" to "0" at the beginning and set it back to original at the end?
Yes, that is the usual approach.

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.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...