Author Topic: Assistance with rtosr function in AutoCAD 2008  (Read 1843 times)

0 Members and 1 Guest are viewing this topic.

terrycadd

  • Guest
Assistance with rtosr function in AutoCAD 2008
« on: October 17, 2007, 11:13:45 AM »
I just received an email from an Engineer from Japan telling me that my rtosr function is not working correctly in AutoCAD 2008.  It was first written in AutoCAD R14 as a replacement for the rtos function for converting numbers without specifying the precision, and it’s accurate up to 8 decimal places.  We only have AutoCAD 2004 thru 2006 here at work, and it works just like it was intended to.  One of the Engineers has AutoCAD 2007 on his lap top at home and came across this change in the AutoLISP function rtos.  The rtos function truncates the leading 0 in numbers less than 0, where as in previous versions it left the 0.  In AutoCAD 2006 (rtos 0.12345678 2 8 ) = “0.12345678”.  In AutoCAD 2007 (rtos 0.12345678 2 8 ) = “.12345678”.

If you have AutoCAD 2008 can you test the revised function for me?
Here are a few of the examples and the returns you should see:

(rtosr 0.000000004) = "0"
(rtosr 0.000000005) = "0.00000001"
(rtosr 1.01562500) = "1.015625"
(rtosr 2.031250) = "2.03125"
(rtosr 3.062500) = "3.0625"
(rtosr 4.125000) = "4.125"

In the following revision the leading “0” is added back in if it was truncated.
Code: [Select]
;-------------------------------------------------------------------------------
; rtosr - Used to change a real number into a short real number string
; stripping off all trailing 0's.
; Arguments: 1
;   RealNum~ = Real number to convert to a short string real number
; Returns: ShortReal$ the short string real number value of the real number.
;-------------------------------------------------------------------------------
(defun rtosr (RealNum~ / Loop ShortReal$)
  (setq ShortReal$ (rtos RealNum~ 2 8))
  (if (= (substr ShortReal$ 1 1) ".")
    (setq ShortReal$ (strcat "0" ShortReal$))
  );if
  (setq Loop t)
  (while Loop
    (if (= (substr ShortReal$ (strlen ShortReal$) 1) "0")
      (setq ShortReal$ (substr ShortReal$ 1 (1- (strlen ShortReal$))))
      (setq Loop nil)
    );if
  );while
  (if (= (substr ShortReal$ (strlen ShortReal$) 1) ".")
    (setq ShortReal$ (substr ShortReal$ 1 (1- (strlen ShortReal$))))
  );if
  (if (= ShortReal$ "")
    (setq ShortReal$ "0")
  );if
  ShortReal$
);defun rtosr
This function was used in my GetExcel.lsp and DrawExcel.lsp functions.
« Last Edit: October 17, 2007, 09:54:14 PM by Terry Cadd »

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8704
  • AKA Daniel
Re: Assistance with rtosr function in AutoCAD 2008
« Reply #1 on: October 17, 2007, 11:20:59 AM »
Works here!

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: Assistance with rtosr function in AutoCAD 2008
« Reply #2 on: October 17, 2007, 11:38:06 AM »
Hi,

Quote
The rtos function truncates the leading 0 in numbers less than 0, where as in previous versions it left the 0.  In AutoCAD 2006 (rtos 0.12345678 2 8 ) = “0.12345678”.  In AutoCAD 2007 (rtos 0.12345678 2 8 ) = “.12345678”.

It doesn't depend the release but the DIMZIN sysvar value

_$ (setvar "dimzin" 4)
4
_$ (rtos 0.12345678 2 8 )
".12345678"
_$ (setvar "dimzin" 1)
1
_$ (rtos 0.12345678 2 8 )
"0.12345678"

Speaking English as a French Frog

terrycadd

  • Guest
Re: Assistance with rtosr function in AutoCAD 2008
« Reply #3 on: October 17, 2007, 11:44:11 AM »
So since I'm not checking for DIMZIN or changing it's value, is the new version the best way to go.  I know you are pretty slick with condensing code down to a minimum.
Terry

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: Assistance with rtosr function in AutoCAD 2008
« Reply #4 on: October 17, 2007, 12:29:47 PM »
Here's a purpose :

Code: [Select]
(defun rtosr (RealNum~ / old_dimzin ShortReal$)
  (and
    (numberp RealNum~)
    (setq old_dimzin (getvar "DIMZIN"))
    (setvar "DIMZIN" 8)
    (setq ShortReal$ (rtos RealNum~ 2 8))
    (setvar "DIMZIN" old_dimzin)
  )
  ShortReal$
)

_$ (rtosr 0.000000004)
"0"
_$ (rtosr 0.000000005)
"0.00000001"
_$ (rtosr 1.01562500)
"1.015625"
_$ (rtosr 2.031250)
"2.03125"
_$ (rtosr 3.062500)
"3.0625"
_$ (rtosr 4.125000)
"4.125"

Speaking English as a French Frog

terrycadd

  • Guest
Re: Assistance with rtosr function in AutoCAD 2008
« Reply #5 on: October 17, 2007, 12:32:54 PM »
Seven lines!  You're too cool.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Assistance with rtosr function in AutoCAD 2008
« Reply #6 on: October 17, 2007, 05:15:50 PM »
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.