using System; using Autodesk.AutoCAD.ApplicationServices; using Autodesk.AutoCAD.DatabaseServices; using Autodesk.AutoCAD.EditorInput; using Autodesk.AutoCAD.Geometry; using Autodesk.AutoCAD.Runtime; namespace Plumbing_Pipes { public class MyMain { private static void createAbbriviation(Point3d pointA, Point3d pointB, double dist, String style, String layer, String contents, int color) { Document doc = Application.DocumentManager.MdiActiveDocument; Database db = doc.Database; using (Transaction trans = db.TransactionManager.StartTransaction()) { BlockTable blockTbl = trans.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable; BlockTableRecord modelSpace = trans.GetObject(blockTbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord; // Add abbriviation. if (pointA.DistanceTo(pointB) >= dist) { // SAVE the original text style, set the user defined text style and restore original text style at the end TextStyleTable textstyleTbl = trans.GetObject(db.TextStyleTableId, OpenMode.ForRead) as TextStyleTable; ObjectId originalTextStyleRec = db.Textstyle; db.Textstyle = textstyleTbl[style]; MText abbr = new MText(); abbr.SetDatabaseDefaults(); abbr.Layer = layer; abbr.Contents = contents; Point3d middleOfPipe = new Point3d((pointA[0] + pointB[0]) / 2, (pointA[1] + pointB[1]) / 2, (pointA[2] + pointB[2]) / 2); abbr.Location = middleOfPipe; // locate in the middle of pipe //TextStyleTableRecord textstyleTblRec = trans.GetObject(textstyleTbl[pt.TagStyle], OpenMode.ForRead) as TextStyleTableRecord; //if (textstyleTblRec.Annotative == AnnotativeStates.True) //{ ObjectContextManager ocm = db.ObjectContextManager; ObjectContextCollection occ = ocm.GetContextCollection("ACDB_ANNOTATIONSCALES"); //Autodesk.AutoCAD.Internal.ObjectContexts.AddContext(abbr, occ.CurrentContext); abbr.AddContext(occ.CurrentContext); //abbr.Annotative = AnnotativeStates.True; //} abbr.ColorIndex = color; abbr.Attachment = AttachmentPoint.MiddleCenter; // Justification abbr.BackgroundFill = true; // Add Mask abbr.BackgroundScaleFactor = 1.2; // Normalize the text to be readable on sheet Point2d pointA_2d = new Point2d(pointA[0], pointA[1]); Point2d pointB_2d = new Point2d(pointB[0], pointB[1]); double rotationAngle = pointA_2d.GetVectorTo(pointB_2d).Angle; rotationAngle = (rotationAngle > Math.PI / 2 && rotationAngle <= 3 * Math.PI / 2) ? rotationAngle + Math.PI : rotationAngle; Matrix3d curUCSMatrix = doc.Editor.CurrentUserCoordinateSystem; CoordinateSystem3d curUCS = curUCSMatrix.CoordinateSystem3d; abbr.TransformBy(Matrix3d.Rotation(rotationAngle, curUCS.Zaxis, middleOfPipe)); // rotate db.Textstyle = originalTextStyleRec; modelSpace.AppendEntity(abbr); trans.AddNewlyCreatedDBObject(abbr, true); trans.Commit(); } } } [CommandMethod("test")] public static void test() { Point3d point1 = new Point3d(0, 0, 0); Point3d point2 = new Point3d(1000, 1000, 0); createAbbriviation(point1, point2, 200, "Standard", "0", "AAA", 256); } } }