huwanjie 发表于 2007-5-28 01:48:00

[求助][如何循环读取excel中的数据来建立生成三维模型]

<p>各位高手,小弟我刚学autocad 一个星期.现在开始学习autocad vba下的编程.在明经上也看了一些帖子.现在遇到一个问题,望大侠们给点帮助.</p><p>我要在vba下与excel建立通信(这以解决),然后从excel中读取(有好几千行)A B C三列数据,既x y z的坐标,然后想同过一个3Dmesh的命令来生成三维模型我想通过一个do loop循环读取这 x y z的数据.请高手帮助我如何来循环读取这写数据.我刚刚学习,还不是很懂这autocad.</p><p>下面是我通过的一个用两个点建立一条直线的程序</p><p><br/>Public Sub BlkAttr_Extract()<br/>&nbsp;&nbsp;&nbsp; <br/>&nbsp;&nbsp;&nbsp; Dim Excel As Excel.Application<br/>&nbsp;&nbsp;&nbsp; Dim ExcelSheet As Object<br/>&nbsp;&nbsp;&nbsp; Dim ExcelWorkbook As Object<br/>&nbsp;&nbsp;&nbsp; <br/>&nbsp;&nbsp;&nbsp; '创建Excel应用程序实例<br/>&nbsp;&nbsp;&nbsp; On Error Resume Next<br/>&nbsp;&nbsp;&nbsp; Set Excel = GetObject(, "Excel.Application")<br/>&nbsp;&nbsp;&nbsp; If Err &lt;&gt; 0 Then<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Set Excel = CreateObject("Excel.Application")<br/>&nbsp;&nbsp;&nbsp; End If<br/>&nbsp;&nbsp;&nbsp; <br/>&nbsp;&nbsp;&nbsp; '创建一个新工作簿<br/>&nbsp;&nbsp; Set ExcelWorkbook = Excel.Workbooks.Open("demo.xls")<br/>&nbsp;&nbsp;&nbsp; '确保Sheet1工作表为当前工作表<br/>&nbsp;&nbsp;&nbsp; Set ExcelSheet = Excel.ActiveSheet<br/>&nbsp;&nbsp;&nbsp; '将新创建的工作簿保存为Excel文件<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br/>&nbsp;&nbsp; Dim lineObj As AcadLine<br/>&nbsp;&nbsp; Dim startPoint(0 To 2) As Double<br/>&nbsp;&nbsp; Dim endPoint(0 To 2) As Double</p><p><br/>startPoint(0) = ExcelSheet.Cells(1, 1).Value '将EXCEL中A1的值赋予startPoint(0)<br/><br/>startPoint(1) = ExcelSheet.Cells(1, 2).Value '将EXCEL中B1的值赋予startPoint(1)<br/><br/>startPoint(2) = ExcelSheet.Cells(1, 3).Value '将EXCEL中B1的值赋予startPoint(2)<br/></p><p>endPoint(0) = ExcelSheet.Cells(2, 1).Value '将EXCEL中B1的值赋予endPoint(0)<br/><br/>endPoint(1) = ExcelSheet.Cells(2, 2).Value '将EXCEL中B1的值赋予endPoint(1)<br/><br/>endPoint(2) = ExcelSheet.Cells(2, 3).Value '将EXCEL中B1的值赋予endPoint(2)<br/></p><p>Set lineObj = ThisDrawing.ModelSpace.AddLine(startPoint, endPoint)<br/>ZoomAll</p><p>excelApp.Quit</p><p>End Sub<br/></p><p>现在我想用一个3dmesh来生成一个三维图形想同过do loop循环来读取.</p><p>Public Sub BlkAttr_Extract()<br/>&nbsp;&nbsp;&nbsp; <br/>&nbsp;&nbsp;&nbsp; Dim Excel As Excel.Application<br/>&nbsp;&nbsp;&nbsp; Dim ExcelSheet As Object<br/>&nbsp;&nbsp;&nbsp; Dim ExcelWorkbook As Object<br/>&nbsp;&nbsp;&nbsp; <br/>&nbsp;&nbsp;&nbsp; '创建Excel应用程序实例<br/>&nbsp;&nbsp;&nbsp; On Error Resume Next<br/>&nbsp;&nbsp;&nbsp; Set Excel = GetObject(, "Excel.Application")<br/>&nbsp;&nbsp;&nbsp; If Err &lt;&gt; 0 Then<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Set Excel = CreateObject("Excel.Application")<br/>&nbsp;&nbsp;&nbsp; End If<br/>&nbsp;&nbsp;&nbsp; <br/>&nbsp;&nbsp;&nbsp; '创建一个新工作簿<br/>&nbsp;&nbsp; Set ExcelWorkbook = Excel.Workbooks.Open("三维曲线.xls")<br/>&nbsp;&nbsp;&nbsp; '确保Sheet1工作表为当前工作表<br/>&nbsp;&nbsp;&nbsp; Set ExcelSheet = Excel.ActiveSheet<br/>&nbsp;&nbsp;&nbsp; '将新创建的工作簿保存为Excel文件<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Dim meshObj As AcadPolygonMesh</p><p>&nbsp;&nbsp;&nbsp; Dim mSize,nSize,Count As Integer</p><p><font color="#ff0000">DO </font></p><p><font color="#ff0000">(我遍不出来了这里,请高手帮助)</font></p><p><font color="#ff0000">LOOP</font></p><p>mSize = 73: nSize = 21'(这是M 和N的网格,)</p><p>Set meshObj = ThisDrawing.ModelSpace.Add3DMesh(mSize, nSize, startPoint)<br/><br/>&nbsp;&nbsp;ZoomAll</p><p>excelApp.Quit</p><p>&nbsp;End Sub</p><p>(付上我的数据和在手工操作出来的图)</p>
页: [1]
查看完整版本: [求助][如何循环读取excel中的数据来建立生成三维模型]