MLeaders issues in .NET
I'm having some trouble with mleaders I'm creating in Bricscad via C#. I'm using the same code to create Mleaders in AutoCAD (ACAD_APP), and haven't had any issues. The Bricscad leaders (BRX_APP) are coming in with a landing gap, and the ones I create programatically don't work correctly when grip edited in CAD. If I use the MLeader command (BRX_CMD) in Bricscad, they translate to AutoCAD just fine. To me that's indicating there's fundamental property or something I'm not setting that needs to be included for the Bricscad code.
I've also noticed, that if I explode the code generated leader, the polyline for the leader contains 3 points where as the command generated leader only has 2. Since I'm only setting first and last vertex, this indicates another problem.
Any ideas what I need to include (or delete) in my code to get the Bricscad Mleaders working properly?
Here's the code I'm using in the sample drawing:
#if BRX_APP using System.Diagnostics; using Teigha.Runtime; using Teigha.DatabaseServices; using Bricscad.ApplicationServices; using Teigha.Geometry; using Bricscad.EditorInput; using Arx_App = Bricscad.ApplicationServices.Application; #elif ACAD_APP using System.Diagnostics; using Autodesk.AutoCAD.ApplicationServices; using Autodesk.AutoCAD.DatabaseServices; using Autodesk.AutoCAD.EditorInput; using Autodesk.AutoCAD.Geometry; using Autodesk.AutoCAD.Runtime; using Bricscad.PlottingServices; using Arx_App = Autodesk.AutoCAD.ApplicationServices.Application; #endif namespace AID { public class SwampCode { [CommandMethod("TestMleader")] public void MleaderTest() { Document doc = Arx_App.DocumentManager.MdiActiveDocument; Database db = doc.Database; Editor ed = doc.Editor; // set the active style db.MLeaderstyle = GetMLeaderStyleID("Test"); #if BRX_APP // points for the leader Point3d leaderPoint = new Point3d(40, 40, 0); // point for leader end point Point3d textPoint = new Point3d(50, 20, 0); // point for label text string labelString = "BRX_APP"; #elif ACAD_APP Point3d leaderPoint = new Point3d(0, 40, 0); // point for leader end point Point3d textPoint = new Point3d(10, 20, 0); // point for label text string labelString = "ACAD_APP"; #endif // create the MLeader MLeader label = new MLeader(); label.SetDatabaseDefaults(); label.ContentType = ContentType.MTextContent; label.Layer = "mleader"; int ldNum = label.AddLeader(); int idx = label.AddLeaderLine(ldNum); label.AddFirstVertex(idx, leaderPoint); label.AddLastVertex(idx, textPoint); // new Mtext for the text MText mText = new MText(); mText.SetDatabaseDefaults(); mText.Contents = labelString; mText.TextHeight = 2.5; mText.Attachment=AttachmentPoint.MiddleCenter; mText.SetAttachmentMovingLocation(AttachmentPoint.MiddleCenter); label.MText = mText; // add the mleader to currspace using (Transaction tr = (doc.TransactionManager).StartTransaction()) { BlockTableRecord curSpace = (BlockTableRecord)tr.GetObject(Active.Database.CurrentSpaceId, OpenMode.ForWrite); curSpace.AppendEntity(label); tr.AddNewlyCreatedDBObject(label, true); tr.Commit(); } } /// <summary> /// Checks if the MLeader Style definition is in the active drawing. /// </summary> /// <param name="styleName">Name of the MLeader style.</param> /// <returns>ObjectID of MLeader style, ObjectId.Null on error</returns> public static ObjectId GetMLeaderStyleID(string styleName) { ObjectId styleId = ObjectId.Null; ; using (Transaction tr = (Arx_App.DocumentManager.MdiActiveDocument.TransactionManager).StartTransaction()) { DBDictionary mlDictionary = (DBDictionary)tr.GetObject(Active.Database.MLeaderStyleDictionaryId, OpenMode.ForRead); if (mlDictionary.Contains(styleName)) { styleId = mlDictionary.GetAt(styleName); MLeaderStyle style = tr.GetObject(styleId, OpenMode.ForWrite) as MLeaderStyle; if (style != null) { Debug.Print($"style.LandingGap: {style.LandingGap}"); Debug.Print($"style.EnableLanding: {style.EnableLanding}"); } } tr.Commit(); return styleId; } } } }
And this image shows the MLeader not working correctly in AutoCAD.
Comments
-
Turns out the problem was that Bricscad needs to have SetLastVertex() called instead of AddLastVertex(). If AddLastVertex() is called in Bricscad, an additional control point is added to the leader.
AutoCAD, however, requires the .AddLastVertex() and doesn't seem to care about the .SetLastVertex().
My code for setting vertices now looks like this:
int ldNum = label.AddLeader(); int idx = label.AddLeaderLine(ldNum); label.AddFirstVertex(idx, leaderPoint); #if ACAD_APP label.AddLastVertex(idx, textPoint); #endif label.SetLastVertex(idx, textPoint);
Thanks to Bricscad technical support for helping me out.
0