addition on centerpoint

Discussion in 'AutoCAD' started by jlspartz, Feb 24, 2005.

  1. jlspartz

    jlspartz Guest

    I'm trying to do an equation on a centerpoint. I have the centerpoint as the variable "center" and then I am trying to extract the x and y which I do like this:

    (setq x (car center))
    (setq y (cadr center))

    and then use the x and y in my equation. Here's what I get from the user and then the equation to modify the centerpoint:

    (cond
    ((= gridsize "1")(setq defshift-h "6"))
    ((= gridsize "2")(setq defshift-h "12"))
    ((= gridsize "3")(setq defshift-h "24"))
    ((= gridsize "4")(setq defshift-h "12"))
    )
    (setq shift-h (getreal (strcat "\nEnter distance to shift grid horizontally <" defshift-h ">: ")))
    (if (not shift-h)(setq shift-h defshift-h))
    (setq center ((+ x shift-h ) y))
    (command "hatchsetorigin" "last" "" center)

    Any suggestions?
     
    jlspartz, Feb 24, 2005
    #1
  2. jlspartz

    Josh Guest

    this line: (setq center ((+ x shift-h ) y))

    should be: (setq center (list (+ x shift-h ) y))


    the variable "center" and then I am trying to extract the x and y which I do
    like this:
    and then the equation to modify the centerpoint:
     
    Josh, Feb 24, 2005
    #2
  3. jlspartz

    jlspartz Guest

    It's still not working. It just quits out of the command (does the error function) without any string output on what the error is, when it gets to this line:

    (setq center (list (+ x shift-h ) y))
     
    jlspartz, Feb 24, 2005
    #3
  4. jlspartz

    T.Willey Guest

    If the user hits enter, then you are trying to add a string and number. Change this
    (if (not shift-h)(setq shift-h (atoi defshift-h)))
    See it that helps.

    Tim
     
    T.Willey, Feb 24, 2005
    #4
  5. jlspartz

    jlspartz Guest

    Here's something that might help.

    Here's is my input:

    (setq x (car center))
    (setq y (cadr center))
    (setq newcenter (list '(+ x shift-h ) 'y))
    (princ center)
    (princ newcenter)
    (princ x)
    (princ y)

    Here's my output:

    (2016.0 1440.0)((+ X SHIFT-H) Y)2016.01440.0

    You can see I got center and the x and y values, but newcenter is reading as ((+ X SHIFT-H) Y) and not the calculated values.
     
    jlspartz, Feb 24, 2005
    #5
  6. jlspartz

    jlspartz Guest

    So, does that mean my other string:

    (setq newcenter (list '(+ x shift-h ) 'y))

    Needs to be:

    (setq newcenter (list '(+ (atoi x)(atoi shift-h)) '(atoi y)))

    ?
     
    jlspartz, Feb 24, 2005
    #6
  7. The quote (') marks in your list cause the expressions to be returned
    WITHOUT being evaluated (look in the AutoLisp Reference under "quote").
    This will try to set newcenter to a point whose x coordinate is literally (+
    x shift-h ) and whose y coordinate is literally y, rather than the numerical
    values calculated from the first and assigned to the second. It can do
    that, of course (that's the "value" of newcenter you mention), but it won't
    be able to use that as a point, which needs to be a list of two or three
    numerical values. Try it without using ' in there.
     
    Kent Cooper, AIA, Feb 24, 2005
    #7
  8. jlspartz

    jlspartz Guest

    Ok, I changed both lines, and the shift-h variable is coming out as 12 now when I princ it. But, my newcenter variable is still coming out text instead of number. When I princ it, it reads:

    ((+ (ATOI X) (ATOI SHIFT-H)) (ATOI Y))
     
    jlspartz, Feb 24, 2005
    #8
  9. Because of the quotes (see my reply on the other sub-thread).
     
    Kent Cooper, AIA, Feb 24, 2005
    #9
  10. jlspartz

    T.Willey Guest

    When you grab you "x" and "y" there are real numbers.
    Here
    (setq shift-h (getreal (strcat "\nEnter distance to shift grid horizontally <" defshift-h ">: ")))
    (if (not shift-h)(setq shift-h defshift-h))
    (setq center ((+ x shift-h ) y))

    Is where you have to make sure you get a real number, now if they hit enter, then it defaults to what "defshift-h" is, and you set that to a string. So you just have to make sure that if enter is hit you get a number returned. That is why I posted before, because you were not getting a number.

    Also you want to remove the quote ( ' ) before the y here
    (setq newcenter (list '(+ x shift-h ) 'y))

    This will work
    (setq newcenter (list '(+ x shift-h ) y))
    If "shift-h" is a number.

    Hope that helps.
    Tim
     
    T.Willey, Feb 24, 2005
    #10
  11. And another little thing....
    You need to set values to defshift-h without the double-quotes, because with
    them, it sets that variable to a text string, rather than a numerical value.
    Do
    And make sure the gridsize variable is a text string if you're going to
    check its value with double-quotes. If it's a numerical value, remove the
    double-quotes from that equality test, too.
     
    Kent Cooper, AIA, Feb 24, 2005
    #11
  12. jlspartz

    jlspartz Guest

    Thanks Kent and Tim! You guys are great!
     
    jlspartz, Feb 24, 2005
    #12
  13. jlspartz

    jlspartz Guest

    It seems to work fine within the quotes right now.
     
    jlspartz, Feb 24, 2005
    #13
  14. jlspartz

    Tom Smith Guest

    Any suggestions?

    You seem to have gotten this squared away, but to my eye it's unnecessarily
    complicated to bust the point into x-y components, then work on them
    separately, then put the point list back again. I'm surprised nobody
    suggested operating on the point variable directly. It's simpler, and
    suggests a more generalized way of structuring your program.

    One way to transpose a point is with the polar function. To shift "center"
    horizontally by "shift" amount:

    (setq center (polar center 0 shift))

    Or to shift it vertically:

    (setq center (polar center (/ pi 2.0) shift))

    If this is a repetitive calc I'd set a constant to avoid repeating the (/ pi
    2.0) division.

    To shift left or down instead of right or up, you can use the same two
    angles, but with negative shift amounts. Note that there's no difference in
    transposing horizontally vs vertically, except the angle. Implying that the
    point shift should be handled by a single function, rather than by
    considering horizontal and vertical shifts as completely separate cases:

    (setq center (polar center <angle> shift)) ;where <angle> is in radians

    You can also do any kind of arithmetic on a point by using mapcar, since a
    point variable is nothing but a list. To transpose "center" horizontally or
    vertically by "shift" amount:

    (setq center (mapcar '+ center (list shift 0))) ;horiz
    (setq center (mapcar '+ center (list 0 shift))) ;vert

    Again the shift can be positive or negative. And again you can see this as a
    general algorith for handling both horizontal and vertical transpositions:

    (setq center (mapcar '+ center <displacement>)) ;where displacement is a
    point-like list

    Your program will shrink in length and complexity if you think of
    point-shifting as a single general operation, with variables to accomodate
    the amount and direction.
     
    Tom Smith, Feb 25, 2005
    #14
  15. jlspartz

    jlspartz Guest

    Cool, I put the mapcar option in to use. Works good! I thought there must have been some way to add to a list easily. I might also put the displacement option in their so the person can pick a shifting distance (horizontal and vertical at the same time).
     
    jlspartz, Feb 28, 2005
    #15
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.