Jump to content

Recommended Posts

Posted (edited)

Hello,

 

I was wondering if I could use a LISP or macro of some kind to do the following task...

 

I have to take the area of a rectangle

Convert it to SqMetres

Multiply by a number(different every time)

Then type the given answer inside the rectangle

 

This task is for mezzanine floor column loadings onto the floor slab. There are usually many columns with different loading areas. Something like this could save me a lot of time.

 

Could any body advise me that this is possible. I'm completely new at macro's, VBA and LISP, but willing to learn. I'm using Autocad 2007 predominantly but also use 2010 on another machine.

Thanks in advance.

 

All the best

Andy

 

 

EDIT - I found half of the required code here - http://www.cadtutor.net/forum/showthread.php?57782-Area-Lisp-Routine-error-in-Autocad-2011-and-Win-7-64bit

Edited by Andy0902
Posted

(vl-load-com)
(defun c:ccc ( / )
 (setq ms (vla-get-modelspace(vla-get-activedocument(vlax-get-acad-object))))
 (setq poly(entsel "\nSelect polyline:"))
 (setq rec(entsel "Select rectangle for text"))
 (setq poly (vlax-ename->vla-object (car poly)))
 (setq rec (vlax-ename->vla-object (car rec)))
 (setq area(/(vla-get-area poly) 1000000))
 (setq textStartPoint(vla-get-coordinate rec 0))
 (setq textSecondPoint (vla-get-coordinate rec 1))
 (setq width (abs (- (nth 0(vlax-safearray->list(vlax-variant-value textSecondPoint)) )
            (nth 0 (vlax-safearray->list(vlax-variant-value textStartPoint)) ))))
 (setq height (* 0.25(abs (- (nth 0(vlax-safearray->list(vlax-variant-value textSecondPoint)) )
            (nth 0 (vlax-safearray->list(vlax-variant-value textStartPoint)) )))))
 (setq textStartPoint(append (vlax-safearray->list(vlax-variant-value textStartPoint))(list  0.0)))
 (setq mText (vla-addMtext ms (vlax-3d-point textStartPoint)  width (rtos area 2 2)) )
 (vla-put-height mText height)
)

 

The above code should give you an idea of how to go about what you're trying to achieve. While for all intensive purposes this code works please don't use it as production, it's literally just a poorly conceived list of actions solely to provide you with some starting ground for writing the awesome version. For what it's worth it gave me something to do during my coffee break lol.

Posted

Thanks very much SOliver :)

 

I'll give that a go.

Posted

You're welcome.

 

Because I'm too lazy to include it :lol: I'd recommend using while statements with embedded conditional statements around the select entities methods otherwise the remaining code will fail if you accidentally miss the item you are selecting.

 

[EDIT}

 

Too lazy, but apperently to obsessive about coding my words. For example

(while (= poly nil)
 (select poly (entsel "\nSelect polyline:"))
 (if(= poly nil)
   (princ "\nPolyline not selected"))
 )
)
(while (= rec nil)
 (select poly (entsel "\nSelect rectangle for text:"))
 (if(= rec nil)
   (princ "\nRectangle not selected"))
 )
)

 

Remember to localise your variables or it'll only work once lol

 

[code c:localise ( / .. ..)

;CODE

)

Posted (edited)
Doesn't seem to work (gives 0)?

 

Hmm that's strange I've tried it here prior to posting.

 

What units are you using metres, millimetres etc? The above is set for millimetres

 

If you're working with metres

 

;Replace
(setq area(/(vla-get-area poly) 1000000))
;With
(setq area(vla-get-area poly))

Edited by SOliver
Typo
Posted

Works great for typing the SqM. I'll be able to figure out the rest.

 

Much appreciated. I think this has kick started me writing my own/more LISP routines. I'm sat dreaming of all the possibilities :lol:

Posted
Works great for typing the SqM. I'll be able to figure out the rest.

 

Much appreciated. I think this has kick started me writing my own/more LISP routines. I'm sat dreaming of all the possibilities :lol:

 

No problem just to round it off 'cause I forgot it

(strcat (rtos area 2 2) "SqM")

That'll add the "SqM to the string; you may want to decrease the height value though.

Posted (edited)
Why not use a field?

 

I agree. An attribute would be better for future use.

Edited by SOliver
Typo
Posted (edited)

Simplifying the code I posted here:

 

(defun c:AreaField ( / LM:Select LM:GetObjectID acdoc acspc e i p ) (vl-load-com)

 (setq acdoc (vla-get-ActiveDocument (vlax-get-acad-object))
       acspc (vlax-get-property acdoc (if (= 1 (getvar 'CVPORT)) 'Paperspace 'Modelspace))
 )

 (defun LM:Select ( msg pred fun / e ) (setq pred (eval pred))
   ;; © Lee Mac 2011
   (while
     (progn (setvar 'ERRNO 0) (setq e (car (fun msg)))      
       (cond
         ( (= 7 (getvar 'ERRNO))

           (princ "\n** Missed, Try again **")
         )
         ( (eq 'ENAME (type e))

           (if (and pred (not (pred e)))
             (princ "\n** Invalid Object Selected **")
           )
         )
       )
     )
   )
   e
 )

 (defun LM:GetObjectID ( doc obj )
   ;; © Lee Mac 2011
   (if (vl-string-search "64" (getenv "PROCESSOR_ARCHITECTURE"))
     (vlax-invoke-method (vla-get-Utility doc) 'GetObjectIdString obj :vlax-false)
     (itoa (vla-get-Objectid obj))
   )
 )
 
 (if
   (and
     (setq e
       (LM:Select "\nSelect Object to Retrieve Area: "
         (function
           (lambda ( x ) (vlax-property-available-p (vlax-ename->vla-object x) 'Area))
         )
         entsel
       )
     )
     (setq p (getpoint "\nSpecify Point for Field: "))
   )
   (vla-AddText acspc
     (strcat "%<\\AcObjProp.16.2 Object(%<\\_ObjId "
       (LM:GetObjectID acdoc (vlax-ename->vla-object e)) ">%).Area \\f \"%lu6%qf1%ps[, m²]%ct8[1e-006]\">%"
     )
     (vlax-3D-point (trans p 1 0))
     (getvar 'TEXTSIZE)
   )
 )

 (princ)
)

Or there's this or this from my site.

Edited by Lee Mac
Posted

Thanks Lee!

 

Your website is full of great LISP's. I especially like the slinky text. :D

Posted
Your website is full of great LISP's. I especially like the slinky text. :D

 

Thanks Andy, I enjoyed writing that one too :)

Posted

Lee , this command is useful, however, what if you need to subtract an area. For example a floor plan with a stairwell hole in it?

Your commands sum areas well, but how can we deal with an overall area with 'holes'?

Posted
Lee , this command is useful, however, what if you need to subtract an area. For example a floor plan with a stairwell hole in it?

Your commands sum areas well, but how can we deal with an overall area with 'holes'?

 

How about something like the attached:

Areas2Field2.lsp

Posted

Great work as usual Lee, exactly what I need :wink:

I will replace my old 'region' based routines with this. Creating a field is brilliant.

Thanks for your time and expertise :D

Posted

You're very welcome Amarcon, I enjoyed writing this one :)

 

Glad it helps!

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...