Announcements
Attention for Customers without Multi-Factor Authentication or Single Sign-On - OTP Verification rolls out April 2025. Read all about it here.

Calculate area in Meter Square

Anonymous

Calculate area in Meter Square

Anonymous
Not applicable
This lisp is used to Calculate the area of an object in square feet.

1. Please Edit it to calculate in Square meter.
2. Please Edit it to calculate for multiple objects.


;;; AreaText.LSP ver 3.0 ;;; Command name is AT ;;; Select a polyline and where to place the text ;;; Sample result: 2888.89 SQ. FT. ;;; As this is a FIELD it is updated based on the FIELDEVAL ;;; or the settings found in the OPTIONS dialog box ;;; By Jimmy Bergmark ;;; Copyright (C) 2007-2010 JTB World, All Rights Reserved ;;; Website: www.jtbworld.com ;;; E-mail: info@jtbworld.com ;;; 2007-09-05 - First release ;;; 2009-08-02 - Updated to work in both modelspace and paperspace ;;; 2010-10-29 - Updated to work also on 64-bit AutoCAD ;;; Uses TEXTSIZE for the text height (defun Get-ObjectIDx64 (obj / util) (setq util (vla-get-Utility (vla-get-activedocument (vlax-get-acad-object)))) (if (= (type obj) 'ENAME)(setq obj (vlax-ename->vla-object obj))) (if (= (type obj) 'VLA-OBJECT) (if (> (vl-string-search "x64" (getvar "platform")) 0) (vlax-invoke-method util "GetObjectIdString" obj :vlax-False) (rtos (vla-get-objectid obj) 2 0) ) ) ) (defun c:AT (/ entObject entObjectID InsertionPoint ad) (vl-load-com) (setq entObject (vlax-ename->vla-object(car (entsel))) entObjectID (Get-ObjectIDx64 entObject) InsertionPoint (vlax-3D-Point (getpoint "Select point: ")) ad (vla-get-ActiveDocument (vlax-get-acad-object)) ) (vla-addMText (if (= 1 (vla-get-activespace ad)) (vla-get-modelspace ad) (if (= (vla-get-mspace ad) :vlax-true) (vla-get-modelspace ad) (vla-get-paperspace ad) ) ) InsertionPoint 0.0 (strcat "%<\\AcObjProp Object(%<\\_ObjId " entObjectID ">%).Area \\f \"%pr2%lu2%ct4%qf1 SQ. FT.\">%" )) )

 

 

 

 

 

 

 

Reply
Accepted solutions (1)
7,048 Views
13 Replies
Replies (13)

ВeekeeCZ
Consultant
Consultant

You can try this one... from my archive..

 

Spoiler
(defun c:PLArea  (/ acsp ss e ptList ID StrField txt p)
  (vl-load-com)
  (command "_.undo" "_be")
  (setq  acsp (vla-get-modelspace (vla-get-activedocument (vlax-get-acad-object))))
  (if (setq ss (ssget ;(progn
			;(initget 1 "Y N")
                        ;(setq ans (getkword "\nProcess All Polylines [Yes/No]: "))
			;(if (eq "Y" ans)
			  ;"_X"
			  ":L";))
		      '((0 . "*POLYLINE")			
                        ;(8 . "A-AREA-BDRY")
                        (-4 . "&")
                        (70 . 1)(410 . "Model"))))
    (repeat (sslength ss)
            (setq e     (ssname ss 0)
                  sum   '(0 0)
                  verts (cdr (assoc 90 (entget e))))
            (setq ptList
                       (mapcar 'cdr
                               (vl-remove-if-not
                                     '(lambda (x) (= (car x) 10))
                                     (entget e))))
            (foreach x ptList (setq sum (mapcar '+ x sum)))
            (setq ID (itoa (vla-get-objectid (vlax-ename->vla-object e))))
            (setq StrField
                       (strcat "%<\\AcObjProp Object(%<\\_ObjId "
			       ID
			       ">%).Area \\f \"%lu2\">%"))
            (vla-put-AttachmentPoint
                  (setq txt (vla-addMText
                                  acsp
                                  (setq p (vlax-3d-point
                                                   (mapcar '/ sum
                                                         (list verts
                                                               verts))))
                                  0  StrField))
                  acAttachmentPointMiddleCenter)
            (vla-put-InsertionPoint txt p)
            (ssdel e ss)
            )(princ "\0 Objects found:"))
  (command "_.undo" "_e")
  (princ)
)

Just be careful if you have some more complex shapes (typically "C" shape) -- label may be outside the closed area.

0 Likes

Anonymous
Not applicable

Yes,

This is what i wanted exactly, Thanks

Can you add Unit "m2" For me in this lisp?

0 Likes

ВeekeeCZ
Consultant
Consultant

@Anonymous wrote:

Yes,

This is what i wanted exactly, Thanks

Can you add Unit "m2" For me in this lisp?


ok...

 

Spoiler
(defun c:PLArea  (/ acsp ss e ptList ID StrField txt p)
  (vl-load-com)
  (command "_.undo" "_be")
  (setq  acsp (vla-get-modelspace (vla-get-activedocument (vlax-get-acad-object))))
  (if (setq ss (ssget ;(progn
			;(initget 1 "Y N")
                        ;(setq ans (getkword "\nProcess All Polylines [Yes/No]: "))
			;(if (eq "Y" ans)
			  ;"_X"
			  ":L";))
		      '((0 . "*POLYLINE")			
                        ;(8 . "A-AREA-BDRY")
                        (-4 . "&")
                        (70 . 1)(410 . "Model"))))
    (repeat (sslength ss)
            (setq e     (ssname ss 0)
                  sum   '(0 0)
                  verts (cdr (assoc 90 (entget e))))
            (setq ptList
                       (mapcar 'cdr
                               (vl-remove-if-not
                                     '(lambda (x) (= (car x) 10))
                                     (entget e))))
            (foreach x ptList (setq sum (mapcar '+ x sum)))
            (setq ID (itoa (vla-get-objectid (vlax-ename->vla-object e))))
            (setq StrField
                       (strcat "%<\\AcObjProp Object(%<\\_ObjId "
			       ID
			       ">%).Area \\f \"%lu2\">%"
                               "m2"))
            (vla-put-AttachmentPoint
                  (setq txt (vla-addMText
                                  acsp
                                  (setq p (vlax-3d-point
                                                   (mapcar '/ sum
                                                         (list verts
                                                               verts))))
                                  0  StrField))
                  acAttachmentPointMiddleCenter)
            (vla-put-InsertionPoint txt p)
            (ssdel e ss)
            )(princ "\0 Objects found:"))
  (command "_.undo" "_e")
  (princ)
)

 

0 Likes

Anonymous
Not applicable

It is not working on circles.

0 Likes

Kent1Cooper
Consultant
Consultant

@Anonymous wrote:

It is not working on circles.


It's not written to do that.  Curiously, your original code describes itself as only for Polylines, but in fact works on other entity types, too [even Lines! -- it puts 0.00 in for the area].

 

Here is one that reports in square meters.  But it also says it's just for Polylines.  Maybe you can easily take the part of it that determines the text content and transfer that into your code in Post 1 [and change the description in that, to not be in terms of only Polylines].

Kent Cooper, AIA
0 Likes

ВeekeeCZ
Consultant
Consultant

@Anonymous wrote:

It is not working on circles.


Spoiler
(vl-load-com)

(defun c:PLArea  (/ adoc acsp ss e ptList ID StrField txt p)
  
  (setq  acsp (vla-get-modelspace (setq adoc (vla-get-activedocument (vlax-get-acad-object)))))
  (vla-startundomark adoc)
  (if (setq ss (ssget ;(progn
			;(initget 1 "Y N")
                        ;(setq ans (getkword "\nProcess All Polylines [Yes/No]: "))
			;(if (eq "Y" ans)
			  ;"_X"
			  ":L";))
		      '((-4 . "<OR")
                        	(0 . "CIRCLE")
                        	(-4 . "<AND")
                        		(0 . "*POLYLINE")
                        		(-4 . "&")
                        		(70 . 1)
                        	(-4 . "AND>")
                        (-4 . "OR>")	
                        )))
    (repeat (sslength ss)
            (setq e     (ssname ss 0)
                  sum   '(0 0)
                  verts (cond ((cdr (assoc 90 (entget e))))
                              (1)))
            (setq ptList (mapcar 'cdr
                                 (vl-remove-if-not
                                   '(lambda (x)
                                      (= (car x) 10))
                                   (entget e))))
            (foreach x ptList (setq sum (mapcar '+ x sum)))
            (setq ID (itoa (vla-get-objectid (vlax-ename->vla-object e))))
            (setq StrField (strcat "%<\\AcObjProp Object(%<\\_ObjId "
                                   ID
                                   ">%).Area \\f \"%lu2\">%"
                                   "m2"))
            (vla-put-AttachmentPoint
                  (setq txt (vla-addMText
                                  acsp
                                  (setq p (vlax-3d-point
                                                   (mapcar '/ sum
                                                         (list verts
                                                               verts))))
                                  0  StrField))
                  acAttachmentPointMiddleCenter)
            (vla-put-InsertionPoint txt p)
            (ssdel e ss)
            )
    (princ "\n0 Objects found:"))
  (vla-endundomark adoc)
  (princ)
)
0 Likes

gpcattaneo
Advocate
Advocate
Accepted solution

@ВeekeeCZ wrote:

 Just be careful if you have some more complex shapes (typically "C" shape) -- label may be outside the closed area.


Try this...

 

 576A.gif

ВeekeeCZ
Consultant
Consultant

@gpcattaneo, I must admit I'm impressed!! Nice solution!

 

 

-- haven't dig into algorithm much -- do you somehow test if a text is really inside?

 

I tried a little test... not really hard, these are the only shapes I've drawn... 

 

http://autode.sk/2dXLZB5

0 Likes

F.Camargo
Advisor
Advisor

@gpcattaneo wrote:

@ВeekeeCZ wrote:

 Just be careful if you have some more complex shapes (typically "C" shape) -- label may be outside the closed area.


Try this...

 

 576A.gif


Fantastic, Gian!!

 

It will be better if the texts were as a field. Smiley Happy

 

Fabricio

 

0 Likes

gpcattaneo
Advocate
Advocate

Thanks Smiley Happy

 

@ВeekeeCZ

Increase the value of "step1" on very narrow polygons.

                    

@F.Camargo

The field version...

F.Camargo
Advisor
Advisor
Just amazing Gian!!
Thank you
0 Likes

Anonymous
Not applicable

 I tried this lisp it is very useful to me 

but..why the result values are too big

how can  I reduce the size of this lisp text

0 Likes

gpcattaneo
Advocate
Advocate

Change the TEXTSIZE system variable.

0 Likes