How to Autolisp calculate area ratio of two groups?

How to Autolisp calculate area ratio of two groups?

raymondcheng0922
Enthusiast Enthusiast
1,006 Views
12 Replies
Message 1 of 13

How to Autolisp calculate area ratio of two groups?

raymondcheng0922
Enthusiast
Enthusiast

How to Autolisp calculate area ratio of two groups?

0 Likes
Accepted solutions (1)
1,007 Views
12 Replies
Replies (12)
Message 2 of 13

raymondcheng0922
Enthusiast
Enthusiast

(defun C:area-ratio ( / ent1 ent2 area1 area2 total)

  (setq ent1 (entsel "

Select the first group of objects: "))

  (setq area1 (vl-cmdf "_.area" ent1))

  (setq ent2 (entsel "

Select the second group of objects: "))

  (setq area2 0)

  (foreach ent ent2

    (setq area2 (+ area2 (vl-cmdf "_.area" ent)))

  )

  (setq total (+ area1 area2))

  (setq first-ratio (* 100.0 (/ area1 total)))

  (setq second-ratio (* 100.0 (/ area2 total)))

  (setq pt (getpoint "

Specify the insertion point for the output: "))

  (setq output (strcat "The area of the first group is " (rtos first-ratio 2) "% of the total area."))

  (setq output (strcat output (strcat "\nThe area of the second group is " (rtos second-ratio 2) "% of the total area.")))

  (entmake (list (cons 0 "TEXT") (cons 10 pt) (cons 1 output)))

)

0 Likes
Message 3 of 13

pbejse
Mentor
Mentor

@raymondcheng0922 wrote:

How to Autolisp calculate area ratio of two groups?


Are you referring to AutoCAD  GROUP selection type or group of objects ? ( Polylines/Circles/Hatch...) ?

 

0 Likes
Message 4 of 13

raymondcheng0922
Enthusiast
Enthusiast

Two sets of polygons total area 

0 Likes
Message 5 of 13

raymondcheng0922
Enthusiast
Enthusiast

Exsamlpe 

What is the percentage of one of the 8 unit buildings?

0 Likes
Message 6 of 13

pbejse
Mentor
Mentor
Accepted solution

@raymondcheng0922 wrote:

Two sets of polygons total area 


(defun C:area-ratio (/ _sel firstGroup area1 area2 secondGroup total first-ratio second-ratio pt output )
  (defun _sel (ss / ent i totalArea)
    (setq totalArea 0)
    (repeat (setq i (sslength ss))
      (setq ent (ssname ss (setq i (1- i))))
      (setq totalArea (+ totalArea (getpropertyvalue ent "Area")))
    )
    totalArea
  )

  (if (and
	(princ "\nFirst group of objects: ")
	(setq firstGroup (ssget '((0 . "LWPOLYLINE"))))
	(princ "\nSecond group of objects: ")
	(setq secondGroup (ssget '((0 . "LWPOLYLINE"))))
	(setq pt (getpoint "\nSpecify the insertion point for the output: "))	
      )
    (progn
      (setq area1 (_sel firstGroup))
      (setq area2 (_sel secondGroup))
      (setq total (+ area1 area2))
      (setq first-ratio (* 100.0 (/ area1 total)))
      (setq second-ratio (* 100.0 (/ area2 total)))
     
      (setq output (strcat "The area of the first group is "
			   (rtos first-ratio 2)
			   "% of the total area."
		   )
      )
      (setq 	output (strcat output
		       (strcat "\nThe area of the second group is "
			       (rtos second-ratio 2)
			       "% of the total area."
		       )
	       )
      )
      (entmakex	(list (cons 0 "MTEXT")
		      (cons 100 "AcDbEntity")
		      (cons 100 "AcDbMText")
		      (cons 10 pt)
		      (cons 1 output)
		)
      )
	(princ (Strcat "\n" output))
    )
  )
  (princ)
)

Command: AREA-RATIO

First group of objects:
Select objects: Specify opposite corner: 2 found

Select objects: < presss enter/spacebar >
Second group of objects:
Select objects: Specify opposite corner: 2 found

Select objects: < presss enter/spacebar >
Specify the insertion point for the output:


The area of the first group is 58.0943% of the total area.
The area of the second group is 41.9057% of the total area.

 

HTH

Message 7 of 13

raymondcheng0922
Enthusiast
Enthusiast

Just ask, can it be combined with the attached LSP

0 Likes
Message 8 of 13

pbejse
Mentor
Mentor

@raymondcheng0922 wrote:

Just ask, can it be combined with the attached LSP


I guess we can do that. how you would want the result to look? The highlighted as field value?


The area of the first group is 58.0943% of the total area.
The area of the second group is 41.9057% of the total area.

 

 

0 Likes
Message 9 of 13

raymondcheng0922
Enthusiast
Enthusiast

Like you think 。thank you

0 Likes
Message 10 of 13

raymondcheng0922
Enthusiast
Enthusiast

Thanks for the demo, it works elsewhere, very useful

0 Likes
Message 11 of 13

pbejse
Mentor
Mentor

@raymondcheng0922 wrote:

Thanks for the demo, it works elsewhere, very useful


Are you responding to my post or some other random help you got from  someplace/someone  else?

 

0 Likes
Message 12 of 13

raymondcheng0922
Enthusiast
Enthusiast

Just replying for your help, but no one else has a solution

0 Likes
Message 13 of 13

raymondcheng0922
Enthusiast
Enthusiast

In fact, can my own lsp calculate part of it, and use other lsp? Calculate part call LengthAreaField V1-4.lsp?

0 Likes