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