- UID
- 658062
- 积分
- 2147
- 精华
- 贡献
-
- 威望
-
- 活跃度
-
- D豆
-
- 在线时间
- 小时
- 注册时间
- 2008-10-22
- 最后登录
- 1970-1-1
|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
×
本帖最后由 csharp 于 2014-5-29 18:42 编辑
源帖 http://bbs.xdcad.net/thread-674146-1-1.html
- // (C) Copyright 2014 by
- using System;
- using System.Diagnostics;
- using System.IO;
- using Autodesk.AutoCAD.Runtime;
- using Autodesk.AutoCAD.ApplicationServices;
- using Autodesk.AutoCAD.DatabaseServices;
- using Autodesk.AutoCAD.Geometry;
- using Autodesk.AutoCAD.EditorInput;
- using Exception = System.Exception;
- [assembly: CommandClass(typeof(AutoCAD_CSharp_plug_in14.MyCommands))]
- namespace AutoCAD_CSharp_plug_in14
- {
- public class MyCommands
- {
- [CommandMethod("MyGroup", "MyCommand", "MyCommandLocal", CommandFlags.Modal)]
- public void MyCommand() // This method can have any name
- {
- Document doc = Application.DocumentManager.MdiActiveDocument;
- Editor ed = doc.Editor;
- Database db = doc.Database;
- Transaction tr = db.TransactionManager.StartTransaction();
- PromptOpenFileOptions pfo = new PromptOpenFileOptions("选择坐标文件");
- pfo.DialogCaption = "选择坐标文件";
- pfo.Filter = "Txt Flie (*.txt)|*.txt";
- PromptFileNameResult fr = ed.GetFileNameForOpen(pfo);
- if (fr.Status != PromptStatus.OK)
- {
- return;
- }
- string coordFile = fr.StringResult;
- pfo.DialogCaption = "选择三角网文件";
- PromptFileNameResult tfr = ed.GetFileNameForOpen(pfo);
- if (tfr.Status != PromptStatus.OK)
- {
- return;
- }
- string sjwFile = tfr.StringResult;
- Stopwatch watch = new Stopwatch();
- watch.Restart();
- ed.WriteMessage("\n请稍候.... 文件处理中!");
- var coords = CoordFromFile(coordFile , true) as Point3d [];
- var indexs = CoordFromFile(sjwFile,false ) as int [];
- watch.Stop();
- ed.WriteMessage("\n文件读取完毕. 用时 = {0}s", watch.Elapsed.TotalSeconds.ToString());
- //ed.WriteMessage("\n开始绘制三角网,请稍候.....");
- ProgressMeter pm = new ProgressMeter();
- pm.Start("开始绘制三角网");
- pm.SetLimit(indexs.Length/3);
- watch.Restart();
- using (tr)
- {
- try
- {
- //Extents3d ext = new Extents3d();
- BlockTableRecord btr = (BlockTableRecord) tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
- for (int i = 0; i < indexs .Length -1; i = i + 3)
- {
- int n = indexs[i];
- int nn = indexs[i + 1];
- int nnn = indexs[i + 2];
- Point3dCollection npts = new Point3dCollection();
- npts.Add(coords[n - 1]);
- npts.Add(coords[nn - 1]);
- npts.Add(coords[nnn - 1]);
- Curve poly3d = new Polyline3d(Poly3dType.SimplePoly,npts,true);
-
- btr.AppendEntity((Entity )poly3d);
- tr.AddNewlyCreatedDBObject(poly3d, true);
- //ext.AddExtents((Entity) poly3d.GeometricExtents);
- pm.MeterProgress();
- }
- tr.Commit();
- pm.Stop();
- watch.Stop();
- ed.WriteMessage("\n绘制用时 = {0}", watch.Elapsed.TotalSeconds.ToString());
- ed.WriteMessage("\n绘制完成, 共 {0} 个三角形", Convert .ToString(indexs.Length / 3));
- //View
- }
- catch (Exception)
- {
- throw;
- }
- }
- }
- private Object CoordFromFile(string pathFile , bool mod)
- {
- string content = File.ReadAllText(@pathFile );
- string[] values = content.Replace("\r\n", "\r").Split('\r');
- string[] splitstring = new string[1] { ' '.ToString() };
- if (mod)
- {
- Point3d[] pts = new Point3d[values.Length - 1];
- for (int i = 0; i < values.Length; i++)
- {
- string str = values[i];
- if (str != "")
- {
- string[] nstr = str.Split(splitstring, StringSplitOptions.RemoveEmptyEntries);
- Point3d pt = new Point3d(Convert.ToDouble(nstr[0]), Convert.ToDouble(nstr[1]), Convert.ToDouble(nstr[2]));
- pts[i] = pt;
- }
- }
- return pts;
- }
- else
- {
- int nl = values.Length - 1;
- int[] nlist = new int[nl*3];
- int j = 0;
- for (int i = 0; i < nl; i++)
- {
- string str = values[i];
- string[] nstr = str.Split(splitstring, StringSplitOptions.RemoveEmptyEntries);
- nlist[j] = Convert .ToInt32(nstr[0]);
- nlist[j + 1] = Convert.ToInt32(nstr[1]);
- nlist[j + 2] = Convert .ToInt32(nstr[2]);
- j = j + 3;
- }
- return nlist;
- }
- }
- }
- }
绘制 Polyline3d 折腾了好长时间,Google搜的方法很多都不行(2014) ,最后找到现在这个 Curve 绘制
命令: NETLOAD
命令: MYCOMMAND
请稍候.... 文件处理中!
文件读取完毕. 用时 = 0.3322694s
绘制用时 = 30.0766156
绘制完成, 共 88440 个三角形
|
评分
-
查看全部评分
|