Empty layers referenced in a block

Discussion in 'AutoCAD' started by johnsonm, Aug 16, 2004.

  1. johnsonm

    johnsonm Guest

    I have written a function to set all objects in a drawing, that I get from the city, to bylayer. At the same time creating a layer matching the properties that the entity was. Basically, if a line was on the layer 30 with the color red and linetype hidden, the function would make a layer called 30-1-hidden, color red and linetype hidden and place the line on that layer. I do all this so that when I xref the drawing into my own, I can change layer colors without changing the basic layer structure of the original. What I'm coming across is, there are about 50 blocks with layers empty layers referenced to them, so I cant purge them. The blocks were probably created with that layer, and not layer 0 as the current layer (poor drafting). How do I get VBA to find these layers and delete them or merge them to layer 0?

    Here is my code so far. Feel free to bash my methods, thats the only way to learn.

    Sub Citysitefix()
    Dim entcol As String, entlay As String, entline As String
    Dim blkcol As String, blklay As String, blkline As String
    Dim OldLayer As AcadLayer, laycol As New AcadAcCmColor, layname As String
    Dim oLayer As AcadLayer, oLayername As Variant
    For Each blk In ThisDrawing.Blocks
    For Each ent In blk
    entcol = ent.color
    entlay = ent.Layer
    entline = ent.Linetype
    If entcol = 256 Then
    Set OldLayer = ThisDrawing.Layers(entlay)
    entcol = OldLayer.color
    End If
    If entline = "Bylayer" Then
    Set OldLayer = ThisDrawing.Layers(entlay)
    entline = OldLayer.Linetype
    End If
    laycol.ColorIndex = entcol
    layname = entlay & "-" & entcol & "-" & entline
    Set oLayer = ThisDrawing.Layers.Add(layname)
    oLayer.Plottable = False
    oLayer.TrueColor = laycol
    oLayer.Linetype = entline
    oLayername = oLayer.Name
    ent.Layer = oLayername
    ent.color = acByLayer
    ent.Linetype = "Bylayer"
    ent.Lineweight = Bylayer
    Next
    Next
    ThisDrawing.PurgeAll
    End Sub
     
    johnsonm, Aug 16, 2004
    #1
  2. You can't purge them because they are still be in use - blocks *cannot*
    contain empty layers. You need to re-look at the file and determine what is
    actually on these layers. My guess is the blocks were created with their
    own layers [instead of 0] and then inserted onto a layer, lets call it "X".
    "X" cannot be deleted because it has the poorly created blocks on it and
    there is no way you can tell unless you specifcally select one of those
    blocks and check its layer - remember, visually you'll never "see" it since
    the block is displaying the property's of its nested layering, NOT its host
    layer.

    -- Mike
    ___________________________
    Mike Tuersley
    CADalyst's CAD Clinic
    Rand IMAGINiT Technologies
    ___________________________
    the trick is to realize that there is no spoon...
     
    Mike Tuersley, Aug 17, 2004
    #2
  3. johnsonm

    Mark Propst Guest


    I may be misreading or misunderstanding something in the code
    but I would think his code would find those insertions when it iterates the
    modelspace block
    it would put them on his newly named layer and set its' properties to by
    layer.
    That would clear the insertions off that original layer.
    Also when he iterates each block def, each object is likewise 're-layered'.
    So when he's done all the 'direct' entities should be on newly named layers.

    so that would leave sequends, frozen vplayers, dictionary references or
    xdata or any other kind of reference to the layer object which that code
    won't pick up and which might also hang onto the layer and prevent it's
    deletion would they not???

    and as a side issue, is a block which contains items on multiple layers
    really "poorly created"?
    I use many blocks which need various lineweights and linetypes and since
    everything is bylayer, they have multiple layers. Is that considered
    universally wrong?
    Do you put everything in every block on layer "0"? and then override
    lineweight and linetype to get it to plot correctly?
     
    Mark Propst, Aug 17, 2004
    #3
  4. Well, I didn't read his code just the premise as I said, blocks cannot
    contain empty layers ALL of the things you mentioned are are objects and,
    therefore, the layers are not empty. As for "poorly created" the generally
    accepted practice states "yes" - blocks should not contain layers in most
    cases. For lineweights and colors, the entities should be created using
    BYBLOCK properties for those values. Most cad users have no clue what
    BYBLOCK means. Of course, it all boils down to personal preference and
    doing whatever it takes to get it out the door. For you specifically, since
    you are writing apps, I'd start looking into byblock properties.

    -- Mike
    ___________________________
    Mike Tuersley
    CADalyst's CAD Clinic
    Rand IMAGINiT Technologies
    ___________________________
    the trick is to realize that there is no spoon...
     
    Mike Tuersley, Aug 17, 2004
    #4
Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.