hchm 发表于 2008-3-29 17:12:00

请问怎样修改此段程序?VB中做CAD外部引用

<p>我按照国防工业出版社的《AUTOCAD ACTIVEX/VBA二次开发技术基础及应用实例》的VB编一个例子:外部引用</p><p>但是一编译就是出错,请看看怎么回事?</p><p>Private Sub Command3_Click()<br/>&nbsp;&nbsp;&nbsp; Dim xrefInserted As AcadExternalReference<br/>&nbsp;&nbsp;&nbsp; Dim insertionPnt(0 To 2) As Double<br/>&nbsp;&nbsp;&nbsp; Dim PathName As String<br/>&nbsp;&nbsp;&nbsp; insertionPnt(0) = 10: insertionPnt(1) = 100: insertionPnt(2) = 0<br/>&nbsp;&nbsp;&nbsp; PathName = "D:\Personal\My Documents\My eBooks\border60.dwg"<br/>&nbsp;&nbsp;<font color="#ff0000">&nbsp; Set xrefInserted = ThisDrawing.ModelSpace.AttachExternalReference(PathName, "WXREF", insertionPnt, 1, 1, 1, 0, False)<br/></font>End Sub</p><p>一编译红色区域就讲出错,望高手看看。</p>

azjmjsj 发表于 2008-3-30 19:45:00

<p>给你完整代码<br/>Private Sub Command1_Click()<br/>'AutoCAD与visual basic连接<br/>On Error Resume Next<br/>Set obj_Acad = GetObject(, "autocad.application")<br/>If Err Then<br/>&nbsp;&nbsp; Err.Clear<br/>&nbsp;&nbsp; On Error Resume Next<br/>&nbsp;&nbsp; Set obj_Acad = CreateObject("autocad.application")<br/>&nbsp;&nbsp; If Err Then<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Err.Clear<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; MsgBox "不能运行AutoCAD,请检查是否安装!", vbOKCancel, "警告!"<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Exit Sub<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; End If<br/>End If<br/>obj_Acad.Visible = True<br/>Set obj_Doc = obj_Acad.ActiveDocument<br/>Set obj_ModelSpace = obj_Doc.ModelSpace</p><p><br/>&nbsp;&nbsp;&nbsp; Dim xrefInserted As Object<br/>&nbsp;&nbsp;&nbsp; Dim insertionPnt(0 To 2) As Double<br/>&nbsp;&nbsp;&nbsp; Dim PathName As String<br/>&nbsp;&nbsp;&nbsp; Dim tempBlock As Object<br/>&nbsp;&nbsp;&nbsp; Dim msg As String<br/>&nbsp;&nbsp;&nbsp; insertionPnt(0) = 10: insertionPnt(1) = 100: insertionPnt(2) = 0<br/>&nbsp;&nbsp;&nbsp; PathName = "D:\Personal\My Documents\My eBooks\border60.dwg"<br/>&nbsp;&nbsp;&nbsp; Set xrefInserted = obj_ModelSpace.AttachExternalReference(PathName, "WXREF", insertionPnt, 1, 1, 1, 0, False)<br/>&nbsp;&nbsp;&nbsp; GoSub ListBlocks<br/>&nbsp;&nbsp;&nbsp; Exit Sub</p><p>ListBlocks:<br/>&nbsp;&nbsp;&nbsp; msg = vbCrLf&nbsp;<br/>&nbsp;&nbsp;&nbsp; For Each tempBlock In obj_Doc.Blocks<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; msg = msg &amp; tempBlock.Name &amp; vbCrLf&nbsp;&nbsp;&nbsp;&nbsp;<br/>&nbsp;&nbsp;&nbsp; Next<br/>&nbsp;&nbsp;&nbsp; MsgBox "图中包含的块有: " &amp; msg<br/>&nbsp;&nbsp;&nbsp; Return<br/>End Sub</p><p></p><p></p><p>*************************************************************</p><p>西北凡人-----http://www.abofanyi.com/blog</p>

liusong0517 发表于 2008-4-1 22:27:00

不错,谢谢

illuminiti 发表于 2011-12-7 17:39:08

azjmjsj 发表于 2008-3-30 19:45 static/image/common/back.gif
给你完整代码Private Sub Command1_Click()'AutoCAD与visual basic连接On Error Resume NextSet obj_Acad = ...

调试了下怎么不行?其它人可以吗?
xrefInserted的返回值是nothing图片没显示出来
望指教 谢谢

geabus 发表于 2011-12-8 10:13:35

在VB中调用CAD,ThisDrawing.不可用,应该用acitvedocument

geabus 发表于 2011-12-8 10:14:45

完整代码应该没问题啊

illuminiti 发表于 2011-12-8 15:45:20

geabus 发表于 2011-12-8 10:14 static/image/common/back.gif
完整代码应该没问题啊

是没用ThisDrawing   可是我调试了很多次了
AttachExternalReference这句总是不成功,返回错误:访问额文件失败 它的对象值是nothing
这跟版本有关系吗?用的是autocad2007
你调试通过了?
页: [1]
查看完整版本: 请问怎样修改此段程序?VB中做CAD外部引用