How to self write variable in Autolisp

Discussion in 'AutoCAD' started by Adesu, Oct 14, 2004.

  1. Adesu

    Adesu Guest

    Now you write (create a text) on drawing area a "Adesu",this text are
    consist 5 string,in the program "while" after 5th this program would off or
    finish,and now how self create/write memory from 1st to 5th,as like ;
    (setq 1st "J") =>>> "J" get from (setq opt (getstring "\nENTER NEW NAME
    TEXT: "))
    (setq 2nd "a")
    (setq 3rd "s")
    (setq 4th "o")
    (setq 5th "n")
    I plan replace "Adesu" to "Jason",the problem is if user create a text more
    than 5 or 6 or 7 etc.,anybody help to solve this problem?,thanks.

    (setq ss (ssget '((0 . "MTEXT,TEXT"))))
    (setq ent (ssname ss 0))
    (setq dxf (entget ent))
    (setq leng (strlen (cdr (assoc 1 dxf))))
    (setq count 1)
    (setq num leng)
    (while
    (< count num)
    (setq opt (getstring "\nENTER NEW NAME TEXT: "))
    (setq count (1+ count))
    )
     
    Adesu, Oct 14, 2004
    #1
  2. Adesu

    BillZ Guest

    Are you saying that if the user enters "Jeremy" instead of "Jason", then you want "Adesu" to be changed to "Jerem"?

    Bill
     
    BillZ, Oct 14, 2004
    #2
  3. I suggest looking into (substr), which will allow you to save only the first
    5 characters if someone enters more:

    (setq replacetext (substr opt 1 5))

    and to save each character in the string separately:

    (setq 1st (substr replacetext 1 1)
    (setq 2nd (substr replacetext 2 1)
    (setq 3rd (substr replacetext 3 1)
    etc.
     
    Kent Cooper, AIA, Oct 14, 2004
    #3
  4. Come to think of it, if you just want the first five characters saved
    separately, you could skip my first suggestion, and just go straight to
    saving them from the original string, even if it's longer than five
    characters:

    (setq 1st (substr opt 1 1)
    (setq 2nd (substr opt 2 1)
    (setq 3rd (substr opt 3 1)
    (setq 4th (substr opt 4 1)
    (setq 5th (substr opt 5 1)

    You don't need to worry about whether there are more after that.

    But what if they enter a string that's SHORTER than five characters?
     
    Kent Cooper, AIA, Oct 14, 2004
    #4
  5. Adesu

    Doug Broad Guest

    Adesu,
    I never can quite understand what you want but I'll give this a stab.

    (setq str "This is a test")
    (mapcar 'chr (vl-string->list str))
    returns:
    ("T" "h" "i" "s" " " "i" "s" " " "a" " " "t" "e" "s" "t")

    The above shows how to break a string up into a list of its characters.

    If there are a variable number of characters, it is best not to assign
    those to individual variables. Instead keep them in list form.
     
    Doug Broad, Oct 14, 2004
    #5
  6. Adesu

    Adesu Guest

    Hi Doug,I ask "a" but you answer "b",the "b" is the "best" input for me.I
    've got that,thanks a lot
     
    Adesu, Oct 15, 2004
    #6
  7. Adesu

    Adesu Guest

    Hi Kent & BillZ,please forgot last post,sorry,now we start discuss new
    post,here my code

    ; jsf is stand for joint style font
    ; Design by Ade Suharna <>
    ; 13 October 2004
    ; Program no 115/10/2004
    ; Edit by petersciganek <> 13/10/2004 1).
    (defun c:jsf (/ oldosmode oldcmdecho ss ent dxf info40
    info71 loc pos1 texhei recwid pos2)
    (setq oldosmode (getvar "osmode")) ; get osmode setting
    (setvar "osmode" 0) ; osmode set to 0
    (setq oldcmdecho (getvar "cmdecho")) ; get cmdecho setting
    (setvar "cmdecho" 0) ; cmdecho set to 0
    (setq ss (ssget '((0 . "MTEXT,TEXT"))))
    (setq ent (ssname ss 0))
    (setq dxf (entget ent))
    (setq info40 (rtos (cdr (assoc 40 dxf)) 2 0))
    (setq info71 (cdr (assoc 71 dxf)))
    (setq loc (cdr (assoc 10 dxf)))
    (setq leng (strlen (cdr (assoc 1 dxf))))
    (setq count 1)
    (setq num leng)
    (setq str "{") ; 1).
    (while
    (< count num)
    (setq opt (getstring "\nENTER NEW NAME STYLE: "))
    (setq str (strcat str "\\f" opt "|b0|i0|c0|p18;d"))) ; 1).
    (setq count (1+ count))
    )
    (prompt "\n1=TL;2=TC;3=TR;4=ML;5=MC;6=MR;7=BL;8=BC;9=BR")
    (setq pos1
    (atoi
    (rtos
    (getreal "\nENTER NEW ATTACHMENT POINT<1 ; 2 ; 3 or > : ") 2 0)))
    (setq texhei
    (getreal
    (strcat"\nENTER TEXT HEIGHT<" info40 ">: ")))
    (setq recwid (getdist "\nENTER NEW RECTANGLE WIDTH: "))
    (prompt "\n1=LtR;3=TtB;5=BY STYLE")
    (setq pos2
    (atoi
    (rtos
    (getreal"\nENTER NEW DRAWING DIRECTION<1 ; 3 ; 5>: ") 2 0)))
    (command "_erase" ss "")
    (entmake (list '(0 . "MTEXT")
    ;1)
    '(100 . "AcDbEntity")
    '(100 . "AcDbMText")
    (cons 71 pos1)
    (cons 10 loc)
    (cons 40 texhei)
    (cons 41 recwid)
    (cons 72 pos2)
    (cons 1 str
    \\fCentury|b0|i0|c0|p18;d ; here
    still problem !!!
    \\fLucida Sans|b0|i0|c0|p34;e
    \\fSimSun|b0|i0|c0|p2;s
    \\fTahoma|b0|i0|c0|p34;u}")
    ))
    (princ)
    (setvar "osmode" oldosmode) ; return setting
    (setvar "cmdecho" oldcmdecho) ; return setting
    )

    We explain to you about my code,here my problem,I want after user input ;
    (setq opt (getstring "\nENTER NEW NAME STYLE: "))
    (setq str (strcat str "\\f" opt "|b0|i0|c0|p18;d")))
    the first "opt" save in str1,2nd "opt" save in str2,3rd "opt" save in str3
    and etc,I would using by while,but difficult to put them,because the user
    input not always choose 5 character maybe 6 ,7 ,8 or more,after that str1
    put on "(cons 1 str1 str2 str3 etc", this here I got problem,can you help
    me ?
     
    Adesu, Oct 15, 2004
    #7
  8. Adesu

    Doug Broad Guest

    You're welcome. (I think) ;-)
     
    Doug Broad, Oct 15, 2004
    #8
  9. Adesu

    BillZ Guest

    Okay,
    Gotcha.... :^| I think.

    In a while loop (which I don't recommend a getxxx function in),
    If you want to set a variable dynamically during the loop.


    (setq num 5
    count 0
    SetCnt 1)
    (while
    (< count num)
    (setq opt (getstring "\nENTER NEW NAME STYLE: ")
    )
    (set (read (strcat "Str" (itoa SetCnt))) opt)
    (setq Var_names (cons (strcat "Str" (itoa SetCnt)) Var_Names)
    )
    (setq count (1+ count)
    SetCnt (1+ SetCnt))
    )

    This will setq the variable Str1, Str2 ect. to the value of opt and makes a list of the variable names.

    Hope this is what you want.

    Or, If you just want a list of the strings:

    (setq opt (getstring "\nENTER NEW NAME STYLE: ")
    StringList (cons opt StringList)
    )


    Bill student@universityofadversity
     
    BillZ, Oct 15, 2004
    #9
  10. Adesu

    Adesu Guest

    Hi BillZ,thanks a lot for your help,that code it work,now problem in
    (setq str (strcat str "\\f" opt "|b0|i0|c0|p18;d")),because set to "d"
    "|b0|i0|c0|p18;d" >>>>this set all to character "d".here my code this below

    ; jsf is stand for joint style font
    ; Design by Ade Suharna <>
    ; 13 October 2004
    ; Program no 115/10/2004
    ; Edit by petersciganek <> 13/10/2004 1).
    (defun c:jsf (/ oldosmode oldcmdecho ss ent dxf info40
    info71 loc pos1 texhei recwid pos2)
    (setq oldosmode (getvar "osmode")) ; get osmode setting
    (setvar "osmode" 0) ; osmode set to 0
    (setq oldcmdecho (getvar "cmdecho")) ; get cmdecho setting
    (setvar "cmdecho" 0) ; cmdecho set to 0
    (setq ss (ssget '((0 . "MTEXT,TEXT"))))
    (setq ent (ssname ss 0))
    (setq dxf (entget ent))
    (setq info40 (rtos (cdr (assoc 40 dxf)) 2 0))
    (setq info71 (cdr (assoc 71 dxf)))
    (setq loc (cdr (assoc 10 dxf)))
    (setq leng (strlen (cdr (assoc 1 dxf))))
    (setq count 0)
    (setq num leng)
    (setq str "{") ; 1).
    (setq setcnt 1)
    (while
    (< count num)
    (setq opt (getstring "\nENTER NEW NAME STYLE: "))
    (setq str (strcat str "\\f" opt "|b0|i0|c0|p18;d")) ; 1).
    (set (read (strcat "Str" (itoa SetCnt))) opt)
    (setq Var_names
    (cons
    (strcat "Str" (itoa SetCnt)) Var_Names))
    (setq count (1+ count))
    (setq setcnt (1+ setcnt)))
    (prompt "\n1=TL;2=TC;3=TR;4=ML;5=MC;6=MR;7=BL;8=BC;9=BR")
    (setq pos1
    (atoi
    (rtos
    (getreal "\nENTER NEW ATTACHMENT POINT<1 ; 2 ; 3 or > : ") 2 0)))
    (setq texhei
    (getreal
    (strcat"\nENTER TEXT HEIGHT<" info40 ">: ")))
    (setq recwid (getdist "\nENTER NEW RECTANGLE WIDTH: "))
    (prompt "\n1=LtR;3=TtB;5=BY STYLE")
    (setq pos2
    (atoi
    (rtos
    (getreal"\nENTER NEW DRAWING DIRECTION<1 ; 3 ; 5>: ") 2 0)))
    (command "_erase" ss "")
    (entmake (list '(0 . "MTEXT")
    ;1)
    '(100 . "AcDbEntity")
    '(100 . "AcDbMText")
    (cons 71 pos1)
    (cons 10 loc)
    (cons 40 texhei)
    (cons 41 recwid)
    (cons 72 pos2)
    (cons 1 str )
    ))
    (princ)
    (setvar "osmode" oldosmode) ; return setting
    (setvar "cmdecho" oldcmdecho) ; return setting
    )
     
    Adesu, Oct 18, 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.