Author Topic: Do I have to check if exists before I entmake a layer  (Read 1212 times)

0 Members and 1 Guest are viewing this topic.

JohnK

  • Administrator
  • Seagull
  • Posts: 10626
Do I have to check if exists before I entmake a layer
« on: December 08, 2021, 03:06:45 PM »
Given a layer maker function (entmake based) below do I technically have to check to see if the layer exists before I make it?
Code - Auto/Visual Lisp: [Select]
  1. (defun makelayer ( name )
  2.              '(0 . "LAYER")
  3.              '(100 . "AcDbSymbolTableRecord")
  4.              '(100 . "AcDbLayerTableRecord")
  5.              (cons 2 name )                     ; layername
  6.              (cons 70 0)                        ; vis stuff
  7.              (cons 62 7)                        ; color number
  8.              (cons 6 "Continuous")              ; linetype
  9.              )
  10.            )
  11.   )
  12. ;; Should I use:
  13. (if (not (tblsearch "layer" "MyLayer"))
  14.   (makelayer "MyLayer")
  15.   )
  16.  
  17. ;; OR can I just try to make it?
  18. (makelayer "MyLayer")
  19.  

I know that if I were to use VisualLisp to create the layer, I would have to release the object if I tried to take a similar approach.

VL EXAMPLE:
Code - Auto/Visual Lisp: [Select]
  1. ;;;===================================================================;
  2. ;;; ax:MakeLayer                                                      ;
  3. ;;;-------------------------------------------------------------------;
  4. ;;; Create a new layer.                                               ;
  5. ;;;                                                                   ;
  6. ;;; Args: The new layer name                                          ;
  7. ;;;                                                                   ;
  8. ;;; History: Got this from AcadX.com                                  ;
  9. ;;;                                                                   ;
  10. ;;; NOTES: Returns the new layer object on successful creation,       ;
  11. ;;;        an existing layer object if the layer already exists,      ;
  12. ;;;        or NIL if the layer name cannot be created.                ;
  13. ;;;                                                                   ;
  14. ;;; Usage: (ax:MakeLayer "A-Layer")                                   ;
  15. ;;;===================================================================;
  16. (defun ax:MakeLayer (lName / oLayer)
  17.   (if
  18.       (setq oLayer
  19.           (vl-catch-all-apply
  20.             'vla-add
  21.               (list
  22.                 (vla-get-layers
  23.                   (vla-get-activedocument
  24.                     (vlax-get-acad-object)
  25.                     )
  26.                   )
  27.                 lName
  28.                 )
  29.              )
  30.           )
  31.         )
  32.     nil
  33.     oLayer
  34.     )
  35.   )
  36.  
  37. ;; When making the layer with VL I need to release the object.
  38.   (setq LayerName (ax:MakeLayer "MyLayer"))
  39.   (if (not (vlax-object-released-p LayerName))
  40.     (vlax-release-object LayerName)
  41.    )
  42. )
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: Do I have to check if exists before I entmake a layer
« Reply #1 on: December 08, 2021, 04:44:55 PM »
You don't have to check although it also depends on if you want to return something like the layer ename. If the layer exists without the check, you get a nil return.
Code - Auto/Visual Lisp: [Select]
  1. (defun makelayer (name)
  2.   (cond ((tblobjname "layer" name))
  3.         ((entmakex (list '(0 . "LAYER")
  4.                          '(100 . "AcDbSymbolTableRecord")
  5.                          '(100 . "AcDbLayerTableRecord")
  6.                          (cons 2 name)  ; layername
  7.                          (cons 70 0)    ; vis stuff
  8.                          (cons 62 1)    ; color number
  9.                          (cons 6 "Continuous") ; linetype
  10.                    )
  11.          )
  12.         )
  13.   )
  14. )
  15. (makelayer "test")
« Last Edit: December 08, 2021, 04:48:38 PM by ronjonp »

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

Tharwat

  • Swamp Rat
  • Posts: 710
  • Hypersensitive
Re: Do I have to check if exists before I entmake a layer
« Reply #2 on: December 08, 2021, 04:51:25 PM »
Hello,
I believe there is no need for vl-catch* functions since vla-add function won't return error if layer name was existed or absent. :grinwink:

So checking if object was released is also unnecessarily in this case since vla-add function will return vla-object all the time whilst this is enough to release it but its also not necessarily.

Code - Auto/Visual Lisp: [Select]
  1. (defun Vla:MakeLayer (lName / oLayer)
  2.   (setq oLayer
  3.          (vla-add
  4.            (vla-get-layers
  5.              (vla-get-activedocument
  6.                (vlax-get-acad-object)
  7.              )
  8.            )
  9.            lName
  10.          )
  11.   )
  12. )
  13.  
  14. (vlax-release-object (Vla:MakeLayer "MyLayer"))
  15.  

Hope this helps.
 

JohnK

  • Administrator
  • Seagull
  • Posts: 10626
Re: Do I have to check if exists before I entmake a layer
« Reply #3 on: December 08, 2021, 05:35:44 PM »
@ronjonp
Nice. Thank you!  No, I don't care to return the layer information at all, to be honest; this is going to be a quick "set the current layer situation". -i.e.
Code: [Select]
( (lambda ()
   ; ...
   (makelayer "test")
   (setvar 'CLAYER "test")
   ; ...
  )
)
Thanks again!

@Tharwat
Nice. Didn't even think of that. But if I'm honest, I didn't write that function I just used it for this post. But that's good info; I'll have to add those notes to that function in my notes. Thanks.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

JohnK

  • Administrator
  • Seagull
  • Posts: 10626
Re: Do I have to check if exists before I entmake a layer
« Reply #4 on: December 08, 2021, 09:14:04 PM »
I found one of my old routines (starting over is hard; I have forgotten all of the little nuances).

Code - Auto/Visual Lisp: [Select]
  1. (defun make-or-modify-layer (LNAM$ LCLR# LTYP$ LWGT# / #prog)
  2.    ;;
  3.    ;; Ex:
  4.    ;; (make-or-modify-layer "Test-Layer" 1 "Continuous" 18)
  5.    ;;  > create the layer
  6.    ;; (make-or-modify-layer "Test-Layer" 1 "Continuous" 18)
  7.    ;;  > would override the current layer def.
  8.    ;;
  9.    ;; By: John K
  10.    ;;
  11.   (if (null (tblsearch "layer" LNAM$))
  12.     (set '#prog (lambda ( x ) (entmake x)))
  13.     (set '#prog (lambda (x / ent y)
  14.                   (setq ent (entget (tblobjname "LAYER" LNAM$)))
  15.                   (foreach y x
  16.                            (if (not (member (car y) '(0 100 2)))
  17.                              (setq ent (subst y (assoc (car y) ent) ent))))
  18.                   (entmod ent) )))
  19.   (#prog
  20.    (list
  21.      (cons 0   "LAYER")
  22.      (cons 100 "AcDbSymbolTableRecord")
  23.      (cons 100 "AcDbLayerTableRecord")
  24.      (cons 2    LNAM$)
  25.      (cons 6    LTYP$)
  26.      (cons 62   LCLR#)
  27.      (cons 70   0)
  28.      (cons 290  1)
  29.      (cons 370  LWGT#)
  30.      )
  31.    )
  32.  (princ)
  33.  )
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

Tharwat

  • Swamp Rat
  • Posts: 710
  • Hypersensitive
Re: Do I have to check if exists before I entmake a layer
« Reply #5 on: December 09, 2021, 06:06:02 AM »
I found one of my old routines (starting over is hard; I have forgotten all of the little nuances).

Code - Auto/Visual Lisp: [Select]
  1. (defun make-or-modify-layer (LNAM$ LCLR# LTYP$ LWGT# / #prog)

Good stuff John,
Just one issue is that the LineType must be loaded with your codes in prior if it is not available / loaded into your current running drawing.

JohnK

  • Administrator
  • Seagull
  • Posts: 10626
Re: Do I have to check if exists before I entmake a layer
« Reply #6 on: December 09, 2021, 08:47:07 AM »
No it wont. Yes they do, but that is a good point to make for anyone that finds this code later. I have a separate routine for setup that does all that; this is one of those routines that quite likely never gets used (it is just passed through). I am working on a routine to draw some parametric fittings and I am going to use this to make sure that the centerlines I draw are on a no-plot layer.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org