Author Topic: Sorting out a list of points  (Read 1917 times)

0 Members and 1 Guest are viewing this topic.

RussP

  • Guest
Sorting out a list of points
« on: July 17, 2018, 03:14:56 PM »
Hi everyone, (my first post)

I have a simple bit of code that is supposed to allow multiple user selected points, put them in a list and then set a variable to the last element in that list. What is puzzling me is that the code works as long as I leave out the line command, which is essential for the purpose of the routine. As soon as I add the line command, so the user can see where they are putting the line, pt2, which should be the last element in the list, becomes pt, the first element in the list. Here is the code:

Code: [Select]
(defun c:pointlist (/)
  (setq ptlist nil)
  (while
  (setq pt (getpoint "\nPick Point: "))
  (command-s ".line" pt)
  (setq ptlist (append ptlist (list pt)))
)
  (setq pt2 (last ptlist))
  (command-s ".line" pt2)
) [code\]
 

I'm sure there is something simple that I'm missing here. I would appreciate any help to point me in the right direction.

Thank you,

Russ Parker

kpblc

  • Bull Frog
  • Posts: 396
Re: Sorting out a list of points
« Reply #1 on: July 17, 2018, 03:30:08 PM »
Code - Auto/Visual Lisp: [Select]
  1. (defun c:check (/ adoc pt pt1)
  2.   (if (setq pt (getpoint "\nStart point <Cancel> : "))
  3.            (while (setq pt1 (getpoint pt "\nNext point <Cancel> : "))
  4.              (entmakex (list (cons 0 "line") (cons 10 pt) (cons 11 pt1)))
  5.              (setq pt pt1)
  6.              ) ;_ end of while
  7.            (vla-endundomark adoc)
  8.            ) ;_ end of progn
  9.     ) ;_ end of if
  10.   (princ)
  11.   ) ;_ end of defun
??
Sorry for my English.

JohnK

  • Administrator
  • Seagull
  • Posts: 10626
Re: Sorting out a list of points
« Reply #2 on: July 17, 2018, 03:37:14 PM »
Posted for nostalgia.
Code - Auto/Visual Lisp: [Select]
  1. (defun points (/ plst pt p1 sp cnt)
  2.   ;;===================================================================;
  3.   ;; Smadsens function for getting points and generating a list of     ;
  4.   ;; points.                                                           ;
  5.   ;;===================================================================;
  6.   (cond ((setq pt (getpoint "\nStart point: "))
  7.          (setq plst (cons pt plst)
  8.                p1   pt
  9.                sp   pt
  10.                cnt  2)))
  11.   (while pt
  12.          (initget "Close")
  13.          (setq pt (getpoint p1 (strcat "\nGimme point no. " (itoa cnt) ": ")))
  14.          (cond ((vl-consp pt)(grdraw p1 pt 2)
  15.                 (setq plst (cons pt plst) p1 pt))
  16.                ((= pt "Close")(grdraw p1 sp 2)
  17.                 (setq pt sp plst (cons pt plst) pt nil))
  18.                )
  19.          (setq cnt (1+ cnt))
  20.          )
  21.   (reverse plst)
  22. )
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

JohnK

  • Administrator
  • Seagull
  • Posts: 10626
Re: Sorting out a list of points
« Reply #3 on: July 17, 2018, 03:42:52 PM »
Oh?! I just found one of my old routines (I don't remember ever using this myself, so I imagine it was made for someone).

Code - Auto/Visual Lisp: [Select]
  1. (defun c:my-line
  2.   ;; My-Line
  3.   ;;
  4.   ;; This procedure is ment to be a replacement for the line command.
  5.   ;; Although it uses the defualt line command for its core opperation
  6.   ;; it changes the current layer to that of an entity selected.
  7.   ;; If an object is not detected, the current layer is used.
  8.   ;;
  9.   ;; UPDATE: See revisions. I removed the selection of an xref.
  10.   ;;
  11.   ;; By: John Kaul
  12.   ;; Date: 05.14.06
  13.   ;;
  14.   ;; NOTE: This app is still in BETA so here is a rev. log.
  15.   ;; Revison log:       0.1 -- initial trial
  16.   ;;                    0.2 -- Removed xrefs from becoming ``objects''.
  17.   ;;                    0.3 -- Cleaned up a variable left declaired.
  18.   ;;
  19.   ;; Author Notes:
  20.   ;;            o A Known limitation of this code is that the use
  21.   ;;              of transparent commands while in line command is
  22.   ;;              limited.
  23.  
  24.  
  25.          ( /
  26.              ;; variables
  27.              lay
  28.              x
  29.              ;; procedures...
  30.              line-vl-Put-ActiveLayer
  31.              line-GetPointObj
  32.              line-vl-put-ObjLayerCurrent
  33.              AweSh0t
  34.           )
  35.   (
  36.    (lambda ()
  37.      ;; get the point from the user.
  38.      (while (not (setq x (getpoint "\nSelect Point: ")))
  39.             (princ "\nYou did not select a point, please try again. ")) x)
  40.    )
  41.  
  42.      ;;
  43.      ;; set up error handler.
  44.      ;;
  45.      (defun AweSh0t (s / line-vl-Put-ActiveLayer
  46.                          line-GetPointObj
  47.                          line-vl-put-ObjLayerCurrent
  48.                          AweSh|t)
  49.        (setq *error* olderr
  50.              olderr  nil)
  51.        (setvar 'clayer lay)
  52.        (princ) )
  53.      (setq olderr *error* *error* AweSh0t)
  54.  
  55.      ;; and some other routines we will need.
  56.      (defun line-vl-Put-ActiveLayer (Name / x)
  57.        ;; (setq obj (getpointobj pnt))
  58.        (cond
  59.          (name
  60.            (and
  61.              (vla-put-ActiveLayer x (vla-add (vla-get-layers x ) Name))))) )
  62.  
  63.      (defun line-GetPointObj (pt / obj pt)
  64.        (setvar "LASTPOINT" pt)
  65.        (cond
  66.          ((ssget pt)
  67.           (setq pt (ssname (ssget pt) 0))
  68.           (cond
  69.              ;; disable xref objects from the list of items.
  70.              ;; if we get any further objects to eliminate, redo
  71.              ;; entire lisp.
  72.             ((assoc 2 (entget pt))
  73.              (not (assoc 1 (tblsearch "BLOCK" (cdr (assoc 2 (entget pt)))))))
  74.              ;; otherwise just create an object from picked point.
  75.             ((setq obj (vlax-ename->vla-object pt))))))
  76.        obj )
  77.  
  78.      (defun line-vl-put-ObjLayerCurrent (obj)
  79.        (cond (obj (line-vl-put-ActiveLayer (vlax-get-property obj 'Layer)))) )
  80.  
  81.  
  82.   ;; Now that we have support procedures set up, we can now get on with the work.
  83.   (setq lay (getvar 'clayer))
  84.  
  85.   (line-vl-put-ObjLayerCurrent (setq obj (line-getpointobj x)))
  86.   (princ "\n ")
  87.   (command "_line" x)
  88.   (while (eq (getvar 'cmdactive) 1)
  89.          (command PAUSE))
  90.   (AweSh0t nil)
  91. )
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

ronjonp

  • Needs a day job
  • Posts: 7527
Re: Sorting out a list of points
« Reply #4 on: July 17, 2018, 04:05:30 PM »
Some more food for thought:

Code - Auto/Visual Lisp: [Select]
  1. (defun c:pointlist (/ pt pt2)   ;<-This will localize the variable ( set to nothing )
  2.   ;; (setq ptlist nil)
  3.   (while ;; 'AND' makes sure that all calls return something
  4.          (and ;; if 'pt' exists skip the first prompt and go to second point
  5.               (or pt (setq pt (getpoint "\nPick Point: ")))
  6.               (setq pt2 (getpoint pt "\nPick Next Point: "))
  7.          )
  8.     ;; (command-s ".line" pt pt2)
  9.     ;; Can use entmake to avoid a caommand call
  10.     (entmakex (list (cons 0 "line") (cons 10 pt) (cons 11 pt2)))
  11.     ;; Not needed
  12.     ;; (setq ptlist (append ptlist (list pt)))
  13.     ;; Set the first point to the second point
  14.     (setq pt pt2)
  15.   )
  16.   ;; Exit routine quietly
  17.   (princ)
  18. )

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

RussP

  • Guest
Re: Sorting out a list of points
« Reply #5 on: July 17, 2018, 04:40:12 PM »
Thank you all for the input. The first examples are over my head. I don't understand what you guys are doing. Although I'm sure it would work, I wouldn't know why it did. Ronjonp showed me where I had unnecessary code and the fix for what I was trying to do. Since I am inserting this function into an existing routine it looks like it will fit nicely with the changes I've been shown. And I can now see where I went wrong. I was not creating a proper list that could be used by another variable. The (entmakex) is new to me. I will have to look that one up.

Thank you all again.

RussP

  • Guest
Re: Sorting out a list of points
« Reply #6 on: July 17, 2018, 05:18:07 PM »
Perhaps it would help if put the whole routine in for context. Here is the code:

Code - Auto/Visual Lisp: [Select]
  1. (defun C:SectTest ( / blk blk2 pt pt2 )
  2.   (initerr)
  3.   (setq blk "C:/Lisp/A2T-MASK.dwg"
  4.    blk2 "C:/Lisp/Tail.dwg"
  5.   )
  6.   (command-s "undo" "m")
  7.   (defun SetLayer ()
  8.    (setvar "cmdecho" 0)
  9.     (if (tblsearch "layer" "SYMBOLS")
  10.       (command-s "-layer" "set" "SYMBOLS" "")
  11.       (command-s "-layer" "make" "SYMBOLS" "c" 2 "" "")
  12.     )
  13.   )
  14.   (SetLayer)
  15.    (setvar "cmdecho" 0)
  16.    (setvar "orthomode" 1)
  17.    (setvar "dimscale" 1)
  18.    (setvar "osmode" 16)
  19.    (prompt (strcat "\Section Bubble Insertion Point: "))
  20.    (command-s "-insert" blk "s" "" "" "r" 0 pause)
  21.    (setvar "osmode" 767)
  22.      (while
  23.           (and
  24.             (or pt (setq pt (getpoint "\nStart of Cut Line : "))
  25.                    (setq pt2 (getpoint pt "\nEnd of Cut Line :")))
  26.      )
  27.         (entmakex (list (cons 0 "line") (cons 10 pt) (cons 11 pt2)))
  28.         (setq pt pt2)
  29.      )
  30.      (setvar "osmode" 16)
  31.      (command-s "-insert" blk2 pt2 "" "" pause "")
  32.     (reset)
  33.   (princ)
  34. )  

As you can see I've added the suggestion by ronjonp. But autocad is rejecting the entmakex and reporting the (cons 11) as incorrect. Not sure why. Thanks for helping.

Russ Parker
« Last Edit: July 18, 2018, 09:23:33 AM by CAB »

RussP

  • Guest
Re: Sorting out a list of points
« Reply #7 on: July 18, 2018, 10:30:32 AM »
It is fixed. I had the one of the blocks trying to insert at the wrong point. It works as it should.

Again, thank you for your help.

Russ Parker

ronjonp

  • Needs a day job
  • Posts: 7527
Re: Sorting out a list of points
« Reply #8 on: July 18, 2018, 11:20:15 AM »
Glad to see you're working through it. Don't hesitate to post questions when you hit a snag :) .


There are many ways to do this but here is an example of using lists to get, set and restore variables.
Code - Auto/Visual Lisp: [Select]
  1. ;; Store the vars and their current values in a list
  2. (setq vars (mapcar '(lambda (x) (cons x (getvar x))) '(cmdecho orthomode dimscale osmode)))
  3. ;;Returns ((cmdecho . 1) (orthomode . 0) (dimscale . 1.0) (osmode . 16386))
  4. ;; Change them to your liking
  5. (mapcar '(lambda (var val) (setvar (car var) val)) vars '(0 1 1 16))
  6. ;; Your code
  7. ;; Reset vars
  8. (mapcar '(lambda (x) (setvar (car x) (cdr x))) vars)
« Last Edit: July 18, 2018, 11:31:24 AM by ronjonp »

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC