Sweety Posted January 11, 2011 Share Posted January 11, 2011 Hello Everybody . I made these codes to make a layer , but I do not know how to give it a rare name by numbering if the layer name is already existed . for example I want these codes to check if Layer name "Layer1" is existed then add one to the Lay name to become "Layer2" and if the last one is also existed then "Layer3" and so on . (setq layName "LAYER") (setq num 1) (setq layName (strcat layName (itoa num ))) (if (tblsearch "LAYER" layName) (setq layName (strcat layName (setq num (1+ n)))) (command "_.-layer" layName "" "" ) ) But there is something wrong among these codes that I could not solve . Please help me with it . Thankxxxxxxxxxxxxxxx Quote Link to comment Share on other sites More sharing options...
Lt Dan's legs Posted January 11, 2011 Share Posted January 11, 2011 (edited) Maybe something like.. ((lambda (layName n) (while (tblsearch "LAYER" layname) (setq layName (strcat "LAYER" (itoa (setq n (1+ n))))) ) (command "_.-layer" "New" layName nil) (setvar 'cmdecho 1) ) "LAYER" (setvar 'cmdecho 0) ) Edited January 11, 2011 by Lt Dan's legs Quote Link to comment Share on other sites More sharing options...
pBe Posted January 12, 2011 Share Posted January 12, 2011 (edited) Try this: ;;; pBe Jan 2011 ;;; ;;; ;;; (defun laysearch (ln) (setq nmpst 0 [color=blue]ln_lnt (strlen ln)) [/color] (if (setq eln (tblsearch "LAYER" ln)) (progn (while [color=blue](and (= (atoi ln) 0) (< nmpst ln_lnt)) [/color] (setq ln (substr ln 2) nmpst (1+ nmpst)) ) (setq ln (strcat (substr (cdr (assoc 2 eln)) 1 nmpst) (itoa (+ 1 (atoi ln))))) (laysearch ln) ) (command "_.layer" "M" ln "") ) ) Which incidentally is my first attempt at recursive programming. Comments and suggestions are always welcome Also its good only for numeric suffix Edited January 12, 2011 by pBe code updated Quote Link to comment Share on other sites More sharing options...
irneb Posted January 12, 2011 Share Posted January 12, 2011 My versions. Helper function: (vl-load-com) (defun SplitLayer (name / n c pre num) (setq n (strlen name) num "" ) (while (and (> n 0) (setq c (substr name n 1)) (vl-position c '("0" "1" "2" "3" "4" "5" "6" "7" "8" "9" "-")) ) (setq num (strcat c num) n (1- n) ) ) (setq pre "") (while (> n 0) (setq pre (strcat (substr name n 1) pre) n (1- n) ) ) (cons pre (atoi num)) ) Recursive search: (defun layerUnique (name / val) (if (tblsearch "LAYER" name) (layerUnique (strcat (car (setq val (SplitLayer name))) (itoa (1+ (cdr val))))) name ) ) Iterative (if you prefer): (defun layerUnique (name / val) (while (tblsearch "LAYER" name) (setq name (strcat (car (setq val (SplitLayer name))) (itoa (1+ (cdr val))))) ) name ) So you simply call it thus: (command "_.Layer" "_Make" (LayerUnique "Layer1") "") Edit: the above is really inefficient ... I can already see 2 places to improve: (1) The iterative does not need to call LayerSplit at each iteration. (2) Getting the pre portion inside LayerSplit could be done with: (setq pre (substr str 1 n)) instead of the while loop. Quote Link to comment Share on other sites More sharing options...
pBe Posted January 12, 2011 Share Posted January 12, 2011 Problem i had with mine is if the layername has a numeric suffix and a number in the middle of the layer name, the code works with a specific set of rules (laysearch "Spark1") Layer "Spark1" layer created (laysearch "Spark1") Layer "Spark2" layer created (laysearch "1_Spark") Layer "1_Spark" layer created (laysearch "1_Spark") Layer "2" created (laysearch "Spark2_lite") Layer "Spark2_lite" layer created (laysearch "Spark2_lite") Layer "Spark3" layer created Bummer a bit of tweaking can fix that i guess Nice code Irneb, I still got a long way to go Quote Link to comment Share on other sites More sharing options...
Lee Mac Posted January 12, 2011 Share Posted January 12, 2011 Another: (defun GetUniqueLayerName ( name / prefix suffix ) (vl-load-com) (setq prefix (vl-string-right-trim "0123456789" name) suffix (atoi (substr name (1+ (strlen prefix)))) ) (while (tblsearch "LAYER" name) (setq name (strcat prefix (itoa (setq suffix (1+ suffix))))) ) name ) (GetUniqueLayerName "Layer1") Quote Link to comment Share on other sites More sharing options...
pBe Posted January 12, 2011 Share Posted January 12, 2011 Another: (setq prefix (vl-string-right-trim "0123456789" name) [/quote] Now why didnt i think of that Question for you Lee, what if the numeric value is in the middle of the string? i know there's [i]left-trim[/i] and [i]right-trim [/i]but what of the midde? on another note. [code] (defun rev_str (str ) (vl-list->string (reverse (vl-string->list str))) ) Is this the only way to reverse a string? Quote Link to comment Share on other sites More sharing options...
irneb Posted January 12, 2011 Share Posted January 12, 2011 Awesome one Lee! May I modify it a bit to allow for layers named like Layer001, Layer 002 ... ? (defun itoa_pad (num pad / str) (setq str (itoa num)) (while (< (strlen str) pad) (setq str (strcat "0" str))) str ) (defun GetUniqueLayerName ( name / prefix suffix pad) (vl-load-com) (setq prefix (vl-string-right-trim "0123456789" name) suffix (substr name (1+ (strlen prefix))) pad (strlen suffix) suffix (atoi suffix) ) (while (tblsearch "LAYER" name) (setq name (strcat prefix (itoa_pad (setq suffix (1+ suffix)) pad))) ) name ) Quote Link to comment Share on other sites More sharing options...
Lee Mac Posted January 12, 2011 Share Posted January 12, 2011 Question for you Lee, what if the numeric value is in the middle of the string?i know there's left-trim and right-trim but what of the midde? You would have to create a function to parse the numbers - my method assumes a continuous numerical section at either the start/end of the string. (defun rev_str (str ) (vl-list->string (reverse (vl-string->list str))) ) Is this the only way to reverse a string? Not the only way of course, but I would think the most efficient - when dealing with string manipulation, converting to a list and back again is usually the most efficient method, due to the way LISP allocates memory when using string functions. Awesome one Lee! May I modify it a bit to allow for layers named like Layer001, Layer 002 ... ? Thanks Irne! Certainly, modify at will Quote Link to comment Share on other sites More sharing options...
Lee Mac Posted January 12, 2011 Share Posted January 12, 2011 You would have to create a function to parse the numbers.... Example: (defun LM:ParseNumbers ( str / isString isNumber lst tmp ) ;; Based on original code by Gile, modified by Lee Mac 2010 (defun isString ( x lst ) (cond ( (null lst) (list x)) ( (< 47 (car lst) 58) (cons x (isNumber (chr (car lst)) (cdr lst))) ) ( (= 45 (car lst)) (if (and (cadr lst) (numberp (read (setq tmp (strcat "-" (chr (cadr lst))))) ) ) (cons x (isNumber tmp (cddr lst))) (isString (strcat x (chr (car lst))) (cdr lst)) ) ) (t (isString (strcat x (chr (car lst))) (cdr lst))) ) ) (defun isNumber ( x lst ) (cond ( (null lst) (list (read x))) ( (= 46 (car lst)) (if (and (cadr lst) (numberp (read (setq tmp (strcat x "." (chr (cadr lst))))) ) ) (isNumber tmp (cddr lst)) (cons (read x) (isString (chr (car lst)) (cdr lst))) ) ) ( (< 47 (car lst) 58) (isNumber (strcat x (chr (car lst))) (cdr lst)) ) (t (cons (read x) (isString (chr (car lst)) (cdr lst)))) ) ) (if (setq lst (vl-string->list str)) ( (if (or (and (= 45 (car lst)) (< 47 (cadr lst) 58)) (< 47 (car lst) 58)) isNumber isString ) (chr (car lst)) (cdr lst) ) ) ) Quote Link to comment Share on other sites More sharing options...
irneb Posted January 12, 2011 Share Posted January 12, 2011 Or just modify it slightly: (defun itoa_pad (num pad / str) (setq str (itoa num)) (while (< (strlen str) pad) (setq str (strcat "0" str))) str ) (defun GetUniqueLayerName (name / prefix num suffix pad) (vl-load-com) (setq num (1+ (strlen name))) (while (and (not pad) (> (setq num (1- num)) 0)) (if (wcmatch (substr name num 1) "#") (setq pad (1+ num)) ) ) (setq suffix (substr name pad) prefix (vl-string-right-trim "0123456789" (substr name 1 (1- pad))) num (substr name (1+ (strlen prefix)) (- (strlen name) (strlen prefix) (strlen suffix))) pad (strlen num) num (atoi num) ) (while (tblsearch "LAYER" name) (setq name (strcat prefix (itoa_pad (setq num (1+ num)) pad) suffix)) ) name ) Notice it's only going to use that last consecutive batch of numbers in the name. So if you have a name like "Layer03garble056moregarble", it's going to increment 056 and not 03. I suppose one could have gone with regular expressions, but this one's quite simple. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.