Line length round up to nearest 100

Line length round up to nearest 100

arvhee24
Contributor Contributor
2,559 Views
16 Replies
Message 1 of 17

Line length round up to nearest 100

arvhee24
Contributor
Contributor

Is there a LISP to round up the length of line to nearest 100?

it will be lengthen/shorten from both sides, starting from midpoint.

 

Thanks in advance 🙂

0 Likes
Accepted solutions (2)
2,560 Views
16 Replies
Replies (16)
Message 2 of 17

ВeekeeCZ
Consultant
Consultant
Accepted solution

Easy enough to write it quickly...

 

(defun c:LineLengthenTo100 ( / i ss ed p1 p2 pm ds)

  (if (setq ss (ssget "_:L" '((0 . "LINE"))))
    (repeat (setq i (sslength ss))
      (setq ed (entget (ssname ss (setq i (1- i))))
            p1 (cdr (assoc 10 ed))
            p2 (cdr (assoc 11 ed))
            pm (mapcar '(lambda (a b) (/ (+ a b) 2.)) p1 p2)
            ds (* 50. (atoi (rtos (/ (distance p1 p2) 100.) 2 0))))
      (entmod (append ed
                      (list (cons 10 (polar pm (angle p2 p1) ds))
                            (cons 11 (polar pm (angle p1 p2) ds)))))))
  (princ)
)

 

Message 3 of 17

arvhee24
Contributor
Contributor

Wow that was fast... Smiley Surprised

 

Thanks so much BeekeeCZ.

Appreciate it.. Cheers!

🙂

0 Likes
Message 4 of 17

ВeekeeCZ
Consultant
Consultant
Actually did you meant round UP to 100... or round to 100... ??
0 Likes
Message 5 of 17

arvhee24
Contributor
Contributor

I meant round off to 100. 🙂

But what I really need is to be rounded to 500 so i change the values of 50. to 250 and the 100 to 500 and fortunately, it works!

0 Likes
Message 6 of 17

ВeekeeCZ
Consultant
Consultant
OK then, glad to help!
Cheers!
Message 7 of 17

arvhee24
Contributor
Contributor

BeekeeCZ, if supposed i need it to round up to 500?

I mean if it is 1 then the result will be 500, and 501 will 1000...

 

I tried to edit it but, i had a very little, almost none, knowledge on writing LISP. Smiley Very Happy

If you get the time, can you modify it?

I can use the first one though, but this will be better if ever.

 

Sorry, and thanks. Smiley Happy

Cheers!

0 Likes
Message 8 of 17

ВeekeeCZ
Consultant
Consultant
Accepted solution

@arvhee24 wrote:

BeekeeCZ, if supposed i need it to round up to 500?

I mean if it is 1 then the result will be 500, and 501 will 1000...

... 

 


Sure, no trouble, I have the code ready to ship out since the morning. 🙂

 

(defun c:LineLengthenUp500 ( / i ss ed p1 p2 pm ds)

  (if (setq ss (ssget "_:L" '((0 . "LINE"))))
    (repeat (setq i (sslength ss))
      (setq ed (entget (ssname ss (setq i (1- i))))
            p1 (cdr (assoc 10 ed))
            p2 (cdr (assoc 11 ed))
            pm (mapcar '(lambda (a b) (/ (+ a b) 2.)) p1 p2)
            ds (* (1+ (fix (/ (distance p1 p2) 500.))) 250.))
      (entmod (append ed
                      (list (cons 10 (polar pm (angle p2 p1) ds))
                            (cons 11 (polar pm (angle p1 p2) ds)))))))
  (princ)
)

 

Message 9 of 17

arvhee24
Contributor
Contributor

Nice! Smiley Very Happy

Thank you so much BeekeeCZ, or is it Děkuji mnohokrát (google translate lol)

 

Cheers mate!

This is very useful to me. Smiley Very Happy

Message 10 of 17

ВeekeeCZ
Consultant
Consultant
You're welcome!

Skoro jsem uronil slzu jak mě to dojalo! 😉
Message 11 of 17

arvhee24
Contributor
Contributor

Hahaha! Smiley Very Happy

 

Thanks!

0 Likes
Message 12 of 17

john.uhden
Mentor
Mentor

That's the first time I have ever seen the use of the decimal character only to define a real.  Just think of all the key strokes I have squandered through the years.  Smiley Embarassed

John F. Uhden

Message 13 of 17

ВeekeeCZ
Consultant
Consultant

One more version of rounding up function. This one is little more sophisticated, written by Lee Mac, see HERE.

This time lines does not grow to heaven if you apply the function multiple times. 

 

 

(defun c:LineLengthenUp500 ( / LM:roundup i ss ed p1 p2 pm ds)

  (defun LM:roundup (n m) ; Lee Mac
    ((lambda (r) (cond ((equal 0.0 r 1e-8) n) ((< n 0) (- n r)) ((+ n (- m r))))) (rem n m)))
  
  (if (setq ss (ssget "_:L" '((0 . "LINE"))))
    (repeat (setq i (sslength ss))
      (setq ed (entget (ssname ss (setq i (1- i))))
            p1 (cdr (assoc 10 ed))
            p2 (cdr (assoc 11 ed))
            pm (mapcar '(lambda (a b) (/ (+ a b) 2.)) p1 p2)
            ds (/ (LM:roundup (distance p1 p2) 500) 2.))
      (entmod (append ed
                      (list (cons 10 (polar pm (angle p2 p1) ds))
                            (cons 11 (polar pm (angle p1 p2) ds)))))))
  (princ)
)
0 Likes
Message 14 of 17

Kent1Cooper
Consultant
Consultant

@john.uhden wrote:

That's the first time I have ever seen the use of the decimal character only to define a real.  Just think of all the key strokes I have squandered through the years.


You could save even more!  None of those decimals are actually needed in Posts 2 & 8.  The only time they might be necessary is in divisions, and you get an undesired integer result from a division that doesn't divide exactly only if both  numbers involved are integers.  If either  is a real, the result won't be rounded unnecessarily, so you're covered.  All divisions in those routines involve values that will always  be real numbers already [point coordinates in the 'pm' variable, and the result of a (distance) function in the 'ds' variable], without the need to make those other numbers into reals.

Kent Cooper, AIA
0 Likes
Message 15 of 17

john.uhden
Mentor
Mentor

Thank you, Kent.

I may be a pea-brain, as Tony T. called me once, but I'm not that dumb.

 

I wish I could find that thread.  It was really pretty humorous.

 

BTW, that should be "None of those decimals IS..." since none is singular (not one).

John F. Uhden

0 Likes
Message 16 of 17

Anonymous
Not applicable

where do I need to write this code in AutoCAD, so it will run?

0 Likes
Message 17 of 17

ВeekeeCZ
Consultant
Consultant

@Anonymous wrote:

where do I need to write this code in AutoCAD, so it will run?


 

HERE is short tutorial on how to use LISP.