Commander, here is updated code
seemed all bugs is fixed, to be honestly I'm not sure
Anyway all is ok on my end
use short commands MakeEm and SetEm,
agreed, looks ugly but working
Try again:
#Region "Mleader style commands"
<CommandMethod("SetMyMleaderStyle", "SetEm", CommandFlags.NoInternalLock)>
Public Shared Sub DemoSet()
Dim doc As Document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument
Dim ed As Editor = doc.Editor
Dim db As Database = doc.Database
Dim mldict As DBDictionary = Nothing
Dim mldstyle As MLeaderStyle = Nothing
Dim mlstylename As String = "MyMleaderStyle"
Dim getvarObj As Object = Autodesk.AutoCAD.ApplicationServices.Application.GetSystemVariable("CMLEADERSTYLE")
ed.WriteMessage(Environment.NewLine + "Current Mleader style before : {0}", getvarObj.ToString)
Dim tr As Transaction = doc.TransactionManager.StartTransaction
Try
Using tr
mldict = CType(tr.GetObject(db.MLeaderStyleDictionaryId, OpenMode.ForRead), DBDictionary)
If Not IsDbDictionaryExists(mldict, mlstylename) Then
ed.WriteMessage("Mleader style ""{0}"" does not exists", mlstylename)
Return
End If
' 1st method:
mldstyle = CType(tr.GetObject(mldict.GetAt(mlstylename), OpenMode.ForRead), MLeaderStyle)
db.MLeaderstyle = mldict.GetAt(mlstylename)
''2nd method
'' getvarObj = mlstylename
''Autodesk.AutoCAD.ApplicationServices.Application.SetSystemVariable("CMLEADERSTYLE", getvarObj)
'' for dispalying result in final part:
getvarObj = Autodesk.AutoCAD.ApplicationServices.Application.GetSystemVariable("CMLEADERSTYLE")
tr.Commit() '<-- important, otherwise new settings will not stored
End Using
Catch ex As System.Exception
ed.WriteMessage(ex.Message & vbCr & ex.StackTrace)
Finally
ed.WriteMessage(Environment.NewLine + "Current Mleader style after : {0}", getvarObj)
mldstyle.Dispose()
mldict.Dispose()
End Try
End Sub
<CommandMethod("MyMleaderStyle", "MakeEm", CommandFlags.Redraw)>
Public Shared Sub DemoCreate()
Dim doc As Document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument
Dim ed As Editor = doc.Editor
Dim docloc As DocumentLock = doc.LockDocument()
Using (docloc)
Try
AddNewMLeaderStyle()
Catch ex As System.Exception
ed.WriteMessage(ex.Message & vbCr & ex.StackTrace)
Finally
''do nothing (optional)
End Try
End Using
End Sub
Public Shared Sub AddNewMLeaderStyle()
Dim doc As Document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument
Dim db As Database = doc.Database
Dim ed As Editor = doc.Editor
Dim mlstylename As String = "MyMleaderStyle" '<-- change mleader style name here
Dim txstylename As String = "Romans" '<-- change textstyle name here
Dim mldict As DBDictionary = Nothing
Dim mldstyle As MLeaderStyle = Nothing
Dim tr As Transaction = doc.TransactionManager.StartTransaction
Try
Using tr
mldict = CType(tr.GetObject(db.MLeaderStyleDictionaryId, OpenMode.ForRead), DBDictionary)
If IsDbDictionaryExists(mldict, mlstylename) Then
ed.WriteMessage("Mleader style ""{0}"" exists", mlstylename)
Return
End If
mldict.UpgradeOpen()
mldstyle = New MLeaderStyle
Dim tt As TextStyleTable = CType(tr.GetObject(db.TextStyleTableId, OpenMode.ForRead), TextStyleTable)
If Not (tt.Has(txstylename)) Then
Autodesk.AutoCAD.ApplicationServices.Application.ShowAlertDialog("Does not exist")
Return
Else
Autodesk.AutoCAD.ApplicationServices.Application.SetSystemVariable("TEXTSTYLE", txstylename)
End If
Dim dbObj As DBObject
dbObj = TryCast(mldstyle, DBObject)
If dbObj Is Nothing Then
ed.WriteMessage("Mleader style ""{0}"" exists", mlstylename)
Return
End If
mldict.SetAt(mlstylename, mldstyle)
Dim txtid As ObjectId = tt(txstylename)
mldstyle.TextStyleId = txtid
mldstyle.ArrowSize = 2.5
mldstyle.ArrowSymbolId = db.Dimldrblk
mldstyle.ContentType = ContentType.MTextContent
mldstyle.DoglegLength = 1.0
mldstyle.DrawLeaderOrderType = DrawLeaderOrderType.DrawLeaderHeadFirst
mldstyle.DrawMLeaderOrderType = DrawMLeaderOrderType.DrawLeaderFirst
mldstyle.EnableLanding = True
mldstyle.FirstSegmentAngleConstraint = AngleConstraint.DegreesAny
mldstyle.MaxLeaderSegmentsPoints = 2
mldstyle.LeaderLineType = LeaderType.StraightLeader
mldstyle.SecondSegmentAngleConstraint = AngleConstraint.DegreesHorz
mldstyle.TextAlignAlwaysLeft = False
mldstyle.TextAngleType = TextAngleType.HorizontalAngle
mldstyle.TextHeight = 2.5
'variations:
'#1
'mldstyle.SetTextAttachmentType(TextAttachmentType.AttachmentMiddle,
'LeaderDirectionType.LeftLeader);
'mldstyle.SetTextAttachmentType(TextAttachmentType.AttachmentMiddle,
'LeaderDirectionType.RightLeader);
'mldstyle.SetTextAttachmentType(TextAttachmentType.AttachmentMiddle,
'LeaderDirectionType.UnknownLeader);
'#2
'mldstyle.SetTextAttachmentType(TextAttachmentType.AttachmentBottomOfTop,
'LeaderDirectionType.LeftLeader);
'mldstyle.SetTextAttachmentType(TextAttachmentType.AttachmentBottomOfTop,
'LeaderDirectionType.RightLeader);
'mldstyle.SetTextAttachmentType(TextAttachmentType.AttachmentBottomOfTop,
'LeaderDirectionType.UnknownLeader);
'#3
mldstyle.SetTextAttachmentType(TextAttachmentType.AttachmentTopOfTop, LeaderDirectionType.LeftLeader)
mldstyle.SetTextAttachmentType(TextAttachmentType.AttachmentTopOfTop, LeaderDirectionType.RightLeader)
mldstyle.SetTextAttachmentType(TextAttachmentType.AttachmentTopOfTop, LeaderDirectionType.UnknownLeader)
tr.Commit()
End Using
Catch ex As Autodesk.AutoCAD.Runtime.Exception
Autodesk.AutoCAD.ApplicationServices.Application.ShowAlertDialog(ex.Message + vbLf + ex.StackTrace)
Finally
mldstyle.Dispose()
mldict.Dispose()
End Try
End Sub
Public Shared Function IsDbDictionaryExists(ByVal parent As DBDictionary, ByVal dictname As String) As Boolean
Dim exists As Boolean = False
For Each edict As DBDictionaryEntry In parent
Try
If edict.Key = dictname Then
exists = True
Exit For
End If
Catch
End Try
Next
Return exists
End Function
#End Region