Script help. How to update a get area script to round up the area to the neares

Script help. How to update a get area script to round up the area to the neares

Anonymous
Not applicable
2,099 Views
7 Replies
Message 1 of 8

Script help. How to update a get area script to round up the area to the neares

Anonymous
Not applicable

I have a script that finds an area of a polyline and creates a field that list the area and places the field in the polyline.  My only problem is I would like it to round the field up to the nearest 10 (example 323 would become 330).     See code below

 

;; local defun
;; get center of closed object
(defun getcenter (obj / acsp cen rgn)
(setq acsp (vla-get-modelspace
	     (vla-get-activedocument
	       (vlax-get-acad-object)))
        rgn (car (vlax-invoke acsp 'Addregion (list obj)))
      cen (vlax-get rgn 'Centroid)
)
(vla-delete rgn)
cen
)

;; main part
;; label [plines w]/area field in sq. meters
(defun c:GAREA (/ acsp adoc axss cpt ins ss txt mtxtobj)
  (vl-load-com)  
  (or adoc
      (setq adoc (vla-get-activedocument
		   (vlax-get-acad-object)
		 )
      )
  )
  (or acsp
      (setq acsp (vla-get-modelspace
		   adoc
		 )
      )
  )

  (if
  (setq ss (ssget (list (cons 0 "*POLYLINE,*CONTOUR"))))
  (progn
  (setq axss (vla-get-activeselectionset adoc))
  ;; iterate through the active selection set collection
  (vlax-for obj axss
         ; get a curve center
         (setq cpt (trans (getcenter obj) 0 1))	 
	 (setq txt
	 ; displayed in meters to 3 decimal place:	
;;;		(strcat	"%<\\AcObjProp Object(%<\\_ObjId "
;;;			(itoa (vlax-get obj 'ObjectID))
;;;			">%).Area \\f \"%lu2%pr3%ps[, m2]%ct8[1e-006]\">%"
;;;		)
	 ; displayed in engineering to 2 decimal place:	
;	       (strcat	"%<\\AcObjProp Object(%<\\_ObjId "
;			(itoa (vlax-get obj 'ObjectID))
;			">%).Area \\f \"%pr2%lu2%ct4%qf1\">%");<--pr2 means 2 decimal places, change to your suit
	 ; displayed in engineering to 2 decimal place with addition SQ. FT.:
	       (strcat	"%<\\AcObjProp Object(%<\\_ObjId "
			(if (vl-string-search "64" (getenv "PROCESSOR_ARCHITECTURE"))
			  (vlax-invoke-method
			    (vla-get-Utility adoc)
			    'GetObjectIdString
			    obj
			    :vlax-false
			  )
			  (itoa (vla-get-Objectid obj))
			)
			">%).Area \\f \"%pr0%lu2%ct4%qf1 SF.\">%")
	 )
; add mtext object to model space
(setq mtxtobj (vlax-invoke
	       acsp 'AddMText
	       	 cpt ;insertion point
	       0.0 ; mtext width, optional = 0
		txt ;string (field value)
	       ))
; change mtext height accordingly to current dimension style text height:
;(vlax-put mtxtobj 'Height (getvar "dimtxt")); change (getvar "dimtxt") on text height you need
(vlax-put mtxtobj 'Height "11")
; set justifying to middle center
(vlax-put  mtxtobj  'AttachmentPoint acAttachmentPointMiddleCenter)  
       )      
     )
  )
     (vla-regen adoc acactiveviewport)

  (princ)
)
(princ "\n   Type GAREA to label objects with area field")
(princ)

 

0 Likes
Accepted solutions (1)
2,100 Views
7 Replies
Replies (7)
Message 2 of 8

Anonymous
Not applicable
(setq NN 323)
(setq NN (* (fix (/ NN 10)) 10))
0 Likes
Message 3 of 8

hmsilva
Mentor
Mentor

@Anonymous wrote:
(setq NN 323)
(setq NN (* (fix (/ NN 10)) 10))

I would suggest

(setq NN (* (fix (/ (+ NN 5) 10)) 10))

323 would return 320

325 would return 330

 

Henrique

 

EESignature

0 Likes
Message 4 of 8

Anonymous
Not applicable

Any idea how would I apply this to the code in the OP.  I assume it would need to be applied in this portion of the code.

 

(strcat "%<\\AcObjProp Object(%<\\_ObjId "
   (if (vl-string-search "64" (getenv "PROCESSOR_ARCHITECTURE"))
     (vlax-invoke-method
       (vla-get-Utility adoc)
       'GetObjectIdString
       obj
       :vlax-false
     )
     (itoa (vla-get-Objectid obj))
   )
   ">%).Area \\f \"%pr0%lu2%ct4%qf1 SF.\">%")
  )

 

 

 

 

 

0 Likes
Message 5 of 8

hmsilva
Mentor
Mentor

@Anonymous wrote:

Any idea how would I apply this to the code in the OP.  I assume it would need to be applied in this portion of the code.

 


Hello dglenn9000,

unfortunately inches and feet are not my world, I'm a metric guy, I had some difficulty to round up in square feet, probably someone used to using those units will give you better help.

 

If the outcome was in default units (with no conversion factor) I would use something like this

 

(strcat "%<\\AcExpr (1e1*trunc(%<\\AcObjProp Object(%<\\_ObjId "
        (if (vl-string-search "64" (getenv "PROCESSOR_ARCHITECTURE"))
          (vlax-invoke-method
            (vla-get-Utility adoc)
            'GetObjectIdString
            obj
            :vlax-false
          )
          (itoa (vla-get-Objectid obj))
        )
        ">%).Area \\f \"%pr0%lu2 \">%*1e-1)) \\f \"%pr0%lu2\">%"
)

 

 

I hope this helps

Henrique

 

 

EESignature

Message 6 of 8

Balaji_Ram
Alumni
Alumni
Accepted solution

For square feet, please give this a try.

 

This is similar to what Henrique suggested but since the "round" works on the original value and not on what is previewed, we need to handle the conversion on our own.

 

   (strcat "%<\\AcExpr (1e1*round((%<\\AcObjProp Object(%<\\_ObjId "
			(if (vl-string-search "64" (getenv "PROCESSOR_ARCHITECTURE"))
			  (vlax-invoke-method
			    (vla-get-Utility adoc)
			    'GetObjectIdString
			    obj
			    :vlax-false
			  )
			  (itoa (vla-get-Objectid obj))
			)
        ">%).Area \\f \"%pr0%lu2\">%/144)*1e-1)) \\f \"%lu2%pr0%ps[, S F (rounded)]\">%")

 

Regards,

Balaji



Balaji
Developer Technical Services
Autodesk Developer Network

Message 7 of 8

Balaji_Ram
Alumni
Alumni

And here is a test drawing to try it with two mtext inserted.

One of them rounds it to nearest 10 while the other displays without a change.

 

Cheers,

Balaji



Balaji
Developer Technical Services
Autodesk Developer Network

0 Likes
Message 8 of 8

Anonymous
Not applicable

A little late in my reply but this works perfect.  Thanks.

0 Likes