Dim acadapp As Object '建立Application对象
Dim acaddoc As Object '建立Document对象
Dim mospace As Object '建立Model Space 对象
On Error Resume Next
Set acadapp = GetObject(, "autocad.application") ‘若AutoCad已启动,则直接得到
If Err Then
Err.Clear
Set acadapp = CreateObject("autocad.application") ‘若AutoCad未启动,则运行它
If Err Then
MsgBox Err.Description
Exit Sub
End If
End If
acadapp.Visible = True ‘使AutoCad可见
Set acaddoc = acadapp.ActiveDocument ‘设acaddoc为当前图形文件
Set mospace = acaddoc.ModelSpace ‘设mospace为当前图形
Dim dwgname As String
dwgname = "c:\acadr14\sample\campus.dwg"
If Dir(dwgname) <> "" Then
acaddoc.Open dwgname '打开一个CAD文件
Else
acaddoc.new("acad") '以acad.dwt为模板建立一个新文件
End If
由用户在屏幕上选择实体
Dim tempset as Object
Dim obj as Object
Set tempset = acaddoc.SelectionSets.Add("newset") '建立新选择集
tempset.SelectOnScreen ‘用户在屏幕上选择
For Each obj In tempset ‘遍历选择集中的实体
If obj.EntityName="AcDbLine" And obj.Layer="wall" Then
obj.HighLight(True) '亮显实体及获取实体信息并显示在你的控件中
End IF
Next