明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 2399|回复: 6

测量展点程序,开源啦~

[复制链接]
发表于 2013-5-21 22:36:58 | 显示全部楼层 |阅读模式
本帖最后由 mrhvslisp 于 2013-5-21 22:58 编辑

程序VS2008写的For CAD2010
测量数据文件格式:
1,kz,22,22,35
2,p,25,25,86
3,f,58,756,96
依此类推




  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Data;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Text;
  8. using Autodesk.AutoCAD.ApplicationServices;
  9. using Autodesk.AutoCAD.DatabaseServices;
  10. using Autodesk.AutoCAD.EditorInput;
  11. using Autodesk.AutoCAD.Geometry;
  12. using Autodesk.AutoCAD.Interop;
  13. using Autodesk.AutoCAD.Interop.Common;
  14. using Autodesk.AutoCAD.Runtime;
  15.       [CommandMethod("PointOutNet")]
  16.         public void PointOutNet()
  17.         {           
  18.             //MessageBox.Show("");
  19.             List<string> files=new List<string> ();

  20.             System.Windows.Forms.OpenFileDialog openDialog = new System.Windows.Forms.OpenFileDialog();
  21.             openDialog.Multiselect = true;
  22.             openDialog.Filter = "数据文件|*.dat";
  23.             openDialog.Title = "选择数据文件(可多选)";
  24.             openDialog.RestoreDirectory = false;
  25.             if (openDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  26.             {
  27.                 foreach (string file in openDialog.FileNames)
  28.                 {
  29.                     files.Add(file);
  30.                 }
  31.             }
  32.             foreach (string file in files)
  33.             {
  34.                 PointOutNet(file);
  35.             }

  36.         }

  37.         public void PointOutNet(string FileName)
  38.         {
  39.             Document doc = Application.DocumentManager.MdiActiveDocument;
  40.             Database db = doc.Database;
  41.             Editor ed = doc.Editor;

  42.             string[] strArray;
  43.             List<string> data = new List<string>();
  44.             List<string[]> dataNew = new List<string[]>();
  45.             FileStream fStream = new FileStream(FileName, FileMode.Open);
  46.             StreamReader sReader = new StreamReader(fStream);
  47.             string dataRow = sReader.ReadLine();
  48.             while (dataRow != null)
  49.             {
  50.                 data.Add(dataRow);
  51.                 dataRow = sReader.ReadLine();
  52.             }
  53.            sReader.Close();
  54.             foreach (string pt in data)
  55.             {
  56.                 strArray = pt.Split(new char[] { ',' });
  57.                 if (strArray.Length >= 5)
  58.                 {
  59.                     dataNew.Add(strArray);
  60.                 }               
  61.             }

  62.             using (Transaction trans = db.TransactionManager.StartTransaction())
  63.             {
  64.                 BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);
  65.                 BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
  66.                 foreach (string[] pt in dataNew)
  67.                 {                     
  68.                     double y = Convert.ToDouble(pt[2]);
  69.                     double x = Convert.ToDouble(pt[3]);
  70.                     double h = Convert.ToDouble(pt[4]);
  71.                     Point3d p1 = new Point3d(y, x, h);
  72.                     DBPoint newPoint = new DBPoint(p1);
  73.                     btr.AppendEntity(newPoint);
  74.                     trans.AddNewlyCreatedDBObject(newPoint, true);
  75.                 }
  76.                 trans.Commit();
  77.             }
  78.         }


发表于 2013-5-22 08:43:26 | 显示全部楼层
支持,这个实现起来倒不难,两个com引用没用到,去掉多好
发表于 2013-5-22 09:50:41 | 显示全部楼层
<CommandMethod("addRibbon")> _Public Sub addRibbon() 'MessageBox.Show(""); Dim files As New List(Of String)() Dim openDialog As New System.Windows.Forms.OpenFileDialog() openDialog.Multiselect = True openDialog.Filter = "数据文件|*.dat" openDialog.Title = "选择数据文件(可多选)" openDialog.RestoreDirectory = False If openDialog.ShowDialog() = System.Windows.Forms.DialogResult.OK Then  For Each file As String In openDialog.FileNames   files.Add(file)  Next End If For Each file As String In files  PointOutNet(file) NextEnd SubPublic Sub PointOutNet(FileName As String) Dim doc As Document = Application.DocumentManager.MdiActiveDocument Dim db As Database = doc.Database Dim ed As Editor = doc.Editor Dim strArray As String() Dim data As New List(Of String)() Dim dataNew As New List(Of String())() Dim fStream As New FileStream(FileName, FileMode.Open) Dim sReader As New StreamReader(fStream) Dim dataRow As String = sReader.ReadLine() While dataRow IsNot Nothing  data.Add(dataRow)  dataRow = sReader.ReadLine() End While sReader.Close() For Each pt As String In data  strArray = pt.Split(New Char() {","C})  If strArray.Length >= 5 Then   dataNew.Add(strArray)  End If Next Using trans As Transaction = db.TransactionManager.StartTransaction()  Dim bt As BlockTable = DirectCast(trans.GetObject(db.BlockTableId, OpenMode.ForRead), BlockTable)  Dim btr As BlockTableRecord = DirectCast(trans.GetObject(bt(BlockTableRecord.ModelSpace), OpenMode.ForWrite), BlockTableRecord)  For Each pt As String() In dataNew   Dim y As Double = Convert.ToDouble(pt(2))   Dim x As Double = Convert.ToDouble(pt(3))   Dim h As Double = Convert.ToDouble(pt(4))   Dim p1 As New Point3d(y, x, h)   Dim newPoint As New DBPoint(p1)   btr.AppendEntity(newPoint)   trans.AddNewlyCreatedDBObject(newPoint, True)  Next  trans.Commit() End UsingEnd Sub
发表于 2013-5-22 09:51:31 | 显示全部楼层
VB版本,怎么这么乱?
  1. <CommandMethod("addRibbon")> _Public Sub addRibbon() 'MessageBox.Show(""); Dim files As New List(Of String)() Dim openDialog As New System.Windows.Forms.OpenFileDialog() openDialog.Multiselect = True openDialog.Filter = "数据文件|*.dat" openDialog.Title = "选择数据文件(可多选)" openDialog.RestoreDirectory = False If openDialog.ShowDialog() = System.Windows.Forms.DialogResult.OK Then  For Each file As String In openDialog.FileNames   files.Add(file)  Next End If For Each file As String In files  PointOutNet(file) NextEnd SubPublic Sub PointOutNet(FileName As String) Dim doc As Document = Application.DocumentManager.MdiActiveDocument Dim db As Database = doc.Database Dim ed As Editor = doc.Editor Dim strArray As String() Dim data As New List(Of String)() Dim dataNew As New List(Of String())() Dim fStream As New FileStream(FileName, FileMode.Open) Dim sReader As New StreamReader(fStream) Dim dataRow As String = sReader.ReadLine() While dataRow IsNot Nothing  data.Add(dataRow)  dataRow = sReader.ReadLine() End While sReader.Close() For Each pt As String In data  strArray = pt.Split(New Char() {","C})  If strArray.Length >= 5 Then   dataNew.Add(strArray)  End If Next Using trans As Transaction = db.TransactionManager.StartTransaction()  Dim bt As BlockTable = DirectCast(trans.GetObject(db.BlockTableId, OpenMode.ForRead), BlockTable)  Dim btr As BlockTableRecord = DirectCast(trans.GetObject(bt(BlockTableRecord.ModelSpace), OpenMode.ForWrite), BlockTableRecord)  For Each pt As String() In dataNew   Dim y As Double = Convert.ToDouble(pt(2))   Dim x As Double = Convert.ToDouble(pt(3))   Dim h As Double = Convert.ToDouble(pt(4))   Dim p1 As New Point3d(y, x, h)   Dim newPoint As New DBPoint(p1)   btr.AppendEntity(newPoint)   trans.AddNewlyCreatedDBObject(newPoint, True)  Next  trans.Commit() End UsingEnd Sub
发表于 2013-5-22 09:54:27 | 显示全部楼层
能帮我删了吗,太乱了!
发表于 2013-5-22 10:17:08 | 显示全部楼层
好东西,谢谢楼主!
  1. <CommandMethod("PointOutNet")> Public Sub PointOutNet()
  2.         'MessageBox.Show("");
  3.         Dim files As New List(Of String)()
  4.         Dim openDialog As New System.Windows.Forms.OpenFileDialog()
  5.         openDialog.Multiselect = True
  6.         openDialog.Filter = "数据文件|*.dat"
  7.         openDialog.Title = "选择数据文件(可多选)"
  8.         openDialog.RestoreDirectory = False
  9.         If openDialog.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
  10.             For Each file As String In openDialog.FileNames
  11.                 files.Add(file)
  12.             Next
  13.         End If
  14.         For Each file As String In files
  15.             PointOutNet(file)
  16.         Next
  17.     End Sub
  18.     Public Sub PointOutNet(ByVal FileName As String)
  19.         Dim doc As Document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument
  20.         Dim db As Database = doc.Database
  21.         Dim ed As Editor = doc.Editor
  22.         Dim strArray As String()
  23.         Dim data As New List(Of String)()
  24.         Dim dataNew As New List(Of String())()
  25.         Dim fStream As New FileStream(FileName, FileMode.Open)
  26.         Dim sReader As New StreamReader(fStream)
  27.         Dim dataRow As String = sReader.ReadLine()
  28.         While dataRow IsNot Nothing
  29.             data.Add(dataRow)
  30.             dataRow = sReader.ReadLine()
  31.         End While
  32.         sReader.Close()
  33.         For Each pt As String In data
  34.             strArray = pt.Split(New Char() {","c})
  35.             If strArray.Length >= 5 Then
  36.                 dataNew.Add(strArray)
  37.             End If
  38.         Next
  39.         Using trans As Transaction = db.TransactionManager.StartTransaction()
  40.             Dim bt As BlockTable = DirectCast(trans.GetObject(db.BlockTableId, OpenMode.ForRead), BlockTable)
  41.             Dim btr As BlockTableRecord = DirectCast(trans.GetObject(bt(BlockTableRecord.ModelSpace), OpenMode.ForWrite), BlockTableRecord)
  42.             For Each pt As String() In dataNew
  43.                 Dim y As Double = Convert.ToDouble(pt(2))
  44.                 Dim x As Double = Convert.ToDouble(pt(3))
  45.                 Dim h As Double = Convert.ToDouble(pt(4))
  46.                 Dim p1 As New Point3d(y, x, h)
  47.                 Dim newPoint As New DBPoint(p1)
  48.                 btr.AppendEntity(newPoint)
  49.                 trans.AddNewlyCreatedDBObject(newPoint, True)
  50.             Next
  51.             trans.Commit()
  52.         End Using
  53.     End Sub
发表于 2013-5-24 22:17:06 | 显示全部楼层
支持开源,虽然功能比我以前写的简单些.贵在共享!
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2024-11-25 15:28 , Processed in 0.294329 second(s), 27 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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