Code Red > AutoLISP (Vanilla / Visual)

HOW TO CONVERT A TEXT WITH NUMBERS AND LETTERS IN A REAL NUM

(1/2) > >>

DEVITG:
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"

--- End quote ---

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

--- End quote ---

Anonymous:
Try this:

 
--- Code: ---

(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


--- End code ---

DEVITG:
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: ---
(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)
--- End code ---


I learn a lot,  :!:  :)  :lol:  :wink:

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

CAB:

--- Code: ---(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))))
)
--- End code ---

Navigation

[0] Message Index

[#] Next page

Go to full version