Author Topic: Draw lines over list of points  (Read 3550 times)

0 Members and 1 Guest are viewing this topic.

Nima2018

  • Newt
  • Posts: 30
Draw lines over list of points
« on: October 10, 2018, 10:37:44 AM »
Hi

I have a list of points :

(setq lst '((5.79273 22.3288 120.0) (16.4714 12.039 125.0) (33.6864 7.97628 125.0) (45.9991 13.5198 125.0)))

I want connect them to each other by line (not by polyline),so I wrote a function but it fire up an error  on
(VLAX-3D-POINT (NTH (+ 1 i) lst2)) .

Please somebody tell me about this error and how to fix it .

Code: [Select]
(DEFUN Dst (lst2 / i lin ds endpt startpt midpt str)
  (SETQ i 0)
  (WHILE (< i (- (LENGTH lst2) 1))
    (SETQ lin (VLA-ADDLINE
(VLAX-3D-POINT (NTH i lst2))
(VLAX-3D-POINT (NTH (+ 1 i) lst2))
      ) ;_ end of VLA-ADDLINE
    ) ;_ end of setq
    (SETQ ds  (VLA-GET-LENGTH lin)
          startpt (VLAX-GET lin 'startpoint)
  endpt (VLAX-GET lin 'endpoint)
  midpt (POLAR endpt (ANGLE endpt startpt) (/ (DISTANCE endpt startpt) 2.0))
          str (vla-addtext midpt 1 (RTOS ds 2 2))
    ) ;_ end of setq
 
    (VLA-PUT-ROTATION str (VLA-GET-ANGLE lin))
    (SETQ i (1+ i))
  ) ;_ end of while
  (PRINC)

) ;_ end of defun

ronjonp

  • Needs a day job
  • Posts: 7526
Re: Draw lines over list of points
« Reply #1 on: October 10, 2018, 10:47:38 AM »
Perhaps:
Code - Auto/Visual Lisp: [Select]
  1. (setq lst '((5.79273 22.3288 120.0)
  2.             (16.4714 12.039 125.0)
  3.             (33.6864 7.97628 125.0)
  4.             (45.9991 13.5198 125.0)
  5.             (45.9991 14 125.0)
  6.            )
  7. )
  8. (if (> (length lst) 1)
  9.   (mapcar '(lambda (a b) (entmakex (list '(0 . "line") (cons 10 a) (cons 11 b)))) lst (cdr lst))
  10. )

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

Nima2018

  • Newt
  • Posts: 30
Re: Draw lines over list of points
« Reply #2 on: October 10, 2018, 10:54:18 AM »
Thanks ronjonp for your reply

Your code is very interesting,but I want do it by vla-addline method . please tell me about that .

ronjonp

  • Needs a day job
  • Posts: 7526
Re: Draw lines over list of points
« Reply #3 on: October 10, 2018, 11:27:26 AM »
Thanks ronjonp for your reply

Your code is very interesting,but I want do it by vla-addline method . please tell me about that .

First you need to fulfill the arguments needed to add the line.
Code - Auto/Visual Lisp: [Select]
  1.   'addline
  2.   '(0. 0. 0.)
  3.   '(1. 1. 1.)
  4. )

Here's most of your? code intact:
Code - Auto/Visual Lisp: [Select]
  1. (defun dst (space lst / ds endpt lin midpt startpt str)
  2.   ;; RJP - While we have two items in the list
  3.   (while (cadr lst)
  4.     ;; https://knowledge.autodesk.com/search-result/caas/CloudHelp/cloudhelp/2016/ENU/AutoCAD-ActiveX/files/GUID-26C95029-14BB-40B9-9987-49EFC980CB9D-htm.html
  5.     (setq lin (vlax-invoke space 'addline (car lst) (cadr lst)) ;_ end of VLA-ADDLINE
  6.     ) ;_ end of setq
  7.     (setq ds      (vla-get-length lin)
  8.           startpt (vlax-get lin 'startpoint)
  9.           endpt   (vlax-get lin 'endpoint)
  10.           midpt   (polar endpt (angle endpt startpt) (/ (distance endpt startpt) 2.0))
  11.           ;; RJP - fixed multiple errors in line below
  12.           ;;https://knowledge.autodesk.com/search-result/caas/CloudHelp/cloudhelp/2015/ENU/AutoCAD-ActiveX/files/GUID-9D3A7DE5-4219-42D8-A2A2-20C723F01ABC-htm.html
  13.           str     (vla-addtext space (rtos ds 2 2) (vlax-3d-point midpt) 1.0)
  14.     ) ;_ end of setq
  15.     (vla-put-rotation str (angle startpt endpt))
  16.     ;; RJP - Remove the first item
  17.     (setq lst (cdr lst))
  18.   ) ;_ end of while
  19.   (princ)
  20. ) ;_ end of defun
  21.  
  22. (setq lst '((5.79273 22.3288 120.0)
  23.             (16.4714 12.039 125.0)
  24.             (33.6864 7.97628 125.0)
  25.             (45.9991 13.5198 125.0)
  26.             (45.9991 14 125.0)
  27.            )
  28. )
  29. (dst ms lst)
  30.  

« Last Edit: October 10, 2018, 11:40:15 AM by ronjonp »

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: Draw lines over list of points
« Reply #4 on: October 10, 2018, 11:35:36 AM »
Try this perhaps?
Code - Auto/Visual Lisp: [Select]
  1. (setq mspace (vla-get-modelspace thisdrawing))
  2.  
  3. (defun drwlines (points / )
  4.   (mapcar '(lambda (pt1 pt2)
  5.          (vla-addline mspace (vlax-3d-point pt1) (vlax-3d-point pt2)))
  6.       points (cdr points)))
  7.  
  8. (defun addlengths (lines / )
  9.   (mapcar '(lambda (lin / len start end mid txt)
  10.          (setq len (vla-get-length lin)
  11.            start (vlax-get lin 'startpoint)
  12.            end (vlax-get lin 'endpoint)
  13.            mid (polar start (angle start end) (/ len 2))
  14.            txt (vla-addtext mspace (rtos len 2 2) (vlax-3d-point mid) 1))
  15.          (vla-put-rotation txt (vla-get-angle lin))
  16.          txt)
  17.       lines))
  18.  
  19. (setq lst '((5.79273 22.3288 120.0)
  20.         (16.4714 12.039 125.0)
  21.         (33.6864 7.97628 125.0)
  22.         (45.9991 13.5198 125.0)
  23.         (45.9991 14 125.0)
  24.        )
  25. )
  26.  
  27. (addlengths (drwlines lst))
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

BIGAL

  • Swamp Rat
  • Posts: 1396
  • 40 + years of using Autocad
Re: Draw lines over list of points
« Reply #5 on: October 16, 2018, 04:37:14 AM »
Do understand the vl approach but this works
Code: [Select]
(setq lst '((5.79273 22.3288 120.0)
        (16.4714 12.039 125.0)
        (33.6864 7.97628 125.0)
        (45.9991 13.5198 125.0)
        (45.9991 14 125.0)
       )
)
(command "_line")
(while (= (getvar "cmdactive") 1 )
(repeat (setq x (length lst))
(command (nth (setq x (- x 1)) lst))
)
(command "")
)
A man who never made a mistake never made anything