Lisp Challenge

Discussion in 'AutoCAD' started by Michael Viscetto, Sep 23, 2004.

  1. I have a lisp routine that generates text in surveyors units for site plans.
    the problem (if you want to call it that) is that using ACAD surveyor units results in;
    for example: "N 89d 8' 10" E opposed to N 89d 08' 10" E.
    There is no zero as a place holder if the number is less than ten.
    Is there any way to modify the lisp routine so that it will add the zero?
    Even if it is "N 00d 00' 00" W"
    Below is the routine.

    Thank you
    Michael
    And thanks to Donnia for writing it.




    ;*******************************************************************
    ;Donnia M. Tabor-Hanson, Sept. 20, 2004 *
    ;This program will allow the user to pick the end points of a *
    ;line to get printout of the line's length and distance. *
    ;Currently it is set to decimal length and surveyors angles. *
    ;A little manipulation of the routine will allow any type of units *
    ;to be used. *
    ;*******************************************************************
    (defun C:TAG ()
    (setq CO (getvar "OSMODE")) ;get the current value of osnap
    (setvar "OSMODE" 1) ;sets osnap to endpoint
    (setq P1 (getpoint "\nSelect first endpoint of line: "))
    (setq P2 (getpoint "\nSelect other endpoint of line: "))
    (setq D (distance P1 P2)) ;sets the value of the distance
    (setq D2 (/ D 12))
    (setq DI (rtos D2 2 2)) ;changes to a string-2 decimal places
    (setq A (angle P1 P2)) ;defines the angle between pickpoints
    (setq ANG (* A (/ 180 pi))) ;converts the points angle for input
    (setq ANT (angtos A 4 4)) ;converts angle information to text
    (setq ANT2 (vl-string-subst "%%d" "d" ANT))
    (setvar "OSMODE" 0) ;sets osnap to NONE
    (command "TEXT" "C" pause "24" ANG ANT2) ;executes text
    command
    (command "TEXT" "" (strcat DI "' "))
    ;executes text command
    (setvar "OSMODE" CO) ;resets osnap to original setting
    );defun
     
    Michael Viscetto, Sep 23, 2004
    #1
  2. Michael Viscetto

    Kelie Guest

    Try the modified code below. Others probably have better ways.

    (defun C:TAG (/ CO P1 P2 D D2 DI A
    ANG ANT ANT2 beginStr degree minute second
    endStr
    )
    (setq CO (getvar "OSMODE")) ;get the current value of osnap
    (setvar "OSMODE" 1) ;sets osnap to endpoint
    (setq P1 (getpoint "\nSelect first endpoint of line: "))
    (setq P2 (getpoint "\nSelect other endpoint of line: "))
    (setq D (distance P1 P2)) ;sets the value of the distance
    (setq D2 (/ D 12))
    (setq DI (rtos D2 2 2)) ;changes to a string-2 decimal places
    (setq A (angle P1 P2)) ;defines the angle between pickpoints
    (setq ANG (* A (/ 180 pi))) ;converts the points angle for input
    (setq ANT (angtos A 4 4)) ;converts angle information to text
    (setq ANT2 (vl-string-subst "%%d" "d" ANT))
    (setq beginStr (substr ant2 1 2)
    degree (atoi (substr ANT2 3))
    minute (atoi (substr ANT2 (+ (vl-string-search "%%" ANT2) 4)))
    second (atoi (substr ANT2 (+ (vl-string-search "'" ANT2) 2)))
    endStr (substr ant2 (- (strlen ANT2) 1))
    ANT2 (strcat beginStr
    (2-LetterStr degree)
    "%%d"
    (2-LetterStr minute)
    "'"
    (2-LetterStr second)
    "\""
    endStr
    )
    )
    (setvar "OSMODE" 0) ;sets osnap to NONE
    (command "TEXT" "C" pause "24" ANG ANT2) ;executes text
    command
    (command "TEXT" "" (strcat DI "' "))
    ;executes text command
    (setvar "OSMODE" CO) ;resets osnap to original setting
    ) ;d


    (defun 2-LetterStr (number)
    (strcat (if (< number 10)
    "0"
    ""
    )
    (itoa number)
    )
    )
     
    Kelie, Sep 23, 2004
    #2
  3. That works AWESOME!
    Great Job!!!
    thank you.
    wait...
    There seems to be a problem when the line is perfectly vertical or horizontal. I receive the following error:
    error: bad argument type: numberp: nil

    I am not sure if this is such a big issue because I don't know how often i will be drawing a site plan with lines that are perfectly north / south or east / west. But if you could check it out I would appreciate it.

    Thanks
    Michael
     
    Michael Viscetto, Sep 23, 2004
    #3
  4. Michael Viscetto

    Kelie Guest

    You're welcome. See if this works for you. The subroutine is the same.

    (defun C:TAG (/ CO P1 P2 D D2 DI A
    ANG ANT ANT2 beginStr degree minute second
    endStr
    )
    (setq CO (getvar "OSMODE")) ;get the current value of osnap
    (setvar "OSMODE" 1) ;sets osnap to endpoint
    (setq P1 (getpoint "\nSelect first endpoint of line: "))
    (setq P2 (getpoint "\nSelect other endpoint of line: "))
    (setq D (distance P1 P2)) ;sets the value of the distance
    (setq D2 (/ D 12))
    (setq DI (rtos D2 2 2)) ;changes to a string-2 decimal places
    (setq A (angle P1 P2)) ;defines the angle between pickpoints
    (setq ANG (* A (/ 180 pi))) ;converts the points angle for input
    (setq ANT (angtos A 4 4)) ;converts angle information to text
    (setq ANT2 (vl-string-subst "%%d" "d" ANT))
    (if (not (vl-position ANT2 (list "E" "S" "W" "N")))
    (progn
    (setq beginStr (substr ant2 1 2)
    degree (atoi (substr ANT2 3))
    minute (atoi (substr ANT2 (+ (vl-string-search "%%" ANT2) 4)))
    second (atoi (substr ANT2 (+ (vl-string-search "'" ANT2) 2)))
    endStr (substr ant2 (- (strlen ANT2) 1))
    ANT2 (strcat beginStr
    (2-LetterStr degree)
    "%%d"
    (2-LetterStr minute)
    "'"
    (2-LetterStr second)
    "\""
    endStr
    )
    )
    )
    )
    (setvar "OSMODE" 0) ;sets osnap to NONE
    (command "TEXT" "C" pause "24" ANG ANT2) ;executes text
    command
    (command "TEXT" "" (strcat DI "' "))
    ;executes text command
    (setvar "OSMODE" CO) ;resets osnap to original setting
    )
     
    Kelie, Sep 24, 2004
    #4
  5. PERFECT.
    Thanks a million.
     
    Michael Viscetto, Sep 24, 2004
    #5
  6. Actually that helped with the zero angle lines but I get this error when I try it on another line.
    TAG2
    Select first endpoint of line:
    Select other endpoint of line: ; error: no function definition: 2-LETTERSTR
     
    Michael Viscetto, Sep 24, 2004
    #6
  7. Michael Viscetto

    Kelie Guest

    you need to include the same subroutine as well.

    (defun 2-LetterStr (number)
    (strcat (if (< number 10)
    "0"
    ""
    )
    (itoa number)
    )
    )
     
    Kelie, Sep 24, 2004
    #7
  8. Got it.
    Thanks again.
    I have one more question.
    I have modified the routine to select the midpoint of the line for text placement.
    How do I change the text justification to "middle center" for this placement of text?


    (defun C:TAG (/ CO P1 P2 D D2 DI A
    ANG ANT ANT2 beginStr degree minute second
    endStr
    )
    (setq CO (getvar "OSMODE")) ;get the current value of osnap
    (setvar "OSMODE" 1) ;sets osnap to endpoint
    (setq P1 (getpoint "\nSelect first endpoint of line: "))
    (setq P2 (getpoint "\nSelect other endpoint of line: "))
    (setq D (distance P1 P2)) ;sets the value of the distance
    (setq D2 (/ D 12))
    (setq DI (rtos D2 2 2)) ;changes to a string-2 decimal places
    (setq A (angle P1 P2)) ;defines the angle between pickpoints
    (setq ANG (* A (/ 180 pi))) ;converts the points angle for input
    (setq ANT (angtos A 4 4)) ;converts angle information to text
    (setq ANT2 (vl-string-subst "%%d" "d" ANT))
    (if (not (vl-position ANT2 (list "E" "S" "W" "N")))
    (progn
    (setq beginStr (substr ant2 1 2)
    degree (atoi (substr ANT2 3))
    minute (atoi (substr ANT2 (+ (vl-string-search "%%" ANT2) 4)))
    second (atoi (substr ANT2 (+ (vl-string-search "'" ANT2) 2)))
    endStr (substr ant2 (- (strlen ANT2) 1))
    ANT2 (strcat beginStr
    (2-LetterStr degree)
    "%%d"
    (2-LetterStr minute)
    "'"
    (2-LetterStr second)
    "\""
    endStr
    )
    )
    )
    )
    (setvar "OSMODE" 2) ;sets osnap to midpoint
    (command "TEXT" "C" pause "24" ANG ANT2) ;executes text
    command
    (command "TEXT" "" (strcat DI "' "))
    ;executes text command
    (setvar "OSMODE" CO) ;resets osnap to original setting
    )



    (defun 2-LetterStr (number)
    (strcat (if (< number 10)
    "0"
    ""
    )
    (itoa number)
    )
    )
     
    Michael Viscetto, Sep 27, 2004
    #8
  9. OOPS,
    I meant Bottom Center.
    not middle center.
     
    Michael Viscetto, Sep 27, 2004
    #9
  10. Michael Viscetto

    Kelie Guest

    Mike, Try this.

    (defun C:TAG (/ CO P1 P2 D D2 DI A
    ANG ANT ANT2 beginStr degree minute second
    endStr
    )
    (setq CO (getvar "OSMODE")) ;get the current value of osnap
    (setvar "OSMODE" 1) ;sets osnap to endpoint
    (setq P1 (getpoint "\nSelect first endpoint of line: "))
    (setq P2 (getpoint "\nSelect other endpoint of line: "))
    (setq D (distance P1 P2)) ;sets the value of the distance
    (setq D2 (/ D 12))
    (setq DI (rtos D2 2 2)) ;changes to a string-2 decimal places
    (setq A (angle P1 P2)) ;defines the angle between pickpoints
    (setq ANG (* A (/ 180 pi))) ;converts the points angle for input
    (setq ANT (angtos A 4 4)) ;converts angle information to text
    (setq ANT2 (vl-string-subst "%%d" "d" ANT))
    (if (not (vl-position ANT2 (list "E" "S" "W" "N")))
    (progn
    (setq beginStr (substr ant2 1 2)
    degree (atoi (substr ANT2 3))
    minute (atoi (substr ANT2 (+ (vl-string-search "%%" ANT2) 4)))
    second (atoi (substr ANT2 (+ (vl-string-search "'" ANT2) 2)))
    endStr (substr ant2 (- (strlen ANT2) 1))
    ANT2 (strcat beginStr
    (2-LetterStr degree)
    "%%d"
    (2-LetterStr minute)
    "'"
    (2-LetterStr second)
    "\""
    endStr
    )
    )
    )
    )
    (setvar "OSMODE" 0) ;sets osnap to NONE
    (command "TEXT" "C" pause "24" ANG ANT2) ;executes text command
    (Set_TextAlignment (vlax-ename->vla-object (entlast)) 13)
    (command "TEXT" "" (strcat DI "' ")) ;executes text command
    (Set_TextAlignment (vlax-ename->vla-object (entlast)) 13)
    (setvar "OSMODE" CO) ;resets osnap to original setting
    ) ;d


    (defun 2-LetterStr (number)
    (strcat (if (< number 10)
    "0"
    ""
    )
    (itoa number)
    )
    )


    (defun Set_TextAlignment (objText alignment / insPt)
    (setq insPt (vlax-get-property objText 'INSERTIONPOINT))
    (vlax-put-property objText 'ALIGNMENT alignment)
    (vla-move objText
    (vlax-get-property objText 'INSERTIONPOINT)
    insPt
    )
    )
     
    Kelie, Sep 27, 2004
    #10
  11. That didn't seem to make a difference.
     
    Michael Viscetto, Sep 27, 2004
    #11
  12. Michael Viscetto

    Walt Engle Guest

    As I understand it, you want the "bottom center" - you don't need the
    first line of your routine (marked with **):

    **(setq CO (getvar "OSMODE")) ;get the current value of osnap
    (setvar "OSMODE" 1) ;sets osnap to endpoint

    Use the second line only and change to:

    (setvar "osmode" 16386) ;midpoint

    You could also use 16400 (quadrant) if your line is an arc (?)

    However, you did say "line" so my question is" Do you want to insert
    text in the middle of a line and have the text "grow" both ways so that
    the text is EVENLY placed in the middle of the line? If so, I have a lsp
    routine that will add text in the middle of a line and keep it evenly
    spaced from both ends of the line.
     
    Walt Engle, Sep 27, 2004
    #12
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.