using System; using System.Diagnostics; // AutoCAD references #if ARX_APP using AcAp = Autodesk.AutoCAD.ApplicationServices; using AcCo = Autodesk.AutoCAD.Colors; using AcDb = Autodesk.AutoCAD.DatabaseServices; using AcEd = Autodesk.AutoCAD.EditorInput; using AcGe = Autodesk.AutoCAD.Geometry; using AcRt = Autodesk.AutoCAD.Runtime; using AcRt2 = Autodesk.AutoCAD.Runtime; using MRAppsA.Classes; #elif BRX_APP using AcAp = Bricscad.ApplicationServices; using AcDb = Teigha.DatabaseServices; using AcEd = Bricscad.EditorInput; using AcGe = Teigha.Geometry; using AcRt2 = Teigha.Runtime; #endif #if ARX_APP namespace MRAppsA #elif BRX_APP namespace MRAppsB #endif { public class UtilitiesPart { [AcRt2.CommandMethod("MLwTEXT")] public static void DrawMultileaderwithCoordsFromPoint() { #if DEBUG if (!Debugger.IsAttached) { Debugger.Launch(); } #endif string textToPresent = string.Empty; AcAp.Document CurrDoc = AcAp.Application.DocumentManager.MdiActiveDocument; AcDb.Database CurrDb = CurrDoc.Database; AcEd.Editor CurrED = CurrDoc.Editor; AcAp.DocumentLock docLock = CurrDoc.LockDocument(); double cAS = CurrDb.Cannoscale.Scale, tH = 3.5 / cAS; bool pointSelectCancelled; AcGe.Point3d selRP = Get3dPt("Select a location to report.", out pointSelectCancelled), selRPinWUCS = UtilitiesPart.TranslateCoordinates(CurrDoc, selRP); if (pointSelectCancelled) return; AcGe.Point3d selOP = Get3dPtRB(selRP, "Select a location for the label", out pointSelectCancelled), selOPinWUCS = TranslateCoordinates(CurrDoc, selOP); if (pointSelectCancelled) return; CreateTextMultileader( selRP, selOP, GetFormattedString( selRPinWUCS, GetValues()[1], 3), tH, true); } public static void CreateTextMultileader(AcGe.Point3d StartPoint, AcGe.Point3d EndPoint, string Text, double TextHeight, bool OrientToUCS = true) { AcAp.Document acDoc = AcAp.Application.DocumentManager.MdiActiveDocument; AcDb.Database acDB = acDoc.Database; AcEd.Editor acEd = acDoc.Editor; using (AcDb.Transaction t = acDB.TransactionManager.StartTransaction()) { AcDb.BlockTable bT = t.GetObject(acDB.BlockTableId, AcDb.OpenMode.ForRead) as AcDb.BlockTable; AcDb.BlockTableRecord bTR = t.GetObject(bT[AcDb.BlockTableRecord.ModelSpace], AcDb.OpenMode.ForWrite) as AcDb.BlockTableRecord; AcDb.MLeader leader = new AcDb.MLeader(); leader.SetDatabaseDefaults(); leader.ContentType = AcDb.ContentType.MTextContent; AcDb.MText mT = new AcDb.MText(); mT.SetDatabaseDefaults(); mT.TextHeight = TextHeight; mT.Contents = Text; mT.Location = new AcGe.Point3d(EndPoint.X, EndPoint.Y, 0); leader.MText = mT; int idx = leader.AddLeaderLine(StartPoint); if (OrientToUCS) leader.TransformBy(acEd.CurrentUserCoordinateSystem); //leader.AddFirstVertex(idx, StartPoint); bTR.AppendEntity(leader); t.AddNewlyCreatedDBObject(leader, true); t.Commit(); } } public static AcGe.Point3d Get3dPt(string PromptText, out bool Cancelled) { AcAp.Document acDoc = AcAp.Application.DocumentManager.MdiActiveDocument; AcDb.Database acDb = acDoc.Database; AcEd.PromptPointResult pPtRes; AcEd.PromptPointOptions pPtOpts = new AcEd.PromptPointOptions(""); pPtOpts.AllowNone = true; pPtOpts.Message = PromptText; pPtRes = acDoc.Editor.GetPoint(pPtOpts); if (pPtRes.Status == AcEd.PromptStatus.None || pPtRes.Status == AcEd.PromptStatus.Cancel) { Cancelled = true; return new AcGe.Point3d(); } else { Cancelled = false; return pPtRes.Value; } } public static AcGe.Point3d Get3dPtRB(AcGe.Point3d Pt1, string PromptText, out bool Cancelled) { AcAp.Document acDoc = AcAp.Application.DocumentManager.MdiActiveDocument; AcDb.Database acDb = acDoc.Database; AcEd.PromptPointResult pPtRes; AcEd.PromptPointOptions pPtOpts = new AcEd.PromptPointOptions(""); // Prompt for the start point pPtOpts.AllowNone = true; pPtOpts.UseDashedLine = true; pPtOpts.Message = PromptText; pPtOpts.BasePoint = Pt1; pPtOpts.UseBasePoint = true; pPtRes = acDoc.Editor.GetPoint(pPtOpts); if (pPtRes.Status == AcEd.PromptStatus.None || pPtRes.Status == AcEd.PromptStatus.Cancel) { Cancelled = true; return new AcGe.Point3d(); } else { Cancelled = false; return pPtRes.Value; } } public static string GetFormattedString(AcGe.Point3d SourcePoint, string FormatString, int Decimals) { if (FormatString == GetValues()[0]) { string pattern = "{0:F" + Decimals + "},{1:F" + Decimals + "}"; return string.Format(pattern, SourcePoint.X, SourcePoint.Y); } else if (FormatString == GetValues()[1]) { string pattern = "X={0:F" + Decimals + "}{2}Y={1:F" + Decimals + "}"; return string.Format(pattern, SourcePoint.X, SourcePoint.Y, Environment.NewLine); } else if (FormatString == GetValues()[3]) { string pattern = "{0:F" + Decimals + "},{1:F" + Decimals + "},{2:F" + Decimals + "}"; return string.Format(pattern, SourcePoint.X, SourcePoint.Y, SourcePoint.Z); } else if (FormatString == GetValues()[4]) { string pattern = "X={0:F" + Decimals + "}{3}Y={1:F" + Decimals + "}{3}Z={2:F" + Decimals + "}"; return string.Format(pattern, SourcePoint.X, SourcePoint.Y, SourcePoint.Z, Environment.NewLine); } return string.Empty; } public static string[] GetValues() { return new string[] { "2D Comma Separated", "2D Separate Lines", "2D Aligned To Grid", "3D Comma Separated", "3D Separate Lines" }; } public static AcGe.Point3d TranslateCoordinates(AcAp.Document acDoc, AcGe.Point3d PointToTranslate) { AcGe.Point3d result; AcDb.Database acCurDb = acDoc.Database; AcEd.Editor ed = acDoc.Editor; AcGe.Matrix3d currUCS = ed.CurrentUserCoordinateSystem; result = PointToTranslate.TransformBy(currUCS); return result; } } }