明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 1202|回复: 1

在Excel中起动AutoCAD

[复制链接]
发表于 2008-11-21 20:46:00 | 显示全部楼层 |阅读模式
本帖最后由 作者 于 2008-12-13 10:06:17 编辑

在Excel中输入以下程序
Sub ls()
  Dim AppCAD As AcadApplication
  On Error Resume Next
  Set AppCAD = GetObject(, "AutoCAD.Application")
  If Err Then
    Debug.Print Err.Number
    Err.Clear
    Set AppCAD = CreateObject("AutoCAD.Application")
  End If
  AppCAD.Visible = True
  Dim objModelSpace As AcadModelSpace
  Dim objDocument As AcadDocument
  Set objModelSpace = AppCAD.ActiveDocument.ModelSpace
  Set objDocument = AppCAD.ActiveDocument
  
End Sub
  1. Sub lll()
  2.   Dim objRegion As Variant
  3.   Dim objCurve() As AcadEntity
  4.   With ConnectCad.ActiveDocument
  5.     ReDim objCurve(Range("A65366").End(xlUp).Row - 2) As AcadEntity
  6.     Debug.Print Range("A65366").End(xlUp).Row, .ModelSpace.Count - 1
  7.     For ii = 2 To Range("A65366").End(xlUp).Row
  8.       Set objCurve(ii - 2) = .HandleToObject(Cells(ii, 1))
  9.     Next ii
  10.     Dim regionObj As Variant
  11.     regionObj = .ModelSpace.AddRegion(objCurve)
  12.     ' Define the extrusion
  13.     Dim Height As Double
  14.     Dim taperAngle As Double
  15.     Height = 20
  16.     taperAngle = 0
  17.    
  18.     ' Create the solid
  19.     Dim SolidObj As Acad3DSolid
  20.     Set SolidObj = .ModelSpace.AddExtrudedSolid(regionObj(0), Height, taperAngle)
  21.     SolidObj.Color = 1
  22.    
  23.     ' Change the viewing direction of the viewport
  24.     Dim NewDirection(0 To 2) As Double
  25.     NewDirection(0) = -1: NewDirection(1) = -1: NewDirection(2) = 1
  26.     .ActiveViewport.Direction = NewDirection
  27.     .ActiveViewport = .ActiveViewport
  28.     ZoomExtents
  29.   End With
  30. End Sub
  1. Function ConnectCad() As AcadApplication
  2.   Dim App As AcadApplication
  3.   On Error Resume Next
  4.   Set App = GetObject(, "AutoCAD.Application")
  5.   If Err Then
  6.     Err.Clear
  7.     Set App = CreateObject("AutoCAD.Application")
  8.   End If
  9.   App.Visible = True
  10.   Set ConnectCad = App
  11. End Function
  12. Function GetCornerSelect(sSetName As String, fTypeVariant As Variant, fDataVariant As Variant) As AcadSelectionSet
  13.    ''
  14.    Dim sSet As AcadSelectionSet
  15.    ''
  16.    Dim fType() As Integer, fData() As Variant
  17.    ReDim fType(UBound(fTypeVariant) + 2) As Integer: ReDim fData(UBound(fDataVariant) + 2) As Variant
  18.    fType(0) = -4: fData(0) = "<Or"
  19.    For ii = 0 To UBound(fTypeVariant)
  20.      fType(ii + 1) = fTypeVariant(ii): fData(ii + 1) = fDataVariant(ii)
  21.    Next ii
  22.    fType(UBound(fType)) = -4: fData(UBound(fData)) = "Or>"
  23.    
  24.    Dim Pt1, Pt2
  25.    With ConnectCad.ActiveDocument
  26.      ''
  27.      On Error Resume Next
  28.      Set sSet = .SelectionSets.Item(sSetName)
  29.      sSet.Delete
  30.      Set sSet = .SelectionSets.Add(sSetName)
  31.      ''
  32.      Pt1 = .Utility.GetPoint(, "Select Forst Point")
  33.      Pt2 = .Utility.GetCorner(Pt1, "Select Corner Point")
  34.      sSet.Select acSelectionSetCrossing, Pt1, Pt2, fType, fData
  35.    End With
  36.    Set GetCornerSelect = sSet
  37. End Function
  38. Sub l()
  39.   Dim sSet As AcadSelectionSet
  40.   Dim fType() As Integer, fData() As Variant
  41.   nn = 0
  42.   ReDim fType(nn) As Integer: ReDim fData(nn) As Variant
  43.   fType(0) = 8: fData(0) = "0"
  44.   Set sSet = GetCornerSelect("testSset", fType, fData)
  45. End Sub
发表于 2008-11-24 21:39:00 | 显示全部楼层
感觉还不错 补充一下 得将excel的VBA编辑器中 工具-引用-AUTOCAD 类型库选中哦。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2024-11-26 06:51 , Processed in 0.156267 second(s), 25 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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