Jump to content

Recommended Posts

Posted

Anybody has a function to replace point with comma in strings?

Posted

Please check the VL-STRING-SUBST function:

(vl-string-subst "." "," "2,50")

  • Like 1
Posted

Another

 
(vl-string-translate  "." "," "2.50")

  • Like 1
Posted

This one is ok! I have a lisp that works only with autolisp, not with Visual Lisp...and for me is easier to have a function defined in Autolisp...thanks!

Posted

I’m afraid that both suggestions are VL functions.

The only reason you should avoid using VL extension is when you are intending to use a routine in older version of AutoCAD (if I remember well, pre-2000 releases).

 

If you really want to use only vanilla AutoLISP, then parse the string with SUBSTR and reconstruct it with STRCAT; test each character and when encounter "," use "." to the new string.

Posted

Show us the string you want to translate.

 

(vl-string-subst .........

(vl-string-translate .....

 

Difference between the two is ... the latter will replace all target character on the whole string.

Posted

Sorry guys, it's ok with vl... is perfect! My mistake...i was thinking that i have to convert object for vl...but is not necessary... :)

Posted

For fun

 
(defun _translate  (str old new / a b)
     (while (and (setq a (substr str 1 1))
                 (> (strlen str) 0))
           (setq b   (strcat (if (null b) "" b)
                             (if (eq old a)
                                   new
                                   a))
                 str (substr str 2))
           )
     b
     )

 

(_translate "2.50" "." ",")

Posted

If i have a real like 5.0 and i make (rtos 5.0 2 1) , the result will be "5" - and i need it "5.0" - how to do it?

Posted

This was my take on a 'consistent rtos' function (quoted from a post at TheSwamp):

 

==================================================

 

Due to variation in the DIMZIN System Variable, the rtos function can sometimes produce undesired results, even when both arguments have been specified.

 

For example:

 

[color=green]DIMZIN = 0[/color]
_$ (rtos 0.234 2 2)
"0.23"

[color=green]DIMZIN = 4[/color]
_$ (rtos 0.234 2 2)
".23"

[color=green]DIMZIN = 0[/color]
_$ (rtos -0.23 2 3)
"-0.230"

[color=green]DIMZIN = 12[/color]
_$ (rtos -0.23 2 3)
"-.23"

That last example may also cause problems with applications using the read function as a method of converting strings to other data types.

 

Usually the solution would be to temporarily change the value of DIMZIN so that the result of a conversion can be predictable and consistent throughout a routine.

 

My 'conceptual' alternative is the following function (I use the term 'conceptual' as I'm really just demonstrating an alternative idea and the function will only currently process decimal format).

 

[color=GREEN];;------------------------=={ rtos }==------------------------;;[/color]
[color=GREEN];;                                                            ;;[/color]
[color=GREEN];;  Subfunction designed to produce a consistent rtos return, ;;[/color]
[color=GREEN];;  independent of the DIMZIN System Variable.                ;;[/color]
[color=GREEN];;                                                            ;;[/color]
[color=GREEN];;  [ Currently restricted to Decimal Format only ]           ;;[/color]
[color=GREEN];;------------------------------------------------------------;;[/color]
[color=GREEN];;  Author: Lee Mac, Copyright © 2011 - www.lee-mac.com       ;;[/color]
[color=GREEN];;------------------------------------------------------------;;[/color]
[color=GREEN];;  Arguments:                                                ;;[/color]
[color=GREEN];;  real - real number to convert to a string                 ;;[/color]
[color=GREEN];;  prec - precision to which the real should be displayed    ;;[/color]
[color=GREEN];;------------------------------------------------------------;;[/color]
[color=GREEN];;  Returns:  String representing the supplied real           ;;[/color]
[color=GREEN];;------------------------------------------------------------;;[/color]

([color=BLUE]defun[/color] LM:rtos ( real prec )
   (
       ([color=BLUE]lambda[/color] ( l )
           ([color=BLUE]repeat[/color] ([color=BLUE]-[/color] prec ([color=BLUE]cond[/color] (([color=BLUE]vl-position[/color] 46 l)) (([color=BLUE]setq[/color] l ([color=BLUE]cons[/color] 46 l)) 0)))
               ([color=BLUE]setq[/color] l ([color=BLUE]cons[/color] 48 l))
           )
           ([color=BLUE]vl-string-right-trim[/color] [color=MAROON]"."[/color]
               ([color=BLUE]vl-list->string[/color]
                   ([color=BLUE]cond[/color]
                       (  ([color=BLUE]=[/color] 46 ([color=BLUE]car[/color] ([color=BLUE]setq[/color] l ([color=BLUE]reverse[/color] l))))
                          ([color=BLUE]cons[/color] 48 l)
                       )
                       (  ([color=BLUE]and[/color] ([color=BLUE]=[/color] 45 ([color=BLUE]car[/color] l)) ([color=BLUE]=[/color] 46 ([color=BLUE]cadr[/color] l)))
                          ([color=BLUE]vl-list*[/color] 45 48 ([color=BLUE]cdr[/color] l))
                       )
                       ( l )
                   )
               )
           )
       )
       ([color=BLUE]reverse[/color] ([color=BLUE]vl-string->list[/color] ([color=BLUE]rtos[/color] real 2 prec)))
   )
)

As demonstrated, the return of this function is independent of the value of the DIMZIN System Variable:

 

[color=green]DIMZIN = 0[/color]
_$ (LM:rtos 0.234 2)
"0.23"

[color=green]DIMZIN = 4[/color]
_$ (LM:rtos 0.234 2)
"0.23"

[color=green]DIMZIN = 0[/color]
_$ (LM:rtos -0.23 3)
"-0.230"

[color=green]DIMZIN = 12[/color]
_$ (LM:rtos -0.23 3)
"-0.230"

==================================================

 

Note that this result differs from Irneb's rtos-dec function for some cases:

 

[color=green]DIMZIN = 4[/color]
_$ (LM:rtos 0.234 2)
"0.23"

_$ (rtos-dec 0.234 2)
".23"

[color=green]DIMZIN = 4[/color]
_$ (LM:rtos -0.234 2)
"-0.23"

_$ (rtos-dec -0.234 2)
"-.23"

==================================================

 

However, nowadays, I would now be far more inclined to simply use:

 

[color=GREEN];; rtos wrapper  -  Lee Mac[/color]
[color=GREEN];; A wrapper for the rtos function to negate the effect of DIMZIN[/color]

([color=BLUE]defun[/color] LM:rtos ( real units prec [color=BLUE]/[/color] dimzin result )
   ([color=BLUE]setq[/color] dimzin ([color=BLUE]getvar[/color] 'dimzin))
   ([color=BLUE]setvar[/color] 'dimzin 0)
   ([color=BLUE]setq[/color] result ([color=BLUE]vl-catch-all-apply[/color] '[color=BLUE]rtos[/color] ([color=BLUE]list[/color] real units prec)))
   ([color=BLUE]setvar[/color] 'dimzin dimzin)
   ([color=BLUE]if[/color] ([color=BLUE]not[/color] ([color=BLUE]vl-catch-all-error-p[/color] result))
       result
   )
)

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...