placing a "-" at the beginning of every line in Mtext

Discussion in 'AutoCAD' started by The Real JD, Jun 24, 2004.

  1. The Real JD

    The Real JD Guest

    I need to add a "-" at the beginning of every hard return in Mtext.

    Can anybody suggest a way to do this?

    I've got hundreds of mtext lines to change.
     
    The Real JD, Jun 24, 2004
    #1
  2. The Real JD

    MP Guest

    ; Parse function by Frank Oquendo
    ; creates multiple strings from a single string
    ; - string, string to breakdown
    ; [d] - string, separator
    ; return: list of strings
    (defun parse (s d / i l tmp rtn)
    (cond
    ((/= "" d)
    (setq l (strlen d))
    (while (setq i (vl-string-search d s))
    (setq tmp (substr s 1 i))
    (setq rtn (cons tmp rtn))
    (setq s (substr s (1+ (+ l (strlen tmp))) (strlen s)))
    )
    (vl-remove "" (reverse (cons s rtn)))
    )
    (t s)
    )
    )

    ;by Mark Propst for the Real JD 6-24-04
    ;limited testing, no error checking
    ;function to add dash in front of every occurance of a hardreturn code (\P)
    in string argument
    ;args: 1 [string]
    ;returns [modified string]
    (defun AddDashToReturn(teststring / newstring stringlist idx part)
    (setq newstring "")
    (setq stringlist(parse teststring "\P"))
    (setq idx -1)
    (repeat(1- (length stringlist))
    (setq part(nth (setq idx(1+ idx)) stringlist))
    (setq newstring(strcat newstring (substr part 1 (1-(strlen part)))
    "-\\P"))
    );repeat
    (setq newstring(strcat newstring(last stringlist)))
    newstring
    )

    ;function to show usage to modify value of selected mtext object using above
    function
    (defun addDashTest( / newstring teststring)
    (setq tobj(vlax-ename->vla-object(car(entsel))))
    (setq teststring(vlax-get-property tobj 'textstring))
    (setq newstring(AddDashToReturn teststring))
    (vlax-put-property tobj 'textstring newstring)
    (princ)
    )
     
    MP, Jun 25, 2004
    #2
  3. The Real JD

    Steve Doman Guest

    JD,

    Well here's more than a suggestion but a whole little routine. I
    haven't tested it much, but I think it should work ok. Let me know how
    it does works for you please.

    Code:
    (defun c:Mtext-P (/ ss i obj mtext str *error*)
    ;; Steve Doman 06-24-04 beta
    ;; Command to add a hyphen "-" to the end of each
    ;; hard linefeed in a selection set of mtext
    ;;
    (defun *error* (msg)
    (cond
    ((member
    msg
    '("Function cancelled" "quit / exit abort" "console break")
    )
    )
    ((princ (strcat " Error: " msg)))
    )
    (vla-endundomark (vla-get-activedocument (vlax-get-acad-object)))
    (princ)
    )
    (if (setq ss (ssget ":L" '((0 . "MTEXT"))))
    (progn (vla-startundomark
    (vla-get-activedocument (vlax-get-acad-object))
    )
    (setq i 0)
    (repeat (sslength ss)
    (setq obj (vlax-ename->vla-object (ssname ss i)))
    (setq mtext (vla-get-textstring obj))
    (setq str "")
    (while (/= "" mtext)
    ;;
    ;; Walk thru the textstring and look at two characters
    ;; at a time.  If any two characters match a line feed
    ;; formatting code "\\P", then also check that the
    ;; previous character in the textstring is not a slash.
    ;; Since a slash is a literal character, this would
    ;; indicate that the linefeed pattern is not a real
    ;; linefeed but some content.
    ;;
    (if (and (= (substr mtext 1 2) "\\P")
    (/= (substr str (strlen str)) "\\")
    )
    (setq str   (strcat str "-" (substr mtext 1 2))
    mtext (substr mtext 3)
    )
    (setq str   (strcat str (substr mtext 1 1))
    mtext (substr mtext 2)
    )
    )
    )
    (setq str (strcat str "-")) ;_ one for the road
    (vla-put-textstring obj str)
    (setq i (1+ i))
    )
    (vla-endundomark
    (vla-get-activedocument (vlax-get-acad-object))
    )
    )
    )
    (princ)
    )
    
    HTH
    Steve Doman
     
    Steve Doman, Jun 25, 2004
    #3
  4. The Real JD

    The Real JD Guest

    Well Steve,

    Thanks for the time and effort.
    It puts the dash at the END of every line, not at the beginning.
    Using 2005 if it helps.

     
    The Real JD, Jun 25, 2004
    #4
  5. The Real JD

    The Real JD Guest

    Hi Mark,

    I tried it.. and like Steve's it adds a dash at the END of every Mtext line.
    Also, if there's a Capitol "P" in the mtext, it changes it to a dash and
    forces the rest of the word onto another line.
    Also it doesn't "-" the last line

    I appreciate the help guys.

     
    The Real JD, Jun 25, 2004
    #5
  6. The Real JD

    T.Willey Guest

    Here is Steve's code changed a little to fit what I think you want.
    Code:
    (defun c:Mtext-P (/ *error*)
    ;; Steve Doman 06-24-04 beta
    ;; Command to add a hyphen "-" to the end of each
    ;; hard linefeed in a selection set of mtext
    ;;
    (defun *error* (msg)
    (cond
    ((member
    msg
    '("Function cancelled" "quit / exit abort" "console break")
    )
    )
    ((princ (strcat " Error: " msg)))
    )
    (vla-endundomark (vla-get-activedocument (vlax-get-acad-object)))
    (princ)
    )
    (if (setq ss (ssget ":L" '((0 . "MTEXT"))))
    (progn (vla-startundomark
    (vla-get-activedocument (vlax-get-acad-object))
    )
    (setq i 0)
    (repeat (sslength ss)
    (setq obj (vlax-ename->vla-object (ssname ss i)))
    (setq mtext (vla-get-textstring obj))
    (setq str "")
    (while (/= "" mtext)
    ;;
    ;; Walk thru the textstring and look at two characters
    ;; at a time.  If any two characters match a line feed
    ;; formatting code "\\P", then also check that the
    ;; previous character in the textstring is not a slash.
    ;; Since a slash is a literal character, this would
    ;; indicate that the linefeed pattern is not a real
    ;; linefeed but some content.
    ;;
    (if (and (= (substr mtext 1 2) "\\P")
    (/= (substr str (strlen str)) "\\")
    )
    (setq str   (strcat str (substr mtext 1 2) "-");;;;  here is where I switch the order of where the "-" was placed
    mtext (substr mtext 3)
    )
    (setq str   (strcat str (substr mtext 1 1))
    mtext (substr mtext 2)
    )
    )
    )
    ;(setq str (strcat str "-")) ;_ one for the road=============== took out the one for the road
    (vla-put-textstring obj (strcat "-" str));;;  added a "-" to the first line
    (setq i (1+ i))
    )
    (vla-endundomark
    (vla-get-activedocument (vlax-get-acad-object))
    )
    )
    )
    (princ)
    )
    
    I made my own comments where I changed the lines, only three places.

    Tim
     
    T.Willey, Jun 25, 2004
    #6
  7. The Real JD

    Steve Doman Guest

    Good job Tim. ;)

    Thanks,
    Steve Doman


    <clip>
     
    Steve Doman, Jun 26, 2004
    #7
  8. The Real JD

    T.Willey Guest

    Thanks Steve, couldn't have done it without your code. :-D

    Tim
     
    T.Willey, Jun 28, 2004
    #8
  9. The Real JD

    Steve Doman Guest

    It was just an exercise, which I couldn't have done it without John
    Uhden's 'Unformat' parsing algorithm. :)

    Steve
     
    Steve Doman, Jun 29, 2004
    #9
  10. The Real JD

    The Real JD Guest

    Thanks alot guys... I appreciate the help!

     
    The Real JD, Jun 29, 2004
    #10
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.