I am truely stuck, someone help me please.
As the subject states, I am trying to apply a plotstyle to a layer. I can create the layer and apply a plot style but as soon as I create another layer with a different plotstyle, the layers I previously created lose their plot style (it appears to be removed from the drawing)
I have tried workarounds such as placing an object and applying a plotstyle then deleting that plot style and that still doesnt seem to append it to the drawing.
this is my code so far:
Public Sub addPltStToLayer(ByVal id As ObjectId, ByVal pltsty As String) Dim thisDrawing As Document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument Dim thisDB As Database = thisDrawing.Database Dim trans As Transaction = thisDB.TransactionManager.StartTransaction Using locker As DocumentLock = thisDrawing.LockDocument Dim dict As DictionaryWithDefaultDictionary = trans.GetObject(thisDB.PlotStyleNameDictionaryId, DatabaseServices.OpenMode.ForRead) Dim ltr As LayerTableRecord = trans.GetObject(id, DatabaseServices.OpenMode.ForWrite) ltr.PlotStyleNameId = dict.GetAt(pltsty) End Using trans.Commit() trans.Dispose() End Sub Public Sub addPstyle() Dim thisDrawing As Document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument Dim thisDB As Database = thisDrawing.Database Using trans As Transaction = thisDB.TransactionManager.StartTransaction Using placeHold As PlaceHolder = New PlaceHolder Dim dict As DictionaryWithDefaultDictionary = trans.GetObject(thisDB.PlotStyleNameDictionaryId, DatabaseServices.OpenMode.ForWrite) If dict.Contains(PltStBut.Text) Then 'do nothing, its already here Else dict.SetAt(PltStBut.Text, thisDB.AcadDatabase) End If trans.Commit() End Using End Using End Sub
Solved! Go to Solution.
Solved by dheselwoodEV. Go to Solution.
It appears nobody actually knows how to apply a plot style to a layer properly - there must be someone out there who has the infinite wisdom to be able to answer this - please!
After well over 7 hours of coding a bazillion different methods of trying to get this working, I got it!!!!
If and when I get a chance to tidy the code, I will do but this is rough and ready for anyone else stuck
The final code (There is, of course, a dialog behind the code which gives the layer properties):
Private Sub AddLayer_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click '1. append plot style to drawing addPlotStyle() '2. add layer Dim layID As ObjectId = addLayerToDWG() '2. AddPlotStyleTo Layer appendPStyle(layID) End Sub Public Sub appendPStyle(ByVal layID As ObjectId) Dim thisDrawing As Document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument Dim thisDB As Database = thisDrawing.Database If thisDB.PlotStyleMode = False Then Using locker As DocumentLock = thisDrawing.LockDocument Using trans As Transaction = thisDB.TransactionManager.StartTransaction Dim dict As DictionaryWithDefaultDictionary = trans.GetObject(thisDB.PlotStyleNameDictionaryId, DatabaseServices.OpenMode.ForRead) Dim ltr As LayerTableRecord = trans.GetObject(layID, DatabaseServices.OpenMode.ForWrite) If dict.Contains(PltStBut.Text) = True Then ltr.PlotStyleNameId = dict.GetAt(PltStBut.Text) End If trans.Commit() End Using End Using End If End Sub Public Sub addPlotStyle() Dim thisDrawing As Document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument Dim thisDB As Database = thisDrawing.Database Using locker As DocumentLock = thisDrawing.LockDocument Using trans As Transaction = thisDB.TransactionManager.StartTransaction Using plHldr As PlaceHolder = New PlaceHolder Dim dict As DictionaryWithDefaultDictionary = trans.GetObject(thisDB.PlotStyleNameDictionaryId, DatabaseServices.OpenMode.ForWrite) If dict.Contains(PltStBut.Text) = True Then dict.SetAt(PltStBut.Text, plHldr) trans.Commit() End If End Using End Using End Using End Sub Public Function addLayerToDWG() 'create layer here Dim thisDrawing As Document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument Dim thisDB As Database = thisDrawing.Database Dim trans As Transaction = thisDB.TransactionManager.StartTransaction Dim ed As Editor = thisDrawing.Editor Dim lt As LayerTable = trans.GetObject(thisDB.LayerTableId, DatabaseServices.OpenMode.ForRead) 'else continue and create layer Dim lockMeUp As DocumentLock = thisDrawing.LockDocument Dim newLay As New LayerTableRecord newLay.Name = LaNmVal.Text newLay.Color = Autodesk.AutoCAD.Colors.Color.FromColorIndex(Colors.ColorMethod.ByAci, CType(Strings.Right(ColPickBut.Text, Len(ColPickBut.Text) - 7), Short)) If lWeightBut.Text = "Default" Then newLay.LineWeight = -3 Else newLay.LineWeight = Replace(lWeightBut.Text, ".", "") End If 'add the linetype Dim acLinTbl As LinetypeTable acLinTbl = trans.GetObject(thisDB.LinetypeTableId, DatabaseServices.OpenMode.ForRead) If acLinTbl.Has(LStyleList.SelectedItem) = True Then 'Set the linetype for the layer newLay.LinetypeObjectId = acLinTbl(LStyleList.SelectedItem) End If lt.UpgradeOpen() lt.Add(newLay) trans.AddNewlyCreatedDBObject(newLay, True) 'add layer description newLay.Description = DescVal.Text trans.Commit() lockMeUp.Dispose() trans.Dispose() Return newLay.Id End Function