Jump to content

LISP for creating new Line with a sum of numbers of length


Ivy Tr

Recommended Posts

I don't know if it makes sense. I wonder if there is a way to create a line that will ask for a total length and then you able to add multiple lengths in it, for example adding 5'+2'+3' then the line will be create as 10' automatically). Right now I just calculate  a total length myself and make a line with it.  It would save lots of time if I can just add them in while creating a new line. Thank you. 

Edited by Ivy Tr
Link to comment
Share on other sites

Welcome in the forum, Ivy Tr!

I think it is possible, but how do you wish to draw the line? Maybe you wish to click the start point, an other click to define the direction, and next enter those numbers? Or if you wish less user inputs, just enter those numbers end let the program to draw a horizontal line from the origin?

Please give us more data.

  • Like 1
Link to comment
Share on other sites

Just in case you didn't know the "direct distance entry" trick, try this:

Start the line command (say press L followed by <enter> or <space>). AutoCAD prompts you for the first point. Click on the screen to enter it. When AutoCAD prompts you for the next point, move the mouse to the desired place in order to define the direction (OSNAP is useful at this point!), but don't click. Leave the mouse in that place and enter from the keyboard (+ 5 2 3)<enter>

That should draw the line you are after. From there you can continue the line command, meaning to draw the next line, or press <enter> again to exit.

Please note that you must enter the plus sign first, a space, and next enter all the numbers to be added, separated by a space.

It is not so complicated... :)

  • Like 1
Link to comment
Share on other sites

Try this and let me know if it works for you.

;;; Creates a fixed line based on different given distances
;;; By Isaac A 20240124
;;; https://www.cadtutor.net/forum/topic/79256-lisp-for-creating-new-line-with-a-sum-of-numbers-of-length/?_fromLogin=1
(defun c:fl (/ a b c d dw e oe)
  (vl-load-com)
  (vla-startundomark (setq dw (vla-get-activedocument (vlax-get-acad-object))))
  (setq oe (getvar 'cmdecho))
  (setvar 'cmdecho 0)
  (setq a (getpoint "\nPick the starting point")
        b (getpoint "\Pick a second point for the direction" a)
        c (angle a b)
  )
  (initget 2)
  (if (not (= (setq d (getreal "\nType the starting distance: ")) (or 0 nil)))
     (progn
        (while (setq e (getreal "\nType the next distance: "))
           (setq d (+ d e))
        )
        (entmake (list '(0 . "LINE")
                        (cons 10 a)
                        (cons 11 (mapcar '+ a (list (* d (cos c)) (* d (sin c)))))
                       '(8  . "test_lines")
                       '(62 . 1)
                 )
        )
     )
  )
  (setvar 'cmdecho oe)
  (vla-endundomark dw)
  (princ)
)

 

  • Like 1
Link to comment
Share on other sites

On 1/24/2024 at 8:48 PM, Isaac26a said:
;;; Creates a fixed line based on different given distances

This code creates a line with a total length of: 1 distance + 2 distance + 3 distance + ...

Is it possible to do this with a polyline so that these part are visible?

line_polyline.png

Link to comment
Share on other sites

Yes, this is possible, although no one think a lisp is needed because you can do this using the polyline command, but just to keep programming here it is my version of it

;;; Creates a fixed lwpolyline based on different given distances
;;; By Isaac A 20240128
;;; https://www.cadtutor.net/forum/topic/79256-lisp-for-creating-new-line-with-a-sum-of-numbers-of-length/
(defun c:fpl (/ a b c d dw e f l oe)
  (vl-load-com)
  (vla-startundomark (setq dw (vla-get-activedocument (vlax-get-acad-object))))
  (setq oe (getvar 'cmdecho))
  (setvar 'cmdecho 0)
  (setq a (getpoint "\nPick the starting point")
        b (getpoint "\Pick a second point for the direction" a)
        c (angle a b)
        f (list a)
  )
  (initget 2)
  (if (not (= (setq d (getreal "\nType the starting distance: ")
		    f (append f (list (polar a c d))) ) (or 0 nil)))
     (progn
        (initget 6)
        (while (setq e (getreal "\nType the next distance: "))
           (setq d (+ d e)
                 f (append f (list (polar a c d)))
           )
        )
        (entmakex (append (list '(0   . "LWPOLYLINE")
                                '(100 . "AcDbEntity")
                                '(410 . "Model")
                                '(8   . "Test_lwpolylines")
                                '(62  . 40)
                                '(100 . "AcDbPolyline")
                                 (cons 90 (length f))
                          )
                          (mapcar (function (lambda (l) (cons 10 l))) f)
                  )
        )
     )
  )
  (setvar 'cmdecho oe)
  (vla-endundomark dw)
  (princ)
)

 

  • Like 2
Link to comment
Share on other sites

51 minutes ago, Isaac26a said:
;;; Creates a fixed lwpolyline based on different given distances

@Isaac26a thanks!  👏

In this code, it is very valuable that the polyline preserves the angle,
if you draw a polyline in the usual way, the angle is not supported...

Link to comment
Share on other sites

Using a while maybe 

 

(setq tot 0.0)
(while (setq d (getreal "\nEnter distance Enter to exit "))
(setq tot (+ tot d))
)

 

Link to comment
Share on other sites

On 1/24/2024 at 5:29 AM, fuccaro said:

Just in case you didn't know the "direct distance entry" trick, try this:

Start the line command (say press L followed by <enter> or <space>). AutoCAD prompts you for the first point. Click on the screen to enter it. When AutoCAD prompts you for the next point, move the mouse to the desired place in order to define the direction (OSNAP is useful at this point!), but don't click. Leave the mouse in that place and enter from the keyboard (+ 5 2 3)<enter>

That should draw the line you are after. From there you can continue the line command, meaning to draw the next line, or press <enter> again to exit.

Please note that you must enter the plus sign first, a space, and next enter all the numbers to be added, separated by a space.

It is not so complicated... :)

 this is exactly what I need, it work great with decimal number, I just wish it works with fraction number as I mainly work with feets and inches. 

Link to comment
Share on other sites

On 1/24/2024 at 10:48 AM, Isaac26a said:

Try this and let me know if it works for you.

;;; Creates a fixed line based on different given distances
;;; By Isaac A 20240124
;;; https://www.cadtutor.net/forum/topic/79256-lisp-for-creating-new-line-with-a-sum-of-numbers-of-length/?_fromLogin=1
(defun c:fl (/ a b c d dw e oe)
  (vl-load-com)
  (vla-startundomark (setq dw (vla-get-activedocument (vlax-get-acad-object))))
  (setq oe (getvar 'cmdecho))
  (setvar 'cmdecho 0)
  (setq a (getpoint "\nPick the starting point")
        b (getpoint "\Pick a second point for the direction" a)
        c (angle a b)
  )
  (initget 2)
  (if (not (= (setq d (getreal "\nType the starting distance: ")) (or 0 nil)))
     (progn
        (while (setq e (getreal "\nType the next distance: "))
           (setq d (+ d e))
        )
        (entmake (list '(0 . "LINE")
                        (cons 10 a)
                        (cons 11 (mapcar '+ a (list (* d (cos c)) (* d (sin c)))))
                       '(8  . "test_lines")
                       '(62 . 1)
                 )
        )
     )
  )
  (setvar 'cmdecho oe)
  (vla-endundomark dw)
  (princ)
)

 

this works for me. Thanks Isaac

Link to comment
Share on other sites

  • 3 weeks later...
On 1/28/2024 at 9:09 PM, Isaac26a said:
;;; Creates a fixed lwpolyline based on different given distances
;;; By Isaac A 20240128

@Isaac26a

you can add to your lisp the ability to make sections of the same polyline in different colors,
for example, red 1 and blue 5?

PL_color.png

Link to comment
Share on other sites

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...