PDA

View Full Version : Lisp routine help, Layer check problem


Craig.Baldie
2007-10-11, 05:23 AM
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:


(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

d_m_hopper
2007-10-11, 05:51 AM
(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
)

Opie
2007-10-11, 06:14 AM
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.
(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

Craig.Baldie
2007-10-11, 07:13 AM
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?

Opie
2007-10-11, 03:44 PM
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.

kennet.sjoberg
2007-10-11, 11:20 PM
. . .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.

(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

Craig.Baldie
2007-10-12, 08:27 AM
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 :)