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

Discussion in 'AutoCAD' started by Friptzap, Apr 27, 2004.

  1. Friptzap

    Friptzap Guest

    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?
     
    Friptzap, Apr 27, 2004
    #1
  2. [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

    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?
     
    Kent Cooper, AIA, Apr 27, 2004
    #2
  3. Friptzap

    Scott Davis Guest

    Why not just use the CAL command with MEE (Midpoint between to endpoints)?

    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?
     
    Scott Davis, Apr 27, 2004
    #3
  4. Friptzap

    Walt Engle Guest

    That's my preference - simple.
     
    Walt Engle, Apr 27, 2004
    #4
  5. 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
     
    Kent Cooper, AIA, Apr 27, 2004
    #5
  6. 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

    ...
     
    Kent Cooper, AIA, Apr 27, 2004
    #6
  7. Friptzap

    David Allen Guest

    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
     
    David Allen, Apr 27, 2004
    #7
  8. [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
     
    Kent Cooper, AIA, Apr 27, 2004
    #8
  9. Friptzap

    Scott Davis Guest

    True, but the original posting asked how to find the center of a square. CAL
    works for this with no customization needed.
     
    Scott Davis, Apr 27, 2004
    #9
  10. 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: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
     
    Kent Cooper, AIA, Apr 27, 2004
    #10
  11. 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

    ...
     
    Kent Cooper, AIA, Apr 27, 2004
    #11
  12. Friptzap

    PG. Guest

    (inters pt1 pt3 pt2 pt4)
    where
    pt1 and pt3: end points of diagnol1
    pt2 and pt4: end points of diagnol2

    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?
     
    PG., Apr 27, 2004
    #12
  13. (... and if the square is drawn orthogonally.)

    Kent Cooper, AIA

    ...
     
    Kent Cooper, AIA, Apr 27, 2004
    #13
  14. Friptzap

    btlsp Guest

    btlsp, Apr 27, 2004
    #14
  15. Friptzap

    Friptzap Guest

    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
     
    Friptzap, Apr 27, 2004
    #15
  16. To find midpoint between 2 points
    (setq PT3 (polar PT1 (angle PT1 PT2) (/ (distance PT1 PT2) 2.0)))
     
    Alan Henderson, Apr 27, 2004
    #16
  17. Friptzap

    Friptzap Guest

    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 :)
     
    Friptzap, Apr 27, 2004
    #17
  18. 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
    --------------------------------------------------------------------


    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?
     
    Daniel J. Altamura, R.A., Apr 27, 2004
    #18
  19. Friptzap

    OLD-CADaver Guest

    'cal <enter>
    (cur+cur)/2 <enter>

    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;
     
    OLD-CADaver, Apr 27, 2004
    #19
  20. Friptzap

    Friptzap Guest

    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?
     
    Friptzap, Apr 27, 2004
    #20
Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.