Author Topic: Trailing zero issue  (Read 3553 times)

0 Members and 1 Guest are viewing this topic.

Biscuits

  • Swamp Rat
  • Posts: 502
Trailing zero issue
« on: September 14, 2015, 08:55:47 AM »
Good morning all. In using AutoCAD 2013 with Windows 7, the following routine is designed to get the scale of a block called "Border" and rename a particular dimstyle to that value. Regardless of what my units are set at, I get trailing zero's in the renaming process. Any ideas how to correct this issue? Any help would be appreciated...thanks

Code: [Select]
(defun C:STUFF  (/ EE ENT ELIST X)


(setq flag (tblsearch "BLOCK" "Border")) ;looking for Border
;then do this if exist
(if flag
(progn ;grouping statement
 
(setq ee nil)
(setq ee (ssget "X" (list (cons 2 "Border"))))
(setq ent (ssname ee 0)) ;
(setq elist (entget ent)) ; entity list
(setq XB (cdr (assoc 41 elist))) ; X Scale -- 42 Y -- 43 Z


   (princ)

(command "-rename" "D" "Default - Fraction (ANSI)" XB  )

) ;end statement group
;else - and give warning if it doesn't exist
(alert "Border does not exist!")
) ; end if
)

ronjonp

  • Needs a day job
  • Posts: 7528
Re: Trailing zero issue
« Reply #1 on: September 14, 2015, 09:07:07 AM »
Just a guess:
Code - Auto/Visual Lisp: [Select]

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

Biscuits

  • Swamp Rat
  • Posts: 502
Re: Trailing zero issue
« Reply #2 on: September 14, 2015, 09:16:19 AM »
Thank you Sensei.... I added decimal in front of the zero and it works perfect.

ronjonp

  • Needs a day job
  • Posts: 7528
Re: Trailing zero issue
« Reply #3 on: September 14, 2015, 10:17:30 AM »
Thank you Sensei.... I added decimal in front of the zero and it works perfect.
:)  Glad to help.

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

Biscuits

  • Swamp Rat
  • Posts: 502
Re: Trailing zero issue
« Reply #4 on: September 16, 2015, 02:08:07 PM »
One issue has come up with this. When a scale of 10, 20, 30, etc. is found the dimstyle in question is renamed 1, 2, 3 etc..
I only want the decimal and following zeros removed. Any ideas?
Thanks

Crank

  • Water Moccasin
  • Posts: 1503
Re: Trailing zero issue
« Reply #5 on: September 16, 2015, 02:23:18 PM »
Study the lisp function RTOS and the DIMZIN variable.
Vault Professional 2023     +     AEC Collection

ronjonp

  • Needs a day job
  • Posts: 7528
Re: Trailing zero issue
« Reply #6 on: September 16, 2015, 02:39:04 PM »
Perhaps this:
Code - Auto/Visual Lisp: [Select]
  1. (setq xb 30.25000)
  2. (if (= (type xb) 'real)
  3.   (itoa xb)
  4. )

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

Lee Mac

  • Seagull
  • Posts: 12913
  • London, England
Re: Trailing zero issue
« Reply #7 on: September 16, 2015, 03:53:30 PM »
Alternatively, as Crank suggests:

Code - Auto/Visual Lisp: [Select]
  1. ;; Number to String  -  Lee Mac
  2. ;; Converts a supplied numerical argument to a string
  3.  
  4. (defun LM:num->str ( num / dim rtn )
  5.     (if (equal num (atoi (rtos num 2 0)) 1e-8)
  6.         (rtos num 2 0)
  7.         (progn
  8.             (setq dim (getvar 'dimzin))
  9.             (setvar 'dimzin 8)
  10.             (setq rtn (rtos num 2 8))
  11.             (setvar 'dimzin dim)
  12.             rtn
  13.         )
  14.     )
  15. )
Code - Auto/Visual Lisp: [Select]
  1. _$ (LM:num->str 30.2500)
  2. "30.25"
  3. _$ (LM:num->str 30)
  4. "30"

Biscuits

  • Swamp Rat
  • Posts: 502
Re: Trailing zero issue
« Reply #8 on: September 16, 2015, 04:52:57 PM »
Works great!
Thanks a lot guys.........we're cookin' with gas now.

ChrisCarlson

  • Guest
Re: Trailing zero issue
« Reply #9 on: September 18, 2015, 08:16:07 AM »
Alternatively, as Crank suggests:

Code - Auto/Visual Lisp: [Select]
  1. ;; Number to String  -  Lee Mac
  2. ;; Converts a supplied numerical argument to a string
  3.  
  4. (defun LM:num->str ( num / dim rtn )
  5.     (if (equal num (atoi (rtos num 2 0)) 1e-8)
  6.         (rtos num 2 0)
  7.         (progn
  8.             (setq dim (getvar 'dimzin))
  9.             (setvar 'dimzin 8)
  10.             (setq rtn (rtos num 2 8))
  11.             (setvar 'dimzin dim)
  12.             rtn
  13.         )
  14.     )
  15. )
Code - Auto/Visual Lisp: [Select]
  1. _$ (LM:num->str 30.2500)
  2. "30.25"
  3. _$ (LM:num->str 30)
  4. "30"

If I'm reading this correctly its an expanded conversion which will take any numerical value (real, integer and angle) and export as a string?

Lee Mac

  • Seagull
  • Posts: 12913
  • London, England
Re: Trailing zero issue
« Reply #10 on: September 19, 2015, 08:59:29 AM »
If I'm reading this correctly its an expanded conversion which will take any numerical value (real, integer and angle) and export as a string?

I'm not quite sure what you mean by 'expanded conversion', but the function I have posted will accept any numerical value and will convert the value to a decimal format string with up to 8 decimal places of precision, excluding all trailing zeros. Doubles within 1e-8 of an integer value will be expressed as integers.

Note that there are only two types of numerical values in AutoLISP: integers (represented using signed 32-bit integers) and reals (represented using 64-bit double-precision floating point format); angles may be represented using either of these data types. However, it is usually more useful to use the angtos function for angle formatting.
« Last Edit: September 19, 2015, 09:06:30 AM by Lee Mac »