测量展点程序,开源啦~
本帖最后由 mrhvslisp 于 2013-5-21 22:58 编辑程序VS2008写的For CAD2010
测量数据文件格式:
1,kz,22,22,35
2,p,25,25,86
3,f,58,756,96
依此类推
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Interop;
using Autodesk.AutoCAD.Interop.Common;
using Autodesk.AutoCAD.Runtime;
public void PointOutNet()
{
//MessageBox.Show("");
List<string> files=new List<string> ();
System.Windows.Forms.OpenFileDialog openDialog = new System.Windows.Forms.OpenFileDialog();
openDialog.Multiselect = true;
openDialog.Filter = "数据文件|*.dat";
openDialog.Title = "选择数据文件(可多选)";
openDialog.RestoreDirectory = false;
if (openDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
foreach (string file in openDialog.FileNames)
{
files.Add(file);
}
}
foreach (string file in files)
{
PointOutNet(file);
}
}
public void PointOutNet(string FileName)
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
string[] strArray;
List<string> data = new List<string>();
List<string[]> dataNew = new List<string[]>();
FileStream fStream = new FileStream(FileName, FileMode.Open);
StreamReader sReader = new StreamReader(fStream);
string dataRow = sReader.ReadLine();
while (dataRow != null)
{
data.Add(dataRow);
dataRow = sReader.ReadLine();
}
sReader.Close();
foreach (string pt in data)
{
strArray = pt.Split(new char[] { ',' });
if (strArray.Length >= 5)
{
dataNew.Add(strArray);
}
}
using (Transaction trans = db.TransactionManager.StartTransaction())
{
BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);
BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt, OpenMode.ForWrite);
foreach (string[] pt in dataNew)
{
double y = Convert.ToDouble(pt);
double x = Convert.ToDouble(pt);
double h = Convert.ToDouble(pt);
Point3d p1 = new Point3d(y, x, h);
DBPoint newPoint = new DBPoint(p1);
btr.AppendEntity(newPoint);
trans.AddNewlyCreatedDBObject(newPoint, true);
}
trans.Commit();
}
}
支持,这个实现起来倒不难,两个com引用没用到,去掉多好 <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 ThenFor Each file As String In openDialog.FileNames files.Add(file)Next End If For Each file As String In filesPointOutNet(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 Nothingdata.Add(dataRow)dataRow = sReader.ReadLine() End While sReader.Close() For Each pt As String In datastrArray = 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)Nexttrans.Commit() End UsingEnd Sub VB版本,怎么这么乱?<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 ThenFor Each file As String In openDialog.FileNames files.Add(file)Next End If For Each file As String In filesPointOutNet(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 Nothingdata.Add(dataRow)dataRow = sReader.ReadLine() End While sReader.Close() For Each pt As String In datastrArray = 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)Nexttrans.Commit() End UsingEnd Sub 能帮我删了吗,太乱了! 好东西,谢谢楼主!<CommandMethod("PointOutNet")> Public Sub PointOutNet()
'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)
Next
End Sub
Public Sub PointOutNet(ByVal FileName As String)
Dim doc As Document = Autodesk.AutoCAD.ApplicationServices.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 Using
End Sub 支持开源,虽然功能比我以前写的简单些.贵在共享!
页:
[1]