TheSwamp

Code Red => VB(A) => Topic started by: ML on August 16, 2007, 12:51:34 PM

Title: All Layers to ByLayer
Post by: ML on August 16, 2007, 12:51:34 PM

This one should be really easy but for some reason I am still getting an error
Does anyone see the problem???

Thank you

Mark

Code: [Select]
Sub AllLayersToByLayer()

Dim layer As AcadLayer


For Each layer In ThisDrawing.Layers
 layer.color = acByLayer
 
Next layer

End Sub
Title: Re: All Layers to ByLayer
Post by: Guest on August 16, 2007, 12:58:21 PM
.COLOR is obsolete.  .TRUECOLOR is the new replacement.

What version of AutoCAD are you using?  2008 has a command called SETBYLAYER which you might want to look into.


Code: [Select]
Sub AllLayersToByLayer()
    Dim color As AcadAcCmColor
    Dim layer As AcadLayer
   
    Set color = New AcadAcCmColor
    With color
        .ColorMethod = acColorMethodByACI
        .ColorIndex = "256"
    End With
   
    For Each layer In ThisDrawing.Layers
        layer.TrueColor = color
    Next layer
End Sub
Title: Re: All Layers to ByLayer
Post by: ML on August 16, 2007, 01:04:43 PM

WOW

That's interesting Matt

You would think out of the 3 books I have and the help screens, they would have had a proper example

Let me give it a whirl

Thank you

Mark
Title: Re: All Layers to ByLayer
Post by: ML on August 16, 2007, 01:10:04 PM
Hi Matt

May be I wasn't totally clear; I am looking to change all layer colors to bylayer

Thank you

Mark
Title: Re: All Layers to ByLayer
Post by: kpblc on August 16, 2007, 01:49:58 PM
Did i have right understand? Change layer colors to ByLayer?
Of course you can do it but this could crash AutoCAD or dwg file (can't open it next time). I receive this error one time and have no wishes to repeat it.
Layer objects can't be with color ByLayer or ByBlock. You have to set color by ACI (from 1 to 255) or TrueColor.
Title: Re: All Layers to ByLayer
Post by: ML on August 16, 2007, 01:59:55 PM

Ahh

You make a good point

You would need to grab all the objects (entities) in the drawing and change the object names to ByLayer

I believe

Thank you

Mark
Title: Re: All Layers to ByLayer
Post by: Guest on August 16, 2007, 02:06:36 PM
So you want all of the objects to be color bylayer, right?  Example: A line is drawn on layer "TEMP" and layer "TEMP"'s color is blue, but the line shows as red, you want that line's color property to be changed to BYLAYER instead of 1 (red), correct?

If you're using 2008, SETBYLAYER is by far the best/fastest way to accomplish this.

If not, we'll have to come up with something (unless somebody has already rolled one that they'd like to pass around).   ^-^
Title: Re: All Layers to ByLayer
Post by: ML on August 16, 2007, 02:42:58 PM

I am using 2006 and it doesn't exist in there :(

I tried this little bit of code:

Code: [Select]
Sub AllLayersToByLayer()

Dim Obj As Object

For Each Obj In ThisDrawing.ModelSpace
 Obj.color = acByLayer
Next

End Sub

It says, get all objects in Modelspace and make them to Bylayer
Title: Re: All Layers to ByLayer
Post by: ML on August 16, 2007, 02:43:34 PM

But it is still not working
Title: Re: All Layers to ByLayer
Post by: deegeecees on August 16, 2007, 02:47:26 PM
Non-programmatic way:
Select all objects, then in the properties dialog, select "Color", "ByLayer".
Title: Re: All Layers to ByLayer
Post by: ML on August 16, 2007, 02:47:58 PM

BOOOO!

You are no fun  LOL :)
Title: Re: All Layers to ByLayer
Post by: Guest on August 16, 2007, 02:48:22 PM
Try this.... (or use what DGCs suggests)

Code: [Select]
Sub AllLayersToByLayer()
    Dim color As AcadAcCmColor
    Dim obj As AcadEntity
    
    Set color = New AcadAcCmColor
    With color
        .ColorMethod = acColorMethodByACI
        .ColorIndex = "256"
    End With
    
    For Each obj In ThisDrawing.ModelSpace
        obj.TrueColor = color
    Next obj
End Sub
Title: Re: All Layers to ByLayer
Post by: deegeecees on August 16, 2007, 02:48:57 PM
I know, sometimes I feel like a wet blanket.

 :-)
Title: Re: All Layers to ByLayer
Post by: ML on August 16, 2007, 02:55:43 PM

No, I agree with you
I use to program just for fun, now I generally have a rule which is program out of nec.

However, somone earlier asked if you can globally change all objects to bylayer at once, so the intellect kicked in  :-(   LOL

I thought it would take me 5 minutes to put together but it obviously didn't LOL

There are somethings you can do.
I could use the filter command in acad and ask him specifically what layers he runs into this the most with, then create a filter that he could run.

It's no big deal really, I am just curious now

Mark
Title: Re: All Layers to ByLayer
Post by: deegeecees on August 16, 2007, 03:03:26 PM
Matts code is what you need then.
Title: Re: All Layers to ByLayer
Post by: Jeff_M on August 16, 2007, 03:03:47 PM
I did a similar thing in lisp a few years ago, except it goes beyond what you can do with the Properties Palette. Such as unlocking any unlocked layers first, includes all objects in all layouts and in all blocks, then resets any locked layers.
Title: Re: All Layers to ByLayer
Post by: Guest on August 16, 2007, 03:07:09 PM
I did a similar thing in lisp a few years ago, except it goes beyond what you can do with the Properties Palette. Such as unlocking any unlocked layers first, includes all objects in all layouts and in all blocks, then resets any locked layers.

Wasn't that "All2ByLayer"?  I think I used/modified something like that of yours, but alas, now that 2008 has SETBYLAYER, well..... I just don't need it anymore.  I hope you understand.  It's not you; it's me.   :lol:
Title: Re: All Layers to ByLayer
Post by: deegeecees on August 16, 2007, 03:07:16 PM
This is worth a look (http://www.theswamp.org/index.php?topic=8573.0)
Title: Re: All Layers to ByLayer
Post by: David Hall on August 16, 2007, 03:08:31 PM
Here is what I use, modify to suit
Code: [Select]
Public Sub Everythingbylayer()

      Dim objSelected As Object
      Dim OBJSELSET As AcadSelectionSet
      Dim N As Integer

      On Error GoTo ERR_HAND
      ThisDrawing.SetVariable "CECOLOR", "BYLAYER"
      ThisDrawing.SetVariable "CELTYPE", "BYLAYER"
      Set OBJSELSET = ThisDrawing.SelectionSets.Add("EBL")
      OBJSELSET.Select acSelectionSetAll

      For Each objSelected In OBJSELSET
            objSelected.color = acByLayer
            objSelected.Linetype = "ByLayer"
            objSelected.Lineweight = acLnWtByLayer
            objSelected.Update
      Next

      ThisDrawing.SelectionSets.Item("EBL").Delete
      ZoomExtents
      ThisDrawing.Application.Update
Exit_Here:
      Exit Sub
ERR_HAND:
      Select Case Err.Number
            Case -2145320851
                  ThisDrawing.SelectionSets("EBL").Delete
                  Resume
            Case -2145386413
                  Resume Next
            Case Else
                  MsgBox Err.Number & " " & Err.Description
      End Select
End Sub
Title: Re: All Layers to ByLayer
Post by: David Hall on August 16, 2007, 03:10:02 PM
This is worth a look (http://www.theswamp.org/index.php?topic=8573.0)
Wow, I forgot all about that!  Cool blast from the past, thanks DGCs
Title: Re: All Layers to ByLayer
Post by: David Hall on August 16, 2007, 03:12:35 PM
I just looked at SETBYLAYER, and I had everything but the material and plot style options.  It also processes blocks, FYI, just in case you miss the prompt.  Some of my blocks are hard coded to layers on purpose
Title: Re: All Layers to ByLayer
Post by: Guest on August 16, 2007, 03:13:35 PM
I just looked at SETBYLAYER, and I had everything but the material and plot style options.  It also processes blocks, FYI, just in case you miss the prompt.  Some of my blocks are hard coded to layers on purpose

Nested blocks, too!
Title: Re: All Layers to ByLayer
Post by: ML on August 16, 2007, 03:18:41 PM


Yes

Matts code did work fine
I had forgotten to put some objects in my drawing to test it  DUH  LOL

I will certainly keep it for future use

And yes, of course you could do a Ctrl A to select all objects, then choose Bylayer from the pulldown

Dam intellect  LOL

Thanks Matt
Title: Re: All Layers to ByLayer
Post by: ronjonp on August 16, 2007, 03:20:27 PM
Here is MP's solution.. (http://www.theswamp.org/index.php?topic=3020.msg37746#msg37746)
Title: Re: All Layers to ByLayer
Post by: Guest on August 16, 2007, 03:25:21 PM
Thanks Matt

You're welcome.


You got plenty of good solutions here, so use whatever works best for you.
Title: Re: All Layers to ByLayer
Post by: ML on August 16, 2007, 03:32:54 PM

Yes

Matt yours worked well, I was making the silly mistake of trying to change a lyer's property to bylayer.
Well, that won't work because Bylayer is the object's property and you got it.

Yes, your code worked fine

I saved the above code as well because I like how he applied the selection set to achieve the same result with a bit more functionality

Mark
Title: Re: All Layers to ByLayer
Post by: Bryco on August 16, 2007, 11:30:26 PM
To be complete you need to add an Mtext to bylayer function (As in make the text inside the mtext bylayer). I've found some client drawings have all their mtext forced to another color.
Yuk.