Author Topic: Analytic Calculation of area - Help to updatethe code  (Read 928 times)

0 Members and 1 Guest are viewing this topic.

mhy3sx

  • Newt
  • Posts: 120
Analytic Calculation of area - Help to updatethe code
« on: March 23, 2023, 05:36:21 PM »
Hi. I am using this code to analytic calculate multi areas. The code work fine, but I want to do a simple update, but I am stuck. This code add in each polygon  numbers  as E1,E2....En . I want to change that and ask me for the letter. for example If a have 5 group of polygons the first group will be A1,A2,...An  the second B1,B2..Bn etc. This will be done if I have the option to give the letter , not to be all the times E.

Code - Auto/Visual Lisp: [Select]
  1.  (defun c:areacal ( / AcDoc Space nw_style js nb ent dxf_ent ptlst n old_textsize count app_txt surf cum_area nb pt_ins val_txt lst_bis l_4d max_d pos pt1 pt2 pt3 d1 d2 h t_spc nw_obj ent_text key)
  2.     (setq scl (getvar "useri1"))
  3.      (setq ht (* 0.003 scl))
  4.         (vla-startundomark AcDoc)
  5.         (vla-put-ActiveSpace AcDoc acModelSpace)
  6.         (cond
  7.                 ((null (tblsearch "LAYER" "AREA CALC"))
  8.                         (vlax-put (vla-add (vla-get-layers AcDoc) "AREA CALC") 'color 7)
  9.                 )
  10.         )
  11.         (cond
  12.                 ((null (tblsearch "STYLE" "AREA CALC"))
  13.                         (setq nw_style (vla-add (vla-get-textstyles AcDoc) "AREA CALC"))
  14.                         (mapcar
  15.                                 '(lambda (pr val)
  16.                                         (vlax-put nw_style pr val)
  17.                                 )
  18.                                 (list 'FontFile 'Height 'ObliqueAngle 'Width 'TextGenerationFlag)
  19.                                 (list (strcat (getenv "windir") "\\fonts\\arial.ttf") 0.0 0.0 1.0 0.0)
  20.                         )
  21.                 )
  22.         )
  23.         (while (null (setq js (ssget '((0 . "LWPOLYLINE") (-4 . "<AND") (-4 . "&") (70 . 1) (-4 . ">") (90 . 2) (-4 . "<") (90 . 5) (-4 . "AND>"))))))
  24.         (repeat (setq nb (sslength js))
  25.                 (setq
  26.                         ent (ssname js (setq nb (1- nb)))
  27.                         dxf_ent (entget ent)
  28.                         ptlst (mapcar 'cdr (vl-remove-if-not '(lambda (x) (= (car x) 10)) dxf_ent))
  29.                         n (length ptlst)
  30.                 )
  31.                 (if (eq n 4)
  32.                         (if
  33.                                 (and
  34.                                         (not (equal (rem (angle (car ptlst) (cadr ptlst)) pi) (rem (angle (caddr ptlst) (cadddr ptlst)) pi) 1E-08))
  35.                                         (not (equal (rem (angle (cadr ptlst) (caddr ptlst)) pi) (rem (angle (cadddr ptlst) (car ptlst)) pi) 1E-08))
  36.                                 )
  37.                                 (ssdel ent js)
  38.                         )
  39.                 )
  40.         )
  41.         (cond
  42.                 ((and js (> (sslength js) 0))
  43.                         (sssetfirst nil js)
  44.                         (initget "Yes No")
  45.                         (cond
  46.                                 ((not (eq (getkword "\n Insert calculations [Yes/No]? <Yes>: ") "No"))
  47.                                         (sssetfirst nil nil)
  48.                                         (setq
  49.                                                 old_textsize (getvar "TEXTSIZE")
  50.                                                 count 0
  51.                                                 app_txt ""
  52.                                                 cum_area 0.0
  53.                                         )
  54.                                         (setvar "TEXTSIZE" 2.5)
  55.                                         (repeat (setq nb (sslength js))
  56.                                                 (setq
  57.                                                         ent (ssname js (setq nb (1- nb)))
  58.                                                         dxf_ent (entget ent)
  59.                                                         ptlst (mapcar 'cdr (vl-remove-if-not '(lambda (x) (= (car x) 10)) dxf_ent))
  60.                                                         n (length ptlst)
  61.                                                         pt_ins (list (/ (apply '+ (mapcar 'car ptlst)) n) (/ (apply '+ (mapcar 'cadr ptlst)) n))
  62.                                                         val_txt
  63.                                                         (if (eq n 3)
  64.                                                                 (progn
  65.                                                                         (setq
  66.                                                                                 lst_bis (append (cdr ptlst) (list (car ptlst)))
  67.                                                                                 l_4d (mapcar 'distance ptlst lst_bis)
  68.                                                                                 max_d (apply 'max l_4d)
  69.                                                                                 pos (vl-position max_d l_4d)
  70.                                                                                 pt1 (nth pos ptlst)
  71.                                                                                 pt2 (nth pos lst_bis)
  72.                                                                                 pt3 (car (vl-remove pt2 (vl-remove pt1 ptlst)))
  73.                                                                                 d1
  74.                                                                                 (distance
  75.                                                                                         pt3
  76.                                                                                         (inters
  77.                                                                                                 pt1
  78.                                                                                                 pt2
  79.                                                                                                 pt3
  80.                                                                                                 (polar pt3 (+ (angle pt1 pt2) (* pi 0.5)) (distance pt1 pt2))
  81.                                                                                                 nil
  82.                                                                                         )
  83.                                                                                 )
  84.                                                                                 surf (* (atof(rtos max_d 2 2)) (atof (rtos d1 2 2)) 0.5)
  85.                                                                                 cum_area (atof (rtos (+ surf cum_area) 2 3))
  86.                                                                         )
  87.                                                                         (strcat
  88.                                                                                 "E" (itoa (setq count (1+ count))) " = "
  89.                                                                                 "1/2 x "
  90.                                                                                 (rtos max_d 2 2)
  91.                                                                                 " x "
  92.                                                                                 (rtos d1 2 2)
  93.                                                                                 " = "
  94.                                                                                 (rtos surf 2 2) " sqm\\P"
  95.                                                                         )
  96.                                                                 )
  97.                                                                 (if
  98.                                                                         (and
  99.                                                                                 (equal (abs (- (rem (angle (car ptlst) (cadr ptlst)) pi) (rem (angle (car ptlst) (cadddr ptlst)) pi))) (* 0.5 pi) 1E-08)
  100.                                                                                 (equal (abs (- (rem (angle (cadr ptlst) (caddr ptlst)) pi) (rem (angle (caddr ptlst) (cadddr ptlst)) pi))) (* 0.5 pi) 1E-08)
  101.                                                                         )
  102.                                                                         (progn
  103.                                                                                 (setq
  104.                                                                                         d1 (atof (rtos (distance (car ptlst) (cadr ptlst)) 2 2))
  105.                                                                                         d2 (atof (rtos (distance (cadr ptlst) (caddr ptlst)) 2 2))
  106.                                                                                         surf (atof (rtos (* d1 d2) 2 2))
  107.                                                                                         cum_area (atof (rtos (+ surf cum_area) 2 2))
  108.                                                                                 )
  109.                                                                                 (strcat
  110.                                                                                         "E" (itoa (setq count (1+ count))) " = "
  111.                                                                                         (rtos d1 2 2)
  112.                                                                                         " x "
  113.                                                                                         (rtos d2 2 2)
  114.                                                                                         " = "
  115.                                                                                         (rtos surf 2 2)
  116.                                                                                         " sqm\\P"
  117.                                                                                 )
  118.                                                                         )
  119.                                                                         (progn
  120.                                                                                 (if (equal (rem (angle (car ptlst) (cadr ptlst)) pi) (rem (angle (caddr ptlst) (cadddr ptlst)) pi) 1E-08)
  121.                                                                                         (setq
  122.                                                                                                 d1 (atof (rtos (distance (car ptlst) (cadr ptlst)) 2 2))
  123.                                                                                                 d2 (atof (rtos (distance (caddr ptlst) (cadddr ptlst)) 2 2))
  124.                                                                                                 h (atof (rtos (distance (car ptlst) (inters (car ptlst) (polar (car ptlst) (+ (angle (car ptlst) (cadr ptlst)) (* 0.5 pi)) 1.0) (caddr ptlst) (cadddr ptlst) nil)) 2 2))
  125.                                                                                         )
  126.                                                                                         (setq
  127.                                                                                                 d1 (atof (rtos (distance (cadr ptlst) (caddr ptlst)) 2 2))
  128.                                                                                                 d2 (atof (rtos (distance (car ptlst) (cadddr ptlst)) 2 2))
  129.                                                                                                 h (atof (rtos (distance (cadr ptlst) (inters (cadr ptlst) (polar (cadr ptlst) (+ (angle (cadr ptlst) (caddr ptlst)) (* 0.5 pi)) 1.0) (car ptlst) (cadddr ptlst) nil)) 2 2))
  130.                                                                                         )
  131.                                                                                 )
  132.                                                                                 (setq
  133.                                                                                         surf (atof (rtos (* (+ d1 d2) h 0.5) 2 2))
  134.                                                                                         cum_area (atof (rtos (+ surf cum_area) 2 2))
  135.                                                                                 )
  136.                                                                                 (strcat
  137.                                                                                         "E" (itoa (setq count (1+ count))) " =  1/2 x ("
  138.                                                                                         (rtos d1 2 2)
  139.                                                                                         " + "
  140.                                                                                         (rtos d2 2 2)
  141.                                                                                         ") x "
  142.                                                                                         (rtos h 2 2)
  143.                                                                                         " = "
  144.                                                                                         (rtos surf 2 2)
  145.                                                                                         " sqm\\P"
  146.                                                                                 )
  147.                                                                         )
  148.                                                                 )
  149.                                                         )
  150.                                                         app_txt (strcat app_txt val_txt)
  151.                                                 )
  152.                                                 (entmake
  153.                                                         (list
  154.                                                                 '(0 . "TEXT")
  155.                                                                 '(100 . "AcDbEntity")
  156.                                                                 (cons 8 "AREA CALC")
  157.                                                                 '(100 . "AcDbText")
  158.                                                                 (cons 10 pt_ins)
  159.                                                                 ;(cons 40 (getvar "TEXTSIZE"))
  160.                                                                 (cons 40 ht)
  161.                                                                 (cons 1 (strcat "E" (itoa count)))
  162.                                                                 (cons 50 (angle '(0 0 0) (getvar "UCSXDIR")))
  163.                                                                 '(41 . 1.0)
  164.                                                                 '(51 . 0.0)
  165.                                                                 (cons 7 "Layout")
  166.                                                                 '(71 . 0)
  167.                                                                 '(72 . 1)
  168.                                                                 (cons 11 pt_ins)
  169.                                                                 (assoc 210 dxf_ent)
  170.                                                                 '(100 . "AcDbText")
  171.                                                                 '(73 . 2)
  172.                                                         )
  173.                                                 )
  174.                                         )
  175.                                         (initget "Modelspace Paperspace")
  176.                                         (setq t_spc (getkword "\n Insert calculations [Modelspace/Paperspace]? <Modelspace>: "))
  177.                                         (cond
  178.                                                 ((eq t_spc "Paperspace")
  179.                                                         (vla-put-ActiveSpace AcDoc acPaperSpace)
  180.                                                         (vla-put-MSpace AcDoc :vlax-false)
  181.                                                         (setq Space (vla-get-PaperSpace AcDoc))
  182.                                                 )
  183.                                                 (T
  184.                                                         (vla-put-ActiveSpace AcDoc acModelSpace)
  185.                                                         (if (not (eq (getvar "TILEMODE") 1)) (vla-put-MSpace AcDoc :vlax-true))
  186.                                                         (setq Space (vla-get-ModelSpace AcDoc))
  187.                                                 )
  188.                                         )
  189.                                         (setq nw_obj
  190.                                                 (vla-addMtext Space
  191.                                                         (vlax-3d-point (trans (getvar "VIEWCTR") 1 0))
  192.                                                         0.0
  193.                                                         (strcat app_txt "Eall = " (rtos cum_area 2 2) " sqm")
  194.                                                 )
  195.                                         )
  196.                                         (mapcar
  197.                                                 '(lambda (pr val)
  198.                                                         (vlax-put nw_obj pr val)
  199.                                                 )
  200.                                                 (list 'AttachmentPoint 'Height 'DrawingDirection 'StyleName 'Layer 'Rotation 'BackgroundFill 'Color)
  201.                                                 (list 1 (getvar "TEXTSIZE") 5 "Layout" "Layout" 0.0 0 0)
  202.                                         )
  203.                                         (setq
  204.                                                 ent_text (entlast)
  205.                                                 dxf_ent (entget ent_text)
  206.                                                 dxf_ent (subst (cons 90 1) (assoc 90 dxf_ent) dxf_ent)
  207.                                                 dxf_ent (subst (cons 63 255) (assoc 63 dxf_ent) dxf_ent)
  208.                                         )
  209.                                         (entmod dxf_ent)
  210.                                         (while (and (setq key (grread T 4 0)) (/= (car key) 3))
  211.                                                 (cond
  212.                                                         ((eq (car key) 5)
  213.                                                                 (setq dxf_ent (subst (cons 10 (trans (cadr key) 1 0)) (assoc 10 dxf_ent) dxf_ent))
  214.                                                                 (entmod dxf_ent)
  215.                                                         )
  216.                                                 )
  217.                                         )
  218.                                         (setvar "TEXTSIZE" old_textsize)
  219.                                 )
  220.                                 (T (sssetfirst nil nil) (princ "\nFunction canceled"))
  221.                         )
  222.                 )
  223.                 (T (princ "\nSelected items are invalid"))
  224.         )
  225.         (vla-endundomark AcDoc)
  226.         (prin1)
  227. ;layer 0
  228. (mapcar 'setvar '("clayer" "cecolor" "celtype" "celweight") (list "0" "BYLAYER" "BYLAYER" -1))
  229. (*error* "")
  230.     )
  231.  
  232.  

Any options ?

Thanks














« Last Edit: March 23, 2023, 05:42:29 PM by mhy3sx »

BIGAL

  • Swamp Rat
  • Posts: 1411
  • 40 + years of using Autocad
Re: Analytic Calculation of area - Help to updatethe code
« Reply #1 on: March 23, 2023, 08:08:39 PM »
1st comment (setq scl (getvar "useri1")), the useri1 can be overridden by other software have a look at LDATA.

Code: [Select]
(while (null (setq js (ssget '((0 . "LWPOLYLINE") (-4 . "<AND") (-4 . "&") (70 . 1) (-4 . ">") (90 . 2) (-4 . "<") (90 . 5) (-4 . "AND>"))))))
 (setq label (getstring "\nEnter label eg A "))
       (repeat (setq nb (sslength js))

................
"E" (itoa (setq count (1+ count))) " = "
now
label (itoa (setq count (1+ count))) " = "

You can do the setq label much better but simple for now.
A man who never made a mistake never made anything

mhy3sx

  • Newt
  • Posts: 120
Re: Analytic Calculation of area - Help to updatethe code
« Reply #2 on: March 24, 2023, 03:51:46 AM »
Thanks BIGAL .

DEVITG

  • Bull Frog
  • Posts: 480
Re: Analytic Calculation of area - Help to updatethe code
« Reply #3 on: March 26, 2023, 10:38:47 AM »
@mhy3sx

Just as I'm learning to  ssget filter 
Please clear me what is the meaning to
Code: [Select]
(WHILE (NULL (SETQ JS (SSGET '((0 . "LWPOLYLINE")
                                  (-4 . "<AND")
                                  (-4 . "&")
                                  (70 . 1)
                                  (-4 . ">")
                                  (90 . 2)
                                  (-4 . "<")
                                  (90 . 5)
                                  (-4 . "AND>"))))))

70 . 1 for closed
90 . 5 vertex

but why or for the ?
 90 .2

I did this  LWPOLYLINE , but it can not be select



Thanks in advance



 
Location @ Córdoba Argentina Using ACAD 2019  at Window 10

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: Analytic Calculation of area - Help to updatethe code
« Reply #4 on: March 26, 2023, 06:53:03 PM »
The ssget filter is requiring DXF group 90 to be greater than 2 and less than 5, hence an alternative way to write it could be:
Code - Auto/Visual Lisp: [Select]
  1. (ssget '((0 . "LWPOLYLINE") (-4 . "&") (70 . 1) (-4 . "<OR") (90 . 3) (90 . 4) (-4 . "OR>")))

DEVITG

  • Bull Frog
  • Posts: 480
Re: Analytic Calculation of area - Help to updatethe code
« Reply #5 on: March 26, 2023, 07:17:10 PM »
The ssget filter is requiring DXF group 90 to be greater than 2 and less than 5, hence an alternative way to write it could be:
Code - Auto/Visual Lisp: [Select]
  1. (ssget '((0 . "LWPOLYLINE") (-4 . "&") (70 . 1) (-4 . "<OR") (90 . 3) (90 . 4) (-4 . "OR>")))

Hi LEE-MAC,  thanks for it .
So  if  (-4 . "<OR") (90 . 3) (90 . 4) (-4 . "OR>") . only it will select  Triangles or Quadrilateral 
Location @ Córdoba Argentina Using ACAD 2019  at Window 10

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: Analytic Calculation of area - Help to updatethe code
« Reply #6 on: March 31, 2023, 10:32:24 AM »
Yes  :-)