Add or subtract a constant from selected number text

Discussion in 'AutoCAD' started by JBK, Jun 21, 2004.

  1. JBK

    JBK Guest

    Anyone know of a routine to add or subtract a constant from a selected
    number that looks like this? 100+00.
    We would like a simple way to revise route stationing text in alignment
    drawings.

    Thanks for your help.
     
    JBK, Jun 21, 2004
    #1
  2. JBK

    andywatson Guest

    JBK,
    It's kinda hard to understand where to begin. Why don't you explain the process you imagine one would go through and the outcome you expect from the lisp routine you are creating.
    i.e. User will pick a text entity, routine will add/subtract a constant value (where is this constant value stored?) and the sum will be displayed on the command line/in a message box/in another piece of text...etc.

    I can venture a method here...
    Code:
    (setq strStation "100+00"
    myConstant 50.0)
    ;; remove "+" sign
    (setq strStation (vl-string-subst "" "+" strStation))
    ;; convert string to decimal number
    (setq reaStation (distof strStation 2))
    ;; add constant
    (setq reaSum (+ reaStation myConstant))
    
     
    andywatson, Jun 21, 2004
    #2
  3. JBK

    JBK Guest

    Yes, that is what we need to do. I have always been lucky to work with
    other brilliant people, never learned to write lisp, and have used this type
    of routine before in AutoCAD and Microstation. But, being the ethical
    little drafter that I am....I have never stolen it from anyone. Sure do
    need it right now though.

    Sound possible?
     
    JBK, Jun 21, 2004
    #3
  4. I've written many commands to deal with Stationing. Contact me via email to
    further discuss.
    Alan
     
    Alan Henderson @ A'cad Solutions, Jun 21, 2004
    #4
  5. JBK

    andywatson Guest

    JBK,

    Try this out and let me know how it goes.
    Load and start with "AddStation".
    Enter prefix for station text ("Sta:", "STA: ", etc.).
    Enter rounding precision next.
    Select the text.

    Hope it helps.
    Andrew

    Code:
    ;; getFormattedText
    ;; receives a real number and returns a string in civil station notation
    ;; i.e. 1000.00 = 10+00.00
    (defun getFormattedText (s / stext hundredPlace returnList)
    ;; convert number to string
    (setq stext (rtos s 2 *roundingPrecision*))
    ;; format text
    (cond
    ((< s 10)
    (setq stext (strcat "0+0" stext))
    )
    ((< s 100)
    (setq stext (strcat "0+" stext))
    )
    (T
    (if (= *roundingPrecision* 0)
    (setq hundredPlace 2)
    (setq hundredPlace (+ *roundingPrecision* 3))
    ); if
    (setq stext
    (strcat
    (substr stext 1 (- (strlen stext) hundredPlace))
    "+"
    (substr stext (- (strlen stext) (1- hundredPlace))
    ) ;_ end of strcat
    ) ;_ end of setq
    )
    )
    ) ;_ end of cond
    (setq stext (strcat *stationPrefix* stext))
    ) ;_ end of defun
    
    
    ;; ss->list
    ;; converts a selection set to a list of vla-objects
    (defun ss->list (ss / returnList)
    (if ss
    (progn
    (setq i (sslength ss))
    (while (> i 0)
    (setq i (1- i)
    e (ssname ss i)
    v (vlax-ename->vla-object e)
    returnList (cons v returnList))
    ); while
    ); progn
    ); if
    ); defun
    
    
    ;; AddStation.lsp
    ;; routine asks for user selection of station text
    ;; and adds user-specified amount to each station
    (defun c:AddStation ( / tmp ssText lstText vlaText strStation reaStation reaNewStation strNewStation)
    ;; get station prefix preference
    (if (null *stationPrefix*) (setq *stationPrefix* "STA: "))
    (setq tmp (getstring (strcat "\nStation prefix <" *stationPrefix* "> : ")))
    (if (/= tmp "") (setq *stationPrefix* tmp))
    ;; get rounding precision preference
    (if (null *roundingPrecision*) (setq *roundingPrecision* 0))
    (setq tmp (getint (strcat "\nRounding precision for station text <" (itoa *roundingPrecision*) "> : ")))
    (if tmp (setq *roundingPrecision* tmp))
    ;; get adjust amount
    (if (null *reaAdjust*) (setq *reaAdjust* 0.0))
    (setq tmp (getreal (strcat "\nAmount by which to adjust all stations <" (rtos *reaAdjust* 2 *roundingPrecision*) "> : ")))
    (if tmp (setq *reaAdjust* tmp))
    ;; get station text
    (prompt "\nSelect station text...")
    ;; filter selection for text and mtext
    ;; and convert to a list of vlaobjects
    (setq ssText (ssget '((0 . "TEXT,MTEXT")))
    lstText (ss->list ssText))
    ;; cycle through every station text
    (foreach vlaText lstText
    ;; get text string
    (setq strStation (vla-get-textstring vlaText)
    ;; trim prefix from string
    strStation (vl-string-left-trim *stationPrefix* strStation)
    ;; remove "+" sign
    strStation (vl-string-subst "" "+" strStation)
    ;; convert to number
    reaStation (atof strStation)
    ;; adjust by user-specified amount
    reaNewStation (+ reaStation *reaAdjust*)
    ;; convert number to civil notation
    strNewStation (GetFormattedText reaNewStation))
    ;; edit text with new station string
    (vla-put-textstring vlaText strNewStation)
    ); foreach
    (princ)
    ); defun
    
     
    andywatson, Jun 21, 2004
    #5
  6. I have never had a problem sharing ideas with the group. I sell this
    particular code, it is extensive in nature and the programs are too
    interlaced (non-technical term) and might not be easy to follow. And yes, if
    I can make some money, that's also "one" of the reasons I am here.
     
    Alan Henderson @ A'cad Solutions, Jun 21, 2004
    #6
  7. JBK

    John Uhden Guest

    Andy:

    That's really nice of you. Watch out for what DIMZIN can do to (rtos).
    Also check for negative stations (yes, we do use them, e.g. "-1+32.15").




     
    John Uhden, Jun 22, 2004
    #7
  8. JBK

    Joe Burke Guest

    Andrew,

    A suggestion, if I may. You could remove the ss->list function. Do it like this
    instead.

    (setq doc (vla-get-ActiveDocument (vlax-get-acad-object)))
    (ssget '((0 . "TEXT,MTEXT")))
    (vlax-for vlaText (vlax-get doc 'ActiveSelectionSet)
    ;; process like foreach
    )

    Use vlax-for instead of foreach. IOW, think of the ActiveSelectionSet collection as a
    list of vla objects.

    Joe Burke


     
    Joe Burke, Jun 22, 2004
    #8
  9. JBK

    andywatson Guest

    John,
    Thanks for pointing that out. I had not been aware of the power of DIMZIN, I had no idea that it would modify the values returned by RTOS & ANGTOS.

    Joe,
    I had never used the ActiveSelectionSet property. That's a great bypass to how I normally operate.

    Thanks for the info.
    Andrew
     
    andywatson, Jun 22, 2004
    #9
  10. JBK

    JBK Guest

    Andy, thank you very much. It works perfectly, with one minor exception.
    Please contact me at
    To all of you who offered help...thank you, as well.


     
    JBK, Jun 22, 2004
    #10
  11. JBK

    JBK Guest

    Sorry, Andy. I'm mixing home and work addresses. Use this.



     
    JBK, Jun 22, 2004
    #11
  12. JBK

    andywatson Guest

    JBK,
    I tried your email, got an 'undeliverable message'. Are you sure the email address was correct?
    You can contact me at , just remove the 'nospam.'
    Andrew
     
    andywatson, Jun 22, 2004
    #12
  13. JBK

    Joe Burke Guest

    Andrew,

    You're welcome.

    BTW, I would write your ss->list function something like this.

    ;; convert a selection set to a list of vla-objects
    (defun ss->list ( ss / i e v returnList )
    (setq i 0)
    (repeat (sslength ss)
    (setq e (ssname ss i)
    v (vlax-ename->vla-object e)
    returnList (cons v returnList)
    i (1+ i)
    )
    )
    returnList
    ) ;end

    Repeat is more efficient than while when the number of iterations is known.

    Also (if ss... doesn't serve much purpose in a sub-routine such as this. You could
    pass any data and (if ss... would return something. Rather, check the validity of ss
    in the calling function. This also makes debugging easier. An error occurs at its
    source, rather than in a sub-routine, which didn't cause the error.

    Joe Burke
     
    Joe Burke, Jun 23, 2004
    #13
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.