muli 发表于 2006-10-17 14:33:00

.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>&nbsp;/// &lt;summary&gt;<BR>&nbsp;/// Summary description for MyClass.<BR>&nbsp;/// &lt;/summary&gt;<BR>&nbsp;public class MyClass<BR>&nbsp;{<BR>&nbsp;&nbsp;public MyClass()<BR>&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;//<BR>&nbsp;&nbsp;&nbsp;// TODO: Add constructor logic here<BR>&nbsp;&nbsp;&nbsp;//<BR>&nbsp;&nbsp;}</P>
<P>&nbsp;&nbsp;static public ObjectId CreateLayer()<BR>&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;ObjectId layerId; //它返回函数的值<BR>&nbsp;&nbsp;&nbsp;Database db = HostApplicationServices.WorkingDatabase;<BR>&nbsp;&nbsp;&nbsp;Transaction trans = db.TransactionManager.StartTransaction();<BR>&nbsp;&nbsp;&nbsp;//首先取得层表……<BR>&nbsp;&nbsp;&nbsp;LayerTable lt = (LayerTable)trans.GetObject(db.LayerTableId, OpenMode.ForWrite);<BR>&nbsp;&nbsp;&nbsp;//检查EmployeeLayer层是否存在……<BR>&nbsp;&nbsp;&nbsp;if (lt.Has("LAJ"))<BR>&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;layerId = lt["LAJ"];<BR>&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;else<BR>&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;//如果EmployeeLayer层不存在,就创建它<BR>&nbsp;&nbsp;&nbsp;&nbsp;LayerTableRecord ltr = new LayerTableRecord();<BR>&nbsp;&nbsp;&nbsp;&nbsp;ltr.Name = "LAJ"; //设置层的名字<BR>&nbsp;&nbsp;&nbsp;&nbsp;ltr.Color = Autodesk.AutoCAD.Colors.Color.FromColorIndex(Autodesk.AutoCAD.Colors.ColorMethod.ByAci, 1);<BR>&nbsp;&nbsp;&nbsp;&nbsp;layerId = lt.Add(ltr);<BR>&nbsp;&nbsp;&nbsp;&nbsp;trans.AddNewlyCreatedDBObject(ltr, true);<BR>&nbsp;&nbsp;&nbsp;}<BR>&nbsp;<BR>&nbsp;&nbsp;&nbsp;trans.Commit();<BR>&nbsp;&nbsp;&nbsp;trans.Dispose();<BR>&nbsp;&nbsp;&nbsp;return layerId;<BR>&nbsp;&nbsp;}</P>
<P>&nbsp;&nbsp;static public Point3dCollection InputCoo()<BR>&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;Point3dCollection point3dCollection = new Point3dCollection();<BR>&nbsp;&nbsp;&nbsp;&nbsp;System.Windows.Forms.OpenFileDialog openFileDialog1 = new System.Windows.Forms.OpenFileDialog();<BR>&nbsp;&nbsp;&nbsp;&nbsp;OpenFileDialog openFileDialog = new OpenFileDialog();<BR>&nbsp;&nbsp;&nbsp;&nbsp;openFileDialog1.InitialDirectory =&nbsp; @"D:\Documents and Settings\muli\My Documents\";<BR>&nbsp;&nbsp;&nbsp;&nbsp;openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";<BR>&nbsp;&nbsp;&nbsp;&nbsp;openFileDialog1.FilterIndex = 1;<BR>&nbsp;&nbsp;&nbsp;&nbsp;openFileDialog1.RestoreDirectory = true ;<BR>&nbsp;&nbsp;&nbsp;&nbsp;if (openFileDialog1.ShowDialog() == DialogResult.OK)<BR>&nbsp;&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;StreamReader sr = new StreamReader(openFileDialog1.FileName,System.Text.Encoding.Default);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;string [] sLine = null;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ArrayList arrText = new ArrayList();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;&nbsp;&nbsp;&nbsp;string delimStr = ",,,";<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;&nbsp;&nbsp;&nbsp;char [] delimiter = delimStr.ToCharArray();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;while (sr.Peek() != -1)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;string aa = sr.ReadLine();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;string delimStr = " ,";<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;char [] delimiter = delimStr.ToCharArray();</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sLine = aa.Split(delimiter);<BR>&nbsp;&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for (int i = 0; i &lt; 3; i++) <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;string sL = sLine;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;arrText.Add(sL);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;string[] arrString = new string;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; arrText.CopyTo(arrString,0);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//以下代码将arrString中的元素转为Point3dCollection<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for (int C = 0; C &lt; arrText.Count/3; C++)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;double X = System.Convert.ToDouble(arrString);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;double Y = System.Convert.ToDouble(arrString);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Point3d point3d = new Point3d(X,Y,0);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;point3dCollection.Add(point3d);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sr.Close();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return point3dCollection;&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;&nbsp;else<BR>&nbsp;&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;MessageBox.Show("打开/读写文件出错");<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return null;<BR>&nbsp;&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;}<BR>&nbsp;&nbsp;<BR>&nbsp;&nbsp;static public void createpline() <BR>&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;Point3dCollection ptArr = InputCoo();//定义三维坐标数组对象 <BR>&nbsp;&nbsp;&nbsp;Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor; <BR>&nbsp;&nbsp;&nbsp;Database db = HostApplicationServices.WorkingDatabase;<BR>&nbsp;&nbsp;&nbsp;Transaction trans = db.TransactionManager.StartTransaction();<BR>&nbsp;&nbsp;&nbsp;try<BR>&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;Polyline2d pline = new Polyline2d(Poly2dType.SimplePoly, ptArr, 0.0, true, 0.0, 0.0, null); <BR>&nbsp;&nbsp;&nbsp;&nbsp;ObjectId layerLAJ = CreateLayer();<BR>&nbsp;&nbsp;&nbsp;&nbsp;pline.Closed = true;<BR>&nbsp;&nbsp;&nbsp;&nbsp;pline.LayerId = layerLAJ;<BR>&nbsp;&nbsp;&nbsp;&nbsp;BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);<BR>&nbsp;&nbsp;&nbsp;&nbsp;BlockTableRecord btr = (BlockTableRecord)trans.GetObject(db.CurrentSpaceId,OpenMode.ForWrite );<BR>&nbsp;&nbsp;&nbsp;&nbsp;btr.AppendEntity(pline);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <BR>&nbsp;&nbsp;&nbsp;&nbsp;trans.AddNewlyCreatedDBObject(pline,true);<BR>&nbsp;&nbsp;&nbsp;&nbsp;trans.Commit();<BR>&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;catch<BR>&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;ed.WriteMessage("Error!");<BR>&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;finally<BR>&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;trans.Dispose();<BR>&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;}<BR>&nbsp;}</P>
<P>&nbsp;}</P>

<P>另,坐标文件的格式如下:</P>
<P>1,X,Y</P>
<P>2,X,Y</P>
<P>3,x,Y</P>
<P>......</P>
<P>其中,分隔符可以是“,”,或者空格。</P>

muli 发表于 2006-10-25 09:45:00

为什么没有高手帮我看一下呢?才哥呢?

cooolseee 发表于 2013-11-18 23:20:33

我也想学I学习
页: [1]
查看完整版本: .net开发的从text读取特定格式数据并生成图形