一、画图类
public static class DrawTools
{
public static ObjectId AddToModelSpace(this Database db, Entity ent, string layer)
{
ObjectId entId;
using (Transaction trans = db.TransactionManager.StartTransaction())
{
BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);
BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
ent.Layer = layer;
entId = btr.AppendEntity(ent);
trans.AddNewlyCreatedDBObject(ent, true);
trans.Commit();
}
return entId;
}
public static void DrawLine(Point3d startPoint, Point3d endPoint, string layer, LineWeight lineWeight)
{
Database db = HostApplicationServices.WorkingDatabase;
Line line = new Line(startPoint, endPoint);
using (Transaction trans = db.TransactionManager.StartTransaction())
{
BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);
BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
line.Layer = layer;
line.LineWeight = lineWeight;
btr.AppendEntity(line);
trans.AddNewlyCreatedDBObject(line, true);
trans.Commit();
}
}
public static void CreateCircle(Point3d pt1, Point3d pt2, Point3d pt3, string layer)
{
Database db = HostApplicationServices.WorkingDatabase;
Circle circle = new Circle();
Vector3d va = pt1.GetVectorTo(pt2);
Vector3d vb = pt1.GetVectorTo(pt3);
if (va.GetAngleTo(vb) == 0 | va.GetAngleTo(vb) == Math.PI)
{
}
else
{
CircularArc3d geArc = new CircularArc3d(pt1, pt2, pt3);
circle.Center = geArc.Center;
circle.Radius = geArc.Radius;
circle.Layer = layer;
}
using (Transaction trans = db.TransactionManager.StartTransaction())
{
BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);
BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
btr.AppendEntity(circle);
trans.AddNewlyCreatedDBObject(circle, true);
trans.Commit();
}
}
public static void CreateCircle(Point3d center, double radius, string layer)
{
Database db = HostApplicationServices.WorkingDatabase;
Circle circle = new Circle();
circle.Center = center;
circle.Radius = radius;
circle.Layer = layer;
using (Transaction trans = db.TransactionManager.StartTransaction())
{
BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);
BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
btr.AppendEntity(circle);
trans.AddNewlyCreatedDBObject(circle, true);
trans.Commit();
}
}
}
二、几何计算类
public static class GeTools
{
public static Point3d MidPoint(Point3d pt1, Point3d pt2)
{
return new Point3d((pt1.X + pt2.X) / 2, (pt1.Y + pt2.Y) / 2, (pt1.Z + pt2.Z) / 2);
}
public static Point3d PolarPoint(this Point3d point, double angle, double dist)
{
return new Point3d(point.X + dist * Math.Cos(angle), point.Y + dist * Math.Sin(angle), point.Z);
}
public static double DegreeToRadian(double degree)
{
return degree * Math.PI / 180;
}
}
三、标注类
public static class DimensionTools
{
private static Database db = HostApplicationServices.WorkingDatabase;
public static void addHorizonRotatedDimension(Point3d pt1, Point3d pt2, string text, double distance, double textHeight)
{
using (Transaction trans = db.TransactionManager.StartOpenCloseTransaction())
{
if (text != "")
{
RotatedDimension dimRotated = new RotatedDimension();
dimRotated.XLine1Point = pt1;
dimRotated.XLine2Point = pt2;
dimRotated.DimLinePoint = GeTools.MidPoint(pt1, pt2).PolarPoint(-Math.PI / 2, distance);
dimRotated.DimensionText = text;
dimRotated.Dimtxt = textHeight;
dimRotated.DimensionStyle = db.Dimstyle;
db.AddToModelSpace(dimRotated, "Dim");
}
}
}
public static void addVerticalRotatedDimension(Point3d pt1, Point3d pt2, string text, double distance)
{
using (Transaction trans = db.TransactionManager.StartOpenCloseTransaction())
{
if (text != "")
{
RotatedDimension dimRotated = new RotatedDimension();
dimRotated.Rotation = Math.PI / 2;
dimRotated.XLine1Point = pt1;
dimRotated.XLine2Point = pt2;
dimRotated.DimLinePoint = GeTools.MidPoint(pt1, pt2).PolarPoint(0, distance);
dimRotated.DimensionText = text;
dimRotated.DimensionStyle = db.Dimstyle;
db.AddToModelSpace(dimRotated, "Dim");
}
}
}
public static void addAlignedDimension(Point3d pt1, Point3d pt2, string text, string layer)
{
using (Transaction trans = db.TransactionManager.StartOpenCloseTransaction())
{
if (text != "")
{
AlignedDimension dimAligned = new AlignedDimension();
dimAligned.XLine1Point = pt1;
dimAligned.XLine2Point = pt2;
dimAligned.DimLinePoint = GeTools.MidPoint(pt1, pt2).PolarPoint(Math.PI / 2, 10);
dimAligned.DimensionText = text;
dimAligned.DimensionStyle = db.Dimstyle;
db.AddToModelSpace(dimAligned, layer);
}
}
}
public static void addRadialDimension(Point3d center, string text, double R, Int32 len, double textHeight)
{
using (Transaction trans = db.TransactionManager.StartOpenCloseTransaction())
{
if (text != "")
{
RadialDimension dimRadial = new RadialDimension();
dimRadial.Center = center;
dimRadial.ChordPoint = center.PolarPoint(GeTools.DegreeToRadian(len), R);
dimRadial.DimensionText = text;
dimRadial.LeaderLength = 5;
dimRadial.DimensionStyle = db.Dimstyle;
dimRadial.Dimtxt = textHeight;
db.AddToModelSpace(dimRadial, "dim");
}
}
}
public static void addDiametricDimension(Point3d center, string text, string layer)
{
using (Transaction trans = db.TransactionManager.StartOpenCloseTransaction())
{
if (text != "")
{
DiametricDimension dimDiametric = new DiametricDimension();
dimDiametric.ChordPoint = center.PolarPoint(GeTools.DegreeToRadian(45), 10);
dimDiametric.FarChordPoint = center.PolarPoint(GeTools.DegreeToRadian(-135), 10);
dimDiametric.LeaderLength = 0;
dimDiametric.DimensionText = text;
dimDiametric.DimensionStyle = db.Dimstyle;
db.AddToModelSpace(dimDiametric, layer);
}
}
}
public static void addDiametricDimension(Point3d start, Point3d center, Point3d end, string text, double distance)
{
using (Transaction trans = db.TransactionManager.StartOpenCloseTransaction())
{
if (text != "")
{
Point3AngularDimension dimLineAngular = new Point3AngularDimension();
dimLineAngular.CenterPoint = center;
dimLineAngular.XLine1Point = start;
dimLineAngular.XLine2Point = end;
dimLineAngular.ArcPoint = center.PolarPoint(GeTools.DegreeToRadian(135), distance);
dimLineAngular.DimensionText = text;
if (text.Length > 8)
{
dimLineAngular.Dimtxt = 2;
}
dimLineAngular.DimensionStyle = db.Dimstyle;
db.AddToModelSpace(dimLineAngular, "Dim");
}
}
}
public static void addDiametricDimension2(Point3d start1, Point3d start2, Point3d end1, Point3d end2, string text, double distance, Point3d TextPosition)
{
using (Transaction trans = db.TransactionManager.StartOpenCloseTransaction())
{
if (text != "")
{
LineAngularDimension2 dimLineAngular = new LineAngularDimension2();
dimLineAngular.XLine1Start = start1;
dimLineAngular.XLine2Start = start2;
dimLineAngular.XLine1End = end1;
dimLineAngular.XLine2End = end2;
dimLineAngular.ArcPoint = GeTools.MidPoint(start1, start2).PolarPoint(GeTools.DegreeToRadian(90), distance);
dimLineAngular.Dimtmove = 1;
dimLineAngular.Dimasz = 2;
dimLineAngular.TextPosition = TextPosition;
dimLineAngular.DimensionText = text;
if (text.Length > 8)
{
dimLineAngular.Dimtxt = 2;
}
dimLineAngular.DimensionStyle = db.Dimstyle;
db.AddToModelSpace(dimLineAngular, "Dim");
}
}
}
public static void addDiametricDimension3(Point3d start1, Point3d start2, Point3d end1, Point3d end2, string text, double distance)
{
using (Transaction trans = db.TransactionManager.StartOpenCloseTransaction())
{
if (text != "")
{
LineAngularDimension2 dimLineAngular = new LineAngularDimension2();
dimLineAngular.XLine1Start = start1;
dimLineAngular.XLine2Start = start2;
dimLineAngular.XLine1End = end1;
dimLineAngular.XLine2End = end2;
dimLineAngular.ArcPoint = GeTools.MidPoint(start1, start2).PolarPoint(GeTools.DegreeToRadian(0), distance);
dimLineAngular.DimensionText = text;
if (text.Length > 8)
{
dimLineAngular.Dimtxt = 2;
}
dimLineAngular.DimensionStyle = db.Dimstyle;
db.AddToModelSpace(dimLineAngular, "Dim");
}
}
}
public static void addDiametricDimension3(Point3d start1, Point3d start2, Point3d end1, Point3d end2, string text, double distance, double textHeight)
{
using (Transaction trans = db.TransactionManager.StartOpenCloseTransaction())
{
if (text != "")
{
LineAngularDimension2 dimLineAngular = new LineAngularDimension2();
dimLineAngular.XLine1Start = start1;
dimLineAngular.XLine2Start = start2;
dimLineAngular.XLine1End = end1;
dimLineAngular.XLine2End = end2;
dimLineAngular.ArcPoint = GeTools.MidPoint(start1, start2).PolarPoint(GeTools.DegreeToRadian(0), distance);
dimLineAngular.DimensionText = text;
dimLineAngular.Dimtxt = textHeight;
dimLineAngular.DimensionStyle = db.Dimstyle;
db.AddToModelSpace(dimLineAngular, "Dim");
}
trans.Commit();
}
}
public static void addCoordinateDimension(bool isView, Point3d pt1, Point3d pt2, string text, double length, double textHeight)
{
using (Transaction trans = db.TransactionManager.StartOpenCloseTransaction())
{
if (text != "")
{
OrdinateDimension dimY = new OrdinateDimension();
dimY.UsingXAxis = isView;
dimY.DefiningPoint = pt1;
dimY.LeaderEndPoint = pt2.PolarPoint(0, length);
dimY.DimensionText = text;
dimY.DimensionStyle = db.Dimstyle;
dimY.Dimtxt = textHeight;
db.AddToModelSpace(dimY, "Dim");
}
}
}
public static void AddMLeader(Point3d pt1, Point3d pt2, Point3d pt3, string text1, string layer)
{
Database db = HostApplicationServices.WorkingDatabase;
using (Transaction trans = db.TransactionManager.StartTransaction())
{
Point3d ptStart1 = pt1;
Point3d ptEnd = pt2;
Point3d ptStart2 = pt3;
MText mtext = new MText();
mtext.Contents = text1;
mtext.TextHeight = 2.2;
MLeader mleader = new MLeader();
int leaderIndex = mleader.AddLeader();
int lineIndex = mleader.AddLeaderLine(leaderIndex);
mleader.AddFirstVertex(lineIndex, pt1);
mleader.AddLastVertex(lineIndex, pt2);
lineIndex = mleader.AddLeaderLine(leaderIndex);
mleader.AddFirstVertex(lineIndex, pt3);
mleader.ContentType = ContentType.MTextContent;
mleader.MText = mtext;
mleader.ArrowSize = 2.5;
mleader.DoglegLength = 0.2;
BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);
BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
mleader.Layer = layer;
btr.AppendEntity(mleader);
trans.AddNewlyCreatedDBObject(mleader, true);
trans.Commit();
}
}
public static void AddLeader(Point3d location, Point3d TextLocation, string text, string layer, double textHeight)
{
Database db = HostApplicationServices.WorkingDatabase;
using (Transaction trans = db.TransactionManager.StartTransaction())
{
MText mtext = new MText();
mtext.Contents = text;
mtext.Location = TextLocation;
mtext.TextHeight = textHeight;
db.AddToModelSpace(mtext, layer);
Leader leader = new Leader();
leader.AppendVertex(location);
leader.AppendVertex(TextLocation);
db.AddToModelSpace(leader, layer);
leader.Dimgap = 0.3;
leader.Dimasz = 2.5;
leader.Annotation = mtext.ObjectId;
leader.EvaluateLeader();
trans.Commit();
}
}
public static void CreateTolerance(this FeatureControlFrame frame, string geometricSym, string torlerance, string firstDatum)
{
if (frame == null) return;
frame.Text = geometricSym + "%%v" + torlerance + "%%v" + firstDatum;
}
public struct DimFormatCode
{
public static readonly string Bounce = @"{\Fgdt;" + "h}";
public static readonly string Lkd = @"{\Fgdt;" + "k}";
public static readonly string Tzd = @"{\Fgdt;" + "r}";
}
public static void AddTolerance(Point3d pt1, string gc, double size, string layer)
{
Database db = HostApplicationServices.WorkingDatabase;
using (Transaction trans = db.TransactionManager.StartTransaction())
{
FeatureControlFrame frame = new FeatureControlFrame();
string geometricSym = DimFormatCode.Bounce;
string torlerance = gc;
string firstDatum = "A";
frame.CreateTolerance(geometricSym, torlerance, firstDatum);
frame.Location = pt1;
frame.Dimscale = size;
frame.Layer = layer;
BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);
BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
btr.AppendEntity(frame);
trans.AddNewlyCreatedDBObject(frame, true);
trans.Commit();
}
}
public static void AddTolerance1(Point3d pt1, string torlerance, string torlerance1, double size, string layer)
{
Database db = HostApplicationServices.WorkingDatabase;
using (Transaction trans = db.TransactionManager.StartTransaction())
{
FeatureControlFrame frame = new FeatureControlFrame();
string geometricSym = DimFormatCode.Lkd;
frame.CreateTolerance(geometricSym, torlerance, torlerance1);
frame.Location = pt1;
frame.Dimscale = size;
frame.Layer = layer;
BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);
BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
btr.AppendEntity(frame);
trans.AddNewlyCreatedDBObject(frame, true);
trans.Commit();
}
}
public static void AddTolerance2(Point3d pt1, string torlerance, string torlerance1, double size, string layer)
{
Database db = HostApplicationServices.WorkingDatabase;
using (Transaction trans = db.TransactionManager.StartTransaction())
{
FeatureControlFrame frame = new FeatureControlFrame();
string geometricSym = DimFormatCode.Tzd;
frame.CreateTolerance(geometricSym, torlerance, torlerance1);
frame.Location = pt1;
frame.Dimscale = size;
frame.Layer = layer;
BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);
BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
btr.AppendEntity(frame);
trans.AddNewlyCreatedDBObject(frame, true);
trans.Commit();
}
}
}
四、图块类
public static class BlockTools
{
public static ObjectId InsertBlockReference(this ObjectId spaceId, string layer, string blockName, Point3d position, Scale3d scale, double rotateAngle)
{
ObjectId blockRefId;
Database db = spaceId.Database;
BlockTable bt = (BlockTable)db.BlockTableId.GetObject(OpenMode.ForRead);
if (!bt.Has(blockName)) return ObjectId.Null;
BlockTableRecord space = (BlockTableRecord)spaceId.GetObject(OpenMode.ForWrite);
BlockReference br = new BlockReference(position, bt[blockName]);
br.ScaleFactors = scale;
br.Layer = layer;
br.Rotation = rotateAngle;
blockRefId = space.AppendEntity(br);
db.TransactionManager.AddNewlyCreatedDBObject(br, true);
space.DowngradeOpen();
return blockRefId;
}
public static ObjectId CreateStyle(string name, string smallfont, string bigfont, double height, double xscale)
{
Database dbH = HostApplicationServices.WorkingDatabase;
using (Transaction trans = dbH.TransactionManager.StartTransaction())
{
TextStyleTable TST = trans.GetObject(dbH.TextStyleTableId, OpenMode.ForWrite) as TextStyleTable;
ObjectId id = GetIdFromSymbolTable(TST, name);
if (id == ObjectId.Null)
{
TextStyleTableRecord TSTR = new TextStyleTableRecord();
TSTR.Name = name;
TSTR.FileName = smallfont;
TSTR.BigFontFileName = bigfont;
TSTR.TextSize = height;
TSTR.XScale = xscale;
TST.UpgradeOpen();
id = TST.Add(TSTR);
trans.AddNewlyCreatedDBObject(TSTR, true);
}
return id;
}
}
public static ObjectId GetIdFromSymbolTable(SymbolTable st, string key)
{
Database dbH = HostApplicationServices.WorkingDatabase;
using (Transaction trans = dbH.TransactionManager.StartTransaction())
{
if (st.Has(key))
{
ObjectId idres = st[key];
if (!idres.IsErased)
return idres;
foreach (ObjectId id in st)
{
if (!id.IsErased)
{
SymbolTableRecord str = (SymbolTableRecord)trans.GetObject(id, OpenMode.ForRead);
if (str.Name == key)
return id;
}
}
}
}
return ObjectId.Null;
}
public static void InsertBlockFromDwg(string blockName, string fileName, Point3d position, Scale3d scale)
{
Document doc = Application.DocumentManager.MdiActiveDocument;
using (Database db = new Database(false, true))
{
using (Transaction trans = doc.TransactionManager.StartTransaction())
{
if (File.Exists(fileName))
{
db.ReadDwgFile(fileName, FileShare.Read, true, null);
db.CloseInput(true);
string name = blockName;
ObjectId btrId = doc.Database.Insert(name, db, false);
BlockTable bt = (BlockTable)trans.GetObject(doc.Database.BlockTableId, OpenMode.ForRead);
BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
using (BlockReference br = new BlockReference(position, btrId))
{
br.ScaleFactors = scale;
btr.AppendEntity(br);
trans.AddNewlyCreatedDBObject(br, true);
}
}
trans.Commit();
}
}
}
}