Character substitution

Discussion in 'AutoCAD' started by GaryDF, Nov 3, 2004.

  1. GaryDF

    GaryDF Guest

    I have blocks named "KV-30", "KUR-30"....etc.
    I want to subtract and get the block name containing only the characters to the
    left of "-".
    (chr 45)
    Ex: I only want: "KV-" and "KUR-"

    Using strlen and substr, how would I do this?


    Gary
     
    GaryDF, Nov 3, 2004
    #1
  2. GaryDF

    GaryDF Guest

    Thanks for your help....

    I am still having trouble with subtracting the correct characters from the
    selected block name.
    If the block name is either "KB-PN" or "KUV-30", I only want the characters to
    the left of
    the "-". I am using unequally scaled blocks. The text I am using is a
    combination of the characters
    to the left of and including "-" plus the distance picked.

    Thus "KB-PN" block selected would have text inserted as "KB-24" if the distanced
    picked was 24.
    This is a block labeling routine for my floor plans for cabinet labeling.

    Here is the routine I am working on:

    (defun C:CASETX (/ EL IP BLK WIDP PX PY BLK-LEN FC NU-STR P1 BLKN)
    (defun CASESTR (/ COUNT CH)
    (setq count 1 Flag 0)
    (repeat (strlen BLK)
    (setq ch (substr BLK count 1))
    (if (= ch "-")(setq Flag 1))
    (if (and (/= Flag 1)(/= ch "-"))
    (setq BLKN (strcat BLK ch))
    )
    (setq count (+ count 1))
    )
    )
    (while (not
    (setq EN (car (entsel "\n* Select block *")))
    )
    )
    (setvar "osmode" 111)
    (setq EL (entget EN)
    IP (cdr (assoc 10 EL))
    BLK (cdr (assoc 2 EL))
    WIDP (getpoint IP "\n* Pick cabinet width *")
    PX (car IP)
    PY (cadr IP)
    BLK-LEN (strlen BLK)
    )
    (CASESTR)
    (setq NU-STR (strcase (strcat BLKN "" (rtos (distance IP WIDP) 2 0))))
    (setvar "osmode" 2)
    (setq P1 (getpoint "*\n Pick Text Placement *"))
    (command "_.text"
    "j"
    "bc"
    P1
    (getvar "textsize")
    0
    NU-STR
    ""
    )
    (command)
    (princ)
    )
     
    GaryDF, Nov 3, 2004
    #2
  3. GaryDF

    Jeff Mishler Guest

    Something like this?
    _$ (stripright "KM-KP" "-")
    "KM-"
    _$

    (defun stripright (str delim / )
    (if (setq pos (vl-string-position (ascii delim) str))
    (substr str 1 (1+ pos))
    nil
    )
    )
     
    Jeff Mishler, Nov 3, 2004
    #3
  4. GaryDF

    GaryDF Guest

    Thank you...that works perfectly.

    (stripright BLK "-")

    Gary
     
    GaryDF, Nov 3, 2004
    #4
  5. GaryDF

    GaryDF Guest

    I'm sorry about the miss communication....I was trying to modify an older
    routine.

    Thanks again Jeff for the code...it is now in my library.

    Gary
     
    GaryDF, Nov 3, 2004
    #5
  6. I thought you wanted/needed this...

    Nonetheless, glad you got what works.
     
    Jason Piercey, Nov 3, 2004
    #6
  7. If there happen to be ALWAYS TWO characters after the "-" that you want to
    remove, you can use
    (setq length (strlen <blockname>)) to find out how long the block name is,
    then
    (setq shorten (- length 2)) to reduce that number by two, then
    (substr <blockname> 1 shorten) to extract all but the last two characters.
     
    Kent Cooper, AIA, Nov 3, 2004
    #7
  8. GaryDF

    Jeff Mishler Guest

    Well I used 1 out of 2.... ;-)

    Glad it works for you, Gary.
     
    Jeff Mishler, Nov 3, 2004
    #8
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.