Jump to content

Set by layer


loudy000

Recommended Posts

(defun c:tt (/ CNT DXDT DXFDNEW DXFNEW DXFOLD ENT SSET )
 (command "_.Layer" "_Make" "L-ANNO_TEXT" "_Color" "7" "" "_LType" "Continuous" "" "")
 (setq sset (ssget))
 (setq cnt 0)
 (repeat (sslength sset)
   (setq ent (ssname sset cnt))
   (setq dxdt (entget ent))
   (setq dxfold (assoc 8 dxdt))
   (setq dxfnew '(8 . "L-ANNO_TEXT"))
   (setq dxfdnew (subst dxfnew dxfold dxdt))
   (entmod dxfdnew)
   (setq cnt (1+ cnt))
 )
[color="olive"][color="olive"][color="red"](command "change" "P" "c" "" "BYLAYER" "" sset)[/color][/color][/color]
 (princ)
)

 

First of all thanks to Grrr for helping me on above routine. It's working great. I just want to add 1 more command to set the layer color to "BY LAYER". I tried to add the highlighted code but it doesn't work. Any help would be appreciated. Thanks in advance.

Link to comment
Share on other sites

(defun c:tt (/ CNT DXDT DXFDNEW DXFNEW DXFOLD ENT SSET )
 (command "_.Layer" "_Make" "L-ANNO_TEXT" "_Color" "7" "" "_LType" "Continuous" "" "")
 (setq sset (ssget))
 (setq cnt 0)
 (repeat (sslength sset)
   (setq ent (ssname sset cnt))
   (setq dxdt (entget ent))
   (setq dxfold (assoc 8 dxdt))
   (setq dxfnew '(8 . "L-ANNO_TEXT"))
   (setq dxfdnew (subst dxfnew dxfold dxdt))
   (entmod dxfdnew)
   (setq cnt (1+ cnt))
 )
(command "CHPROP" sset "" "COLOR" "BYLAYER" "LTYPE" "BYLAYER" "")
 (princ)
)

 

Got it!

Link to comment
Share on other sites

You could also use a subfunction to change the entity color within the repeat loop:

(defun c:tt (/ CNT DXDT DXFDNEW DXFNEW DXFOLD ENT SSET )
 (command "_.Layer" "_Make" "L-ANNO_TEXT" "_Color" "7" "" "_LType" "Continuous" "" "")
 (setq sset (ssget "_:L"))
 (setq cnt 0)
 (repeat (sslength sset)
   (setq ent (ssname sset cnt))
   (setq dxdt (entget ent))
   (setq dxfold (assoc 8 dxdt))
   (setq dxfnew '(8 . "L-ANNO_TEXT"))
   (setq dxfdnew (subst dxfnew dxfold dxdt))
   (entmod dxfdnew)
   (setq cnt (1+ cnt))
   (PutIndexColor ent 256)
 )
 (princ)
)

(defun PutIndexColor ( e col / enx )
 (and 
   (eq 'ENAME (type e)) (eq 'INT (type col)) (<= 0 col 256)
   (setq enx (vl-remove-if (function (lambda (x) (= 420 (car x)))) (entget e))) ; remove the true color if present
   (or
     (and (assoc 62 enx) (entmod (subst (cons 62 col) (assoc 62 enx) enx)))
     (entmod (append enx (list (cons 62 col))))
   ) 
 )
)

Link to comment
Share on other sites

Here's one that may suit your needs as a way to make things ByLayer. I didn't write it, but I use it a lot.

Found it some time ago in a search, but neglected to record the author or a link back to it.

 

(defun c:BL (/ i obj ss)
 (vl-load-com)
 (setq i -1 *doc (cond (*doc) ((vla-get-ActiveDocument (vlax-get-acad-object)))))
 (cond ((ssget "_:L")
        (vlax-for obj (setq ss (vla-get-ActiveSelectionSet *doc))
          (mapcar
            (function (lambda (prop value) (vlax-put-property obj prop value)))
            '(Color Linetype Lineweight) (list acByLayer "ByLayer" acLnWtByLayer)))
        (vla-delete ss)))
 (princ))

Steve

Link to comment
Share on other sites

@StevJ have you tried the command setbylayer?

 

Not sure which version of AutoCAD it was added but works similar to the code you posted. Has the advantage of working on blocks. Note the settings prompt on the commandline, can modify how the command works - setbylayer only color, linetype etc..

Link to comment
Share on other sites

@StevJ have you tried the command setbylayer?

 

Not sure which version of AutoCAD it was added but works similar to the code you posted. Has the advantage of working on blocks. Note the settings prompt on the commandline, can modify how the command works - setbylayer only color, linetype etc..

Yes, and I'm not really a fan of it. Too many questions to answer to get results.

I want to select my objects, issue the command (BL), press Enter and move on.

 

Steve

Link to comment
Share on other sites

You could also use a subfunction to change the entity color within the repeat loop:

(defun c:tt (/ CNT DXDT DXFDNEW DXFNEW DXFOLD ENT SSET )
 (command "_.Layer" "_Make" "L-ANNO_TEXT" "_Color" "7" "" "_LType" "Continuous" "" "")
 (setq sset (ssget "_:L"))
 (setq cnt 0)
 (repeat (sslength sset)
   (setq ent (ssname sset cnt))
   (setq dxdt (entget ent))
   (setq dxfold (assoc 8 dxdt))
   (setq dxfnew '(8 . "L-ANNO_TEXT"))
   (setq dxfdnew (subst dxfnew dxfold dxdt))
   (entmod dxfdnew)
   (setq cnt (1+ cnt))
   (PutIndexColor ent 256)
 )
 (princ)
)

(defun PutIndexColor ( e col / enx )
 (and 
   (eq 'ENAME (type e)) (eq 'INT (type col)) (<= 0 col 256)
   (setq enx (vl-remove-if (function (lambda (x) (= 420 (car x)))) (entget e))) ; remove the true color if present
   (or
     (and (assoc 62 enx) (entmod (subst (cons 62 col) (assoc 62 enx) enx)))
     (entmod (append enx (list (cons 62 col))))
   ) 
 )
)

 

Thanks Grrr as much as i wanted to but i only know basic codes ;(

Link to comment
Share on other sites

Here's one that may suit your needs as a way to make things ByLayer. I didn't write it, but I use it a lot.

Found it some time ago in a search, but neglected to record the author or a link back to it.

 

(defun c:BL (/ i obj ss)
 (vl-load-com)
 (setq i -1 *doc (cond (*doc) ((vla-get-ActiveDocument (vlax-get-acad-object)))))
 (cond ((ssget "_:L")
        (vlax-for obj (setq ss (vla-get-ActiveSelectionSet *doc))
          (mapcar
            (function (lambda (prop value) (vlax-put-property obj prop value)))
            '(Color Linetype Lineweight) (list acByLayer "ByLayer" acLnWtByLayer)))
        (vla-delete ss)))
 (princ))

Steve

 

Thanks StevJ i will use this in the future. :)

Link to comment
Share on other sites

OK. It was really bothering me that I couldn't credit the author, so I found it again after a bit of searching.

 

Written by LeeMac 02 FEB 2010

http://www.cadtutor.net/forum/showthread.php?44398-Macro

Post #2

 

Many thanks Steve, I appreciate your nobility.

 

Here is a Vanilla AutoLISP version, which may offer better performance:

(defun c:bl ( / i s )
   (if (setq s (ssget "_:L"))
       (repeat (setq i (sslength s))
           (entmod (append (entget (ssname s (setq i (1- i)))) '((006 . "ByLayer") (062 . 256) (370 . -1))))
       )
   )
   (princ)
)

Or, to avoid selection of objects which already have the desired properties:

(defun c:bl ( / i l s )
   (setq l
      '(
           (006 . "ByLayer")
           (062 . 256)
           (370 . -1)
       )
   )
   (if (setq s (ssget "_:L" (append '((-4 . "<NOT") (-4 . "<AND")) l '((-4 . "AND>") (-4 . "NOT>")))))
       (repeat (setq i (sslength s)) (entmod (append (entget (ssname s (setq i (1- i)))) l)))
   )
   (princ)
)

Link to comment
Share on other sites

Or, to avoid selection of objects which already have the desired properties:

(defun c:bl ( / i l s )
   (setq l
      '(
           (006 . "ByLayer")
           (062 . 256)
           (370 . -1)
       )
   )
   (if (setq s (ssget "_:L" (append '((-4 . "<NOT") (-4 . "<AND")) l '((-4 . "AND>") (-4 . "NOT>")))))
       (repeat (setq i (sslength s)) (entmod (append (entget (ssname s (setq i (1- i)))) l)))
   )
   (princ)
)

 

This one looks very creative, thanks for posting it! :thumbsup:

Link to comment
Share on other sites

Many thanks Steve, I appreciate your nobility.

 

Here is a Vanilla AutoLISP version, which may offer better performance:

(defun c:bl ( / i s )
   (if (setq s (ssget "_:L"))
       (repeat (setq i (sslength s))
           (entmod (append (entget (ssname s (setq i (1- i)))) '((006 . "ByLayer") (062 . 256) (370 . -1))))
       )
   )
   (princ)
)

Or, to avoid selection of objects which already have the desired properties:

(defun c:bl ( / i l s )
   (setq l
      '(
           (006 . "ByLayer")
           (062 . 256)
           (370 . -1)
       )
   )
   (if (setq s (ssget "_:L" (append '((-4 . "<NOT") (-4 . "<AND")) l '((-4 . "AND>") (-4 . "NOT>")))))
       (repeat (setq i (sslength s)) (entmod (append (entget (ssname s (setq i (1- i)))) l)))
   )
   (princ)
)

 

Thanks Lee for another awesome code :)

Link to comment
Share on other sites

BTW, Lee:

(entmod (append (entget (ssname s (setq i (1- i)))) '((006 . "ByLayer") (062 . 256) (370 . -1))))

By looking at this I understand that its unnecessary to remove the original 6,62,370 group codes,

but does this mean one simply can do this:

(entmod (append (entget (ssname s (setq i (1- i)))) '((008 . "0"))))

And even this (regarding if theres applied or not a index/true/book color):

(entmod (append (entget (ssname s (setq i (1- i)))) '((062 . 1))))

Where for example can occur the additional GCs like:

(62 . 31)
(420 . 16564615)
(430 . DIC COLOR GUIDE(R)$DIC 7)

:idea: wheres the question smiley dammit :cute:

Link to comment
Share on other sites

This one looks very creative, thanks for posting it! :thumbsup:
Thanks Lee for another awesome code :)

 

Thanks guys! :thumbsup:

 

BTW, Lee:

(entmod (append (entget (ssname s (setq i (1- i)))) '((006 . "ByLayer") (062 . 256) (370 . -1))))

By looking at this I understand that its unnecessary to remove the original 6,62,370 group codes,

but does this mean one simply can do this:

(entmod (append (entget (ssname s (setq i (1- i)))) '((008 . "0"))))

And even this (regarding if theres applied or not a index/true/book color):

(entmod (append (entget (ssname s (setq i (1- i)))) '((062 . 1))))

Where for example can occur the additional GCs like:

(62 . 31)

(420 . 16564615)

(430 . DIC COLOR GUIDE®$DIC 7)[/code[color=silver][size=1]][/size][/color]

 

Indeed, it's not necessarily considered the 'correct' way to modify entity properties and this method may also fail in earlier versions of AutoCAD, but in newer releases, for DXF groups whose key should only occur once, the last such occurrence of the group will take precedence. Though, there are some exceptions to this rule, as certain DXF groups must also appear in a certain order (for example, DXF groups 70 & 90 for an LWPolyline).

Link to comment
Share on other sites

Thanks for answering Lee,

I performed some tests:


[color=#8b4513]; Asigning Color:[/color]
[color=#8b4513]; Doesn't work on multi-segment LWPOLYLINES, SPLINES, where is assigned truecolor [b][color=BLACK]([/color][/b]or colorbook[b][color=BLACK])[/color][/b] ;; but works when they have index color[/color]
[color=#8b4513]; When the GC 420 is removed, works on everything[/color]
[b][color=BLACK]([/color][/b]defun C:test [b][color=FUCHSIA]([/color][/b] / e enx [b][color=FUCHSIA])[/color][/b]
 [b][color=FUCHSIA]([/color][/b]while [b][color=NAVY]([/color][/b]not [b][color=MAROON]([/color][/b]setq e [b][color=GREEN]([/color][/b]car [b][color=BLUE]([/color][/b]entsel [color=#2f4f4f]"\nSelect entity: "[/color][b][color=BLUE])[/color][/b][b][color=GREEN])[/color][/b][b][color=MAROON])[/color][/b][b][color=NAVY])[/color][/b] e[b][color=FUCHSIA])[/color][/b]
 [b][color=FUCHSIA]([/color][/b]setq enx [b][color=NAVY]([/color][/b]entget e[b][color=NAVY])[/color][/b][b][color=FUCHSIA])[/color][/b]
 [b][color=FUCHSIA]([/color][/b]setq enx [b][color=NAVY]([/color][/b]vl-remove-if [b][color=MAROON]([/color][/b]function [b][color=GREEN]([/color][/b]lambda [b][color=BLUE]([/color][/b]x[b][color=BLUE])[/color][/b] [b][color=BLUE]([/color][/b]= 420 [b][color=RED]([/color][/b]car x[b][color=RED])[/color][/b][b][color=BLUE])[/color][/b][b][color=GREEN])[/color][/b][b][color=MAROON])[/color][/b] enx[b][color=NAVY])[/color][/b][b][color=FUCHSIA])[/color][/b] [color=#8b4513]; <- Included[/color]
 [b][color=FUCHSIA]([/color][/b]entmod [b][color=NAVY]([/color][/b]append enx [b][color=MAROON]([/color][/b]list [b][color=GREEN]([/color][/b]cons 62 1[b][color=GREEN])[/color][/b][b][color=MAROON])[/color][/b][b][color=NAVY])[/color][/b][b][color=FUCHSIA])[/color][/b]
[b][color=BLACK])[/color][/b]


[color=#8b4513]; Asigning Lineweight:[/color]
[b][color=BLACK]([/color][/b]defun C:test [b][color=FUCHSIA]([/color][/b] / e enx [b][color=FUCHSIA])[/color][/b]
 [b][color=FUCHSIA]([/color][/b]while [b][color=NAVY]([/color][/b]not [b][color=MAROON]([/color][/b]setq e [b][color=GREEN]([/color][/b]car [b][color=BLUE]([/color][/b]entsel [color=#2f4f4f]"\nSelect entity: "[/color][b][color=BLUE])[/color][/b][b][color=GREEN])[/color][/b][b][color=MAROON])[/color][/b][b][color=NAVY])[/color][/b] e[b][color=FUCHSIA])[/color][/b]
 [b][color=FUCHSIA]([/color][/b]setq enx [b][color=NAVY]([/color][/b]entget e[b][color=NAVY])[/color][/b][b][color=FUCHSIA])[/color][/b]
 [b][color=FUCHSIA]([/color][/b]entmod [b][color=NAVY]([/color][/b]append enx [b][color=MAROON]([/color][/b]list [b][color=GREEN]([/color][/b]cons 370 -1[b][color=GREEN])[/color][/b][b][color=MAROON])[/color][/b][b][color=NAVY])[/color][/b][b][color=FUCHSIA])[/color][/b]
[b][color=BLACK])[/color][/b]

[color=#8b4513]; Asigning Linetype:[/color]
[b][color=BLACK]([/color][/b]defun C:test [b][color=FUCHSIA]([/color][/b] / e enx [b][color=FUCHSIA])[/color][/b]
 [b][color=FUCHSIA]([/color][/b]while [b][color=NAVY]([/color][/b]not [b][color=MAROON]([/color][/b]setq e [b][color=GREEN]([/color][/b]car [b][color=BLUE]([/color][/b]entsel [color=#2f4f4f]"\nSelect entity: "[/color][b][color=BLUE])[/color][/b][b][color=GREEN])[/color][/b][b][color=MAROON])[/color][/b][b][color=NAVY])[/color][/b] e[b][color=FUCHSIA])[/color][/b]
 [b][color=FUCHSIA]([/color][/b]and
   [b][color=NAVY]([/color][/b]tblsearch [color=#2f4f4f]"LTYPE"[/color] [color=#2f4f4f]"ACAD_ISO07W100"[/color][b][color=NAVY])[/color][/b]
   [b][color=NAVY]([/color][/b]setq enx [b][color=MAROON]([/color][/b]entget e[b][color=MAROON])[/color][/b][b][color=NAVY])[/color][/b]
   [b][color=NAVY]([/color][/b]entmod [b][color=MAROON]([/color][/b]append enx [b][color=GREEN]([/color][/b]list [b][color=BLUE]([/color][/b]cons 6 [color=#2f4f4f]"ACAD_ISO07W100"[/color][b][color=BLUE])[/color][/b][b][color=GREEN])[/color][/b][b][color=MAROON])[/color][/b][b][color=NAVY])[/color][/b]
 [b][color=FUCHSIA])[/color][/b]
[b][color=BLACK])[/color][/b]

Looks like you have to remove the 420 GC in order to work on SPLINE/multisegment LWPOLYLINE entities, that have assigned truecolor or colorbook color. ( I remember once that was trying to assign index color, using vanilla and it wasn't that easy ).

I'm leaving this info here, in case I have to 'reinvent the wheel' about this again. o:)

Link to comment
Share on other sites

  • 10 months later...

Hi there,

I tried setbylayer to change the color of a dynamic block but for reasons unknown even I select only 3 block from design to change the color it will change also the color of others blocks not selected.

I tried also these lisps but without any success.

Many thanks

Link to comment
Share on other sites

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.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...