Author Topic: HOW TO CONVERT A TEXT WITH NUMBERS AND LETTERS IN A REAL NUM  (Read 2992 times)

0 Members and 1 Guest are viewing this topic.

DEVITG

  • Bull Frog
  • Posts: 481
HOW TO CONVERT A TEXT WITH NUMBERS AND LETTERS IN A REAL NUM
« on: April 17, 2004, 02:15:36 PM »
I could have any of this combination of text on a drawing as TEXT they will be pick one by one .

Quote

"##395.89mm"
"@452,58##"
"452.36"
"785,52"

in fact any combination of numbers with text pre and or post separated with . or ,
How could I clean it to convert to real number separated by . as follow??
Quote

395.89
452.58
452.36
785.52
Location @ Córdoba Argentina Using ACAD 2019  at Window 10

Anonymous

  • Guest
HOW TO CONVERT A TEXT WITH NUMBERS AND LETTERS IN A REAL NUM
« Reply #1 on: April 17, 2004, 03:06:53 PM »
Try this:

 
Code: [Select]


(defun getnum (xstr)
  (if (= (type xstr) 'STR)
    (progn
      (setq num (strlen xstr)
            str ""
            flag nil
      )
      (repeat num
        (setq item (substr xstr 1 1))
        (cond
          ((and flag (or (= item ",") (= item ".")))
             (setq str (strcat str ".")
                   xstr (substr xstr 2)
             )
          )          
          ((numberp (read (substr xstr 1 1)))
             (setq str (strcat str item)
                   xstr (substr xstr 2)
             )
             (if (not flag) (setq flag T))
          )
          (T (setq xstr (substr xstr 2))
             (if flag (setq flag nil))
          )
        );cond
      );repeat
    );progn
  );if
  (read str)
);defun


DEVITG

  • Bull Frog
  • Posts: 481
just perfect ....
« Reply #2 on: April 17, 2004, 08:32:58 PM »
Hi Guest , it works good .

How it could check if the text is a TEXT , so it ask to select the text again
I use this defun to select
Code: [Select]

(defun selec ()
  (setq cif (entsel "select the text, only TEXT, not MTEXT"))
  (setq ecif (car cif))  
  (setq gcif (entget ecif))  
  (setq vcif (assoc 1 gcif))
  (setq vnum (cdr vcif))    
 )

(getnum vnum)


I learn a lot,  :!:  :)  :lol:  :wink:
Location @ Córdoba Argentina Using ACAD 2019  at Window 10

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
HOW TO CONVERT A TEXT WITH NUMBERS AND LETTERS IN A REAL NUM
« Reply #3 on: April 17, 2004, 08:53:35 PM »
This sort of Library Routine may suit you for ensuring that TEXT < or whatever >  is selected :-
http://www.vbdesign.net/expresso/showthread.php?postid=10070#post10070

;;; Arguments:
;;; msg : The prompt string.
;;; kwd : Initget keywords string.
;;; def : Value to return if response is <enter>.
;;; typelist   : Stringlist of entity types allowed for selection. If nil select anything.
;;; selectflag : If true nentsel permitted , otherwise use entsel.
;;; lockflag   : If true dont allow selection from locked layers.
;;;
;;; Note : Arguments may be set to nil
;;;
;;; Return output from (n)entsel, a key word, the default argument, or nil.
;;;
;; example1 : (kpsl_EntSel "Select Arc Object" nil nil (list "ARC" "CIRCLE") nil T)
;;   ==>    (<Entity name: 40bcd540> (-28175.1 154575.0 1250.0))
;; example2 : (kpsl_EntSel "Select Datum Line" nil nil (list "LINE") T T)  ; line in block
;;   ==>    (<Entity name: 4022c680> (-21613.1 142392.0 0.0)
;;   ((70.0 0.0 0.0) (0.0 70.0 0.0) (0.0 0.0 70.0) (-21611.9 142635.0 0.0))
;;                    (<Entity name: 4022c6b8>)    )

Code: [Select]

(if (setq vnum
           (kpsl_EntSel "Select TEXT Object" nil nil (list "TEXT") nil t)
    )
  (getnum (cdr (assoc 1 (entget (car vnum)))))
  (alert "Oooops")
)
 
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.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
HOW TO CONVERT A TEXT WITH NUMBERS AND LETTERS IN A REAL NUM
« Reply #4 on: April 17, 2004, 11:40:47 PM »
Code: [Select]
(defun selec (/ cif gcif loop vnum)
  (setq loop t)
  (while loop ; don't leave this loop until text is selected
    (while ; don't leave this loop until something is selected
      (null
        (setq cif (entsel "\nSelect the text, only TEXT, not MTEXT"))
      )
       (prompt "\nYou must select something.")
    ) ; end while
    (setq gcif (entget (car cif)))
    (if (= (cdr (assoc 0 gcif)) "TEXT")
      (setq loop nil) ; exit loop
      (prompt "\nERROR - object is not text. Try again.")
    )
  ) ; end while
  ;; got text, try to get extract number
  (getnum (setq vnum (cdr (assoc 1 gcif))))
)
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.

DEVITG

  • Bull Frog
  • Posts: 481
HOW TO CONVERT A TEXT WITH NUMBERS AND LETTERS IN A REAL NUM
« Reply #5 on: April 18, 2004, 01:32:06 PM »
Hi Kerry and Cab , thanks for it,  both works.
Location @ Córdoba Argentina Using ACAD 2019  at Window 10

xiaxiang

  • Guest
Re: HOW TO CONVERT A TEXT WITH NUMBERS AND LETTERS IN A REAL NUM
« Reply #6 on: July 26, 2012, 09:05:31 PM »
This sort of Library Routine may suit you for ensuring that TEXT < or whatever >  is selected :-
http://www.vbdesign.net/expresso/showthread.php?postid=10070#post10070

;;; Arguments:
;;; msg : The prompt string.
;;; kwd : Initget keywords string.
;;; def : Value to return if response is <enter>.
;;; typelist   : Stringlist of entity types allowed for selection. If nil select anything.
;;; selectflag : If true nentsel permitted , otherwise use entsel.
;;; lockflag   : If true dont allow selection from locked layers.
;;;
;;; Note : Arguments may be set to nil
;;;
;;; Return output from (n)entsel, a key word, the default argument, or nil.
;;;
;; example1 : (kpsl_EntSel "Select Arc Object" nil nil (list "ARC" "CIRCLE") nil T)
;;   ==>    (<Entity name: 40bcd540> (-28175.1 154575.0 1250.0))
;; example2 : (kpsl_EntSel "Select Datum Line" nil nil (list "LINE") T T)  ; line in block
;;   ==>    (<Entity name: 4022c680> (-21613.1 142392.0 0.0)
;;   ((70.0 0.0 0.0) (0.0 70.0 0.0) (0.0 0.0 70.0) (-21611.9 142635.0 0.0))
;;                    (<Entity name: 4022c6b8>)    )

Code: [Select]

(if (setq vnum
           (kpsl_EntSel "Select TEXT Object" nil nil (list "TEXT") nil t)
    )
  (getnum (cdr (assoc 1 (entget (car vnum)))))
  (alert "Oooops")
)
 

Hi Kerry
http://www.vbdesign.net/expresso/showthread.php?postid=10070#post10070
The link was broken for several years I think.
The function "kpsl_EntSel" is what I need now.
And many functions is pretty too,
like kpsl_savesysvar kpsl_on_error kpsl_ptos kpsl_dtr kpsl_rtd kpsl_deflection
      kpsl_midpoint   kpsl_getpoint
      kpsl_getint   kpsl_entsel
in this topic
http://www.theswamp.org/index.php?topic=15.0
Can you post here again?
Many thanks

chlh_jd

  • Guest
Re: HOW TO CONVERT A TEXT WITH NUMBERS AND LETTERS IN A REAL NUM
« Reply #7 on: July 27, 2012, 02:55:11 PM »
dose strings has "," ?
Code: [Select]
(defun foo (/ ss i e ent str num l)
  (prompt
    "\nSelect Text objects which Contain numbers (*#.#* or *#,#*) :"
  )
  (if (and (setq ss (ssget (list (cons 0 "TEXT") (cons 1 "*#[.,]#*"))))
   (setq i -1)
      )
    (progn
      (while (setq e (ssname ss (setq i (1+ i))))
(setq ent (entget e)
      str (cdr (assoc 1 ent))
      str (vl-string-translate "," "." str)
      num (getnum str)
)
(if num
  (setq l (cons num l))
)
      )
      (reverse l)
    )
  )
)

VVA

  • Newt
  • Posts: 166
Re: HOW TO CONVERT A TEXT WITH NUMBERS AND LETTERS IN A REAL NUM
« Reply #8 on: July 30, 2012, 06:04:29 AM »
Recursion help string->list?
Quote
Example:

(str->list "point.25.4cm.");=> ("point." 25.4 "cm.")
(str->list "point.25,4cm.");=> ("point." 25.4 "cm.")
(str->list "point.3/8cm.");=> ("point." 0.375 "cm.")
(str->list "qvf12qsdf125 5sf 56dfv2");=> ("qvf" 12 "qsdf" 125 " " 5 "sf " 56 "dfv" 2)