using System; using System.Collections.Generic; using System.Text; using Autodesk.AutoCAD.Geometry; using Autodesk.AutoCAD.DatabaseServices; using Autodesk.AutoCAD.ApplicationServices; using Autodesk.AutoCAD.EditorInput; namespace T_cad { class Frame5000 { private Point3d LB; private Point3d LT; private Point3d RB; private Point3d RT; double dist = 500; //格网间距 string Title = ""; string Left_Number; string Right_Number; public void Produce() { getinfo(); //左编号 Point3d p1 = new Point3d(LB.X - dist * 5 - 100, LB.Y - 100, 0); Point3d p2 = new Point3d(LT.X + 50, LT.Y + 50, 0); Left_Number = GetNumber(p1, p2); //右编号 p1 = new Point3d(RT.X + dist * 5 + 100, RT.Y + 100, 0); p2 = new Point3d(RB.X - 50, RB.Y - 50, 0); Right_Number = GetNumber(p1, p2); ////图幅号 //p1 = new Point3d(LB.X, LB.Y, 0); //p2 = new Point3d(RT.X, RT.Y, 0); //Number = GetNumber(p1, p2); //图名 Title = ""; p1 = new Point3d(LB.X, LB.Y, 0); p2 = new Point3d(RT.X, RT.Y, 0); Title = GetNumber(p1, p2); //新建一个dwg //string templatePath = "acad.dwt"; //DocumentCollection documentcoll = Application.DocumentManager; //Document document = documentcoll.Add(templatePath); //Database database = document.Database; LayerControl layerscontrol = new LayerControl(); if (layerscontrol.haslayername("9800") == false) { colorgb col = new colorgb(255, 0, 0); layerscontrol.creatlayer("9800", col); } createTextStyle(); draw(); zoom(); } private void getinfo() { Document document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument; Database database = HostApplicationServices.WorkingDatabase; Transaction traction = database.TransactionManager.StartTransaction(); try { DocumentLock documentlock = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.LockDocument(); BlockTable blocktable = traction.GetObject(database.BlockTableId, OpenMode.ForRead) as BlockTable; Editor editor = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor; TypedValue[] typedvalue = new TypedValue[1]; typedvalue.SetValue(new TypedValue((int)DxfCode.Start, "3dFace,LWPolyline"), 0); SelectionFilter selectionfilter = new SelectionFilter(typedvalue); PromptSelectionResult psr = editor.GetSelection(selectionfilter); if (psr.Status == PromptStatus.OK) { SelectionSet selectionset = psr.Value; ObjectId[] obj = new ObjectId[selectionset.Count]; obj = selectionset.GetObjectIds(); for (int i = 0; i < obj.Length; i++) { ObjectId objid = obj[i]; Entity entity = (Entity)traction.GetObject(objid, OpenMode.ForRead); Point3dCollection coll = new Point3dCollection(); if (entity is Face) { Face f = entity as Face; f.GetStretchPoints(coll); } else if (entity is Polyline) { Polyline line = entity as Polyline; line.GetStretchPoints(coll); } List x_list = new List(); List y_list = new List(); List flags = new List(); for (int ii = 0; ii < 4; ii++) { x_list.Add(coll[ii].X); y_list.Add(coll[ii].Y); } x_list.Sort(); y_list.Sort(); //右上 for (int ii = 0; ii < 4; ii++) { int x1 = (int)coll[ii].X; int y1 = (int)coll[ii].Y; if (x1 == (int)x_list[3] || x1 == (int)x_list[2]) { if (y1 == (int)y_list[3] || y1 == (int)y_list[2]) { RT = coll[ii]; flags.Add(ii); if (x1 == (int)x_list[3]) { x_list.RemoveAt(3); } else { x_list.RemoveAt(2); } if (y1 == (int)y_list[3]) { y_list.RemoveAt(3); } else { y_list.RemoveAt(2); } break; } } } //右下 for (int ii = 0; ii < 4; ii++) { int x1 = (int)coll[ii].X; // int y1 = (int)coll[ii].Y; // if (x1 == (int)x_list[2]) { if (y1 == (int)y_list[0] || y1 == (int)y_list[1]) { RB = coll[ii]; flags.Add(ii); x_list.RemoveAt(2); break; } } } //左上 for (int ii = 0; ii < 4; ii++) { int y1 = (int)coll[ii].Y; // int x1 = (int)coll[ii].X; // if (y1 == (int)y_list[2]) { if (x1 == (int)x_list[0] || x1 == (int)x_list[1]) { LT = coll[ii]; flags.Add(ii); y_list.RemoveAt(2); break; } } } //左下 for (int ii = 0; ii < 4; ii++) { if (flags.Contains(ii) == false) { LB = coll[ii]; } } } } traction.Commit(); documentlock.Dispose(); } catch (Exception ex) { System.Windows.Forms.MessageBox.Show(ex.ToString()); } finally { traction.Dispose(); } } private void draw() { double D2_5 = 2.5; double D15 = 15; double D20 = 20; double D47_5 = 47.5; //内图廓线和外图廓线距离 double D120 = 120; double D225 = 225; double D245 = 245; //右下角附注 double dist1 = 4; //接图表第一格填充间距 double dist2 = 7.071; //接图表填充间距 ObjectId HZ = gettextstyleID("HZ"); ObjectId st = gettextstyleID("st"); ObjectId ht = gettextstyleID("ht"); ObjectId Standard = gettextstyleID("Standard"); ObjectId ID12 = gettextstyleID("12"); ObjectId ID123 = gettextstyleID("123"); //ObjectId XDXT = gettextstyleID("细等线体"); //ObjectId ZDXT = gettextstyleID("中等线体"); //ObjectId ST = gettextstyleID("宋体"); Document document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument; Database database = HostApplicationServices.WorkingDatabase; Transaction traction = database.TransactionManager.StartTransaction(); DocumentLock documentlock = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.LockDocument(); BlockTable blocktable = traction.GetObject(database.BlockTableId, OpenMode.ForWrite) as BlockTable; BlockTableRecord blocktablerecord = traction.GetObject(blocktable[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord; Editor editor = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor; try { //内图廓 ////左边线 //Point3dCollection In1 = new Point3dCollection(); //In1.Add(new Point3d(LT.X, LT.Y, 0)); //In1.Add(new Point3d(LB.X, LB.Y, 0)); //Polyline2d LeftLine = new Polyline2d(Poly2dType.SimplePoly, In1, 0, true, 0, 0, null); ////LeftLine.Color = Autodesk.AutoCAD.Colors.Color.FromColor(System.Drawing.Color.Red); //LeftLine.Color = Autodesk.AutoCAD.Colors.Color.FromColorIndex(Autodesk.AutoCAD.Colors.ColorMethod.ByColor, 1); //LeftLine.Layer = "9800"; //LeftLine.SetDatabaseDefaults(); //blocktablerecord.AppendEntity(LeftLine); //traction.AddNewlyCreatedDBObject(LeftLine, true); ////下边线 //Point3dCollection In2 = new Point3dCollection(); //In2.Add(new Point3d(LB.X, LB.Y, 0)); //In2.Add(new Point3d(RB.X, RB.Y, 0)); //Polyline2d LowLine = new Polyline2d(Poly2dType.SimplePoly, In2, 0, true, 0, 0, null); //LowLine.Color = Autodesk.AutoCAD.Colors.Color.FromColorIndex(Autodesk.AutoCAD.Colors.ColorMethod.ByColor, 1); //LowLine.SetDatabaseDefaults(); //blocktablerecord.AppendEntity(LowLine); //traction.AddNewlyCreatedDBObject(LowLine, true); ////右边线 //Point3dCollection In3 = new Point3dCollection(); //In3.Add(new Point3d(RT.X, RT.Y, 0)); //In3.Add(new Point3d(RB.X, RB.Y, 0)); //Polyline2d RightLine = new Polyline2d(Poly2dType.SimplePoly, In3, 0, true, 0, 0, null); //RightLine.Color = Autodesk.AutoCAD.Colors.Color.FromColorIndex(Autodesk.AutoCAD.Colors.ColorMethod.ByColor, 1); //RightLine.Layer = "9800"; //RightLine.SetDatabaseDefaults(); //blocktablerecord.AppendEntity(RightLine); //traction.AddNewlyCreatedDBObject(RightLine, true); //上边线 //Point3dCollection In4 = new Point3dCollection(); //In4.Add(new Point3d(LT.X, LT.Y, 0)); //In4.Add(new Point3d(RT.X, RT.Y, 0)); //Polyline2d TOPLine = new Polyline2d(Poly2dType.SimplePoly, In4, 0, true, 0, 0, null); //TOPLine.Color = Autodesk.AutoCAD.Colors.Color.FromColorIndex(Autodesk.AutoCAD.Colors.ColorMethod.ByColor, 1); //TOPLine.Layer = "9800"; //TOPLine.SetDatabaseDefaults(); //blocktablerecord.AppendEntity(TOPLine); //traction.AddNewlyCreatedDBObject(TOPLine, true); //外图廓 Point3dCollection Outcoll = new Point3dCollection(); Outcoll.Add(new Point3d(LT.X - D47_5, LT.Y + D47_5, 0)); Outcoll.Add(new Point3d(LB.X - D47_5, LB.Y - D47_5, 0)); Outcoll.Add(new Point3d(RB.X + D47_5, RB.Y - D47_5, 0)); Outcoll.Add(new Point3d(RT.X + D47_5, RT.Y + D47_5, 0)); Outcoll.Add(new Point3d(LT.X - D47_5, LT.Y + D47_5, 0)); Polyline2d line = new Polyline2d(Poly2dType.SimplePoly, Outcoll, 0, true, D2_5, D2_5, null); line.Color = Autodesk.AutoCAD.Colors.Color.FromColorIndex(Autodesk.AutoCAD.Colors.ColorMethod.ByColor, 1); line.Layer = "9800"; line.SetDatabaseDefaults(); blocktablerecord.AppendEntity(line); traction.AddNewlyCreatedDBObject(line, true); // 下边线 Point3dCollection coll_B = new Point3dCollection(); Line ll_B = new Line(); double x1 = LB.X - 100; double ks = (RB.Y - LB.Y) / (RB.X - LB.X); double y1 = ks * (x1 - LB.X) + LB.Y; ll_B.StartPoint = new Point3d(x1, y1, 0); double x2 = RB.X + 100; double y2 = ks * (x2 - LB.X) + LB.Y; ll_B.EndPoint = new Point3d(x2, y2, 0); ll_B.IntersectWith(line, Intersect.ExtendThis, coll_B, 0, 0); Polyline2d Line_B = new Polyline2d(Poly2dType.SimplePoly, coll_B, 0, false, 0, 0, null); Line_B.Layer = "9800"; Line_B.Color = Autodesk.AutoCAD.Colors.Color.FromColorIndex(Autodesk.AutoCAD.Colors.ColorMethod.ByColor, 1); Line_B.SetDatabaseDefaults(); blocktablerecord.AppendEntity(Line_B); traction.AddNewlyCreatedDBObject(Line_B, true); //上边线 Point3dCollection coll_T = new Point3dCollection(); Line ll_T = new Line(); x1 = LT.X - 100; ks = (RT.Y - LT.Y) / (RT.X - LT.X); y1 = ks * (x1 - LT.X) + LT.Y; ll_T.StartPoint = new Point3d(x1, y1, 0); x2 = LT.X + 100; y2 = ks * (x2 - LT.X) + LT.Y; ll_T.EndPoint = new Point3d(x2, y2, 0); ll_T.IntersectWith(line, Intersect.ExtendThis, coll_T, 0, 0); Polyline2d Line_T = new Polyline2d(Poly2dType.SimplePoly, coll_T, 0, false, 0, 0, null); Line_T.Layer = "9800"; Line_T.Color = Autodesk.AutoCAD.Colors.Color.FromColorIndex(Autodesk.AutoCAD.Colors.ColorMethod.ByColor, 1); Line_T.SetDatabaseDefaults(); blocktablerecord.AppendEntity(Line_T); traction.AddNewlyCreatedDBObject(Line_T, true); //左边线 Point3dCollection coll_L = new Point3dCollection(); Line ll_L = new Line(); y1 = LB.Y - 100; ks = (LT.Y - LB.Y) / (LT.X - LB.X); x1 = LB.X + (y1 - LB.Y) / ks; ll_L.StartPoint = new Point3d(x1, y1, 0); y2 = LT.Y + 100; x2 = LB.X + (y2 - LB.Y) / ks; ll_L.EndPoint = new Point3d(x2, y2, 0); ll_L.IntersectWith(line, Intersect.ExtendThis, coll_L, 0, 0); Polyline2d Line_L = new Polyline2d(Poly2dType.SimplePoly, coll_L, 0, false, 0, 0, null); Line_L.Layer = "9800"; Line_L.Color = Autodesk.AutoCAD.Colors.Color.FromColorIndex(Autodesk.AutoCAD.Colors.ColorMethod.ByColor, 1); Line_L.SetDatabaseDefaults(); blocktablerecord.AppendEntity(Line_L); traction.AddNewlyCreatedDBObject(Line_L, true); //右边线 Point3dCollection coll_R = new Point3dCollection(); Line ll_R = new Line(); y1 = RB.Y - 100; ks = (RT.Y - RB.Y) / (RT.X - RB.X); x1 = RB.X + (y1 - RB.Y) / ks; ll_R.StartPoint = new Point3d(x1, y1, 0); y2 = RT.Y + 100; x2 = RB.X + (y2 - RB.Y) / ks; ll_R.EndPoint = new Point3d(x2, y2, 0); ll_R.IntersectWith(line, Intersect.ExtendThis, coll_R, 0, 0); Polyline2d Line_R = new Polyline2d(Poly2dType.SimplePoly, coll_R, 0, false, 0, 0, null); Line_R.Layer = "9800"; Line_R.Color = Autodesk.AutoCAD.Colors.Color.FromColorIndex(Autodesk.AutoCAD.Colors.ColorMethod.ByColor, 1); Line_R.SetDatabaseDefaults(); blocktablerecord.AppendEntity(Line_R); traction.AddNewlyCreatedDBObject(Line_R, true); // //x坐标 List list_x = new List(); double x0 = 500 - LB.X % 500; x0 = LB.X + x0; list_x.Add(x0); while (x0 + 500 < RB.X) { x0 = x0 + 500; list_x.Add(x0); } //y坐标 List list_y = new List(); double y0 = 500 - LB.Y % 500; y0 = LB.Y + y0; list_y.Add(y0); while (y0 + 500 < LT.Y) { y0 = y0 + 500; list_y.Add(y0); } // //十字框标 for (int i = 0; i < list_x.Count; i++) { for (int j = 0; j < list_y.Count; j++) { double x = list_x[i]; double y = list_y[j]; Point3d p1 = new Point3d(x - 25, y, 0); Point3d p2 = new Point3d(x + 25, y, 0); Point3dCollection kb = new Point3dCollection(); kb.Add(p1); kb.Add(p2); Polyline2d kbline = new Polyline2d(Poly2dType.SimplePoly, kb, 0, false, 0, 0, null); kbline.Color = Autodesk.AutoCAD.Colors.Color.FromColorIndex(Autodesk.AutoCAD.Colors.ColorMethod.ByColor, 1); kbline.Layer = "9800"; kbline.SetDatabaseDefaults(); blocktablerecord.AppendEntity(kbline); traction.AddNewlyCreatedDBObject(kbline, true); p1 = new Point3d(x, y - 25, 0); p2 = new Point3d(x, y + 25, 0); Point3dCollection kb2 = new Point3dCollection(); kb2.Add(p1); kb2.Add(p2); Polyline2d kbline2 = new Polyline2d(Poly2dType.SimplePoly, kb2, 0, false, 0, 0, null); kbline2.Color = Autodesk.AutoCAD.Colors.Color.FromColorIndex(Autodesk.AutoCAD.Colors.ColorMethod.ByColor, 1); kbline2.Layer = "9800"; kbline2.SetDatabaseDefaults(); blocktablerecord.AppendEntity(kbline2); traction.AddNewlyCreatedDBObject(kbline2, true); } } List Below_List = new List(); List Top_List = new List(); List Left_List = new List(); List Right_List = new List(); //上下边线的短线 for (int i = 0; i < list_x.Count; i++) { double x = list_x[i]; double k = (RB.Y - LB.Y) / (RB.X - LB.X); double y = k * (x - LB.X) + LB.Y; Point3d p1 = new Point3d(x, y, 0); Point3d p2 = new Point3d(x, y - 47.5, 0); Point3dCollection pc = new Point3dCollection(); pc.Add(p1); pc.Add(p2); Polyline2d Kline = new Polyline2d(Poly2dType.SimplePoly, pc, 0, false, 0, 0, null); Kline.Color = Autodesk.AutoCAD.Colors.Color.FromColorIndex(Autodesk.AutoCAD.Colors.ColorMethod.ByColor, 1); Kline.Layer = "9800"; Kline.SetDatabaseDefaults(); blocktablerecord.AppendEntity(Kline); traction.AddNewlyCreatedDBObject(Kline, true); Below_List.Add(p1); y = k * (x - LT.X) + LT.Y; p1 = new Point3d(x, y, 0); p2 = new Point3d(x, y + 47.5, 0); pc = new Point3dCollection(); pc.Add(p1); pc.Add(p2); Kline = new Polyline2d(Poly2dType.SimplePoly, pc, 0, false, 0, 0, null); Kline.Color = Autodesk.AutoCAD.Colors.Color.FromColorIndex(Autodesk.AutoCAD.Colors.ColorMethod.ByColor, 1); Kline.Layer = "9800"; Kline.SetDatabaseDefaults(); blocktablerecord.AppendEntity(Kline); traction.AddNewlyCreatedDBObject(Kline, true); Top_List.Add(p1); } //左右边线的短线 for (int i = 0; i < list_y.Count; i++) { double y = list_y[i]; double k = (LT.Y - LB.Y) / (LT.X - LB.X); double x = LB.X + (y - LB.Y) / k; Point3d p1 = new Point3d(x, y, 0); Point3d p2 = new Point3d(x - 47.5, y, 0); Point3dCollection pc = new Point3dCollection(); pc.Add(p1); pc.Add(p2); Polyline2d Kline = new Polyline2d(Poly2dType.SimplePoly, pc, 0, false, 0, 0, null); Kline.Color = Autodesk.AutoCAD.Colors.Color.FromColorIndex(Autodesk.AutoCAD.Colors.ColorMethod.ByColor, 1); Kline.Layer = "9800"; Kline.SetDatabaseDefaults(); blocktablerecord.AppendEntity(Kline); traction.AddNewlyCreatedDBObject(Kline, true); Left_List.Add(p1); x = RB.X + (y - RB.Y) / k; p1 = new Point3d(x, y, 0); p2 = new Point3d(x + 47.5, y, 0); pc = new Point3dCollection(); pc.Add(p1); pc.Add(p2); Kline = new Polyline2d(Poly2dType.SimplePoly, pc, 0, false, 0, 0, null); Kline.Color = Autodesk.AutoCAD.Colors.Color.FromColorIndex(Autodesk.AutoCAD.Colors.ColorMethod.ByColor, 1); Kline.Layer = "9800"; Kline.SetDatabaseDefaults(); blocktablerecord.AppendEntity(Kline); traction.AddNewlyCreatedDBObject(Kline, true); Right_List.Add(p1); } //下边线坐标 for (int i = 0; i < Below_List.Count; i++) { double x = Below_List[i].X; double y = Below_List[i].Y; DBText text7 = new DBText(); string value7 = (x / 1000.0 % 100).ToString("F1"); if (value7.Length == 3) { value7 = "0" + value7; } text7.TextString = value7; text7.Position = new Point3d(x - 22.5, y - 41.25, 0); text7.Height = 15; text7.Color = Autodesk.AutoCAD.Colors.Color.FromColorIndex(Autodesk.AutoCAD.Colors.ColorMethod.ByColor, 3); text7.Layer = "9800"; text7.TextStyle = HZ; text7.WidthFactor = 0.75; text7.SetDatabaseDefaults(); blocktablerecord.AppendEntity(text7); traction.AddNewlyCreatedDBObject(text7, true); if (i == 0 || i == Below_List.Count - 1) { DBText text = new DBText(); string value = "34" + ((int)x / 1000 / 100).ToString(); text.TextString = value; text.Position = new Point3d(x - 43, y - 36.25, 0); text.Height = 10; text.Color = Autodesk.AutoCAD.Colors.Color.FromColorIndex(Autodesk.AutoCAD.Colors.ColorMethod.ByColor, 3); text.Layer = "9800"; text.TextStyle = HZ; text.WidthFactor = 0.75; text.SetDatabaseDefaults(); blocktablerecord.AppendEntity(text); traction.AddNewlyCreatedDBObject(text, true); } } //上边线坐标 for (int i = 0; i < Top_List.Count; i++) { double x = Top_List[i].X; double y = Top_List[i].Y; DBText text7 = new DBText(); string value7 = (x / 1000.0 % 100).ToString("F1"); if (value7.Length == 3) { value7 = "0" + value7; } text7.TextString = value7; text7.Position = new Point3d(x - 22.5, y + 26.25, 0); text7.Height = 15; text7.Color = Autodesk.AutoCAD.Colors.Color.FromColorIndex(Autodesk.AutoCAD.Colors.ColorMethod.ByColor, 3); text7.Layer = "9800"; text7.TextStyle = HZ; text7.WidthFactor = 0.75; text7.SetDatabaseDefaults(); blocktablerecord.AppendEntity(text7); traction.AddNewlyCreatedDBObject(text7, true); if (i == 0 || i == Below_List.Count - 1) { DBText text = new DBText(); string value = "34" + ((int)x / 1000 / 100).ToString(); text.TextString = value; text.Position = new Point3d(x - 43, y + 31.25, 0); text.Height = 10; text.Color = Autodesk.AutoCAD.Colors.Color.FromColorIndex(Autodesk.AutoCAD.Colors.ColorMethod.ByColor, 3); text.Layer = "9800"; text.TextStyle = HZ; text.WidthFactor = 0.75; text.SetDatabaseDefaults(); blocktablerecord.AppendEntity(text); traction.AddNewlyCreatedDBObject(text, true); } } //左边线坐标 for (int i = 0; i < Left_List.Count; i++) { double x = Left_List[i].X; double y = Left_List[i].Y; DBText text7 = new DBText(); string value7 = (y / 1000.0 % 100).ToString("F1"); if (value7.Length == 3) { value7 = "0" + value7; } text7.TextString = value7; text7.Position = new Point3d(x - 37.5, y + 5, 0); text7.Height = 15; text7.Color = Autodesk.AutoCAD.Colors.Color.FromColorIndex(Autodesk.AutoCAD.Colors.ColorMethod.ByColor, 3); text7.Layer = "9800"; text7.TextStyle = HZ; text7.WidthFactor = 0.75; text7.SetDatabaseDefaults(); blocktablerecord.AppendEntity(text7); traction.AddNewlyCreatedDBObject(text7, true); if (i == 0 || i == Left_List.Count - 1) { DBText text = new DBText(); string value = ((int)y / 1000 / 100).ToString(); text.TextString = value; text.Position = new Point3d(x - 46.5, y + 21, 0); text.Height = 10; text.Color = Autodesk.AutoCAD.Colors.Color.FromColorIndex(Autodesk.AutoCAD.Colors.ColorMethod.ByColor, 3); text.Layer = "9800"; text.TextStyle = HZ; text.WidthFactor = 0.75; text.SetDatabaseDefaults(); blocktablerecord.AppendEntity(text); traction.AddNewlyCreatedDBObject(text, true); } } //右边线坐标 for (int i = 0; i < Right_List.Count; i++) { double x = Right_List[i].X; double y = Right_List[i].Y; DBText text7 = new DBText(); string value7 = (y / 1000.0 % 100).ToString("F1"); if (value7.Length == 3) { value7 = "0" + value7; } text7.TextString = value7; text7.Position = new Point3d(x + 8.5, y + 5, 0); text7.Height = 15; text7.Color = Autodesk.AutoCAD.Colors.Color.FromColorIndex(Autodesk.AutoCAD.Colors.ColorMethod.ByColor, 3); text7.Layer = "9800"; text7.TextStyle = HZ; text7.WidthFactor = 0.75; text7.SetDatabaseDefaults(); blocktablerecord.AppendEntity(text7); traction.AddNewlyCreatedDBObject(text7, true); if (i == 0 || i == Right_List.Count - 1) { DBText text = new DBText(); string value = ((int)y / 1000 / 100).ToString(); text.TextString = value; text.Position = new Point3d(x, y + 21, 0); text.Height = 10; text.Color = Autodesk.AutoCAD.Colors.Color.FromColorIndex(Autodesk.AutoCAD.Colors.ColorMethod.ByColor, 3); text.Layer = "9800"; text.TextStyle = HZ; text.WidthFactor = 0.75; text.SetDatabaseDefaults(); blocktablerecord.AppendEntity(text); traction.AddNewlyCreatedDBObject(text, true); } } //右下角附注 double xx = 0; double yy = 0; xx = RB.X - D245; yy = RB.Y - 78.75; List RLnote = new List(); string str = ""; str = "调查员: 赵永健"; RLnote.Add(str); str = "检查员: 葛礼辉"; RLnote.Add(str); for (int i = 0; i < RLnote.Count; i++) { DBText text1 = new DBText(); text1.TextString = RLnote[i]; text1.Position = new Point3d(xx, yy, 0); text1.Height = D15; text1.Color = Autodesk.AutoCAD.Colors.Color.FromColorIndex(Autodesk.AutoCAD.Colors.ColorMethod.ByColor, 4); text1.Layer = "9800"; text1.TextStyle = HZ; text1.SetDatabaseDefaults(); blocktablerecord.AppendEntity(text1); traction.AddNewlyCreatedDBObject(text1, true); yy = yy - 30; } //比例尺 xx = (RB.X - LB.X) / 2 + LB.X; yy = LB.Y - 93.75; DBText text2 = new DBText(); text2.TextString = "1:5000"; text2.Position = new Point3d(xx, yy, 0); text2.Height = D20; text2.Color = Autodesk.AutoCAD.Colors.Color.FromColorIndex(Autodesk.AutoCAD.Colors.ColorMethod.ByColor, 2); text2.Layer = "9800"; text2.HorizontalMode = TextHorizontalMode.TextCenter; text2.AlignmentPoint = new Point3d(xx, yy, 0); text2.TextStyle = ID12; text2.WidthFactor = 0.75; text2.SetDatabaseDefaults(); blocktablerecord.AppendEntity(text2); traction.AddNewlyCreatedDBObject(text2, true); //图名 xx = (RB.X - LB.X) / 2 + LB.X; yy = LT.Y + 138.75; DBText text4 = new DBText(); text4.TextString = Title; text4.Position = new Point3d(xx, yy, 0); text4.Height = 25; text4.Color = Autodesk.AutoCAD.Colors.Color.FromColorIndex(Autodesk.AutoCAD.Colors.ColorMethod.ByColor, 6); text4.Layer = "9800"; text4.HorizontalMode = TextHorizontalMode.TextCenter; text4.AlignmentPoint = new Point3d(xx, yy, 0); text4.TextStyle = ID123; text4.WidthFactor = 1; text4.SetDatabaseDefaults(); blocktablerecord.AppendEntity(text4); traction.AddNewlyCreatedDBObject(text4, true); string[] str_lbtxt = new string[3]; str_lbtxt[0] = "2012年8月调查。"; str_lbtxt[1] = "1980西安坐标系。"; str_lbtxt[2] = "1985国家高程基准。"; xx = LB.X; yy = LB.Y - 78.75; for (int i = 0; i < 3; i++) { DBText text_1 = new DBText(); text_1.TextString = str_lbtxt[i]; text_1.Position = new Point3d(xx, yy, 0); text_1.Height = D15; text_1.Color = Autodesk.AutoCAD.Colors.Color.FromColorIndex(Autodesk.AutoCAD.Colors.ColorMethod.ByColor, 4); text_1.Layer = "9800"; text_1.TextStyle = HZ; text_1.WidthFactor = 1; text_1.SetDatabaseDefaults(); blocktablerecord.AppendEntity(text_1); traction.AddNewlyCreatedDBObject(text_1, true); yy = yy - 30; } //公司名称 string Company = "四川中水成勘院测绘工程有限责任公司"; xx = LB.X - 82.75; yy = LB.Y; for (int i = Company.Length - 1; i >= 0; i--) { DBText text6 = new DBText(); text6.TextString = Company[i].ToString(); text6.Position = new Point3d(xx, yy, 0); text6.Height = D20; text6.Color = Autodesk.AutoCAD.Colors.Color.FromColorIndex(Autodesk.AutoCAD.Colors.ColorMethod.ByColor, 2); text6.Layer = "9800"; text6.TextStyle = HZ; text6.WidthFactor = 1; text6.SetDatabaseDefaults(); blocktablerecord.AppendEntity(text6); traction.AddNewlyCreatedDBObject(text6, true); yy = yy + 25; } //接图表 //接图表左下角坐标 xx = LT.X; yy = LT.Y + 63.75; for (int i = 0; i < 4; i++) { Point3dCollection Lcoll = new Point3dCollection(); Lcoll.Add(new Point3d(xx, yy + D120, 0)); Lcoll.Add(new Point3d(xx, yy, 0)); Polyline2d JTB = new Polyline2d(Poly2dType.SimplePoly, Lcoll, 0, true, 0, 0, null); JTB.Color = Autodesk.AutoCAD.Colors.Color.FromColorIndex(Autodesk.AutoCAD.Colors.ColorMethod.ByColor, 1); JTB.Layer = "9800"; JTB.SetDatabaseDefaults(); blocktablerecord.AppendEntity(JTB); traction.AddNewlyCreatedDBObject(JTB, true); xx = xx + 75; } xx = LT.X; yy = LT.Y + 63.75; for (int i = 0; i < 4; i++) { Point3dCollection Lcoll = new Point3dCollection(); Lcoll.Add(new Point3d(xx, yy, 0)); Lcoll.Add(new Point3d(xx + D225, yy, 0)); Polyline2d JTB = new Polyline2d(Poly2dType.SimplePoly, Lcoll, 0, true, 0, 0, null); JTB.Color = Autodesk.AutoCAD.Colors.Color.FromColorIndex(Autodesk.AutoCAD.Colors.ColorMethod.ByColor, 1); JTB.Layer = "9800"; JTB.SetDatabaseDefaults(); blocktablerecord.AppendEntity(JTB); traction.AddNewlyCreatedDBObject(JTB, true); yy = yy + 40; } //接图表填充 //接图表左上角坐标 xx = LT.X + D225 / 3; yy = LT.Y + 63.75 + (D120 / 3) * 2; Point3dCollection coll = new Point3dCollection(); coll.Add(new Point3d(xx, yy, 0)); coll.Add(new Point3d(xx, yy - D120 / 3, 0)); coll.Add(new Point3d(xx + D225 / 3, yy - D120 / 3, 0)); coll.Add(new Point3d(xx + D225 / 3, yy, 0)); coll.Add(new Point3d(xx, yy, 0)); Polyline2d line2d = new Polyline2d(Poly2dType.SimplePoly, coll, 0, false, 0, 0, null); xx = xx + dist1; for (int i = 0; i < 30; i++) { Line l = new Line(); l.StartPoint = new Point3d(xx + 90, yy + 90, 0); l.EndPoint = new Point3d(xx - 90, yy - 90, 0); Point3dCollection IP = new Point3dCollection(); l.IntersectWith(line2d, Intersect.ExtendThis, IP, 0, 0); Polyline2d FillLine = new Polyline2d(Poly2dType.SimplePoly, IP, 0, false, 0, 0, null); FillLine.Layer = "9800"; FillLine.Color = Autodesk.AutoCAD.Colors.Color.FromColorIndex(Autodesk.AutoCAD.Colors.ColorMethod.ByColor, 1); FillLine.SetDatabaseDefaults(); blocktablerecord.AppendEntity(FillLine); traction.AddNewlyCreatedDBObject(FillLine, true); xx = xx + dist2; } Point3d pp1 = new Point3d(LT.X - 3000, LT.Y + 2000, 0); Point3d pp2 = LT; string str1 = GetNumber(pp1, pp2); pp1 = LT; pp2 = new Point3d(LT.X + 3000, LT.Y + 2000, 0); string str2 = GetNumber(pp1, pp2); pp1 = RT; pp2 = new Point3d(RT.X + 3000, RT.Y + 2000, 0); string str3 = GetNumber(pp1, pp2); pp1 = LT; pp2 = new Point3d(LT.X - 3000, LT.Y - 2000, 0); string str4 = GetNumber(pp1, pp2); pp1 = RB; pp2 = new Point3d(RB.X + 3000, RB.Y + 2000, 0); string str5 = GetNumber(pp1, pp2); pp1 = LB; pp2 = new Point3d(LB.X - 3000, LB.Y - 2000, 0); string str6 = GetNumber(pp1, pp2); pp1 = RB; pp2 = new Point3d(RB.X - 3000, RB.Y - 2000, 0); string str7 = GetNumber(pp1, pp2); pp1 = RB; pp2 = new Point3d(RB.X + 3000, RB.Y - 2000, 0); string str8 = GetNumber(pp1, pp2); double xx1 = LT.X + 37.5; double yy1 = LT.Y + 83.75; double xx2 = 0; double yy2 = 0; for (int i = 1; i < 9; i++) { string value = ""; switch (i) { case 1: value = str1; xx2 = xx1; yy2 = yy1 + 80; break; case 2: value = str2; xx2 = xx1 + 75; yy2 = yy1 + 80; break; case 3: value = str3; xx2 = xx1 + 150; yy2 = yy1 + 80; break; case 4: value = str4; xx2 = xx1; yy2 = yy1 + 40; break; case 5: value = str5; xx2 = xx1 + 150; yy2 = yy1 + 40; break; case 6: value = str6; xx2 = xx1; yy2 = yy1; break; case 7: value = str7; xx2 = xx1 + 75; yy2 = yy1; break; case 8: value = str6; xx2 = xx1 + 150; yy2 = yy1; break; default: break; } if (value != "") { value = value.Substring(value.Length - 3); } DBText text_L = new DBText(); text_L.TextString = value; text_L.Position = new Point3d(xx2, yy2, 0); text_L.Height = 12.5; text_L.WidthFactor = 0.75; text_L.Color = Autodesk.AutoCAD.Colors.Color.FromColorIndex(Autodesk.AutoCAD.Colors.ColorMethod.ByColor, 3); text_L.Layer = "9800"; text_L.TextStyle = HZ; text_L.HorizontalMode = TextHorizontalMode.TextCenter; text_L.VerticalMode = TextVerticalMode.TextVerticalMid; text_L.AlignmentPoint = new Point3d(xx2, yy2, 0); text_L.SetDatabaseDefaults(); blocktablerecord.AppendEntity(text_L); traction.AddNewlyCreatedDBObject(text_L, true); } ////接图表中间行——左 //if (string.IsNullOrEmpty(Left_Number) == false) //{ // //接图表左下角坐标 // xx1 = LT.X; // yy1 = LT.Y + D22 + D2 + D6; // xx2 = xx1 + D90 / 3 / 2; // yy2 = yy1 + D48 / 3 * 1.5; // DBText text_L = new DBText(); // text_L.TextString = Left_Number; // text_L.Position = new Point3d(xx2, yy2, 0); // text_L.Height = D5; // text_L.Color = Autodesk.AutoCAD.Colors.Color.FromColorIndex(Autodesk.AutoCAD.Colors.ColorMethod.ByColor, 3); // text_L.Layer = layername; // text_L.TextStyle = HZ; // text_L.HorizontalMode = TextHorizontalMode.TextCenter; // text_L.VerticalMode = TextVerticalMode.TextVerticalMid; // text_L.AlignmentPoint = new Point3d(xx2, yy2, 0); // text_L.SetDatabaseDefaults(); // blocktablerecord.AppendEntity(text_L); // traction.AddNewlyCreatedDBObject(text_L, true); //} ////接图表中间行——右 //if (string.IsNullOrEmpty(Right_Number) == false) //{ // //接图表左下角坐标 // xx1 = LT.X; // yy1 = LT.Y + D22 + D2 + D6; // xx2 = xx1 + D90 / 3 * 2.5; // yy2 = yy1 + D48 / 3 * 1.5; // DBText text_R = new DBText(); // text_R.TextString = Right_Number; // text_R.Position = new Point3d(xx2, yy2, 0); // text_R.Height = D5; // text_R.Color = Autodesk.AutoCAD.Colors.Color.FromColorIndex(Autodesk.AutoCAD.Colors.ColorMethod.ByColor, 3); // text_R.Layer = layername; // text_R.TextStyle = HZ; // text_R.HorizontalMode = TextHorizontalMode.TextCenter; // text_R.VerticalMode = TextVerticalMode.TextVerticalMid; // text_R.AlignmentPoint = new Point3d(xx2, yy2, 0); // text_R.SetDatabaseDefaults(); // blocktablerecord.AppendEntity(text_R); // traction.AddNewlyCreatedDBObject(text_R, true); //} traction.Commit(); } catch (Autodesk.AutoCAD.Runtime.Exception ex) { System.Windows.Forms.MessageBox.Show(ex.ToString()); } finally { traction.Dispose(); documentlock.Dispose(); } } private void createTextStyle() { Database database = HostApplicationServices.WorkingDatabase; Transaction traction = database.TransactionManager.StartTransaction(); DocumentLock documentlock = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.LockDocument(); TextStyleTable styletable = traction.GetObject(database.TextStyleTableId, OpenMode.ForWrite) as TextStyleTable; string stylename = "ht"; if (styletable.Has(stylename) == false) { TextStyleTableRecord R1 = new TextStyleTableRecord(); R1.Name = stylename; //R1.FileName = "方正细等线简体"; R1.FileName = "SIMHEI.TTF"; //record.BigFontFileName = "hztxt.shx"; R1.ObliquingAngle = 0; R1.XScale = 1; ObjectId textstyleID = styletable.Add(R1); traction.AddNewlyCreatedDBObject(R1, true); //database.Textstyle = textstyleID; } stylename = "HZ"; if (styletable.Has(stylename) == false) { TextStyleTableRecord R1 = new TextStyleTableRecord(); R1.Name = stylename; //R1.FileName = "方正细等线简体"; R1.FileName = "rs.shx"; R1.BigFontFileName = "hztxt.shx"; R1.ObliquingAngle = 0; R1.XScale = 0.75; ObjectId textstyleID = styletable.Add(R1); traction.AddNewlyCreatedDBObject(R1, true); //database.Textstyle = textstyleID; } stylename = "st"; if (styletable.Has(stylename) == false) { TextStyleTableRecord R1 = new TextStyleTableRecord(); R1.Name = stylename; //R1.FileName = "方正细等线简体"; R1.FileName = "STSONG.TTF"; //R1.BigFontFileName = "hztxt.shx"; R1.ObliquingAngle = 0; R1.XScale = 1; ObjectId textstyleID = styletable.Add(R1); traction.AddNewlyCreatedDBObject(R1, true); //database.Textstyle = textstyleID; } stylename = "Standard"; if (styletable.Has(stylename) == false) { TextStyleTableRecord R1 = new TextStyleTableRecord(); R1.Name = stylename; //R1.FileName = "方正细等线简体"; R1.FileName = "rs.shx"; R1.BigFontFileName = "hztxt.shx"; R1.ObliquingAngle = 0; R1.XScale = 0.75; ObjectId textstyleID = styletable.Add(R1); traction.AddNewlyCreatedDBObject(R1, true); //database.Textstyle = textstyleID; } stylename = "123"; if (styletable.Has(stylename) == false) { TextStyleTableRecord R1 = new TextStyleTableRecord(); R1.Name = stylename; //R1.FileName = "方正细等线简体"; R1.FileName = "SIMHEI.TTF"; //R1.BigFontFileName = "hztxt.shx"; R1.ObliquingAngle = 0; R1.XScale = 0.75; ObjectId textstyleID = styletable.Add(R1); traction.AddNewlyCreatedDBObject(R1, true); //database.Textstyle = textstyleID; } stylename = "12"; if (styletable.Has(stylename) == false) { TextStyleTableRecord R1 = new TextStyleTableRecord(); R1.Name = stylename; //R1.FileName = "方正细等线简体"; R1.FileName = "complex.shx"; //R1.BigFontFileName = "hztxt.shx"; R1.ObliquingAngle = 0; R1.XScale = 0.75; ObjectId textstyleID = styletable.Add(R1); traction.AddNewlyCreatedDBObject(R1, true); //database.Textstyle = textstyleID; } stylename = "细等线体"; if (styletable.Has(stylename) == false) { TextStyleTableRecord R1 = new TextStyleTableRecord(); R1.Name = stylename; //R1.FileName = "方正细等线简体"; R1.FileName = "FZXDXJW.TTF"; //record.BigFontFileName = "hztxt.shx"; R1.ObliquingAngle = 0; R1.XScale = 1; ObjectId textstyleID = styletable.Add(R1); traction.AddNewlyCreatedDBObject(R1, true); //database.Textstyle = textstyleID; } stylename = "中等线体"; if (styletable.Has(stylename) == false) { TextStyleTableRecord R2 = new TextStyleTableRecord(); R2.Name = stylename; //R2.FileName = "方正中等线简体"; R2.FileName = "FZZDXJW.TTF"; //record.BigFontFileName = "hztxt.shx"; R2.ObliquingAngle = 0; R2.XScale = 1; ObjectId textstyleID = styletable.Add(R2); traction.AddNewlyCreatedDBObject(R2, true); //database.Textstyle = textstyleID; } stylename = "宋体"; if (styletable.Has(stylename) == false) { TextStyleTableRecord R3 = new TextStyleTableRecord(); R3.Name = stylename; //R3.FileName = "华文宋体"; R3.FileName = "STSONG.TTF"; //record.BigFontFileName = "hztxt.shx"; R3.ObliquingAngle = 0; R3.XScale = 1; ObjectId textstyleID = styletable.Add(R3); traction.AddNewlyCreatedDBObject(R3, true); //database.Textstyle = textstyleID; } traction.Commit(); documentlock.Dispose(); } private ObjectId gettextstyleID(string Name) { Database database = HostApplicationServices.WorkingDatabase; Transaction traction = database.TransactionManager.StartTransaction(); DocumentLock documentlock = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.LockDocument(); TextStyleTable styletable = traction.GetObject(database.TextStyleTableId, OpenMode.ForWrite) as TextStyleTable; //BlockTableRecord blocktablerecord = traction.GetObject(blocktable[BlockTableRecord.ModelSpace], // OpenMode.ForWrite) as BlockTableRecord; TextStyleTableRecord record = traction.GetObject(styletable[Name], OpenMode.ForRead) as TextStyleTableRecord; ObjectId ID = record.ObjectId; traction.Commit(); documentlock.Dispose(); return ID; } private void zoom() { Document document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument; Database database = HostApplicationServices.WorkingDatabase; Editor editor = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor; using (Transaction traction = database.TransactionManager.StartTransaction()) { ViewTableRecord currview = editor.GetCurrentView(); currview.CenterPoint = new Point2d((RT.X - LT.X) / 2 + LT.X, (LT.Y - LB.Y) / 2 + LB.Y); currview.ViewDirection = new Vector3d(0, 0, 1); currview.Width = RT.X - LT.X + 500; currview.Height = LT.Y - LB.Y + 500; editor.SetCurrentView(currview); traction.Commit(); } } private string GetNumber(Point3d p1, Point3d p2) { string TValue = ""; Document document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument; Database database = HostApplicationServices.WorkingDatabase; using (Transaction traction = database.TransactionManager.StartTransaction()) { DocumentLock documentlock = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.LockDocument(); BlockTable blocktable = traction.GetObject(database.BlockTableId, OpenMode.ForRead) as BlockTable; Editor editor = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor; TypedValue[] typed = new TypedValue[1]; typed.SetValue(new TypedValue((int)DxfCode.Start, "3dFace,LWPolyline,Polyline,Polyline2d"), 0); SelectionFilter filter = new SelectionFilter(typed); PromptSelectionResult psr = editor.SelectWindow(p1, p2, filter); if (psr.Status == PromptStatus.OK) { SelectionSet selectionset = psr.Value; ObjectId[] obj = new ObjectId[selectionset.Count]; obj = selectionset.GetObjectIds(); for (int i = 0; i < obj.Length; i++) { ObjectId objid = obj[i]; Entity entity = (Entity)traction.GetObject(objid, OpenMode.ForRead); if (entity is Face) { TypedValue[] typed_T = new TypedValue[2]; typed_T.SetValue(new TypedValue((int)DxfCode.Start, "Text"), 0); typed_T.SetValue(new TypedValue((int)DxfCode.LayerName, "TREE_LAYER"), 1); SelectionFilter filter_T = new SelectionFilter(typed_T); PromptSelectionResult psr_T = editor.SelectWindow(p1, p2, filter_T); if (psr_T.Status == PromptStatus.OK) { SelectionSet set_T = psr_T.Value; ObjectId[] obj_T = new ObjectId[set_T.Count]; obj_T = set_T.GetObjectIds(); for (int j = 0; j < obj_T.Length; j++) { ObjectId id_T = obj_T[i]; Entity entits = (Entity)traction.GetObject(id_T, OpenMode.ForRead); if (entits is DBText) { DBText text = entits as DBText; TValue = text.TextString; break; } } } break; } else if (entity is Polyline) { TypedValue[] typed_T = new TypedValue[2]; typed_T.SetValue(new TypedValue((int)DxfCode.Start, "Text"), 0); typed_T.SetValue(new TypedValue((int)DxfCode.LayerName, "TREE_LAYER"), 1); SelectionFilter filter_T = new SelectionFilter(typed_T); PromptSelectionResult psr_T = editor.SelectWindow(p1, p2, filter_T); if (psr_T.Status == PromptStatus.OK) { SelectionSet set_T = psr_T.Value; ObjectId[] obj_T = new ObjectId[set_T.Count]; obj_T = set_T.GetObjectIds(); for (int j = 0; j < obj_T.Length; j++) { ObjectId id_T = obj_T[j]; Entity entits = (Entity)traction.GetObject(id_T, OpenMode.ForRead); if (entits is DBText) { DBText text = entits as DBText; TValue = text.TextString; break; } } } break; } } } documentlock.Dispose(); traction.Commit(); } return TValue; } } }