Author Topic: Unselect items (selection set)  (Read 1723 times)

0 Members and 1 Guest are viewing this topic.

JohnK

  • Administrator
  • Seagull
  • Posts: 10661
Unselect items (selection set)
« on: September 26, 2023, 12:53:30 PM »
I am trying to build a more robust command that will act differently if there are items selected but I cannot remember if there is a way to "unselect" items. I built this example code to help show you what I mean. This example will simply draw a line, however if there are items selected, the clayer will be set to the objects layer under the pickpoint. This will work but I want to "unselect the selectionset" because I don't actually need the selected items for anything other than a "flag to do something". So, my question is how can I 'unselect' items that are preselected before my command is called?

EXAMPLE:
Code - Auto/Visual Lisp: [Select]
  1. (defun C:B ( /
  2.              ;; -Function locals.
  3.              *error* *error-push-vars* getlayerfrompickedpoint entmake-line drawline getpoints
  4.              ;; -Variable locals.
  5.               pt1 ss
  6.               )
  7.   ;;
  8.   ;; This function will draw a line and adopt the layer of the ent
  9.   ;; under the picked point.
  10.   ;;
  11.   ;; TODO: 1. Only adopt the layer if there are items selected, otherwise just
  12.   ;;          draw a line with the current layer.
  13.   ;;            a. Unselect items slected.
  14.         (defun *error* (msg) (mapcar 'eval #error_lst#) (setq #error_lst# nil))
  15.         (defun *error-push-vars* (lst / push->lst)
  16.           (defun push->lst (sym lst) (set lst (cons sym (eval lst))))
  17.           (mapcar
  18.             (function
  19.               (lambda (x)
  20.                 (push->lst
  21.                   (list 'setvar (car x) (getvar (car x)))
  22.                   '#error_lst#)
  23.                 (if (cadr x) (setvar (car x) (cadr x)))))
  24.             lst
  25.             )
  26.           )
  27.  
  28.         (defun getlayerfrompickedpoint (pt /)
  29.           ;; (getlayerfrompickedpoint <pt>)
  30.           ;; Return the layer of the entity located at the pickpoint.
  31.           ;; If there isn't an entity or layer property, return the current layer.
  32.           ;;
  33.           ;; ARGS: pt - A point
  34.           ;;
  35.           ;; RETURNS: A layer name
  36.           (setvar 'LASTPOINT pt)
  37.           (setq layer (getvar 'CLAYER))                         ;; -Set default layer
  38.                                                                 ;; NOTE: This value is being set now/here
  39.                                                                 ;;       because it should be overridden in the
  40.                                                                 ;;       code block below. This is so that
  41.                                                                 ;;       the clayer is returned if no entity
  42.                                                                 ;;       is picked.
  43.           (cond
  44.             ((ssget pt)                                         ;; -if we have something...
  45.              (setq pt (ssname (ssget pt) 0))
  46.              (cond
  47.                ((assoc 2 (entget pt))                           ;; -disable xref objects from the list of items.
  48.                 (not (assoc 1 (tblsearch "BLOCK" (cdr (assoc 2 (entget pt)))))))
  49.                ((setq layer (cdr (assoc 8 (entget pt)))))       ;; -otherwise just get the layer of pt/ent at the picked point.
  50.              )
  51.             )
  52.           )
  53.           layer                                                 ;; -return the layer
  54.         )
  55.  
  56.         (defun entmake-line (p1 p2)
  57.           ;; Draws a line from two points.
  58.           ;;
  59.           ;; ARGS: p1 - A point
  60.           ;;       p2 - A Point
  61.           ;;
  62.           ;; RETURNS: A ent list.
  63.           (entmakex (list (cons 0 "LINE")
  64.                           (cons 10 p1)
  65.                           (cons 11 p2))) )
  66.  
  67.         (defun drawline (lst)
  68.           ;; A simple list processor that draws lines from the points
  69.           ;;
  70.           ;; ARGS: lst - A list of points
  71.           ;;
  72.           ;; RETURNS: nil
  73.           (cond
  74.             ((null lst) nil)
  75.             ((cadr lst)
  76.               (entmake-line (car lst) (cadr lst))               ;; -draw a line from the
  77.                                                                 ;;  first and second point in list
  78.               (drawline (cdr lst)))))                           ;; -send the remaining list back
  79.                                                                 ;;  through again to draw another line.
  80.  
  81.         (defun getpoints ( pt / pt p1 sp cnt)
  82.           ;; This is a very basterdized version of Smadsens function for getting
  83.           ;; points and generating a list of points.
  84.           ;;
  85.           ;; I have overhauled the routine to draw lines instead of just
  86.           ;; gathering points (and returning a list).
  87.           ;;
  88.           ;; ARGS: pt   - The first point.
  89.           ;;
  90.           ;; RETURNS: nil
  91.           (setq p1   pt
  92.                 sp   pt
  93.                 cnt  2)
  94.           (while pt
  95.                  (initget "Close")
  96.                  (setq pt (getpoint pt (strcat "\rPoint no. " (itoa cnt) " [Close]: ")))
  97.                  (cond ((vl-consp pt)
  98.                         (drawline (list p1 pt))
  99.                         (setq p1 pt))
  100.                        ((= pt "Close")
  101.                         (drawline (list p1 sp))
  102.                         (setq pt sp
  103.                               pt nil))
  104.                        )
  105.                  (setq cnt (1+ cnt))
  106.                  )
  107.           )
  108.  
  109.   (setq ss (cadr (ssgetfirst)))
  110.   (princ "\n**Line**")
  111.   (setq pt1 (getpoint "\nEnter first point: "))
  112.   (if (eq (type ss) 'PICKSET)                                   ;; -If there are items selected.
  113.     (progn
  114.       (*error-push-vars* '(("CLAYER")))                         ;; -Save the current layer for restoring later.
  115.       (setvar 'CLAYER (getlayerfrompickedpoint pt1)))           ;; -Set the current layer to item under picked point.
  116.     )
  117.   (command "_.regenall")                                        ;; -UNSELECT selection set.
  118.   (getpoints pt1)                                               ;; -Draw a line...
  119.   (quit)
  120.   )
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

danAllen

  • Newt
  • Posts: 134
Re: Unselect items (selection set)
« Reply #1 on: September 26, 2023, 12:57:51 PM »
I think: (sssetfirst nil)
Or/also have you looked at pickfirst variable?

JohnK

  • Administrator
  • Seagull
  • Posts: 10661
Re: Unselect items (selection set)
« Reply #2 on: September 26, 2023, 01:41:15 PM »
(sssetfirst nil) worked great.

Thank you.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org