- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Hi,
I am trying to write a routine that will rename all layers to numbers. (Don't ask.
) It is a random naming; there is no order, or this layer to number 12 and that layer to 15. It does not matter. The code below works great except for one small problem; if there is a layer named that number already I get a duplicate key error.
Here is what i need help with, if the "number/name" exists already, keep that number/name, do the remaining changes, and move onto the next layer. The problem is; that when the routine (integer) gets to say number 27, the table record isn't necessarily the layer that is already named 27. How do I get the code to keep the layer that is already named 27 make the other changes and then move on to renaming the remaining layers. And if it then happens again at say 56...
I pretty sure that this is a simple math thing i am overlooking/missing.
Any help would be greatly appreciated.
<CommandMethod("RenameLayers")>
Public Sub RenameLayers()
Dim acCurDb As Database = Active.Database
Try
Using acTrans As Transaction = acCurDb.TransactionManager.StartTransaction()
Dim acLyrTbl As LayerTable = DirectCast(acTrans.GetObject(acCurDb.LayerTableId, OpenMode.ForWrite), LayerTable)
Dim acLinTbl As LinetypeTable = acTrans.GetObject(acCurDb.LinetypeTableId, OpenMode.ForRead)
Dim la As Integer = 1
Dim alpha As Byte = CType((255 * (100 - 0) / 100), Byte)
Dim layerTransparency As Transparency = New Transparency(alpha)
acCurDb.Clayer = acLyrTbl("0")
For Each LayerId As ObjectId In acLyrTbl
Dim newLayerName As String = la.ToString
Dim acLyrTblRec As LayerTableRecord = DirectCast(acTrans.GetObject(LayerId, OpenMode.ForWrite, False), LayerTableRecord)
If Not acLyrTblRec.Name = "0" Then
'If acLyrTbl.Has(newLayerName) = True Then
If acLyrTblRec.Name = newLayerName Then
'what goes here?
Else
acLyrTblRec.Name = newLayerName
End If
acLyrTblRec.Color = Color.FromColorIndex(ColorMethod.ByAci, 7)
acLyrTblRec.LinetypeObjectId = acLinTbl("Continuous")
acLyrTblRec.LineWeight = LineWeight.ByLineWeightDefault
acLyrTblRec.Transparency = layerTransparency
acLyrTblRec.PlotStyleName = "Normal"
acLyrTblRec.Description = ""
End If
la += 1
Next
acTrans.Commit()
End Using
Catch ex As Exception
Application.ShowAlertDialog("Error Renaming Layers:" & vbLf & ex.Message)
End Try
End Sub
Solved! Go to Solution.