Author Topic: Number conversions  (Read 1989 times)

0 Members and 1 Guest are viewing this topic.

Biscuits

  • Swamp Rat
  • Posts: 502
Number conversions
« on: May 02, 2014, 02:02:41 PM »
Hi gang............long time no see.
Using Windows 7 and Autocad 2013

I have a huge project coming up where we need to change numbers in our bills of materials from fractions to 2 place decimal. This is all in regular text (not dimensions or attributes).
Examples:  1 1/2 to become 1.50, 2 to become 2.00, 3 11/16 to become 3.69, etc.

Two decimal places are required including trailing zeros.

I'm looking to create a routine that would allow a single or window selection.

I've searched the web with no success. How should I start something like this?
Thanks for any ideas.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Number conversions
« Reply #1 on: May 02, 2014, 03:54:16 PM »
Start with Pseudo Code.
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

ymg

  • Guest
Re: Number conversions
« Reply #2 on: May 02, 2014, 07:00:16 PM »
It very much  depends on your data.

Here is a small routine that I had in another program that
I have quickly adapted.

Code: [Select]
; Split the supplied string lstr on delimiters list                           ;
; Otherwise returns a list of string segments.                                ;

(defun parsestr (lstr delimit / bpos epos error segm rtnlst)
     (setq bpos 1)
     (while (and delimit (not error))
(if (setq epos (vl-string-search (car delimit) lstr))
    (setq   segm (substr lstr bpos (- (+ epos 1) bpos))
            segm (vl-string-trim " " segm)
          rtnlst (cons segm rtnlst)
            bpos (+ epos (strlen (car delimit)) 1)
         delimit (cdr delimit)
            )
    (setq error t)
)
     )  
     (setq rtnlst (reverse (cons (substr lstr bpos (- (+ (strlen lstr) 1) bpos)) rtnlst))
           rtnlst (vl-remove "" rtnlst)
     )

)

(defun c:test (/ data delimiters  l n)
  (setq data '("1 1/2" " 2" "3 11/16"))
  (setq delimiters '(" " "/"))
  (foreach num data
     (setq l (parsestr num delimiters))
     (setq n (+ (atof (car l)) (if (cadr l) (/ (atof (cadr l)) (atof (caddr l))) 0)))
     (princ (strcat "\n" num " -> " (rtos n 2 2)))
  )
  (princ)
)

Quote
Result for supplied Data List:

1 1/2 -> 1.50
 2 -> 2.00
3 11/16 -> 3.69

ymg

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: Number conversions
« Reply #3 on: May 03, 2014, 01:08:23 AM »
Another thread concerning extracting the numbers out of text (including fractions) so you can convert them to other representations: http://www.theswamp.org/index.php?topic=38779.0
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

Stefan

  • Bull Frog
  • Posts: 319
  • The most I miss IRL is the Undo button
Re: Number conversions
« Reply #4 on: May 03, 2014, 04:26:41 AM »
Code - Auto/Visual Lisp: [Select]
  1. _$ (setvar 'dimzin 1)
  2. 1
  3. _$ (rtos (distof "1 1/2") 2 2)
  4. "1.50"
  5. _$ (rtos (distof "2") 2 2)
  6. "2.00"
  7. _$ (rtos (distof "3 11/16") 2 2)
  8. "3.69"
  9. _$

ymg

  • Guest
Re: Number conversions
« Reply #5 on: May 03, 2014, 10:35:33 AM »
distof is certainly simpler, though we needed the the double quotes at the end.

ymg

mailmaverick

  • Bull Frog
  • Posts: 493
Re: Number conversions
« Reply #6 on: May 03, 2014, 02:22:41 PM »
Dear Stefan, when we supply fraction to distof, thhen why doesn't we need to give mode as 5 ?.

(distof string [mode])
Modes :
1 Scientific
2 Decimal
3 Engineering (feet and decimal inches)
4 Architectural (feet and fractional inches)
5 Fractional

Stefan

  • Bull Frog
  • Posts: 319
  • The most I miss IRL is the Undo button
Re: Number conversions
« Reply #7 on: May 03, 2014, 03:43:24 PM »
Dear Stefan, when we supply fraction to distof, thhen why doesn't we need to give mode as 5 ?.

(distof string [mode])
Modes :
1 Scientific
2 Decimal
3 Engineering (feet and decimal inches)
4 Architectural (feet and fractional inches)
5 Fractional
Modes 1, 2 and 5 are pretty much the same: the integer part and the fractional part. Autocad does recognize the fractional part either as floating point or as a fraction of two integers. For example, try "scale" command and specify 0.25 as scale factor. Now try again and enter 1/4 as scale factor. The results are the same (as a side note, I use this a lot instead of classical "reference" option in scale command  :-)).
The mode is optional in distof function, but it must be used when the first argument (string) is engineering or architectural type.

ymg

  • Guest
Re: Number conversions
« Reply #8 on: May 03, 2014, 04:11:43 PM »
stefan,

I also find distof a little finicky as to the format.

Say I have as  a string "1  1/2" that is two spaces I will get nil as a result.

Quote
(distof "1  1/2")
nil
_$ (distof "  1 1/2  ")
nil
_$ (distof " 1 1/2")
nil

As a minimum we would need to vl-string-trim what we are passing to it.

ymg

ymg

  • Guest
Re: Number conversions
« Reply #9 on: May 03, 2014, 05:26:09 PM »
Biscuits,

To help you any further we would need at least a sample drawing

ymg

Biscuits

  • Swamp Rat
  • Posts: 502
Re: Number conversions
« Reply #10 on: May 05, 2014, 11:58:54 AM »
Took a bit of stumbling but I got something here that does the job.
Thanks to everyone for your help.
Biscuits

Code: [Select]

;  This routine will convert ( text only ) inches w/fractions to 2 place decimal
;  EXAMPLE: "46 11/16" will convert to "46.69"
;  Caution: Existing decimals will convert to 2 places if included in the selection

(defun c:Frac-Dec (/ EntProps SS Imp Dist)

  (if (setq SS (ssget (list (cons 0 "TEXT"))))

    (foreach EntName (vl-remove-if 'listp (mapcar 'cadr (ssnamex SS)))

      (if (setq EntProps (entget EntName)
Imp (cdr (assoc 1 EntProps))
Dist (distof Imp 4)
  )

(entmod
  (subst
    (cons 1

  (strcat (rtos (cvunit Dist "INCHES" "INCHES") 2 2))

    )
    (cons 1 Imp)
    EntProps
  )
)
      )
    )
  )
  (princ)
)