Author Topic: Manipulating Layers - If they exist?  (Read 3221 times)

0 Members and 1 Guest are viewing this topic.

mike

  • Guest
Manipulating Layers - If they exist?
« on: January 30, 2007, 05:57:15 AM »
Hi

I am trying to write a lisp routine that will Freeze all layers, insert layers, colour layers etc , then ***if they exist*** run other functions
for example, If layer 'comm-grid' exists, I want to rename it to 'Grid'

I knocked up a messy bit of code for some functions but obviously get loads of - layer doesnt exist errors etc.

I guess I need to stick an 'if' statement in somewhere but although I have been given if statements before to use, I have not created one from scratch,

I was going to add the layer first whether it existed or not but then I thought id best ask someone how to do it properly.
Bit I began with..................

Code: [Select]
;Creates Layers
(DEFUN CL ()
(COMMAND "LAYER" "fr" "*" "")
(COMMAND "LAYER" "tH" "*Grid,Grid$,Grid$Txt,Gros,Gros$,Gros$Txt,*S-CORE-GRID" "")
(COMMAND "LAYER" "n" "Grid,Grid$,Grid$Txt,Gros,Gros$,Gros$Txt" "")
(COMMAND "LAYER" "S" "Grid" "")
(COMMAND "LAYER" "C" "6" "Grid$,Grid$Txt" "C" "1" "Gros$,Gros$Txt" "")
)


can anyone assist a novice?

thanks

edit: added code tags
« Last Edit: January 30, 2007, 07:28:11 AM by jonesy »

rkmcswain

  • Swamp Rat
  • Posts: 978
Re: Manipulating Layers - If they exist?
« Reply #1 on: January 30, 2007, 08:25:18 AM »
...then ***if they exist*** run other functions
for example, If layer 'comm-grid' exists, I want to rename it to 'Grid'

Basic example:
Code: [Select]
(if (tblsearch "layer" "comm-grid")
  (command "._rename" "_LA" "comm-grid" "grid")
)


mike

  • Guest
Re: Manipulating Layers - If they exist?
« Reply #2 on: January 30, 2007, 10:03:07 AM »
...then ***if they exist*** run other functions
for example, If layer 'comm-grid' exists, I want to rename it to 'Grid'

Basic example:
Code: [Select]
(if (tblsearch "layer" "comm-grid")
  (command "._rename" "_LA" "comm-grid" "grid")
)

This works a treat thanks, now one step further please is there a way to say check if layers a, b and c exist. If only layer a exists create layer b and c.  What I am trying to construct is a program that will check to see if standard layers exist, if they do not, it creates them.  I read your blog site about your AUGI TOP DAUG win - or not actually, disgusting. Where is their professionalism. Surely a compromise could have been made at worst. A win is a win.....


Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Manipulating Layers - If they exist?
« Reply #3 on: January 30, 2007, 10:09:08 AM »
.............  What I am trying to construct is a program that will check to see if standard layers exist, if they do not, it creates them. 

Dont even bother testing, just make them all ... AutoCAD will not complain if you make a layer that exists.
... and it would probably run at about the same speed anyway.

// kwb
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

rkmcswain

  • Swamp Rat
  • Posts: 978
Re: Manipulating Layers - If they exist?
« Reply #4 on: January 30, 2007, 10:58:12 AM »
Mike, thanks for the comments...

As for creating the layers, I agree with Kerry - don't bother testing, just make the layers you need.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Manipulating Layers - If they exist?
« Reply #5 on: February 01, 2007, 09:25:55 AM »
Here is an example for you to consider. Limited testing & quickly put together.
Code: [Select]
;;  LU.lsp  02/01/07
;;  Layer Utility
;;  Renames layers
;;  Make new layers
;;  Freeze all but layers in the list
;;  Set current layer
(defun c:LU(/ usercmd LAY LAYERNAMES2MAKE LAYERNAMES2RENAME LAYERNAMES2THAW
              LayerMake)

  (setq usercmd (getvar "CMDECHO"))
  (setvar "CMDECHO" 0)
 
  ;;  Function to make a layer or update it if it exist
  (defun LayerMake(lyrname Color ltype)
    (if (tblsearch "LAYER" lyrname)
      (command "._Layer" "_Thaw" lyrname "_On" lyrname "_UnLock" lyrname "_Set" lyrname "")
      (command "._Layer" "_Make" lyrname "_Color"
               (if (or (null color)(= Color "")) "_White" Color) lyrname
               "LT" (if (or (null ltype)(= ltype "")) "Continuous" ltype) lyrname "")
    )
  )


  ;;===========================================================
  ;;  Change the names in this List of layers if they exist
  (setq LayerNames2Rename '(("Old Name" "New name")
                            ("comm-grid" "Grid")
                           )
  )
  (foreach LayName LayerNames2Rename
    (if (tblsearch "layer" (car LayName))
       (command "._rename" "_LA" (car LayName) (cadr LayName))
    )
  )
  ;;===========================================================


 
  ;;  Make New Layers
  (setq LayerNames2Make '(("Grid"     7 "")
                          ("Grid$"    6 "")
                          ("Grid$Txt" 6 "")
                          ("Gros"     7 "")
                          ("Gros$"    1 "")
                          ("Gros$Txt" 1 "")
                         )
   )
  (foreach LayLst LayerNames2Make
    (LayerMake (car LayLst) (cadr LayLst)(caddr LayLst))
  )
  ;;===========================================================


  ;;  Thaw these layers
  (setq LayerNames2Thaw '("*Grid" "Grid$" "Grid$Txt" "Gros"
                          "Gros$" "Gros$Txt" "*S-CORE-GRID")
  )
  (mapcar '(lambda(lay)
             (if (tblsearch "layer" Lay)
               (command "_.layer" "_Thaw" Lay "")
             )
           )
          LayerNames2Thaw
  )
  ;;===========================================================

  (setvar "CLAYER" "Grid") ; set current layer
  (setvar "CMDECHO" usercmd)
  (prompt "\n***  Layer Utility finished  ***")
  (princ)
)
(prompt "\n***  Layer Utility loaded, Enter LU to run.  ***")
(princ)
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: Manipulating Layers - If they exist?
« Reply #6 on: February 01, 2007, 11:07:04 AM »
No command...  :-)

Code: [Select]
(defun make_lay (n c lt / l)
 ;;(make_lay "Layer11" nil nil)
 ;;(make_lay "Layer11" 2 "Continuous")
 (if (tblsearch "Layer" n)
  (progn (setq l (vlax-ename->vla-object (tblobjname "Layer" n)))
         (if c (vla-put-color l c)(vla-put-color l 7))
         (if lt(vla-put-Linetype l lt)(vla-put-Linetype l "Continuous"))
  )
  (progn
   (setq l (vla-add (vla-get-layers (vla-get-activedocument (vlax-get-acad-object))) n))
         (if c (vla-put-color l c)(vla-put-color l 7))
         (if lt(vla-put-Linetype l lt)(vla-put-Linetype l "Continuous"))
  )
 )
)

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Manipulating Layers - If they exist?
« Reply #7 on: February 01, 2007, 11:43:56 AM »
My intent was to give Mike something he could digest easily. :-)
But you never know, I may have underestimated his Lisp Experience Level.
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

mike

  • Guest
Re: Manipulating Layers - If they exist?
« Reply #8 on: February 02, 2007, 09:35:14 AM »
Thanks for the help. I will try out on Monday. My lisp experience is not the best, you did not underestimate me!
have a great weekend
Mike