Author Topic: If Layer Exists Then:  (Read 18505 times)

0 Members and 1 Guest are viewing this topic.

ML

  • Guest
If Layer Exists Then:
« on: February 17, 2005, 01:08:44 PM »
I am working on a module but I need to write an Errorhandler to handle the duplicate record error message associated with duplicate layers.

Unfortunately after many tries, I can not write sufficient code that will check the layers collection to see if a layer already exists

Does anyone happen to have an example around that I can draw from?

Thank you


Mark

hendie

  • Guest
If Layer Exists Then:
« Reply #1 on: February 17, 2005, 01:31:51 PM »
what code do you have just now ?

I'm not sure what you mean by
Quote
to handle the duplicate record error message associated with duplicate layers


this is straight out of the acad help

Code: [Select]
Private Sub addlayer()

' This example creates a new layer called "New_Layer"
    Dim layerObj As AcadLayer
   
    ' Add the layer to the layers collection
    Set layerObj = ThisDrawing.Layers.Add("New_Layer")
   
    ' Make the new layer the active layer for the drawing
    ThisDrawing.ActiveLayer = layerObj
   
    ' Display the status of the new layer
     MsgBox layerObj.Name & " has been added." & vbCrLf & _
            "LayerOn Status: " & layerObj.LayerOn & vbCrLf & _
            "Freeze Status: " & layerObj.Freeze & vbCrLf & _
            "Lock Status: " & layerObj.Lock & vbCrLf & _
            "Color: " & layerObj.color, , "Add Example"

End Sub

you can run it as many times as you wish and it doesn't generate an error message

Anonymous

  • Guest
If Layer Exists Then:
« Reply #2 on: February 17, 2005, 01:35:24 PM »
Hey Hendie

Actually I don't have anything to post related to what I am asking for.

I can tell you what I am doing if you like, or if it will help?

I was hoping for a very generic piece of code that will delete a layer if it already exists in my new layer collection

Mark

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
If Layer Exists Then:
« Reply #3 on: February 17, 2005, 01:45:20 PM »
Quote from: Anonymous
I was hoping for a very generic piece of code that will delete a layer if it already exists in my new layer collection
Mark


Meaning if it exists in the drawing, dont try and recreate it?
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)

Anonymous

  • Guest
If Layer Exists Then:
« Reply #4 on: February 17, 2005, 02:02:50 PM »
Yes CmdrDuh

That is exactly what I am looking for
If the layer exists, then don't try to recreate it

I need code for that exactly

Mark

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
If Layer Exists Then:
« Reply #5 on: February 17, 2005, 02:25:46 PM »
Let me see what i have
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
If Layer Exists Then:
« Reply #6 on: February 17, 2005, 02:58:17 PM »
Something like this ?
Code: [Select]

Option Explicit

Public Sub test()
    Dim bResult        As Boolean
    bResult = DoesLayerExist("XXX_001")
    If bResult Then
        MsgBox "Layer XXX_001 exists"
    Else
        MsgBox "Not Today Mark"
    End If
End Sub

Public Function DoesLayerExist(sLayer As String) As Boolean
    Dim oLayers        As AcadLayers
    Dim oLayer         As AcadLayer
    On Error GoTo Skippy
    Set oLayers = ThisDrawing.Layers
    Set oLayer = oLayers.Item(sLayer)
    DoesLayerExist = True
    Exit Function
Skippy:
   
End Function
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Anonymous

  • Guest
If Layer Exists Then:
« Reply #7 on: February 17, 2005, 04:49:27 PM »
Hey Kerry

Can I use you above code to specify multiple layers?

There are about 8 - 10 layers in my drawing that I want VBA to check to see if they already exist.


Thank you

Mark

ML

  • Guest
If Layer Exists Then:
« Reply #8 on: February 17, 2005, 04:52:45 PM »
Thanks Kerry,

Thanks CMD

Sorry, I didn't Log in the last few times so I was posting as a guest.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
If Layer Exists Then:
« Reply #9 on: February 17, 2005, 06:04:35 PM »
This may give you some ideas ,
Code: [Select]

Public Sub test_01()
    Dim bResult        As Boolean
    Dim svLayerList(0 To 9) As String
    Dim cnt            As Integer
    Dim sLayName       As String

    svLayerList(0) = "Layer0"
    svLayerList(1) = "0"
    svLayerList(2) = "Layer2"
    svLayerList(3) = "NoWay"
    svLayerList(4) = "Layer4"
    svLayerList(5) = "Layer5"
    svLayerList(6) = "Layer6"
    svLayerList(7) = "Layer7"
    svLayerList(8) = "Layer8"
    svLayerList(9) = "Bogus"

    For cnt = 0 To UBound(svLayerList)
        sLayName = svLayerList(cnt)
        bResult = DoesLayerExist(sLayName)
       
        If bResult Then
            MsgBox "Layer : " & sLayName & vbCrLf & " exists"
        Else
            MsgBox "Layer : " & sLayName & vbCrLf & " does NOT exist"
        End If
    Next
End Sub


Public Function DoesLayerExist(sLayer As String) As Boolean
    Dim oLayers        As AcadLayers
    Dim oLayer         As AcadLayer
   
    On Error Resume Next
    Set oLayers = ThisDrawing.Layers
    Set oLayer = oLayers.Item(sLayer)
    DoesLayerExist = (Err.Number = 0)
    Err.Clear
End Function
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
If Layer Exists Then:
« Reply #10 on: February 17, 2005, 06:06:59 PM »
Note that the DoesLayerExist() is slightly different to the prev' version.
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

ML

  • Guest
If Layer Exists Then:
« Reply #11 on: February 18, 2005, 08:24:29 AM »
That looks like it will do the trick
I really appreciate it.

Let me go give it a try.

I'll let you know how it works

Thank you

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
If Layer Exists Then:
« Reply #12 on: February 18, 2005, 08:55:36 AM »
Good job Kerry,  I got busy and couldn't post til this morning, and you beat me to it.
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)

ML

  • Guest
In need of more help
« Reply #13 on: February 18, 2005, 11:36:32 AM »
Hey Guys,

I really appreciate your help thus far.
The above code works great but either I am not utilizing it exactly how I need to or may be one of you will have another suggestion.

Below is an example of the module I am developing

I am inserting a block, it is a legend for Intercomm symbols.

My menu macro allows you to choose what scale (out of 7 or so) you would like to insert the block into your drawing at.

The menu macro pauses for insertion, after the user inserts the block, the Vba Module is fired off.

The Below Code (For Each Loop)
is repeated  for each scale that the user can select from, it was just too much to paste in

On the first block inserted, all works well.
So, if you look at the For Each Loop below, I am saying if The Block Intercomm has an xscale factor of 48, then rename the layer Co-I-SYMB toCo-I-SYMB-48

Now, if I insert a second Intercomm at a different scale (for instance 96)
It seems to be going through all of the possible scale factors in the module, seeing 48 again, then I get the duplicate record error and the layer remains at Co-I-SYMB

I need to somehow have it see that there is a 48, overlook it then proceed to 96 and create Co-I-SYMB-96 layer and so on.


So, I was trying to filter by layer name.
If that layer exists, then move on, but I am still having no luck

I hope this makes sense?


Any help will be APPRECIATED


Thanks

Mark

   

Code: [Select]

Dim Co_Layers As AcadLayers
Dim Intercomm As AcadBlockReference
Dim BlockXScaleFactor As Double

Set Co_Layers = ThisDrawing.Layers
Set Intercomm = ThisDrawing.ModelSpace.Item(Intercomm1)

BlockXScaleFactor = Intercomm.XScaleFactor



If Intercomm.XScaleFactor = 48 Then
 For Each Layer In Co_Layers
  If Layer.Name = ("Co-I-SYMB") Then
     Layer.Name = ("Co-I-SYMB-48")
  End If
 Next
End If


Exit Sub

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
If Layer Exists Then:
« Reply #14 on: February 18, 2005, 11:52:37 AM »
i think your problem might be in the way you are pulling the scalefactor.  Is it possible you are going back to the first block and getting its scale factor not the new block factor?  Thus it sees the 48 again
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)