我是一个初学者,不懂编程,也不懂arx,最近才开始学C#.net + ARX开发CAD,刚作出一个小程序,与初学的交流一下,也请高手帮我完善一下,因我我没有加异常处理^_^,加了老是“并非所有代码路径都返回值”的错误,所以删了~~~希望谁能帮我改写一下,程序如下:
using System ; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; using System.IO; using System.Collections.Specialized; using Autodesk.AutoCAD.Runtime ; using Autodesk.AutoCAD.DatabaseServices; using Autodesk.AutoCAD.ApplicationServices; //using Autodesk.AutoCAD.Colors; using Autodesk.AutoCAD.Geometry; using Autodesk.AutoCAD.EditorInput;
[assembly: CommandClass(typeof(ClassLibrary.MyClass))]
namespace ClassLibrary { /// <summary> /// Summary description for MyClass. /// </summary> public class MyClass { public MyClass() { // // TODO: Add constructor logic here // }
static public ObjectId CreateLayer() { ObjectId layerId; //它返回函数的值 Database db = HostApplicationServices.WorkingDatabase; Transaction trans = db.TransactionManager.StartTransaction(); //首先取得层表…… LayerTable lt = (LayerTable)trans.GetObject(db.LayerTableId, OpenMode.ForWrite); //检查EmployeeLayer层是否存在…… if (lt.Has("LAJ")) { layerId = lt["LAJ"]; } else { //如果EmployeeLayer层不存在,就创建它 LayerTableRecord ltr = new LayerTableRecord(); ltr.Name = "LAJ"; //设置层的名字 ltr.Color = Autodesk.AutoCAD.Colors.Color.FromColorIndex(Autodesk.AutoCAD.Colors.ColorMethod.ByAci, 1); layerId = lt.Add(ltr); trans.AddNewlyCreatedDBObject(ltr, true); } trans.Commit(); trans.Dispose(); return layerId; }
static public Point3dCollection InputCoo() {  oint3dCollection point3dCollection = new Point3dCollection(); System.Windows.Forms.OpenFileDialog openFileDialog1 = new System.Windows.Forms.OpenFileDialog(); OpenFileDialog openFileDialog = new OpenFileDialog(); openFileDialog1.InitialDirectory = @"D:\Documents and Settings\muli\My Documents\"; openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"; openFileDialog1.FilterIndex = 1; openFileDialog1.RestoreDirectory = true ; if (openFileDialog1.ShowDialog() == DialogResult.OK) { StreamReader sr = new StreamReader(openFileDialog1.FileName,System.Text.Encoding.Default); string [] sLine = null; ArrayList arrText = new ArrayList(); // string delimStr = ",,,"; // char [] delimiter = delimStr.ToCharArray(); while (sr.Peek() != -1) { string aa = sr.ReadLine(); string delimStr = " ,"; char [] delimiter = delimStr.ToCharArray();
sLine = aa.Split(delimiter); for (int i = 0; i < 3; i++) { string sL = sLine; arrText.Add(sL); } } string[] arrString = new string[arrText.Count]; arrText.CopyTo(arrString,0); //以下代码将arrString中的元素转为Point3dCollection for (int C = 0; C < arrText.Count/3; C++) { double X = System.Convert.ToDouble(arrString[3*C + 1]); double Y = System.Convert.ToDouble(arrString[3*C + 2]);  oint3d point3d = new Point3d(X,Y,0); point3dCollection.Add(point3d); } sr.Close(); return point3dCollection; } else { MessageBox.Show("打开/读写文件出错"); return null; } } [CommandMethod("cpline")] static public void createpline() {  oint3dCollection ptArr = InputCoo();//定义三维坐标数组对象 Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor; Database db = HostApplicationServices.WorkingDatabase; Transaction trans = db.TransactionManager.StartTransaction(); try {  olyline2d pline = new Polyline2d(Poly2dType.SimplePoly, ptArr, 0.0, true, 0.0, 0.0, null); ObjectId layerLAJ = CreateLayer(); pline.Closed = true; pline.LayerId = layerLAJ; BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead); BlockTableRecord btr = (BlockTableRecord)trans.GetObject(db.CurrentSpaceId,OpenMode.ForWrite ); btr.AppendEntity(pline); trans.AddNewlyCreatedDBObject(pline,true); trans.Commit(); } catch { ed.WriteMessage("Error!"); } finally { trans.Dispose(); } } }
}
另,坐标文件的格式如下:
1,X,Y
2,X,Y
3,x,Y
......
其中,分隔符可以是“,”,或者空格。 |