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
1682 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 2 of 49
Anonymous
in reply to: Friptzap

There were some previous posts about this subject.. Search on CENTROID
Message 3 of 49
Anonymous
in reply to: Friptzap

[MidOf2]^P(list (/ (+ (car (setq A (getpoint "1st POINT: "))) (car (setq B (getpoint "2nd POINT: ")))) 2) (/ (+ (cadr A) (cadr B)) 2)) ^P Works as an OSNAP type setting to find the halfway point between ANY two locations. (I have it added into Osnap pick lists in various places). Just make sure running OSNAP is off (or build a NONE into it, or something) in case the resulting point is within OSNAP range of any drawing entities. You can invoke OSNAP for the 1st & 2nd Point selections. Kent Cooper, AIA "Friptzap" wrote ... > 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?
Message 4 of 49
Anonymous
in reply to: Friptzap

Why not just use the CAL command with MEE (Midpoint between to endpoints)? "Friptzap" wrote in message news:18053516.1083077662624.JavaMail.jive@jiveforum2.autodesk.com... > 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?
Message 5 of 49
Anonymous
in reply to: Friptzap

That's my preference - simple.
Message 6 of 49
Anonymous
in reply to: Friptzap

And on a related subject, here's a way to OSNAP to any FRACTIONAL distance between two points (e.g. in the example usage below, 3/5 of the way from one point to another), in Pop & Screen Menu format but you could probably make it work other ways. To get to it, in A2K (probably similar in other releases), add this (and, while you're at it, the MidOf2 one in my previous post) into the ***POP0 menu section's **SNAP area: ID_OsnapFrac [Fractional]$S=FRACSNAP Or add this anywhere else (e.g. in a Tablet Menu Osnap area): $S=FRACSNAP (or in some other Pop-down Menu): [FracSnap]$S=FRACSNAP Put these lines anywhere else in the Menu file's ***SCREEN area: **FRACSNAP 3 [but change the 3 here for how far down the Screen it should start, as appropriate to your Screen Menu setup] [Fraction] [of a] [distance] [snap:] [SET I/J]^C^P(setq I (getint "Enter Numerator I: ")) \(setq J (getint "Enter Denominator J: ")) \^P [USE I/J]^P(polar (setq A (getpoint "1st POINT: ")) (angle A (setq B (getpoint "2nd POINT: "))) (/ (* (distance A B) I) J)) $S= ^P When you want to use it, you invoke it from wherever, and the Screen Menu will show this: Fraction of a distance snap: SET I/J USE I/J Pick "SET I/J" to set the ratio you want (e.g. answer "3" to the first prompt and "5" to the second), then within any command looking for a location, pick "USE I/J", select two locations (you can use Osnap to do so), and it will return the location that is (in the sample case) 3/5 of the way from the first to the second. It will remember the fraction as long as you're in the same editing session in the same drawing, so you can use the "USE I/J" repeatedly without picking "SET I/J" each time. You can even pick "SET I/J" within a command, and it will set the ratio for you while the command waits. You can have running Osnap on when you select the first Point, but it's a good idea to turn it off (F3) before you pick the second (then invoke the Osnap mode you want explicitly for that point, if you need to), unless the resultant point will be clear of Osnap-able entities. It calculates the fractional-distance point immediately after you pick the second point, so if running Osnap is on, it will use it if it can. (I keep meaning to put "None" in here or something, to fix that, but haven't gotten around to figuring out just where to put it, etc. Maybe if I used it a lot more often than I do, I'd have the incentive....) This requires the numerator and denominator to be integers, but you could probably change the "getint"s to "getreal"s and then you could, say, snap to a point that's pi 137ths of the way from one place to another (not that I can imagine why anyone would WANT to, but anyway....). Kent Cooper, AIA
Message 7 of 49
Anonymous
in reply to: Friptzap

CAL will do that, but your example is limited. It would work for Friptzap's specific case, but only looks for ENDpoints to find a midpoint between; it won't calculate the midpoint between anything else except with a lot of typing in the coding for it, which seems comparatively cumbersome, e.g. typing in (cen+end)/2 instead of just picking a MidOf2 Osnap function. My first response to the original post figures the midpoint between ANY two locations, selected in any way that you can select them. Kent Cooper, AIA "Scott Davis" wrote... > Why not just use the CAL command with MEE (Midpoint between to endpoints)?
Message 8 of 49
Anonymous
in reply to: Friptzap

there is the 2005 object snap m2p (middle between 2 points) Friptzap |>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? David
Message 9 of 49
Anonymous
in reply to: Friptzap

[I should give some credit here.... The Mid-of-2 and Fraction-of-a-distance Snap routines I posted are not originally my work. We've had them around for so long, it didn't even occur to me at first. I've tweaked them a little, and adjusted formatting for different Menu locations, and so on, but they both came originally from the company through which we first bought AutoCAD way back in 1985, an outfit in New York called MiCAD Systems. I have no idea whether they still exist, nor of the name(s) of whoever may have worked out those original routines, but I've valued the routines enough to transfer them along into our Menus for every upgrade. Now that it's too late, I hope MiCAD wouldn't mind the rest of the world knowing about them.] Kent Cooper, AIA
Message 10 of 49
Anonymous
in reply to: Friptzap

True, but the original posting asked how to find the center of a square. CAL works for this with no customization needed. "Kent Cooper, AIA" wrote in message news:408e829d$1_1@newsprd01... > CAL will do that, but your example is limited. It would work for Friptzap's > specific case, but only looks for ENDpoints to find a midpoint between; it > won't calculate the midpoint between anything else except with a lot of > typing in the coding for it, which seems comparatively cumbersome, e.g. > typing in > > (cen+end)/2 > > instead of just picking a MidOf2 Osnap function. My first response to the > original post figures the midpoint between ANY two locations, selected in > any way that you can select them. > > Kent Cooper, AIA > > "Scott Davis" wrote... > > Why not just use the CAL command with MEE (Midpoint between to endpoints)? > >
Message 11 of 49
Anonymous
in reply to: Friptzap

You're absolutely right, of course. I just always prefer the broader solution that will do for not only for the specific case but for lots of other cases, too. And I have to confess that my first answer to your initial question: > > > Why not just use the CAL command with MEE (Midpoint between to endpoints)? > > > was "Because I didn't know it was there!" (Or maybe I used to know long ago, but had forgotten.) I'm sure there are others whose eyes you have opened up to an area to explore further, which is one of the great features of forums like this. Kent Cooper, AIA "Scott Davis" wrote ... > .... the original posting asked how to find the center of a square. CAL > works for this with no customization needed.
Message 12 of 49
Anonymous
in reply to: Friptzap

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 13 of 49
Anonymous
in reply to: Friptzap

(inters pt1 pt3 pt2 pt4) where pt1 and pt3: end points of diagnol1 pt2 and pt4: end points of diagnol2 "Friptzap" wrote in message news:18053516.1083077662624.JavaMail.jive@jiveforum2.autodesk.com... > 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?
Message 14 of 49
Anonymous
in reply to: Friptzap

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 15 of 49
Anonymous
in reply to: Friptzap

(... 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 16 of 49
btlsp
in reply to: Friptzap

See http://www.btlsp2000.com/BXT.LSP
!e should get the Square Center.
Message 17 of 49
Friptzap
in reply to: Friptzap

too much input lol Number 5 is on overload.

I was not expecting so many ways I will have to check them all out thanks for the input.

PLEASE explain cal? I type that I get calculator expression 🙂 (mee?)

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 18 of 49
Anonymous
in reply to: Friptzap

To find midpoint between 2 points (setq PT3 (polar PT1 (angle PT1 PT2) (/ (distance PT1 PT2) 2.0))) "Friptzap" wrote in message news:4180234.1083095939290.JavaMail.jive@jiveforum1.autodesk.com... > too much input lol Number 5 is on overload. > > I was not expecting so many ways I will have to check them all out thanks for the input. > > PLEASE explain cal? I type that I get calculator expression 🙂 (mee?) > > 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 19 of 49
Friptzap
in reply to: Friptzap

excellent idea 🙂 that makes this work even better no line now 🙂

thanks alot. here it is for anyone needing it:

(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 20 of 49
Anonymous
in reply to: Friptzap

Here is one that I wrote a LONG time ago. I still use it often. It works with squares, rectangles, or any regular polygon. Whenever AutoCAD asks for a point you can respond with POLYSNAP or 'POLYSNAP to make it transparent. ;*** OBJECT SNAP TO FIND CENTER POINT OF POLYGON *** (defun c:polysnap () (setq pe (entget (car (entsel "\nSelect polygon: "))) count 0 ptcount 0 ) (if (= (cdr (assoc 0 pe)) "LWPOLYLINE") (progn (repeat (length pe) (if (= (car (nth count pe)) 10) (setq ptcount (1+ ptcount)) ) (setq count (1+ count)) ) (if (< ptcount 3) (prompt "\nPolygon must have at least 3 sides.\n") (progn (if (= (rem ptcount 2) 0) (setq pnum "even") (setq pnum "odd") ) (setq plist (member (assoc 10 pe) pe) pt1 (cdr (nth 0 plist)) pt2 (cdr (nth 4 plist)) ) (findpts plist) (if (= pnum "even") (setq pt3 (nth (- (+ (/ ptcount 2) 1) 1) ptlist) pt4 (nth (- (+ (/ ptcount 2) 2) 1) ptlist) int (inters pt1 pt3 pt2 pt4) ) ) (if (= pnum "odd") (progn (setq pt31 (nth (- (/ (+ ptcount 1) 2) 1) ptlist) pt32 (nth (- (+ (/ (+ ptcount 1) 2) 1) 1) ptlist) pt3 (polar pt31 (angle pt31 pt32) (/ (distance pt31 pt32) 2)) pt41 (nth (- (+ (/ (+ ptcount 1) 2) 1) 1) ptlist) ) (if (= ptcount 3) (setq pt42 pt1) (setq pt42 (nth (- (+ (/ (+ ptcount 1) 2) 2) 1) ptlist)) ) (setq pt4 (polar pt41 (angle pt41 pt42) (/ (distance pt41 pt42) 2)) int (inters pt1 pt3 pt2 pt4) ) ) ) (command int) ) ) ) (prompt "\nThis is not a lwpolyline.\n") ) (princ) ) (defun findpts (plist /) (setq count 1) (setq ptlist (list (cdr (nth 0 plist)))) (repeat (length plist) (if (= (car (nth count plist)) 10) (setq ptlist (cons (cdr (nth count plist)) ptlist)) ) (setq count (+ count 1)) ) (setq ptlist (reverse ptlist)) ) -- Daniel J. Altamura, R.A. Altamura Architectural Consulting and SoftWorx, Autodesk Authorized Developer -------------------------------------------------------------------- "Friptzap" wrote in message news:18053516.1083077662624.JavaMail.jive@jiveforum2.autodesk.com... > 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?

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

Post to forums  

Autodesk Design & Make Report

”Boost