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.
Solved by Virupaksha_aithal. Go to Solution.
Hi,
Sorry, I am not completely clear about the question. I can see you have used LayerTable.Has() API. Is their any issue if you check the new name of the layer first, if duplicate, then skip that number…till you get the unique new layer name?
<CommandMethod("RenameLayers")> Public Sub RenameLayers() Dim acCurDb As Database = Application.DocumentManager.MdiActiveDocument.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 acCurDb.Clayer = acLyrTbl("0") For Each LayerId As ObjectId In acLyrTbl While (acLyrTbl.Has(la) = True) la += 1 End While 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 End If la += 1 Next acTrans.Commit() End Using Catch ex As System.Exception Application.ShowAlertDialog("Error Renaming Layers:" & vbLf & ex.Message) End Try End Sub
Thank you very much, using the while statement before, does allow for the renaming of all layers. It works and will produce the desired outcome. With one small issue, if that layer name does exist (layername as a number), it will skip that number in the rename process. It’s not the end of the world, but I would curious as how to still be able to use that number.
Meaning, if a layer by the name of 27 already exists, the layers will be numbered, ... 25, 26, 28, 29...
again not not the end of the world for what I am doing but would be curious for future things.
thank you very much for your help. And sorry for the late reply.
Hi,
Then, you need to skip renaming the layer which has name as number, (if you are not worried about the order of the layer...)like
have a Object ID list... which stores layer which need not be renamed...
now, when you find a Layer with number name, then store the ID of such layer in the Object ID list and skip the re-naming of that particular layer..
Can't find what you're looking for? Ask the community or share your knowledge.