Jump to content

removing 0's from decimal measurement...


Recommended Posts

Posted

quickly made a lisp and was wondering how do I remove the zero's

(defun c:test (/ dist p1 p2 ent)
 (setq dist (rtos (distance 
   (setq p1 (getpoint "\nSpecify first point: "))
   (setq p2 (getpoint p1 "\nSpecify second point: "))) 2))
 (setq ent (entget (car (entsel "\nSelect text to modify: "))))
 (entmod (subst (cons 1 dist)(assoc 1 ent) ent))
 (princ)
)

 

examples of what I mean

 

measurement 46.25 instead of 46.25000

or 42.375 instead of 42.37500. im not looking for (rtos dist 2 2) or ...dist 2 3)

I'm looking for some sort of condition that changes the precision automatically.

Posted

Try something like this:

 

 
(defun set-prec (num / precn numstr)
 (setq li (reverse (vl-string->list (rtos num 2 (setq precn 12)))))
 (while (= 48 (car li))
   (setq li (reverse (vl-string->list (rtos num 2 (setq precn (1- precn))))))
   )
 (setq numstr (rtos num 2 precn))
 )

 

(set-prec 1.123456789000) returns "1.123456789"

Posted

I've tried to learn as much as I can using AUTOLISP but the call of VLISP is too great

Posted (edited)

You mean like this?

 

(defun c:TEST (/ dist eName eData)
 (if 
   (and 
     (setq dist (vl-string-right-trim "0" (rtos (getdist "\nSpecify first point: "))))
     (setq eName (car (entsel "\nSelect text to modify: "))))
   (entmod (subst (cons 1 dist) (assoc 1 (setq eData (entget eName))) eData)))
 (princ)) ;_end defun

 

 

See, in this case, when you use rtos without a defined precision, it returns the entire real number as a string (no matter how many decimal places). Now pair that with the vl-string-right-trim function, and you have a returned string that dynamically changes in decimal precision based on the distance.

 

Edit: Without changing system variables

Edited by BlackBox
Typo
Posted

Should you want to avoid VL completely:

 

(defun c:test ( / d s )
 (if
   (and
     (setq d (getdist "\nSpecify Distance: "))
     (setq s (ssget "_:L" '((0 . "TEXT,MTEXT"))))
   )
   (
     (lambda ( i / e )
       (while (setq e (ssname s (setq i (1+ i))))
         (entupd
           (cdr
             (assoc -1
               (entmod
                 (subst
                   (cons 1
                     (
                       (lambda ( s )
                         (while (eq "0" (substr s (strlen s)))
                           (setq s (substr s 1 (1- (strlen s))))
                         )
                         s
                       )
                       (rtos d)
                     )
                   )
                   (assoc 1 (entget e)) (entget e)
                 )
               )
             )
           )
         )
       )
     )
     -1
   )
 )

 (princ)
)

Posted (edited)
Sorry for nit picking but

 

(rtos nil)

 

 

Please continue to nit pick, Lee... it's the only way I'm going to get better. :wink:

 

Perhaps, this is preferable:

 

(defun c:TEST (/ dist eName eData)
 (vl-load-com)
 (if 
   (and
     (setq dist (getdist "\nSpecify first point: "))
     (setq dist (vl-string-right-trim "0" (rtos dist)))
     (setq eName (car (entsel "\nSelect text to modify: "))))
   (entmod (subst (cons 1 dist) (assoc 1 (setq eData (entget eName))) eData)))
 (princ)) ;_end defun

Edited by BlackBox
Added (vl-load-com)
Posted
Please continue to nit pick, Lee... it's the only way I'm going to get better. :wink:

 

Perhaps, this is preferable:

 

(defun c:TEST (/ dist eName eData)
 (vl-load-com)
 (if 
   (and
     (setq dist (getdist "\nSpecify first point: "))
     (setq dist (vl-string-right-trim "0" (rtos dist)))
     (setq eName (car (entsel "\nSelect text to modify: "))))
   (entmod (subst (cons 1 dist) (assoc 1 (setq eData (entget eName))) eData)))
 (princ)) ;_end defun

 

 

(defun c:Foo (/ d e l)
 (if (and (setq d (getdist "\nSpecify distance: "))
          (setq e (car (entsel "\nSelect MText/Text to modify: ")))
          (vl-position (cdr (assoc 0 (setq l (entget e)))) '("MTEXT" "TEXT"))
     )
   (entmod (subst (cons 1 (vl-string-right-trim "0" (rtos d))) (assoc 1 l) l))
 )
 (princ)
)

Posted

To learn better methods, how can I improve on this code?

 

 

Distance between points should be twice the distance between point and start/end

 
(defun c:test (/ p1 p2 # ang ins)
 (setq dis   (distance (setq p1  (getpoint "\nSpecify first point :"))
                 (setq p2  (getpoint p1 "\nSpecify second point :")))
         #    (getint "\nSpecify number of evaps: ")  
         ang (angle p1 p2)
         ins  (polar p1 ang (/ dis (* 2 #)))
 )
 (repeat #
   (entmake
       (list
         (cons 0 "POINT")
         (cons 10 ins)
       )
     )
   (setq ins (polar ins ang (/ dis #)))
 )
 (princ)
)

Posted

Not sure what you were talking about double distances, so just spaced them evenly...

 


(defun c:test ( / p1 p2 a d )

 (if (and
       (setq p1 (getpoint "\nSpecify First Point: "))
       (setq p2 (getpoint "\nSpecify Second Point: " p1))
       (setq n*
         (cond
           (
             (getint
               (strcat "\nSpecify Number of Evaps <"
                 (itoa
                   (setq n* (cond ( n* ) ( 3 )))
                 )
                 "> : "
               )
             )
           )
           ( n* )
         )
       )
       (setq a (angle p1 p2) d (distance p1 p2))
     )
   (
     (lambda ( i )
       (repeat n*
         (entmake
           (list
             (cons 0 "POINT")
             (cons 10 (polar p1 a (* (setq i (1+ i)) (/ d (1- n*)))))
           )
         )
       )
     )
     -1
   )
 )

 (princ)
)
       

Posted
To learn better methods' date=' how can I improve on this code?

 

 

Distance between points should be twice the distance between point and start/end

 
(defun c:test (/ p1 p2 # ang ins)
 (setq dis   (distance (setq p1  (getpoint "\nSpecify first point :"))
                 (setq p2  (getpoint p1 "\nSpecify second point :")))
         #    (getint "\nSpecify number of evaps: ")  
         ang (angle p1 p2)
         ins  (polar p1 ang (/ dis (* 2 #)))
 )
 (repeat #
   (entmake
       (list
         (cons 0 "POINT")
         (cons 10 ins)
       )
     )
   (setq ins (polar ins ang (/ dis #)))
 )
 (princ)
)

[/quote']

 

I did this one the other day (it will also return specified begin and end point)...

 

(defun AT:DivideSegment (p1 p2 n)
 ;; Divide Segment
 ;; p1 - first point
 ;; p2 - second point
 ;; n  - number of desired segments (must be > 1)
 ;; Alan J. Thompson, 08.19.10
 (if (and (apply (function and) (mapcar (function vl-consp) (list p1 p2))) (> (fix n) 1))
   ((lambda (a s i / l)
      (repeat (1- (fix n)) (setq l (cons (polar p1 a (setq i (+ s i))) l)))
      (cons p1 (reverse (cons p2 l)))
    )
     (angle p1 p2)
     (/ (distance p1 p2) (fix n))
     0.
   )
 )
)

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...