Search code examples
lispautocadautolisp

AutoCAD LISP automatizing polyline drawings


I would like to ask a question about automatizing polyline drawing. Here I have a FUNC and I want to add more specific commands. What I mean: I want the polyline to be assigned to a chosen layer automatically and i also want to set the line type to my custom line type. Thanks for the answers.

Here is my code:


Solution

  • You can get the newly created polyline with the entlast function and change its properties with _chprop command.

    (defun c:nyomvodal (/ pt lst)
      ;; create a new layer
      (command "_layer" "_new" "nyomvodal"  "_color" 3 "nyomvodal" "")
    
      ;; 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 "_non" p)
          )
          (command "")
          (command "_chprop" (entlast) "" "_layer" "nyomvodal" "_ltype" "axes" "")
        )
      )
      
      (princ)
    )
    

    But, typically, we save current values of the OSMODE, CLAYER and CELTYPE system variables, set new values, draw the polyline, and restore the previous values.

    (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" "AXES")
    
      ;; 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)
    )