Results 1 to 7 of 7

Thread: Lisp routine help, Layer check problem

  1. #1
    Member
    Join Date
    2007-07
    Posts
    3
    Login to Give a bone
    0

    Default Lisp routine help, Layer check problem

    Hi all
    After some help with a LISP routine I'm currently writing. I have got the routine to work fine the way I want it but i need to modify it to include checking to see if layers exists and if they dont skip down to the next part of the code.
    We use XREFs in our drawings which have 3 different layers im interested in.

    heres my code below:
    Code:
    (defun c:safetylayers ()
      (setvar "cmdecho" 0)
      (acet-autoload2 '("FLATTENSUP.LSP" (acet-flatn ss hide)))    ;load flatten command function
      (command "-layer" "s" "0" "")                    ;set layer 0 active
      (command "-layer" "f" "*" "")                    ;freeze all layers
    
    (while (setq LAYER (tblnext "LAYER" (null LAYER)))        ;check to see if similar layer exists
    (if (wcmatch (cdr (assoc 2 LAYER)) "*|SAFETY")
    (setq layercheck 1)
    ); end if
    ); end while
      (if (= layercheck 1)                        ;if layer exists perform function
          (progn
      (command "-layer" "t" "*|SAFETY" "")
      (setq ss1 (ssget))
      (command "copy" "p" "" "0,0,0" "0,0,0")
      (acet-flatn ss1 T)
      (command ".chprop" "p" "" "la" "AS4685 SAFETY LAYER" "")
       ); end progn
      ); end if
    
    (while (setq LAYER2 (tblnext "LAYER" (null LAYER2)))
    (if (wcmatch (cdr (assoc 2 LAYER2)) "*|SAFETY FREE SPACE")
    (setq layercheck2 1)
    ); end if
    ); end while
      (if (= layercheck2 1)
          (progn  
      (command "-layer" "f" "*" "")
      (command "-layer" "t" "0" "")
      (command "-layer" "t" "*|SAFETY FREE SPACE" "")
      (setq ss2 (ssget))
      (command "copy" "p" "" "0,0,0" "0,0,0")
      (acet-flatn ss2 T)
      (command ".chprop" "p" "" "la" "AS4685 SAFETY LAYER" "")
      (command ".chprop" "p" "" "co" "green" "")
      (command "-layer" "f" "*" "")
      ); end progn
      ); end if
      
    (while (setq LAYER3 (tblnext "LAYER" (null LAYER3)))
    (if (wcmatch (cdr (assoc 2 LAYER3)) "*|SAFETY499")
    (setq layercheck3 1)
    ); end if
    ); end while
      (if (= layercheck3 1)
          (progn
      (command "-layer" "f" "*" "")
      (command "-layer" "t" "0" "")
      (command "-layer" "t" "*|SAFETY499" "")
      (setq ss3 (ssget)) 
      (command "copy" "p" "" "0,0,0" "0,0,0")
      (acet-flatn ss3 T)
      (command ".chprop" "p" "" "la" "AS4685 SAFETY LAYER" "")
      (command ".chprop" "p" "" "co" "30" "")
      ); end progn
      ); end if
    
    
    (command "-layer" "f" "*safety*" "t" "AS4685 SAFETY LAYER" "") 
    ) ; end c:safetylayers function
    The part I have got stuck on is the check with the while loop and using the WCMATCH.
    I would like the routine to search and see if a certain layer exists on an XREF, if it does perform the flatten function etc If it doesnt then just go onto the next check. I'm only new to LISP so my programming probably isnt too great but like I said before the routine works great for me if all the 3 layers are present in a drawing (without the need for checking)

    Any help would be greatly appreciated
    thanks

    Craig

  2. #2
    I could stop if I wanted to
    Join Date
    2007-05
    Location
    Brookfield WI
    Posts
    331
    Login to Give a bone
    0

    Default Re: Lisp routine help, Layer check problem

    Code:
    (if (not (tblsearch "layer" "epdjc" ))                                       ;Create layer "epdjc" if it does not exist
        (command "-layer" "n" "epdjc" "c" "magenta" "epdjc" "p" "n" "epdjc" "" )
        ( ) ; -------------^ Changed from Make to New to not make it current
      )

  3. #3
    Administrator Opie's Avatar
    Join Date
    2002-01
    Location
    jUSt Here (a lot)
    Posts
    9,100
    Login to Give a bone
    0

    Default Re: Lisp routine help, Layer check problem

    I don't think that will help in this situation. The OP is trying to run some code if an XREF layer is present in the drawing.

    Hopefully this will work. I don't have a drawing setup to verify.

    You are looping through the layers table three different times. I think you can narrow that down to one and use a conditional statement for each of the three layers you are looking for. Let us know how it works.
    Code:
    (defun c:safetylayers ( / LAYER LAYERNAME SS1 SS2 SS3)
      (setvar "cmdecho" 0)
      (acet-autoload2 '("FLATTENSUP.LSP" (acet-flatn ss hide)))
    					;load flatten command function
      (command "-layer" "s" "0" "")		;set layer 0 active
      (command "-layer" "f" "*" "")		;freeze all layers
      (while (setq LAYER (tblnext "LAYER" (null LAYER)))
    					;check to see if similar layer exists
        (setq LayerName (strcase (cdr (assoc 2 LAYER))))
        (cond ((wcmatch LayerName "*|SAFETY")
    	   (progn
    	     (command "-layer" "t" "*|SAFETY" "")
    	     (setq ss1 (ssget))
    	     (command "copy" "p" "" "0,0,0" "0,0,0")
    	     (acet-flatn ss1 T)
    	     (command ".chprop" "p" "" "la" "AS4685 SAFETY LAYER" "")
    	   )
    	  )
    	  ((wcmatch LayerName "*|SAFETY FREE SPACE")
    	   (progn
    	     (command "-layer" "f" "*" "")
    	     (command "-layer" "t" "0" "")
    	     (command "-layer" "t" "*|SAFETY FREE SPACE" "")
    	     (setq ss2 (ssget))
    	     (command "copy" "p" "" "0,0,0" "0,0,0")
    	     (acet-flatn ss2 T)
    	     (command ".chprop" "p" "" "la" "AS4685 SAFETY LAYER" "")
    	     (command ".chprop" "p" "" "co" "green" "")
    	     (command "-layer" "f" "*" "")
    	   )
    	  )
    	  ((wcmatch LayerName "*|SAFETY499")
    	   (progn
    	     (command "-layer" "f" "*" "")
    	     (command "-layer" "t" "0" "")
    	     (command "-layer" "t" "*|SAFETY499" "")
    	     (setq ss3 (ssget))
    	     (command "copy" "p" "" "0,0,0" "0,0,0")
    	     (acet-flatn ss3 T)
    	     (command ".chprop" "p" "" "la" "AS4685 SAFETY LAYER" "")
    	     (command ".chprop" "p" "" "co" "30" "")
    	   )
    	  )
        )
      )
      (command "-layer"	   "f"		   "*safety*"
    	   "t"		   "AS4685 SAFETY LAYER"
    	   ""
    	  )
    )					; end c:safetylayers function
    If you have a technical question, please find the appropriate forum and ask it there.
    You will get a quicker response from your fellow AUGI members than if you sent it to me via a PM or email.
    jUSt

  4. #4
    Member
    Join Date
    2007-07
    Posts
    3
    Login to Give a bone
    0

    Default Re: Lisp routine help, Layer check problem

    Thanks for the quick reply Opie, greatly appreciated.
    I have just tried your code in a drawing with all 3 layers present and it seems to go past the first two in the program and only do the flattening on the last one (safety499 layer).
    I also tried it with only the first 2 layers present (AS4685 and Free Space) and it did the flattening on the first but not on the second Free Space Layer.
    I have searched around a bit on the tblnext and the wcmatch commands because i think thats where im running into trouble but my programming knowledge isnt up to that level yet.
    Would it be helpful for me to provide a sample drawing?

  5. #5
    Administrator Opie's Avatar
    Join Date
    2002-01
    Location
    jUSt Here (a lot)
    Posts
    9,100
    Login to Give a bone
    0

    Default Re: Lisp routine help, Layer check problem

    Quote Originally Posted by Craig.Baldie View Post
    Thanks for the quick reply Opie, greatly appreciated.
    I have just tried your code in a drawing with all 3 layers present and it seems to go past the first two in the program and only do the flattening on the last one (safety499 layer).
    I also tried it with only the first 2 layers present (AS4685 and Free Space) and it did the flattening on the first but not on the second Free Space Layer.
    I have searched around a bit on the tblnext and the wcmatch commands because i think thats where im running into trouble but my programming knowledge isnt up to that level yet.
    Would it be helpful for me to provide a sample drawing?
    I didn't know the flatten command would work on objects within an xref.

    A sample drawing with it's associated xrefs would be helpful.

    You could also use the VLIDE to step through your routine to see what it is doing. You would want to place a break point near the beginning and display the watch window for the last value. After the break point is reached the command will pause until you tell it to proceed, either by one step at a time or continue without interruption. There are more options you could investigate as well.
    If you have a technical question, please find the appropriate forum and ask it there.
    You will get a quicker response from your fellow AUGI members than if you sent it to me via a PM or email.
    jUSt

  6. #6
    AUGI Addict kennet.sjoberg's Avatar
    Join Date
    2002-05
    Posts
    1,707
    Login to Give a bone
    0

    Default Re: Lisp routine help, Layer check problem

    Quote Originally Posted by Craig.Baldie View Post
    . . .tblnext and the wcmatch commands because i think thats where im runn. . .
    I can not run and test the code, but here is a tune up. . 4U to test.
    Code:
    (defun c:safetylayers ( / OldCmd laychk ss Mlayer )
      (setq OldCmd (getvar "CMDECHO" ) ) ;; Save user variable
      (setvar "CMDECHO" 0 )              ;; Change user variable
      (acet-autoload2 '("FLATTENSUP.LSP" (acet-flatn ss hide))) ;; load flatten command function
      (command "-layer" "s" "0" "" ) ;; set layer 0 active
      (command "-layer" "f" "*" "" ) ;; freeze all layers
    
      ;; local function that returns T if layer "*|SAFETY" or "*|SAFETY FREE SPACE" is found
      (defun laychk ( Lay_In / Layer Found )
        (while (and (setq Layer (tblnext "LAYER" (null Layer ))) (not Found ) )
          (if (wcmatch (cdr (assoc 2 Layer )) Lay_In ) (setq Found T ) ( ) )
        )
      )
    
      ;; You can also check if "AS4685 SAFETY LAYER" exist to alert or create, because it must exist.
      (if (not (laychk "AS4685 SAFETY LAYER" )) (alert "Layer 'AS4685 SAFETY LAYER' do not exist" ) (setq Mlayer T ) )
    
      (if (and (laychk "*|SAFETY" ) (setq ss (ssget)) Mlayer )
        ;; if layer exists and objects are selected and Master layer exist, perform progn
        (progn
          (command "-layer" "t" "*|SAFETY" "" )
          (command "copy" "p" "" "0,0,0" "0,0,0" )
          (acet-flatn ss T )
          (command ".chprop" "p" "" "la" "AS4685 SAFETY LAYER" "" )
        )
      ); end if
    
      (if (and (laychk "*|SAFETY FREE SPACE" ) (setq ss (ssget)) Mlayer )
        (progn  
          (command "-layer" "f" "*" "t" "0" "t" "*|SAFETY FREE SPACE" "" )
          (command "copy" "p" "" "0,0,0" "0,0,0" )
          (acet-flatn ss T )
          (command ".chprop" "p" "" "la" "AS4685 SAFETY LAYER" "" )
          (command ".chprop" "p" "" "co" "green" "" )
          (command "-layer" "f" "*" "" )
        )
      ); end if
    
      (if (and (laychk "*|SAFETY499" ) (setq ss (ssget)) Mlayer )
        (progn
          (command "-layer" "f" "*" "t" "0" "t" "*|SAFETY499" "" )
          (command "copy" "p" "" "0,0,0" "0,0,0" )
          (acet-flatn ss T )
          (command ".chprop" "p" "" "la" "AS4685 SAFETY LAYER" "" )
          (command ".chprop" "p" "" "co" "30" "" )
        )
      ); end if
    
      (command "-layer" "f" "*safety*" "t" "AS4685 SAFETY LAYER" "" )
      (setvar "CMDECHO" OldCmd ) ;; Reset user variable
      (princ)
    ) ; end c:safetylayers function
    : ) Happy Computing !

    kennet

  7. #7
    Member
    Join Date
    2007-07
    Posts
    3
    Login to Give a bone
    0

    Default Re: Lisp routine help, Layer check problem

    thanx for the help everyone, played around with the code you suggested Kennet but still didnt want to co-operate. I wasnt able to spend too much more time on it due to other pressing matters but I have gotten around the issue crudely by adding some code to insert a pre made XREF with the 3 layers im working with. This means i dont have to check to see if the layers exist because they will get inserted anyways.
    I know its not the best solution but it works. Maybe when i have some time in a few weeks i might revisit the code to clean it up.
    I did however streamline my code with what has been provided

    thanks all

Similar Threads

  1. LISP routine to overwrite layer descriptions...
    By dortega4269 in forum AutoLISP
    Replies: 39
    Last Post: 2014-07-23, 08:04 PM
  2. Layer Color Change LISP Routine
    By guardianfiredesign774457 in forum AutoLISP
    Replies: 19
    Last Post: 2013-08-28, 05:08 AM
  3. Add layer command line to a lisp routine
    By BrianTFC in forum AutoLISP
    Replies: 1
    Last Post: 2012-02-02, 07:47 AM
  4. Layer creation lisp routine?
    By boeris9333 in forum AutoLISP
    Replies: 10
    Last Post: 2009-10-05, 11:24 AM
  5. Spell Check Lisp Routine
    By SoonerCad in forum AutoLISP
    Replies: 4
    Last Post: 2007-12-21, 03:19 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •