兰州人 发表于 2007-7-28 17:13:00

GetEntity获得ObjectID,到Excel程序

<p>从AutoCAD调用excel的功能函数</p><p>Function xlSheet() As Object</p><p>&nbsp;&nbsp;&nbsp; Dim xlApp As Object  ' This Line ,Not set Excel , run Excel<br/>&nbsp;&nbsp;&nbsp; 'Dim xlsheet As Object<br/>&nbsp;&nbsp;&nbsp; <br/>&nbsp;&nbsp;&nbsp; ' 发生错误时跳到下一个语句继续执行<br/>&nbsp;&nbsp;&nbsp; On Error Resume Next<br/>&nbsp;&nbsp;&nbsp; ' 连接Excel应用程序<br/>&nbsp;&nbsp;&nbsp; Set xlApp = GetObject(, "Excel.Application")<br/>&nbsp;&nbsp;&nbsp; <br/>&nbsp;&nbsp;&nbsp; If Err.Number &lt;&gt; 0 Then<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Set xlApp = CreateObject("Excel.Application")<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; xlApp.Visible = True<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; xlApp.Workbooks.Add<br/>&nbsp;&nbsp;&nbsp; End If</p><p>&nbsp;&nbsp;&nbsp; ' 返回当前活动的工作表<br/>&nbsp;&nbsp;&nbsp; Set xlSheet = xlApp.ActiveSheet<br/>End Function</p><p>主程序, 获取Entity的ObjectID,传输到Excel</p><p>Sub lls()<br/>&nbsp;&nbsp;&nbsp; ' Begin the selection<br/>&nbsp;&nbsp;&nbsp; Dim returnObj As AcadObject<br/>&nbsp;&nbsp;&nbsp; Dim basePnt As Variant<br/>&nbsp;&nbsp;&nbsp; <br/>&nbsp;&nbsp;&nbsp; On Error Resume Next<br/>&nbsp;&nbsp;&nbsp; ii = 1<br/>&nbsp;&nbsp;&nbsp; ' The following example waits for a selection from the user<br/>RETRY:<br/>&nbsp;&nbsp;&nbsp; ThisDrawing.Utility.GetEntity returnObj, basePnt, "Select an object"<br/>&nbsp;&nbsp;&nbsp; <br/>&nbsp;&nbsp;&nbsp; If Err &lt;&gt; 0 Then<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Err.Clear<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; MsgBox "Program ended.", , "GetEntity Example"<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Exit Sub<br/>&nbsp;&nbsp;&nbsp; Else<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; returnObj.Update<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; MsgBox "The object type is: " &amp; returnObj.EntityName, , "GetEntity Example"<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; returnObj.Update<br/>&nbsp;&nbsp;&nbsp; End If<br/>&nbsp;&nbsp;&nbsp; <br/>&nbsp;&nbsp;&nbsp; xlSheet.cells(ii, 5).Value = returnObj.ObjectID<br/>&nbsp;&nbsp;&nbsp; ii = ii + 1<br/>&nbsp;&nbsp;&nbsp; GoTo RETRY</p><p>&nbsp; <br/>End Sub</p><p>以下是一个辅助程序</p><p>Sub Example_GetEntity()<br/>&nbsp;&nbsp;&nbsp; ' This example creates several objects in model space. It then<br/>&nbsp;&nbsp;&nbsp; ' prompts the user to select an object. The example continues to<br/>&nbsp;&nbsp;&nbsp; ' have the user select objects until the user selects in empty space.<br/>&nbsp;&nbsp;&nbsp; <br/>&nbsp;&nbsp;&nbsp; ' Create a Ray object in model space<br/>&nbsp;&nbsp;&nbsp; Dim rayObj As AcadRay<br/>&nbsp;&nbsp;&nbsp; Dim basePoint(0 To 2) As Double<br/>&nbsp;&nbsp;&nbsp; Dim SecondPoint(0 To 2) As Double<br/>&nbsp;&nbsp;&nbsp; basePoint(0) = 3#: basePoint(1) = 3#: basePoint(2) = 0#<br/>&nbsp;&nbsp;&nbsp; SecondPoint(0) = 1#: SecondPoint(1) = 3#: SecondPoint(2) = 0#<br/>&nbsp;&nbsp;&nbsp; Set rayObj = ThisDrawing.ModelSpace.AddRay(basePoint, SecondPoint)<br/>&nbsp;&nbsp;&nbsp; <br/>&nbsp;&nbsp;&nbsp; ' Create a polyline object in model space<br/>&nbsp;&nbsp;&nbsp; Dim plineObj As AcadLWPolyline<br/>&nbsp;&nbsp;&nbsp; Dim points(0 To 5) As Double<br/>&nbsp;&nbsp;&nbsp; points(0) = 3: points(1) = 7<br/>&nbsp;&nbsp;&nbsp; points(2) = 9: points(3) = 2<br/>&nbsp;&nbsp;&nbsp; points(4) = 3: points(5) = 5<br/>&nbsp;&nbsp;&nbsp; Set plineObj = ThisDrawing.ModelSpace.AddLightWeightPolyline(points)<br/>&nbsp;&nbsp;&nbsp; plineObj.Closed = True</p><p>&nbsp;&nbsp;&nbsp; ' Create a line object in model space<br/>&nbsp;&nbsp;&nbsp; Dim lineObj As AcadLine<br/>&nbsp;&nbsp;&nbsp; Dim startPoint(0 To 2) As Double<br/>&nbsp;&nbsp;&nbsp; Dim endPoint(0 To 2) As Double<br/>&nbsp;&nbsp;&nbsp; startPoint(0) = 0: startPoint(1) = 0: startPoint(2) = 0<br/>&nbsp;&nbsp;&nbsp; endPoint(0) = 2: endPoint(1) = 2: endPoint(2) = 0<br/>&nbsp;&nbsp;&nbsp; Set lineObj = ThisDrawing.ModelSpace.AddLine(startPoint, endPoint)<br/>&nbsp;&nbsp;&nbsp; <br/>&nbsp;&nbsp;&nbsp; ' Create a circle object in model space<br/>&nbsp;&nbsp;&nbsp; Dim circObj As AcadCircle<br/>&nbsp;&nbsp;&nbsp; Dim centerPt(0 To 2) As Double<br/>&nbsp;&nbsp;&nbsp; Dim radius As Double<br/>&nbsp;&nbsp;&nbsp; centerPt(0) = 20: centerPt(1) = 30: centerPt(2) = 0<br/>&nbsp;&nbsp;&nbsp; radius = 3<br/>&nbsp;&nbsp;&nbsp; Set circObj = ThisDrawing.ModelSpace.AddCircle(centerPt, radius)</p><p>&nbsp;&nbsp;&nbsp; ' Create an ellipse object in model space<br/>&nbsp;&nbsp;&nbsp; Dim ellObj As AcadEllipse<br/>&nbsp;&nbsp;&nbsp; Dim majAxis(0 To 2) As Double<br/>&nbsp;&nbsp;&nbsp; Dim center(0 To 2) As Double<br/>&nbsp;&nbsp;&nbsp; Dim radRatio As Double<br/>&nbsp;&nbsp;&nbsp; center(0) = 5#: center(1) = 5#: center(2) = 0#<br/>&nbsp;&nbsp;&nbsp; majAxis(0) = 10: majAxis(1) = 20#: majAxis(2) = 0#<br/>&nbsp;&nbsp;&nbsp; radRatio = 0.3<br/>&nbsp;&nbsp;&nbsp; Set ellObj = ThisDrawing.ModelSpace.AddEllipse(center, majAxis, radRatio)</p><p>&nbsp;&nbsp;&nbsp; ZoomExtents<br/>&nbsp;&nbsp;&nbsp; <br/>End Sub<br/></p>
页: [1]
查看完整版本: GetEntity获得ObjectID,到Excel程序