| 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为当前图形 文件的模型空间 |
| acadapp.Top=100 '设置AutoCad窗口的位置 acadapp.Left=200 acadapp.Height=1000 '调整AutoCad窗口的大小 acadapp.Width=800 acadapp.Caption="my first application" '设置AutoCad窗口的 标题 |
| 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 Document对象还提供了两个十分有用的方法——SetVariable 和 GetVariable,通过它们可以得到或改变AutoCad的系统变量。 如:acaddoc.SetVariable "Orthomode", 1 '打开正交模式 dim cadver As String cadver=acaddoc.Getvariable("Acadver") '获取AutoCad的版本号 |
| Dim lwpoly As Object Dim ptarray(0 To 5) As Double '设坐标变量 ptarray(0) = 2 ptarray(1) = 4 ptarray(2) = 4 ptarray(3) = 2 ptarray(4) = 10 ptarray(5) = 4 Set lwpolyObj = moSpace.AddLightWeightPolyline(ptarray) ‘画多义线(以(2,4,4)(2,10,4)为端点) ②改变一个现有长方体的颜色(假设此实体句柄为"4C") Dim tobj As object Set tobj=acaddoc.HandletoObject("4C") '通过Handle来获取 实体 tobj.Color=acRed ‘变颜色为红色 tobj.Update ‘更新状态 ③查询当前图形文件中所有实体的实体名、实体句柄、颜色、所在层、线形等参数 Dim ent As Object Dim msgStr, NL As String Dim I as Integer NL = Chr(13) & Chr(10) ‘回车与换行 I=1 For Each ent in mospace '采用迭代遍历模型空间中的实体 msgStr = "第" & Format(I) & "个实体信息" & NL & NL msgStr = msgStr & "实体名: " & ent.EntityName & NL msgStr = msgStr & "所在层: " & ent.Layer & NL msgStr = msgStr & "颜色: " & Str(ent.Color) & NL msgStr = msgStr & "线形: " & ent.Linetype & NL msgStr = msgStr & "句柄: " & ent.Handle & NL MsgBox msgStr I=I+1 Next |