thereisnotime Posted May 18, 2010 Posted May 18, 2010 I'm just getting started into writing routines in LISP and since I'm totally new to this, I'd thought I'd ask for something basic to learn from. I was wondering if someone could write a quick code that allows me to "Please select two closed polylines" and have the routine display the combined area of the two objects? Quote
Lee Mac Posted May 18, 2010 Posted May 18, 2010 A very quickly written code: (defun c:AddAreas ( / e1 e2 obj1 obj2 ) (vl-load-com) (if (and (setq e1 (car (entsel "\nSelect First Object: "))) (vlax-property-available-p (setq obj1 (vlax-ename->vla-object e1)) 'Area) (setq e2 (car (entsel "\nSelect Second Object: "))) (vlax-property-available-p (setq obj2 (vlax-ename->vla-object e2)) 'Area) ) (alert (strcat "Combined Area: " (rtos (+ (vla-get-area obj1) (vla-get-Area obj2))) ) ) ) (princ) ) Quote
Lee Mac Posted May 18, 2010 Posted May 18, 2010 Using FIELDS, probably not best for learning purposes... (defun c:Areas2Field ( / *error* spc doc pt uFlag ss ids ) (vl-load-com) ;; Lee Mac ~ 18.05.10 (defun *error* ( msg ) (and uFlag (vla-EndUndomark doc)) (or (wcmatch (strcase msg) "*BREAK,*CANCEL*,*EXIT*") (princ (strcat "\n** Error: " msg " **"))) (princ) ) (setq spc (if (or (eq AcModelSpace (vla-get-ActiveSpace (setq doc (vla-get-ActiveDocument (vlax-get-acad-object) ) ) ) ) (eq :vlax-true (vla-get-MSpace doc)) ) (vla-get-ModelSpace doc) (vla-get-PaperSpace doc) ) ) (if (and (ssget '((0 . "ARC,CIRCLE,ELLIPE,HATCH,*POLYLINE,REGION"))) (setq pt (getpoint "\nPick Point for Field: "))) (progn (setq uFlag (not (vla-StartUndoMark doc))) (vlax-for obj (setq ss (vla-get-ActiveSelectionSet doc)) (setq Ids (cons (GetObjectID obj doc) Ids) ) ) (vla-delete ss) (vla-AddMText spc (vlax-3D-point pt) 0. (if (= 1 (length Ids)) (strcat "%<\\AcObjProp Object(%<\\_ObjId " (car Ids) ">%).Area \\f \"%lu6%qf1\">%") (strcat "%<\\AcExpr" (lst->str Ids " %<\\AcObjProp Object(%<\\_ObjId " ">%).Area >% +") ">%).Area >% \\f \"%lu6%qf1\">%" ) ) ) (setq uFlag (vla-EndUndomark doc)) ) ) (princ) ) (defun lst->str ( lst d1 d2 ) (if (cdr lst) (strcat d1 (car lst) d2 (lst->str (cdr lst) d1 d2)) (strcat d1 (car lst)) ) ) (defun GetObjectID ( obj doc ) (if (eq "X64" (strcase (getenv "PROCESSOR_ARCHITECTURE") ) ) (vlax-invoke-method (vla-get-Utility doc) 'GetObjectIdString obj :vlax-false ) (itoa (vla-get-Objectid obj)) ) ) Quote
thereisnotime Posted May 19, 2010 Author Posted May 19, 2010 Ohhh okay, that's a little simpler than was I was expecting, which is good news. You wouldn't happen to know if there are any help files in the CAD directory that list all the properties and methods, do you? Quote
Lee Mac Posted May 19, 2010 Posted May 19, 2010 Yes, open the Visual LISP Editor (using VLIDE command), and go to the help file. Quote
Glen Smith Posted May 19, 2010 Posted May 19, 2010 Ohhh okay, that's a little simpler than was I was expecting, which is good news. You wouldn't happen to know if there are any help files in the CAD directory that list all the properties and methods, do you? Heh, heh yeah everyone sez tha... Wait, what? SIMPLER than you were expecting!? What were you expecting? Something along the lines of the search for the God Particle? What programing languages do you already know? Glen Quote
alanjt Posted May 19, 2010 Posted May 19, 2010 I'd say it was really simple, since Lee did all the programming. Quote
thereisnotime Posted May 19, 2010 Author Posted May 19, 2010 Heh, heh yeah everyone sez tha... Wait, what? SIMPLER than you were expecting!? What were you expecting? Something along the lines of the search for the God Particle? What programing languages do you already know? Glen Lol, well I know VB and a bit of Java, so I guess my comment was more along the lines that the overall layout of the code is similar to Java...at least I think so anyway I dabbled in writing VBA scripts for CAD so I'm somewhat familiar with some of the objects and methods that CAD uses for programming. Watch, now after I said this, I'm gonna run into a wall lol I do, however, have a question. After reading through the functions, I understand the " get-attr " part, but I couldn't find what " vla- " does. I could also be blind and it's really in that list, but I skipped over it for some reason... Quote
thereisnotime Posted May 19, 2010 Author Posted May 19, 2010 Well, ran into that wall. I'm working off Lee's code, except now, I'm wanting to display a difference in elevation of two polylines, making sure that it will always subtract largest to smallest, however it's giving me a bad argument type error. I tried using strcat instead of princ, but strcat didn't display anything at all. (defun c:GetElevation ( / e1 e2 obj1 obj2) (vl-load-com) (if (and (setq e1 (car (entsel "\nSelect First Polyline: "))) (vlax-property-available-p (setq obj1 (vlax-ename->vla-object e1)) 'Elevation) (setq e2 (car (entsel "\nSelect Second Polyline: "))) (vlax-property-available-p (setq obj2 (vlax-ename->vla-object e2)) 'Elevation) ) (princ "Elevation difference: " (If (> (vla-get-Elevation obj1) (vla-get-Elevation obj2)) (rtos(- (vla-get-Elevation obj1)(vla-get-Elevation obj2))) (rtos(- (vla-get-Elevation obj2)(vla-get-Elevation obj1))))) ) (princ) ) Quote
alanjt Posted May 19, 2010 Posted May 19, 2010 (princ (strcat "\nElevation: " (rtos (abs (- (vla-get-Elevation obj1) (vla-get-Elevation obj2)))))) Quote
thereisnotime Posted May 20, 2010 Author Posted May 20, 2010 Ah, that's what I was missing. Thanks Quote
alanjt Posted May 20, 2010 Posted May 20, 2010 Ah, that's what I was missing. Thanks You're welcome. Quote
Recommended Posts
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.