代码虽说长了点,但是可以直接使用,或者可以线看看前面得文章
自定义基础类:
public class MyBlockClass:MyBasePoint{public string BlockName { get; set; }/// <summary>/// 图层名称/// </summary>public string LayerName { get; set; }/// <summary>/// 旋转/// </summary>public double Rotation { get; set; }}public class MyTextClass:MyBasePoint{/// <summary>/// 图层名称/// </summary>public string LayerName { get; set; }/// <summary>/// 旋转/// </summary>public double Rotation { get; set; }/// <summary>/// 字体样式/// </summary>public MyTextStyle TextStyle = new MyTextStyle();/// <summary>/// 显示的值/// </summary>public string Value { get; set; }}public class MyLineClass:MyBaseline{/// <summary>/// 图层名称/// </summary>public string LayerName { get; set; }}public class MyBaseline{/// <summary>/// 起点点号/// </summary>public MyBasePoint StartPoint { get; set; }/// <summary>/// 终点点号/// </summary>public MyBasePoint EndPoint { get; set; }/// <summary>/// 返回线的角度/// </summary>/// <returns></returns>public double GetRotation(){double rotation = 0;if (StartPoint != null && EndPoint != null){if (EndPoint.X == StartPoint.X){rotation = Math.PI / 2;}else{rotation = Math.Atan((EndPoint.Y - StartPoint.Y) / (EndPoint.X - StartPoint.X));}}return rotation;}}public class MyBasePoint{public double X { get; set; }public double Y { get; set; }public double Z { get; set; }}
插入实现方法:
/// <summary>/// 创建块/// </summary>/// <param name="myBlocks"></param>/// <param name="layerName"></param>/// <param name="color"></param>public static void CreatePoint(List<MyBlockClass> myBlocks, System.Drawing.Color color){try{Document doc = Application.DocumentManager.CurrentDocument;//获取当前的文档Database db = HostApplicationServices.WorkingDatabase;//获取当前的操作数据库using (DocumentLock docLock = doc.LockDocument())//文档锁住using (Transaction trans = db.TransactionManager.StartTransaction())//开始事务操作{LayerTable lt = (LayerTable)trans.GetObject(doc.Database.LayerTableId, OpenMode.ForRead);//回去当前的图层表BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);//获取当前的块表//获取块表记录-模型空间,三维坐标等BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);//foreach (MyBlockClass myBlock in myBlocks){//判断是否已经包含该图层if (!lt.Has(myBlock.LayerName)){var PnoColor = Color.FromColor(color);lt.AddLayer(myBlock.LayerName, PnoColor, trans);CadProjectInfo.LayersName.Add(myBlock.LayerName);}Point3d position = new Point3d(myBlock.X, myBlock.Y, myBlock.Z);ObjectId objCode;if (bt.Has(myBlock.BlockName)){objCode = bt[myBlock.BlockName];//设置显示块}else{objCode = bt["一般管线点"];//设置显示块}BlockReference pointBlock = new BlockReference(position, objCode);pointBlock.Rotation = myBlock.Rotation;//设置旋转角//创建块应用作为实体//PipePoint pipePoint = new PipePoint(position, objCode);//pt是一个Point3D坐标,这里是插入进当前dwg文件中pointBlock.Layer = myBlock.LayerName;//ObjectId id = btr.AppendEntity(pointBlock);trans.AddNewlyCreatedDBObject(pointBlock, true);}trans.Commit();//提交}}catch (Exception ex){throw;}}/// <summary>/// 创建点/// </summary>/// <param name="myTexts"></param>/// <param name="layerName"></param>/// <param name="color"></param>public static void CreatePoint(List<MyTextClass> myTexts,System.Drawing.Color color){try{Document doc = Application.DocumentManager.CurrentDocument;//获取当前的文档Database db = HostApplicationServices.WorkingDatabase;//获取当前的操作数据库using (DocumentLock docLock = doc.LockDocument())//文档锁住using (Transaction trans = db.TransactionManager.StartTransaction())//开始事务操作{LayerTable lt = (LayerTable)trans.GetObject(doc.Database.LayerTableId, OpenMode.ForRead);//回去当前的图层表BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);//获取当前的块表TextStyleTable tst = (TextStyleTable)trans.GetObject(db.TextStyleTableId, OpenMode.ForRead);//获取当前字体表//获取块表记录-模型空间,三维坐标等BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);//foreach (MyTextClass myText in myTexts){//判断是否已经包含该图层if (!lt.Has(myText.LayerName)){var PnoColor = Color.FromColor(color);lt.AddLayer(myText.LayerName, PnoColor, trans);CadProjectInfo.LayersName.Add(myText.LayerName);}Point3d position = new Point3d(myText.X, myText.Y, myText.Z);DBText pipeText = new DBText();pipeText.Layer = myText.LayerName;//图层名称pipeText.Position = position;//标签位置pipeText.TextString = myText.Value;//设置标注的字段pipeText.Height = myText.TextStyle.Height;//设置文本高度pipeText.Rotation = myText.Rotation;//设置旋转角度//判断字体样式是否存在if (tst.Has(myText.TextStyle.FontName)){pipeText.TextStyleId = tst[myText.TextStyle.FontName];}pipeText.WidthFactor =myText.TextStyle.FactorWidth;//宽度因子ObjectId id = btr.AppendEntity(pipeText);//添加标注trans.AddNewlyCreatedDBObject(pipeText, true);}trans.Commit();//提交}}catch (Exception ex){throw;}}/// <summary>/// 创建线/// </summary>/// <param name="myLines"></param>/// <param name="layerName"></param>/// <param name="color"></param>public static void CreateLine(List<MyLineClass> myLines, System.Drawing.Color color){try{Document doc = Application.DocumentManager.CurrentDocument;//获取当前的文档Database db = HostApplicationServices.WorkingDatabase;//获取当前的操作数据库using (DocumentLock docLock = doc.LockDocument())//文档锁住using (Transaction trans = db.TransactionManager.StartTransaction())//开始事务操作{LayerTable lt = (LayerTable)trans.GetObject(doc.Database.LayerTableId, OpenMode.ForRead);//回去当前的图层表BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);//获取当前的块表//获取块表记录-模型空间,三维坐标等BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);//遍历所有的线进行添加foreach (MyLineClass myLine in myLines){//判断是否已经包含该图层if (!lt.Has(myLine.LayerName)){var PnoColor = Color.FromColor(color);lt.AddLayer(myLine.LayerName, PnoColor, trans);//添加至图层集合CadProjectInfo.LayersName.Add(myLine.LayerName);}Point3d sp = new Point3d(myLine.StartPoint.X, myLine.StartPoint.Y, myLine.StartPoint.Z);//起点Point3d ep = new Point3d(myLine.EndPoint.X, myLine.EndPoint.Y, myLine.EndPoint.Z);//终点Line line = new Line();line.Layer = myLine.LayerName;//图层名称line.StartPoint = sp;//line.EndPoint = ep;//ObjectId id = btr.AppendEntity(line);//添加标注trans.AddNewlyCreatedDBObject(line, true);}trans.Commit();//提交}}catch (Exception ex){throw;}}
创建对应得点线块,static方法,直接可以粘贴使用。