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

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

  1. 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)
    )
     
    Alan Henderson, Apr 27, 2004
    #21
  2. Friptzap

    OLD-CADaver Guest

    Does it work in 3D?
     
    OLD-CADaver, Apr 28, 2004
    #22
  3. ;--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
     
    Alan Henderson @ A'cad Solutions, Apr 28, 2004
    #23
  4. Friptzap

    Friptzap Guest

    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.
     
    Friptzap, Apr 28, 2004
    #24
  5. Friptzap

    Joe Burke Guest

    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
     
    Joe Burke, Apr 28, 2004
    #25
  6. 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

    ...
     
    Kent Cooper, AIA, Apr 28, 2004
    #26
  7. Friptzap

    Chris Holmes Guest

    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, Apr 28, 2004
    #27
  8. 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

    ...
     
    Kent Cooper, AIA, Apr 28, 2004
    #28
  9. Friptzap

    OLD-CADaver Guest

    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).
     
    OLD-CADaver, Apr 28, 2004
    #29
  10. Friptzap

    Joe Burke Guest

    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
     
    Joe Burke, Apr 28, 2004
    #30
  11. Friptzap

    Friptzap Guest

    looks simple I will have to try it out.
     
    Friptzap, Apr 28, 2004
    #31
  12. 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


    ...
     
    Kent Cooper, AIA, Apr 30, 2004
    #32
  13. Friptzap

    Friptzap Guest

    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 :)
     
    Friptzap, May 5, 2004
    #33
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.