该代码将读取CSV点文件,例如空间分析仪(SA软件)将从某些项目、产品或竣工条件的激光扫描中输出什么。然后可以将这些数据读入AutoCAD(在本例中),以查看和创建扫描的点数据。我提供了一个包含49个点的示例CSV文本文件,但我使用此代码读取了20000多个点。
- ' File Name: ReadPointsFromSA
- ' Description: Read a CSV point file from SpatialAnalyzer (SA)
- ' By: Robert Souza
- ' Contact info: dreamtoneamps[AT]gmail.com
- ' Date: June 25, 2010
- Option Strict On
- Imports System.IO
- Imports System.Windows
- Imports Autodesk.AutoCAD.Colors
- Imports Autodesk.AutoCAD.Windows
- Imports Autodesk.AutoCAD.Runtime
- Imports Autodesk.AutoCAD.Geometry
- Imports Autodesk.AutoCAD.EditorInput
- Imports Autodesk.AutoCAD.DatabaseServices
- Imports Autodesk.AutoCAD.ApplicationServices
- Public Class Class1
- Dim ed As Editor = Application.DocumentManager.MdiActiveDocument.Editor
- Dim acDoc As Document = Application.DocumentManager.MdiActiveDocument
- Dim acCurDb As Database = acDoc.Database
- <CommandMethod("SApoints")> _
- Public Sub createLine()
- Dim CSV_FileName As String = Nothing
- ' navigate to the point text csv file using a file dialog
- Dim dlg As New System.Windows.Forms.OpenFileDialog()
- dlg.InitialDirectory = "C:"
- dlg.Filter = "csv text files (*.txt)|*.txt|csv excel files (*.csv)|*.csv|All files (*.*)|*.*"
- If dlg.ShowDialog() = Windows.Forms.DialogResult.OK Then
- CSV_FileName = dlg.FileName().ToString
- ' call sub to create points from CSV file
- readCSVPoints(CSV_FileName)
- Else
- ' if canceled write to the command line and sub not called
- ed.WriteMessage(vbCr & " command canceled")
- End If
- End Sub
- Public Sub readCSVPoints(ByVal CSV_FileName As String)
- Dim myPath As String = CSV_FileName
- ' Start a transaction
- Using acTrans As Transaction = acCurDb.TransactionManager.StartTransaction()
- ' Open the Block table for read
- Dim acBlkTbl As BlockTable
- acBlkTbl = CType(acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead), BlockTable)
- ' Open the Block table record Model space for write
- Dim acBlkTblRec As BlockTableRecord
- acBlkTblRec = CType(acTrans.GetObject(acBlkTbl(BlockTableRecord.ModelSpace), OpenMode.ForWrite), BlockTableRecord)
- ' Read a line containing (point ID, x, y, z) points from a text file that contains coordinates
- Dim inputRecord As String = Nothing
- Dim myPoints(3) As String
- Dim x, y, z As Double
- ' point ID
- Dim pid As String
- Dim inReader As StreamReader = File.OpenText(myPath)
- ' Start reading text file one line at a time
- inputRecord = inReader.ReadLine()
- While (inputRecord IsNot Nothing)
- If inputRecord.Contains(",") Then
- myPoints = inputRecord.Split(CChar(",")) ' get code string
- pid = (myPoints(0).Trim)
- x = CDbl(myPoints(1).Trim)
- y = CDbl(myPoints(2).Trim)
- z = CDbl(myPoints(3).Trim)
- ' Create a point at (x, y, z) in Model space
- Dim acPoint As DBPoint = New DBPoint(New Point3d(x, y, z))
- acPoint.SetDatabaseDefaults()
- ' create point ID text with single line text i.e. (p1 text just above the point)
- Dim acText As DBText = New DBText()
- acText.SetDatabaseDefaults()
- acText.Position = New Point3d((x - 0.25), (y + 0.075), z)
- acText.Height = 0.125
- acText.TextString = pid
- ' call sub and place items on a layer of their own
- CreateLayer(acPoint, acText)
- ' Add the new object to the block table record and the transaction
- acBlkTblRec.AppendEntity(acText)
- acTrans.AddNewlyCreatedDBObject(acText, True)
- ' Add the new object to the block table record and the transaction
- acBlkTblRec.AppendEntity(acPoint)
|