Search code examples
lispautocadcadautolisp

AutoLisp drawing costum polyline


I have this AutoLisp code and I want to ask 2 questins about it:

1. how can I make the polyline visible during drawing it(or while selecting the points)?

2. how can I change the color of the polyline but not the layer?

Many many thanks if i get an answer.

(defun c:nyomvodal (/ osmode clayer celtype)
  ;; save the current osmode, clayer and  celtype
  (setq osmode (getvar "osmode"))
  (setq clayer (getvar "clayer"))
  (setq celtype (getvar "celtype"))

  ;; create a new layer and make it current
  (command "_layer" "_make" "nyomvodal" "_color" 3 "nyomvodal" "")
  ;; set the current osmode and line type
  (setvar "osmode" 0)
  (setvar "celtype" "16-os cso")

  ;; use vla-catch-all-apply to avoid exiting code if user cancels
  (vl-catch-all-apply
    '(lambda (/ pt lst)
       ;; get points form user
       (while (setq pt (getpoint "\nPick point: "))
     (setq lst (cons pt lst))
       )
       (if (< 2 (length lst))
     (progn
       ;; create the polyline
       (command "_pline")
       (foreach p (reverse lst)
         (command p)
       )
       (command "")
     )
       )
     )
  )
  
  ;; restore the previous system variables values
  (setvar "osmode" osmode)
  (setvar "clayer" clayer)
  (setvar "celtype" celtype)
  (princ)
)

Solution

    1. The easiest would be just run command PLINE and wait until user will end.
    2. Color You can change by system variable CECOLOR.

    To restore defaults by system variables You can use error catching (in case when user click Esc ), when user ends with [enter] [space] You will restore defaults by call (*error*) function

    (defun c:nyomvodal (/ osmode clayer celtype 
        *error* )   (defun *error* ( msg / ) 
            (if (not (null msg ) )  (progn (princ "\nname:*error*: " ) (princ msg ) (princ "\n")    ) )
            
            (setvar "osmode" osmode)
            (setvar "clayer" clayer)
            (setvar "celtype" celtype)
            (setvar 'CECOLOR cecolor)
        )
      ;; save the current osmode, clayer and  celtype
      (setq osmode (getvar "osmode"))
      (setq clayer (getvar "clayer"))
      (setq celtype (getvar "celtype"))
      (setq cecolor (getvar "CECOLOR"))
    
      ;; create a new layer and make it current
      (command "_layer" "_make" "nyomvodal" "_color" 3 "nyomvodal" "")
      ;; set the current osmode and line type
      (setvar "osmode" 0)
      (setvar "CECOLOR" "1")
      ;(setvar "celtype" "16-os cso")
    
      (command "_pline" pause)       
      
      (*error* nil) ; to restore default color, layer ... 
      
      (princ)
    )