Author Topic: Layer creation not working in if statement  (Read 2122 times)

0 Members and 1 Guest are viewing this topic.

Windsor

  • Mosquito
  • Posts: 14
Layer creation not working in if statement
« on: December 12, 2023, 11:16:42 AM »
I have an AutoLisp script which I have created to plot a crossed out box on a non printable guide layer, I have incorperated an if statement to check if this layer is already within the current drawing and if not to create it. However if the layer is not present the script doesn't work.

Is there a way to resolve this problem at all? (probably something I haven't noticed as of yet)

Code - Auto/Visual Lisp: [Select]
  1. ;       Creates box on unprintable guide layer which has a
  2. ;       cross trough it to denote an unused area of the drawing
  3.  
  4. (defun c:UNUSED ( / P1 P2 P3 P4 Change)
  5.         (vl-load-com)
  6.        
  7. (setq CL (getvar "clayer"))
  8.  
  9. ;Checks if 1 - Guide layer is already in drawing, if not creates it
  10. (if (tblsearch "layer" "1 - Guide")                                                                     ;Statement
  11.         ()                                                                                                                              ;if true
  12.         (command "_.LAYER" "M" "1 - Guide" "C" "Magenta" "" "P" "N" "") ;if false
  13. )
  14.  
  15. ;Inputs
  16. (setq   P1 (getpoint "\nSpecify First Point:")
  17.                 P3 (getpoint "\nSpecify Second Point:")
  18. )
  19.  
  20. ;Calculations
  21. (setq   P2 (list (car P3) (cadr P1)))
  22. (setq   P4 (list (car P1) (cadr P3)))
  23.  
  24. ;Change to 1 - Guide function
  25. (defun change ()
  26. (command "_.change" (entlast) "" "p" "LA" "1 - Guide" "")
  27. )
  28.  
  29. ;Box Plot              
  30. (Command "PLINE" P1 P2 P3 P4 "c")
  31. (change)
  32. (command "LINE" P1 P3 "")
  33. (change)
  34. (command "LINE" P2 P4 "")
  35. (change)
  36.  
  37. (setvar "clayer" CL)
  38.  
  39. (prompt "\n\tBox Created!")
  40. )

Is there also a method for having a real-time preview of the shape from the first point until the second (opposite corner) is clicked?

kdub_nz

  • Mesozoic keyThumper
  • SuperMod
  • Water Moccasin
  • Posts: 2140
  • class keyThumper<T>:ILazy<T>
Called Kerry in my other life
Retired; but they dragged me back in !

I live at UTC + 13.00

---
some people complain about loading the dishwasher.
Sometimes the question is more important than the answer.

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: Layer creation not working in if statement
« Reply #2 on: December 12, 2023, 03:46:23 PM »
You're missing an extra enter in your command expression:
Code - Auto/Visual Lisp: [Select]
  1. (command "_.LAYER" "M" "1 - Guide" "C" "Magenta" "" "P" "N" "" "")

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: Layer creation not working in if statement
« Reply #3 on: December 12, 2023, 04:01:43 PM »
    A few other points to note:

    • You can use (if (not (tblsearch "layer" "1 - Guide")) followed by your command expression to avoid the empty list forming the 'then' argument, to improve readability.
    • You should either disable OSMODE during the execution of the PLINE command or prefix the point input with "_non" point modifiers to avoid Object Snap affecting the supplied point input.
    • (vl-load-com) is not required here as you are not using COM.
    • You could use entmake to create both the layer and the polyline & lines to automatically assign the layer and avoid having to assign it after the object has been created (it will also be much faster and will be immune to the effect of Object Snap), here is an example to help:
Code - Auto/Visual Lisp: [Select]
  1. (defun c:unused ( / lay ocs pt1 pt2 pt3 pt4 )
  2.  
  3.     (setq lay "1 - Guide") ;; Layer Name
  4.  
  5.     (if
  6.         (and
  7.             (setq pt1 (getpoint "\nSpecify 1st point: "))
  8.             (setq pt3 ((if (zerop (getvar 'worlducs)) getpoint getcorner) pt1 "\nSpecify opposite corner: "))
  9.         )
  10.         (progn
  11.             (if (not (tblsearch "layer" lay))
  12.                 (entmake
  13.                     (list
  14.                        '(000 . "LAYER")
  15.                        '(100 . "AcDbSymbolTableRecord")
  16.                        '(100 . "AcDbLayerTableRecord")
  17.                        '(070 . 0) ;; on/unlocked
  18.                        '(062 . 6) ;; magenta colour
  19.                        '(290 . 0) ;; non-plotting
  20.                         (cons 2 lay)
  21.                     )
  22.                 )
  23.             )
  24.             (setq pt2 (cons (car pt3) (cdr pt1))
  25.                   pt4 (cons (car pt1) (cdr pt3))
  26.                   ocs (trans '(0 0 1) 1 0 t)
  27.             )
  28.             (entmake
  29.                 (list
  30.                    '(000 . "LWPOLYLINE")
  31.                    '(100 . "AcDbEntity")
  32.                    '(100 . "AcDbPolyline")
  33.                    '(090 . 4) ;; 4 vertices
  34.                    '(070 . 1) ;; closed
  35.                     (cons 008 lay) ;; layer
  36.                     (cons 010 (trans pt1 1 ocs))
  37.                     (cons 010 (trans pt2 1 ocs))
  38.                     (cons 010 (trans pt3 1 ocs))
  39.                     (cons 010 (trans pt4 1 ocs))
  40.                     (cons 210 ocs)
  41.                 )
  42.             )
  43.             (entmake
  44.                 (list
  45.                    '(000 . "LINE")
  46.                     (cons 008 lay)
  47.                     (cons 010 (trans pt1 1 0))
  48.                     (cons 011 (trans pt3 1 0))
  49.                 )
  50.             )
  51.             (entmake
  52.                 (list
  53.                    '(000 . "LINE")
  54.                     (cons 008 lay)
  55.                     (cons 010 (trans pt2 1 0))
  56.                     (cons 011 (trans pt4 1 0))
  57.                 )
  58.             )
  59.         )
  60.     )
  61.     (princ)
  62. )



Windsor

  • Mosquito
  • Posts: 14
Re: Layer creation not working in if statement
« Reply #4 on: December 14, 2023, 09:01:12 AM »
Thanks Kdub for the link, good reference for attempting again in the fiture.

Thanks Lee, something I had completely overlooked in my original attempt. And Thanks for the rewrite, many good points which I wouldn't have even thought to attempt!


Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: Layer creation not working in if statement
« Reply #5 on: December 14, 2023, 06:00:38 PM »
You're most welcome Windsor, happy to help.