明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 3082|回复: 2

.net开发的从text读取特定格式数据并生成图形

[复制链接]
发表于 2006-10-17 14:33:00 | 显示全部楼层 |阅读模式

我是一个初学者,不懂编程,也不懂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

......

其中,分隔符可以是“,”,或者空格。

 楼主| 发表于 2006-10-25 09:45:00 | 显示全部楼层
为什么没有高手帮我看一下呢?才哥呢?
发表于 2013-11-18 23:20:33 | 显示全部楼层
我也想学I学习
您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|CAD论坛|CAD教程|CAD下载|联系我们|关于明经|明经通道 ( 粤ICP备05003914号 )  
©2000-2023 明经通道 版权所有 本站代码,在未取得本站及作者授权的情况下,不得用于商业用途

GMT+8, 2024-11-25 14:21 , Processed in 0.250596 second(s), 23 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表