Visual LISP, AutoLISP and General Customization
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Anyone have a lisp that will find the center of a square?

48 REPLIES 48
Reply
Message 1 of 49
Friptzap
1700 Views, 48 Replies

Anyone have a lisp that will find the center of a square?

I always thought there should be something built into AutoCAD to do this but I never thought about it much though as far as making a lisp for it till now. (not just a polyline cause it may be part of a block or 4 separate lines or even a rectangle. Maybe just finds midpoint of a diagonal selection from corner to corner?
48 REPLIES 48
Message 21 of 49
Anonymous
in reply to: Friptzap

'cal
(cur+cur)/2

These work very well behind a button or as a lisp function (without the c:). We have a toolbar for the following:
(cur+cur)/2
(end+end)/2
(cen+cen)/2
(int+int)/2
(mid+mid)/2
(nea+nea)/2

The toolbar acts like an exta osnap bar, I've also added the following:

(CVD) calculates a distance along a vector
That lets you select a point 3 units on or off the endpoint of a line along the line.
(CVT) calculates a percentage of the vector along a vector
Let's you select a point some percentage of a line length on or off a line (0.25 of length from an endpoint)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;
(setq dst 1.0
frc 0.5)
(defun cvd ()
(if (= cal nil)
(arxload "geomcal")
)
(setq pt1 (getpoint "\n Select First Point Of Vector")
pt2 (getpoint "\n Select Second Point Of Vector")
)
(IF (SETQ Ndst (GETdist (STRCAT "Enter Distance along Vector <" (rtos dst) ">: ")))
(setq dst ndst)
)
(cal "pld(pt1,pt2,dst)")
)
(defun cvt ()
(if (= cal nil)
(arxload "geomcal")
)
(setq pt1 (getpoint "\n Select First Point Of Vector")
pt2 (getpoint "\n Select Second Point Of Vector")
)
(IF (SETQ Nfrc (GETdist (STRCAT "Enter Percentage of Distance along Vector <" (rtos frc 2 6) ">: ")))
(setq frc nfrc)
)
(cal "plt(pt1,pt2,frc)")
)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;

toolbar would look a little like:

**TB_CAL_OSNAP
**CAL-OSNAP
ID_CAL_Osnap_0 [_Toolbar("CAL-Osnap", _Top, _Show, 2, 0, 1)]
ID__0 [_Button("CAL MIDPOINT", "midpt.bmp", "ICON_24_BLANK")]none 'cal (cur+cur)/2;
ID__0 [_Button("Cal Vector Distance", "cvd.bmp", "ICON_24_BLANK")]none (cvd)
ID__1 [_Button("Cal Vector Percentage", "cvt.bmp", "ICON_24_BLANK")]none (cvt)
ID__0 [_Button("CAL MEE", "osmee.bmp", "ICON_24_BLANK")]none 'cal mee;
ID__1 [_Button("CAL MCC", "osmcc.bmp", "ICON_24_BLANK")]none 'cal (cen+cen)/2;
ID__2 [_Button("CAL MII", "osmii.bmp", "ICON_24_BLANK")]none 'cal (int+int)/2;
ID__4 [_Button("CENTER OF TRIANGLE", "osctrtri.bmp", "ICON_24_BLANK")]none 'cal (end+end+end)/3;
Message was edited by: OLD-CADaver
Message 22 of 49
Friptzap
in reply to: Friptzap

those both seem so long. I need to try them out though. Maybe my solution is too simple? Could I be missing some finer points? Maybe have it turn certain osnaps on and then reset them back to where they were when it started?

What do you think of that one? I like simple but maybe I left out something needed?

edit: incase it got lost in translation:
(defun c:cs ()
(setq PT1 (getpoint "\nFrom point: "))
(setq PT2 (getpoint "\nTo point: "))
(setq PT3 (polar PT1 (angle PT1 PT2) (/ (distance PT1 PT2) 2.0)))
(command "circle" PT3 6));;this can be anything :)
Message was edited by: Friptzap
Message 23 of 49
Anonymous
in reply to: Friptzap

Make a save settings subroutine and a restore settings subroutine !!!!! Add these (or similar) at beginning of programs to restore settings. These are just examples, you can have less or more in the subroutines. (defun SAVESV () (setq CSV_LN (getvar "CLAYER")) (setq CSV_OS (getvar "OSMODE")) (setq CSV_OM (getvar "ORTHOMODE")) (setvar "OSMODE" 0) (setvar "ORTHOMODE" 0) (setvar "PLINEWID" 0.0) (setvar "CMDECHO" 0) (setvar "BLIPMODE" 0) (setvar "EXPERT" 1) ) (defun RESETSV () (setvar "CLAYER" CSV_LN) (setvar "OSMODE" CSV_OS) (setvar "ORTHOMODE" CSV_OM) ) ;;Example - (defun C:TEST () (SAVESV) (setvar "OSMODE" 1) (setq P1 nil p2 nil) (setq P1 (getpoint "\nPick 1st Point ? ")) (if P1 (setq P2 (getpoint P1 "\nPick 2nd Point ? "))) (if P1 (command "PLINE" P1 P2 "") ) (RESETSV) )
Message 24 of 49
Anonymous
in reply to: Friptzap

Does it work in 3D?
Message 25 of 49
Anonymous
in reply to: Friptzap

;--Subroutine to return midpoint between two 2d or 3d points (defun GETMID (PT1 PT2 /) (if (or (/= (caddr PT1) 0.0) (/= (caddr PT2) 0.0)) (progn ;3dpoint (setq DD (sqrt (+ (expt (- (car PT1) (car PT2)) 2) (expt (- (cadr PT1) (cadr PT2)) 2)))) (setq PT3 (polar PT1 (angle PT1 PT2) (/ DD 2.0))) (setq PT3 (list (car PT3) (cadr PT3) (+ (caddr PT1) (/ (- (caddr PT2) (caddr PT1)) 2.0)))) ) (setq PT3 (polar PT1 (angle PT1 PT2) (/ (distance PT1 PT2) 2.0))) ;2dpoint ) ) I'm sure there might be a more elegant method, but this works for 2d & 3d points Alan Henderson @ A'cad Solutions www.acadsolutions.biz
Message 26 of 49
Friptzap
in reply to: Friptzap

Nice routine Alan covers everything thanks 🙂

No this would work only for 2d. I tested it and it finds a point on the same angle as the line that is half the length of the total line length. But perhaps with some modification it could.

I don't really need it in 3d at the moment. I am sure it would work on any 2d plane though.
Message 27 of 49
Anonymous
in reply to: Friptzap

Kent, I don't know about the new osnap in 2005. But I agree it's important any function/command which tries to return a midpoint needs to deal with running osnaps to be worthwhile. Here's one solution. It can be called transparently within any standard command, but not while inside another lisp function. Joe Burke (defun C:MidPoint2 (/ *Error* Osm p1 p2) (defun *Error* (Msg) (cond ((or (not Msg) (member Msg '("console break" "Function cancelled" "quit / exit abort")))) ((princ (strcat "\nError: " Msg))) ) ;cond (setvar "osmode" Osm) (princ) ) (setq Osm (getvar "osmode")) (initget 1) (setq p1 (getpoint "Pick first point for midpoint: ")) (initget 33) (setq p2 (getpoint p1 "\nPick second point for midpoint: ")) (setvar "osmode" 0) (command (mapcar '/ (mapcar '+ p1 p2) '(2.0 2.0 2.0))) (*Error* nil) ) ;end "Kent Cooper, AIA" wrote in message news:408e8c20_2@newsprd01... > It looks like AutoDesk has realized the need, finally (probably a wish-list > item). I'd be interested to know (we're not up to 2005 yet) whether it > overcomes the running-Osnap "problem" with the MidOf2 thing I posted. That > is, if (for example) you have running Endpoint Osnap on, and the calculated > midpoint-between-2 ends up in Osnap range of something with endpoints, does > it still return the calculated location, or does it go to the nearest > endpoint? > > Kent Cooper, AIA > > "David Allen" wrote... > > there is the 2005 object snap m2p (middle between 2 points) > >
Message 28 of 49
Anonymous
in reply to: Friptzap

I think it would be far simpler to take the shorter MidOf2 thing and put a "None" in it, which IS, in effect, setting Osmode to zero for one point selection. I just have to dust off some old brain cells to put it in the right place in the routine. Kent Cooper, AIA "Joe Burke" wrote ... > I don't know about the new osnap in 2005. But I agree it's important any > function/command which tries to return a midpoint needs to deal with running osnaps > to be worthwhile. Here's one solution. It can be called transparently within any > standard command, but not while inside another lisp function. > > (defun C:MidPoint2 (/ *Error* Osm p1 p2) > (defun *Error* (Msg) > (cond > ((or (not Msg) > (member Msg '("console break" > "Function cancelled" > "quit / exit abort")))) > ((princ (strcat "\nError: " Msg))) > ) ;cond > (setvar "osmode" Osm) > (princ) > ) > > (setq Osm (getvar "osmode")) > (initget 1) > (setq p1 (getpoint "Pick first point for midpoint: ")) > (initget 33) > (setq p2 (getpoint p1 "\nPick second point for midpoint: ")) > (setvar "osmode" 0) > (command (mapcar '/ (mapcar '+ p1 p2) '(2.0 2.0 2.0))) > (*Error* nil) > ) ;end
Message 29 of 49
Anonymous
in reply to: Friptzap

Good point, didn't think of that. I typically don't run into that a lot, but when I do, I would probably just draw a line from diagonal to the other and snap to the midpoint (which is what we are trying to avoid, right?!) :-) I guess i would also use the 'cal mee ... that is if i remember it (I seem to always forget that it exists) -- Chris Holmes Structural Tech/Cadd Manager Facilities Division The Schemmer Associates, Inc. "Kent Cooper, AIA" wrote in message news:408eaea2$1_3@newsprd01... > (... and if the square is drawn orthogonally.) > > Kent Cooper, AIA > > "Chris Holmes" wrote ... > > The method I usually use is to object track from the midpoint of one side > > and the midpoint of another side (see attached image) and you will get an > > intersection where they meet. This is of course only useful if you are > > drawing a line from that point, or want to select that point as the base > > point when moving/copying. > >
Message 30 of 49
Anonymous
in reply to: Friptzap

That requires picking four points instead of two, but if you use it regularly in situations where the square is a polyline, you could write something that would find those four points for you, in which case you could do it with ONE pick (select the polyline), rather than two. Kent Cooper, AIA "PG." wrote... > (inters pt1 pt3 pt2 pt4) > where > pt1 and pt3: end points of diagnol1 > pt2 and pt4: end points of diagnol2
Message 31 of 49
Anonymous
in reply to: Friptzap

I was pretty sure that (angle pt1 pt2) would only return the angle IN the XY plane.

If you use 'CAL it works in all spaces.

We have a button
'cal;(cur+cur)/2;
that finds the "true" midpoint between any to osnaps.

On that same toolbar, we use several other buttons for (cen+cen)/2 (nea+nea)/2 etc.

If you'll look above at an earlier post mine you'll find two lisp routines that will find points along a line, based on either distance or a percentage of the line length (like 1/4).
Message 32 of 49
Anonymous
in reply to: Friptzap

Kent, Agreed. (defun c:MP2 ( / p1 p2 ) (initget 1) (setq p1 (getpoint "Pick first point for midpoint: ")) (initget 33) (setq p2 (getpoint p1 "\nPick second point for midpoint: ")) (command "non" (mapcar '/ (mapcar '+ p1 p2) '(2.0 2.0 2.0))) ) ;end Joe Burke "Kent Cooper, AIA" wrote in message news:408fb624_1@newsprd01... > I think it would be far simpler to take the shorter MidOf2 thing and put a > "None" in it, which IS, in effect, setting Osmode to zero for one point > selection. I just have to dust off some old brain cells to put it in the > right place in the routine. > > Kent Cooper, AIA > > "Joe Burke" wrote ... > > I don't know about the new osnap in 2005. But I agree it's important any > > function/command which tries to return a midpoint needs to deal with > running osnaps > > to be worthwhile. Here's one solution. It can be called transparently > within any > > standard command, but not while inside another lisp function. > > > > (defun C:MidPoint2 (/ *Error* Osm p1 p2) > > (defun *Error* (Msg) > > (cond > > ((or (not Msg) > > (member Msg '("console break" > > "Function cancelled" > > "quit / exit abort")))) > > ((princ (strcat "\nError: " Msg))) > > ) ;cond > > (setvar "osmode" Osm) > > (princ) > > ) > > > > (setq Osm (getvar "osmode")) > > (initget 1) > > (setq p1 (getpoint "Pick first point for midpoint: ")) > > (initget 33) > > (setq p2 (getpoint p1 "\nPick second point for midpoint: ")) > > (setvar "osmode" 0) > > (command (mapcar '/ (mapcar '+ p1 p2) '(2.0 2.0 2.0))) > > (*Error* nil) > > ) ;end > >
Message 33 of 49
Friptzap
in reply to: Friptzap

looks simple I will have to try it out.
Message 34 of 49
Anonymous
in reply to: Friptzap

Lots of good suggestions have been posted for the answer to your question, but just for enlightenment on some of what's not correct about your attempt below: Your third-to-last line is probably better handled by other ways that have been suggested, which, as you wanted, don't require drawing the line. But for future reference, one error is that something in quotes following the getpoint is going to be used as a prompt to the user, not as an Osnap call. The "l" should be some variety of (entlast) or something, but I'm not sure exactly how to spell that out without some help-research and/or testing. And you're missing a closing parenthesis. The error in your last line (assuming mpt is properly set) is the exclamation point. That's necessary to use a setq value as an individual entry in a macro routine (outside any parentheses), or if you were to type it in response to a prompt, but should not be used inside a (command) operation -- that will evaluate the expression mpt without the leading exclamation point. Keep working at it! That's the only way to find out how things work. The further you go with this stuff, the more sensitive you become to the need to spell everything absolutely correctly, have the right number of parentheses, and spaces, and so on. Computers are very fast and powerful, but NOT smart -- they're really quite stupid, and they just don't have the slightest idea what you're talking about if you don't say it just they way they want to hear it. Kent Cooper, AIA "Friptzap" wrote ... > I was trying to do something like this. (but would rather forgo the line itself) : > > (defun c:cs () > (setq PT1 (getpoint "\nFrom point: ")) > (setq PT2 (getpoint "\nTo point: ")) > (command "line" PT1 PT2 "") > (setq mpt (getpoint "mid" "l") < this is obviously not correct > (command "erase" "l") > (command "circle" !mpt 6)) < this is a little off too
Message 35 of 49
Friptzap
in reply to: Friptzap

the revised one looks like this now 🙂

(defun c:cs ()
(setq PT1 (getpoint "\nFrom point: "))
(setq PT2 (getpoint "\nTo point: "))
(setq PT3 (polar PT1 (angle PT1 PT2) (/ (distance PT1 PT2) 2.0)))
(command "circle" PT3 6));;this can be anything 🙂
Message 36 of 49
btlsp2014
in reply to: Friptzap

hi, Bill here(BTLSP)

I make the most use of vectors I can so the centroid of a square:

 

(defun csq

(setq

 

i (list 1 0)

j (list 0 1)

 

ul (entget "Point of Square upper-left corner")

d (getreal "Square dimension")

 

centroid

   (vadd

      (vadd (car ul)(cadr ul))

      (vadd (vmultiply m i) (vmultiply m j) )

)

)

 

 

(defun vadd (u v)

   (list (+ (car u) (cad v)) (+ (cadr u) (cadr v))

)

 

 

(defun vmultiply (m v)

   (list (* m (car v)) (* m (cadr v)))

)

 

 

 

 

Message 37 of 49
mid-awe
in reply to: btlsp2014

Here is a spooky ghost. This thread was dead for a decade before you resurrected it.
Message 38 of 49
Lee_Mac
in reply to: btlsp2014

@Bill, I would recommend you look into the mapcar function Smiley Wink

Message 39 of 49
btlsp2014
in reply to: mid-awe

Hello awe,  occasionally I will do the unexpected.  What a simple task to find the centroid of a square on the paper.  Lori, she was my lovely supervisor at SMI-Joist would use the most elegant ways to get what she wanted always nothing I would have done.  But Jeri had a terrible time  grabing all she wanted to transpose and leave the remainder untouched.  That was then.  I'm 63 and still having fun I'm not sure what era is the most fun.  I did have a lot more energy back then and desire to explore the next thing but it's true today too.  In 11 days I am heading to Cheyenne Wyoming and plunge into a new world.  I've been in Paridise here in Portland now for 7 years.

 

I always knew Vectors( https://www.youtube.com/watch?v=cQqAVWKFoJ8 ) had what I needed to perform miracles but it took 3 semesters of Calculus to get there.  I handled a t-square and triangles like a juggler but the computer, well we all know........  Then Web Sites intrigued and since I was already programming I learned html/css and now I'm looking at doing everything in html in a Web page.  Of my 200+ Autolisp routines the spiral stair case was challenging, and the roof slope with facia and neither of the 2 could have been done without the one I created in 2 hours as a reply to a request from a fellow student of the instructor one night for a way to extend like "Extend" in 2d, a 3dface to a 3dface.  You can get it from the Library of Congress where I registered it in 1993.  It's 1 page long and uses everything from the vector functions, all of them, and the modulus to look for the right line to use to form the bases for the extension.

 

 

...Bill

 

for my blog:  Lonely Road -->  http://btlsp2000.tripod.com/a-inkwell/inkwell.html

 

 

 

 

 

 

 

 

Message 40 of 49
btlsp2014
in reply to: Lee_Mac

Okay, I surrender, PTP is pretty good, the 3dface to 3dface routine I did in 2 hours in class one night in 1993.  It was an Architectural night class.  I knew little about Architecture but was just getting into programming after buying a Timex-Sinclair 2000 computer for $44 and making an extremely simple racing car game that required me to write 200 lines of code...........................................Bill

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report

”Boost