TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: fathihvac on August 12, 2011, 04:33:28 AM

Title: area command ?
Post by: fathihvac on August 12, 2011, 04:33:28 AM
Hello,
Can anyone write an autolisp to use area command and insert the result as text string in the drawing :-)
Title: Re: area command ?
Post by: Harrie on August 12, 2011, 07:54:03 AM
With a area macro with DIESEL expressions?
Code: [Select]
^C^C_.-HATCH;_P;_S;\;_.AREA;_O;_L;_.ERASE;_L;;_.-MTEXT;\@0,0;Area= $m=$(fix,$(getvar,area)) mm²;;or
Code: [Select]
^C^C-H;P;S;\;AA;O;L;E;L;;-MTEXT;\@0,0;Area= $m=$(fix,$(getvar,area)) mm²;;and for square metres
Code: [Select]
-H;P;S;\;AA;O;L;E;L;;-MTEXT;\@0,0;Area= $m=$(fix,$(/,$(getvar,area),1000000)) m²;;and for square metres, with no fix but with mode and precision
Code: [Select]
-H;P;S;\;AA;O;L;E;L;;-MTEXT;\@0,0;Area= $m=$(rtos,$(/,$(getvar,area),1000000),2,2) m²;;http://forums.autodesk.com/t5/Visual-LISP-AutoLISP-and-General/area-command-problems/td-p/2665577
http://www.jefferypsanders.com/autolisp_GetArea.html
http://www.afralisp.net/autolisp/tutorials/working-with-areas-part-1.php

Why text and not fields?
Title: Re: area command ?
Post by: Chris on August 12, 2011, 08:16:40 AM
I'm sure that lots of people here can write one, but as Harrie asked, why not just use a field with the data linked to the area property of the selected objects?
Title: Re: area command ?
Post by: irneb on August 12, 2011, 08:50:47 AM
My point exactly. I prefer using the attached's DimArea (though that places the field into an existing text). To only use text (no fields), perhaps something like this:
Code: [Select]
(vl-load-com)

(setq *AreaSettings*
       '(("Prefix" . "AREA: ")
         ("Suffix" . " m²")
         ("Source" . "sq mm")
         ("Target" . "sq m")
         ("Format" 2 2)
        )
)
(defun c:PlaceArea (/ en area pt eo space Area->Text AddText)
  (setq space (apply (if (or (= (getvar 'TileMode) 1) (> (getvar 'CVport) 1))
                       'vla-get-ModelSpace
                       'vla-get-PaperSpace
                     )
                     (list (vla-get-ActiveDocument (vlax-get-acad-object)))
              )
  )
  (defun Area->Text (area /)
    (strcat (cdr (assoc "Prefix" *AreaSettings*))
            (apply 'rtos
                   (cons
                     (cvunit area (cdr (assoc "Source" *AreaSettings*)) (cdr (assoc "Target" *AreaSettings*)))
                     (cdr (assoc "Format" *AreaSettings*))
                   )
            )
            (cdr (assoc "Suffix" *AreaSettings*))
    )
  )
  (defun AddText (area pt /)
    (vla-AddText space (Area->Text area) (vlax-3D-point pt) (getvar 'TextSize))
  )
  (while (setq en (entsel "\nPick entity for source of area (Enter to exit): "))
    (if (setq area (vlax-curve-getArea (car en)))
      (if (setq pt (getpoint "\nPick point for placing text: "))
        (if (vl-catch-all-error-p (setq eo (vl-catch-all-apply 'AddText (list area pt))))
          (prompt (strcat "\nError placing text: " (vl-catch-all-error-message eo)))
          (progn
            (vla-put-Layer eo (getvar 'CLayer))
            (vla-put-StyleName eo (getvar 'TextStyle))
            (prompt "\nArea text placed.")
          )
        )
        (prompt "\nInvalid point, restarting.")
      )
      (prompt "\nThat entity doesn't have an area. Try again.")
    )
  )
  (princ)
)
Title: Re: area command ?
Post by: Lee Mac on August 12, 2011, 10:05:16 AM
Another variant:

http://lee-mac.com/areastofield.html (http://lee-mac.com/areastofield.html)
Title: Re: area command ?
Post by: fathihvac on August 13, 2011, 06:26:13 AM
To achieve my request Here is the code that i want to correct.Please help
Title: Re: area command ?
Post by: HofCAD on August 13, 2011, 08:11:14 AM
To achieve my request Here is the code that i want to correct.Please help
Why not remove in GA.lsp http://www.jefferypsanders.com/autolisp_GetArea.html
the 2 lines:
Code: [Select]
(command "text" "Justify" "Center" Pt tht 0 myNum)
Code: [Select]
(command "text" "Justify" "Center" (polar Pt (* pi 1.5) (* 2.0(* tht 1.5))) tht 0 (strcat "PERIMETER=" (rtos myPerim)))But if you insist on simplicity:
Code: [Select]
(defun c:test (/ AreaTxt pt)
  (command "_.AREA" "_O" pause)
  (setq AreaTxt (strcat (rtos (getvar "AREA")) " m²")
  )
  (if (setq pt (getpoint "\n Select text insertion point : "))
    (command "_.TEXT"
     "_J"
     "_C"
     pt
     (getvar "TEXTSIZE")
     0
     AreaTxt
    )
  )
)

HofCAD CSI
Title: Re: area command ?
Post by: fathihvac on August 13, 2011, 08:27:39 AM
Thanks hotcad I mean to keep autocad area command as it is so it prompts me to select points that makes the boundaries and at the end i can insert areatxt where i want.
Title: Re: area command ?
Post by: Lee Mac on August 13, 2011, 08:32:15 AM
This seems very similar to this (http://www.cadtutor.net/forum/showthread.php?61805-Help-with-Area-command) recent thread. 
Title: Re: area command ?
Post by: HofCAD on August 13, 2011, 08:38:07 AM
Thanks hotcad I mean to keep autocad area command as it is so it prompts me to select points that makes the boundaries and at the end i can insert areatxt where i want.
Something like this:
Code: [Select]
(defun c:test2 (/ pt)
  (vl-cmdf "_.AREA")
  (while (/= 0 (getvar 'CMDACTIVE))
    (vl-cmdf pause)
  )
  (if (setq pt (getpoint "\n Select text insertion point : "))
    (command "_.TEXT"
     "_J"
     "_C"
     pt
     (getvar 'TEXTSIZE)
     0
     (strcat (rtos (getvar 'AREA)) " m²")
    )
  )
)

HofCAD CSI
Title: Re: area command ?
Post by: Lee Mac on August 13, 2011, 08:51:22 AM
Related, by Alanjt:

http://www.theswamp.org/index.php?topic=37824 (http://www.theswamp.org/index.php?topic=37824)
Title: Re: area command ?
Post by: fathihvac on August 13, 2011, 08:52:04 AM
Great ! it works
Many thank to you Hofcad
Can you make it so the DIMDEC equal 2 because it gives 4 after dot althoug my dimdec is 2 i dont understand where is the problem
Title: Re: area command ?
Post by: HofCAD on August 13, 2011, 08:55:47 AM
Great ! it works
Many thank to you Hofcad
Can you make it so the DIMDEC equal 2 because it gives 4 after dot althoug my dimdec is 2 i dont understand where is the problem
Replace
Code: [Select]
(strcat (rtos (getvar 'AREA)) " m²")with
Code: [Select]
(strcat (rtos (getvar 'AREA) 2 2) " m²")Are you drawing in metres, otherwise you must divide by 1000000?

HofCAD CSI
Title: Re: area command ?
Post by: irneb on August 13, 2011, 09:30:47 AM
Are you drawing in metres, otherwise you must divide by 1000000?
That's why I had the cvunit thing in mine. Many in the building industry draw in mm but give areas in sqm. Same as drawing in inches but areas in sqft. The cvunit makes it a bit simpler to convert, since you don't need to remember the ratios (especially since area is squared).