Visual LISP, AutoLISP and General Customization
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Decimal numbers

1 REPLY 1
Reply
Message 1 of 2
paulof
283 Views, 1 Reply

Decimal numbers

Hi guys,

I have this piece of magic code that increment or reduce a value on text numbers.

I can not apply decimal places in texts whose value is 100.00 for example. In others such as 100.35 works fine.
Any suggestions to correct this situation?

;c:elevar
;User is prompted to select objects for modification
;User is prompted for Value to add to number
;User is prompted for Number of Decimal Places
;Retrieves a number from the string of each text object searching the string from beginning to end
;Return first instance of a number
;Ignores text objects that have no number in them
;Adds Value to Number according to decimal format
;Replaces the string and modifies entity
;Returns (through princ) the number of entities modified


(defun c:elevar ()
(setq ss (ssget '((0 . "TEXT"))))
(setq strval (getreal "\nQual o valor a acrescentar? (para diminuir colocar o sinal -). "))
(setq cnt* 0)
(if (= nil prec)
(setq prec 3))
(if (= nil strval)
(progn
(princ "\nUser Cancelled")(exit))
(progn 
(setq prec* (getreal (strcat "\nQuantas casas decimais [" (itoa prec) "]?")))
(if (= prec* nil)
(setq prec* (fix prec))
(progn 
(setq prec* (fix prec*))
(setq prec prec*))
))
)
(setq sslen (sslength ss))
(setq entmodnum 0)
(while (/= cnt* sslen)
(setq ent (ssname ss cnt*))
(setq entd (entget ent))
(setq str (cdr (assoc 1 entd)))
(setq cnt* (+ cnt* 1))
(setq numpos* (numpos str))
(if (/= numpos* nil)
(progn
(setq val (substr str (nth 0 numpos*) (nth 1 numpos*)))
(setq fmtval (read val))
(setq nval (+ strval fmtval))
(setq valstr (rtos nval 2 prec*)) 
(setq nstr (vl-string-subst valstr val str))
(setq elist (list (cons -1 ent) (cons 1 nstr)))
(entmod elist)
(setq entmodnum (+ entmodnum 1))
))

)
(princ (strcat "\n" (vl-princ-to-string entmodnum) " Entidades modificadas." ))
(princ)
)
;end elevar



;numpos
;arguments = string
;Searches string for a number from the beginning to the end returning the first instance of the number
;Returns list ( begin_number_position length_of_number )
;Example (numpos "Hellosam") = nil
;Example (numpos "Hellosam 1") = (10 1)
;Example (numpos "Hellosam 1: 2") = (10 1)

(defun numpos (str)
(setq strlist (vl-string->list str))
(setq bgnpos nil)
(setq cnt 0)
(setq ignore "F")
(foreach ch strlist (progn
(if (or (and (>= ch 48) (<= ch 57)) (= ch 46))
(progn ;ch = number OR ch = .
(if (and (= nil bgnpos)(= ignore "F"))
(setq bgnpos cnt)))
(progn ;ch does not = number OR ch does not = .
(if (and (/= nil bgnpos) (= ignore "F"))
(progn 
(setq numlen (- cnt bgnpos))
(setq ignore "T")))))
(setq cnt (+ 1 cnt)))
)
(if (= bgnpos nil)
(setq poslst nil)
(progn
(setq bgnpos (+ 1 bgnpos))
(setq poslst (list bgnpos numlen))))
)
;end numpos




;posnum
;arguments = string
;Searches string for a number from the end to the beginning returning the first instance of the number
;Returns list ( begin_number_position length_of_number )
;Example (posnum "Hellosam") = nil
;Example (posnum "Hellosam 1") = (10 1)
;Example (posnum "Hellosam 1: 2") = (13 1)

(defun posnum (str)
(setq strlist (vl-string->list str))
(setq strlist (reverse strlist))
(setq bgnpos nil)
(setq cnt 0)
(setq ignore "F")
(foreach ch strlist (progn
(if (or (and (>= ch 48) (<= ch 57)) (= ch 46))
(progn ;ch = number OR ch = .
(if
(and (= nil bgnpos)(= ignore "F"))(setq bgnpos cnt)))

(progn ;ch does not = number OR ch does not = .
(if (and (/= nil bgnpos) (= ignore "F"))
(progn 
(setq numlen (- cnt bgnpos))
(setq ignore "T")))))
(setq cnt (+ 1 cnt)))
)
(if (= bgnpos nil)
(setq poslst nil)
(progn
(setq bgnpos (- (length strlist) (+ (- bgnpos 1) numlen)))
(setq poslst (list bgnpos numlen))))
)
;end posnum 

 

paulof
1 REPLY 1
Message 2 of 2
pbejse
in reply to: paulof


@Anonymous wrote:

Hi guys,

I have this piece of magic code that increment or reduce a value on text numbers.

I can not apply decimal places in texts whose value is 100.00 for example. In others such as 100.35 works fine.
Any suggestions to correct this situation?


set dimzin to 0, you probably had it on 8

 

 

8 Suppresses trailing zeros in decimal dimensions (for example, 12.5000 becomes 12.5)

DIMZIN also affects real-to-string conversions performed by the AutoLISP rtos and angtos functions....

 

HTH

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report

”Boost