@babysatch:
Apologies for the somewhat late reply, but I am a bit behind in my reading of this forum. The attached AutoLISP file defines a command function, PLLYR, that will process all of the main-object LWPOLYLINES (the "newer" light-weight polylines) in a drawing regardless of whether they are in Model Space or Paper Space and put each on a unique layer. Polylines nested within a Block Reference are ignored. The layer names start with "PLL" followed by numeral characters from 1 to the number of polylines in the file. The numeral characters are zero-padded, so each layer name has the same number of characters. Examples: for eight polylines, the layers would be PLL1 through PLL8. For 49 polylines, the layers would be PLL01 through PLL49. For 1287 polylines, the layers would be PLL0001 through PLL1287. No attempt is made to set any specific layer attributes (color, linetype, lineweight, etc.); default values are assigned for each.
If your requirements vary from the above, you could always edit the code to suit. In particular, if you already have layers that start with "PLL" followed by numerals (or just do not want the layers to start with "PLL"), you could edit the "PLL" string in the edata (subst (cons 8 (strcat "PLL" (zeropad icount ichar))) line and substitute whatever initial layer name characters you want.
(defun C:PLLYR ( ; No arguments.
/ ;_ Local variables:
edata ; Entity data of polyline being processed [association list].
ename ; Entity name of polyline being processed.
icount ; Current polyline being processed [integer].
ichar ; Number of characters required for total number of polylines [integer].
imax ; Total number of polylines to process [integer].
sspl ; Polylines in drawing [selection set].
zeropad ; Local subroutine to generate zero-padded string.
) ;_ End arguments and local variables.
(setq acmde (getvar "CMDECHO"))
(setvar "CMDECHO" 0)
;;;************************************************************************
;;;* Local subroutine to generate zero-padded string. *
;;;************************************************************************
(defun ZEROPAD ( ;_ Arguments:
inum ; Number to be converted to zero-padded string [integer].
ichar ; Number of characters in zero-padded string [integer].
/ ;_ Local variables:
ilen ; Number of characters in integer to be converted [integer].
snum ; Integer to be converted as a string [string].
) ;_ End arguments and local variables.
(setq snum (itoa inum) ; String equivalent of integer to be converted.
ilen (strlen snum) ; Length of integer string.
) ;_ End setq.
(cond ; Condition A.
((>= ilen ichar) ; Integer string length is greater than or equal to the padded string length.
snum ; Return integer string, as is.
) ;_ End condition A1.
(T ; Otherwise, process integer string.
(while (< ilen ichar) ; While integer string length is less than target length...
(setq snum (strcat "0" snum) ; ...add "0" to front of string and...
ilen (1+ ilen) ; ...increment string length.
) ;_ End setq.
) ;_ End while.
snum ; Return final string.
) ;_ End condition A2.
) ;_ End condition A.
) ;_ End ZEROPAD.
;;;************************************************************************
;;;* Start of main routine. *
;;;************************************************************************
(setq sspl (ssget "X" '((0 . "LWPOLYLINE")))
; Get all LWPOLYLINE objects in drawing.
imax (sslength sspl) ; Nunber of polylines in selection set.
ichar (strlen (itoa imax)) ; Number of characters in polyline number.
icount 0 ; Initialize polyline counter.
) ;_ End setq.
(while (setq ename (ssname sspl icount))
; While unprocessed polylines remain, do the following.
(setq icount (1+ icount) ; Increment polyline counter.
edata (entget ename) ; Get polyline data.
edata (subst (cons 8 (strcat "PLL" (zeropad icount ichar)))
(assoc 8 edata)
edata
) ; Update data for new layer name.
) ;_ End setq.
(entmod edata)
) ;_ End while.
(setvar "CMDECHO" acmde)
(prin1)
) ;_ End C:PLLYR.
I did not test this routine on a wide variety of files (only on one test file, that did not have much other than LWPOLYLINES and a few viewports in it), so I would recommend working on a copy of your original files so you still have the original should something go awry.
David Koch
AutoCAD Architecture and Revit User
Blog | LinkedIn
