zdm860114 发表于 2009-5-21 17:04:00

好的,我试试,谢谢斑竹!!

zdm860114 发表于 2009-5-21 17:33:00

本帖最后由 作者 于 2009-5-23 20:32:13 编辑 <br /><br /> <p>这个方法还是不行啊,不过给了些思路</p><p>我换了个思路做,只是麻烦多了</p><p>是用open的方法打开,代码多了不少。。。</p><p>很谢谢版主来热心帮助,真的很感谢!</p><p>还有个问题,希望版主能再帮我一次,占用你大量的时间,真的很感激!</p><p>选择集如何移动呢?从一个指定点到另一个指定点</p>

mccad 发表于 2009-5-21 20:01:00

你把VB与AutoCAD连接的代码贴出来看看。

zdm860114 发表于 2009-5-22 16:05:00

mccad发表于2009-5-21 20:01:00static/image/common/back.gif你把VB与AutoCAD连接的代码贴出来看看。

<p><font style="BACKGROUND-COLOR: #f3f3f3;">Option Explicit<br/>Public acadapp As AcadApplication<br/>Public AcadDocs As AcadDocuments<br/>Public MoSpace As AcadModelSpace<br/>Public AcadDoc As AcadDocument<br/>Public MainForm As VB.Form</font></p><p>Public i As Integer</p><p>'连接AutoCAD子函数<br/>Public Sub AutoCAD_Appliaction()<br/>&nbsp;&nbsp;&nbsp; On Error Resume Next<br/>&nbsp;&nbsp;&nbsp; Dim Thisdrawing As AcadDocument<br/>&nbsp;&nbsp;&nbsp; Set acadapp = GetObject(, "AutoCAD.Application.16")<br/>&nbsp;&nbsp;&nbsp; If Err Then<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Err.Clear<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Set acadapp = CreateObject("AutoCAD.Application.16")<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; If Err Then<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; MainForm.Visible = False<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; MsgBox Err.Description<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; MainForm.Visible = True<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Exit Sub<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; End If<br/>&nbsp;&nbsp;&nbsp; Else<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; If acadapp.Documents.Count &gt;= 1 Then<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; For i = acadapp.Documents.Count To 1 Step -1<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Set Thisdrawing = acadapp.Documents.Item(i - 1)<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; If Not acadapp.ActiveDocument.Saved Then<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; If MsgBox("是否要保存以前打开的.dwg文件?", vbYesNo) = vbYes Then<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; acadapp.ActiveDocument.Save<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Else<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; acadapp.ActiveDocument.Close (False)<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; End If<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; End If<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Thisdrawing.Close<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Next<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; End If<br/>&nbsp;&nbsp;&nbsp; End If<br/>&nbsp;&nbsp;&nbsp; Set Thisdrawing = Nothing<br/>&nbsp;&nbsp;&nbsp; acadapp.Visible = True<br/>&nbsp;&nbsp;&nbsp; acadapp.WindowState = acMax<br/>End Sub</p><p>请指教!谢谢了!</p>

mccad 发表于 2009-5-22 21:07:00

<p>看了你的代码,还不太好说:</p><p>1.acadapp本身是AcadApplication对象,就不能用acadapp.Application,而需要直接acadapp.Documents("文件名")。</p><p>2. 新图形 Dim objNewDoc As AcadDocument 下面的那句语句,应该是Set objNewDoc ...。</p><p>3.你写的VB与AutoCAD连接的代码,判断如果ACAD是打开的话,则关闭所有已经打开的文档,我不知道这点有没有影响,因为没看到你中间的其它代码,是不是自己手动打开你需要的文档还是其它。</p><p>4.你的图库是否就是VB程序所在目录的Gallery子目录中,如果不是则文件会找不到。<br/></p>

zdm860114 发表于 2009-5-23 17:01:00

mccad发表于2009-5-22 21:07:00static/image/common/back.gif看了你的代码,还不太好说:1.acadapp本身是AcadApplication对象,就不能用acadapp.Application,而需要直接acadapp.Documents(\"文件名\")。2. 新图形 Dim objNewDoc As AcadDocument 下面的那

<p>恩,先谢谢了! </p><p>1.这个我也试过。呵呵,不过好像还是不行,不过给我指出了到底该怎么写这句话了。 </p><p>2.恩,这个我后来是修改的时候发现了这个错误,眼力真好! </p><p>3.这点主要是为了判断在运行程序前是否有ACAD打开了,以免受影响,可能是想过了头,可以不要这句的 </p><p>4.图库就是建立在VB程序所在目录中的Gallery子目录中,所以我用了APP.PATH </p><p>最后真的很感谢<font color="#ff0000">mccad<font color="#000000">和</font><strong><font face="Verdana">lzh741206</font></strong></font>!还是要感谢这个论坛给我学习二次开发提供了很多帮助 </p><p>很多问题先是经过搜索论坛和百度解决了,是在没有解决的,最终都在版主们和大家的热心帮助下基本上都解决了 </p><p>谢谢了!</p>

zdm860114 发表于 2009-5-23 20:09:00

本帖最后由 作者 于 2009-5-23 20:33:34 编辑 <br /><br /> <p>目前就只缺那个没有实现,还是会提示错误。如果实现了就好。</p><p>全部代码如下:</p><p>'连接AutoCAD<br/>Public Sub AutoCAD_Appliaction()<br/>&nbsp;&nbsp;&nbsp; On Error Resume Next<br/>&nbsp;&nbsp;&nbsp; Dim Thisdrawing As AcadDocument<br/>&nbsp;&nbsp;&nbsp; Set acadapp = GetObject(, "AutoCAD.Application.16")<br/>&nbsp;&nbsp;&nbsp; If Err Then<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Err.Clear<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Set acadapp = CreateObject("AutoCAD.Application.16")<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; If Err Then<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; MainForm.Visible = False<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; MsgBox Err.Description<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; MainForm.Visible = True<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Exit Sub<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; End If<br/>&nbsp;&nbsp;&nbsp; Else<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; If acadapp.Documents.Count &gt;= 1 Then<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; For i = acadapp.Documents.Count To 1 Step -1<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Set Thisdrawing = acadapp.Documents.Item(i - 1)<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; If Not acadapp.ActiveDocument.Saved Then<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; If MsgBox("是否要保存以前打开的.dwg文件?", vbYesNo) = vbYes Then<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; acadapp.ActiveDocument.Save<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Else<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; acadapp.ActiveDocument.Close (False)<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; End If<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; End If<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Thisdrawing.Close<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Next<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; End If<br/>&nbsp;&nbsp;&nbsp; End If<br/>&nbsp;&nbsp;&nbsp; Set Thisdrawing = Nothing<br/>&nbsp;&nbsp;&nbsp; acadapp.Visible = True<br/>&nbsp;&nbsp;&nbsp; acadapp.WindowState = acMax<br/>End Sub</p><p>'打开图库<br/>Public Sub Open_Gallery(Galleryname As String)<br/>On Error Resume Next<br/>Dim file As String<br/>file = App.Path &amp; "\Gallery\" &amp; Galleryname &amp; ".dwg"<br/>&nbsp;&nbsp;&nbsp; If Dir(file) &lt;&gt; "" Then<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; acadapp.Documents.Open file<br/>&nbsp;&nbsp;&nbsp; Else<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; MsgBox ("文件" &amp; Galleryname &amp; "不存在")<br/>&nbsp;&nbsp;&nbsp; End If<br/>End Sub</p><p>'建立选择集并遍历选择集中实体并复制移动实体<br/>Public Sub copy_moveSset(selectname As String, jielength As Double, jienum As Integer, Layername As String)<br/>On Error Resume Next<br/>&nbsp;&nbsp;&nbsp; acadapp.SelectionSets(selectname).Delete<br/>&nbsp;&nbsp;&nbsp; Set ssetobj = acadapp.ActiveDocument.SelectionSets.Add(selectname)<br/>AppActivate acadapp.Caption<br/>'建立选择集,选择图层<br/>Dim fType(0) As Integer<br/>Dim fData(0) As Variant<br/>&nbsp;&nbsp;&nbsp; fType(0) = 8<br/>&nbsp;&nbsp;&nbsp; fData(0) = Layername<br/>Dim FilterType As Variant<br/>Dim FilterData As Variant<br/>&nbsp;&nbsp;&nbsp; FilterType = fType<br/>&nbsp;&nbsp;&nbsp; FilterData = fData<br/>ssetobj.Select acSelectionSetAll, , , fType, fData<br/>Dim entry As AcadEntity<br/>Dim coent As Variant<br/>Dim point1(0 To 2) As Double<br/>Dim point2(0 To 2) As Double<br/>&nbsp;&nbsp;&nbsp; For i = 1 To jienum<br/>&nbsp;&nbsp;&nbsp; point1(0) = 0#<br/>&nbsp;&nbsp;&nbsp; point1(1) = 0#<br/>&nbsp;&nbsp;&nbsp; point1(2) = 0#<br/>&nbsp;&nbsp;&nbsp; point2(0) = point1(0) + (i - 1) * jielength<br/>&nbsp;&nbsp;&nbsp; point2(1) = 0#<br/>&nbsp;&nbsp;&nbsp; point2(2) = 0#<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; For Each entry In ssetobj<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Set coent = entry.Copy<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Call coent.Move(point1, point2)<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Next<br/>&nbsp;&nbsp;&nbsp; Next i<br/>&nbsp;ssetobj.Delete<br/>End Sub</p><p>'返回包含于选择集中每一项目的变体数,参数:一选择集</p><p>Public Function ssArray(ss As AcadSelectionSet)<br/>&nbsp;&nbsp;&nbsp; Dim retVal() As AcadEntity, k As Long<br/>&nbsp;&nbsp;&nbsp; ReDim retVal(0 To ss.Count - 1)<br/>&nbsp;&nbsp;&nbsp; For k = 0 To ss.Count - 1<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Set retVal(k) = ss.Item(k)<br/>&nbsp;&nbsp;&nbsp; Next<br/>&nbsp;&nbsp;&nbsp; ssArray = retVal<br/>End Function</p><p>'建立选择集<br/>Public Function CreateSelectionSet(Optional ByVal SSetName As String) As AcadSelectionSet<br/>&nbsp;&nbsp;&nbsp; On Error Resume Next<br/>&nbsp;&nbsp;&nbsp; '建立选择集<br/>&nbsp;&nbsp;&nbsp; acadapp.ActiveDocument.SelectionSets(SSetName).Delete<br/>&nbsp;&nbsp;&nbsp; Set CreateSelectionSet = acadapp.ActiveDocument.SelectionSets.Add(SSetName)<br/>End Function</p><p>'打开到一张图纸上<br/>Public Sub CopyFromOuterdwg(CurDocname As String, NewDocname As String)<br/>&nbsp;' 打开第一张图<br/>&nbsp;&nbsp;&nbsp;&nbsp; Dim objCurDoc As AcadDocument<br/>&nbsp;&nbsp;&nbsp;&nbsp; Set objCurDoc = acadapp.Documents(App.Path &amp; "\Gallery\" &amp; CurDocname &amp; ".dwg")<br/>' 打开一个新图形<br/>&nbsp;&nbsp;&nbsp;&nbsp; Dim objNewDoc As AcadDocument<br/>&nbsp;&nbsp;&nbsp;&nbsp; Set objNewDoc = acadapp.Documents(App.Path &amp; "\Gallery\" &amp; NewDocname &amp; ".dwg")<br/>&nbsp;&nbsp;&nbsp;&nbsp; Set objNewDoc = acadapp.ActiveDocument<br/>' 将外部图形的实体复制到当前图形<br/>&nbsp;&nbsp;&nbsp;&nbsp; Set ssetobj = CreateSelectionSet<br/>&nbsp;&nbsp;&nbsp;&nbsp; ssetobj.Select acSelectionSetAll<br/>&nbsp;&nbsp;&nbsp;&nbsp; acadapp.ActiveDocument.CopyObjects ssArray(ssetobj), objCurDoc.ModelSpace<br/>&nbsp;&nbsp;&nbsp;&nbsp; objCurDoc.Regen acAllViewports<br/>&nbsp;' 关闭打开的图形<br/>&nbsp;&nbsp;&nbsp;&nbsp; objNewDoc.Close (False)<br/>End Sub</p><p></p><p>Private Sub Command7_Click()</p><p>&nbsp;&nbsp;&nbsp;&nbsp; AutoCAD_Appliaction<br/>&nbsp;&nbsp;&nbsp; Open_Gallery "预热带前段"<br/>&nbsp;&nbsp;&nbsp; copy_moveSset "NEW1", 232, Val(copy_move.yc_1.Text), "0"<br/>&nbsp;&nbsp;&nbsp; Open_Gallery "预热带中段"<br/>&nbsp;&nbsp;&nbsp; copy_moveSset "NEW1", 232, Val(copy_move.yc_2.Text), "0"<br/>&nbsp;&nbsp;&nbsp; Open_Gallery "预热带后段"<br/>&nbsp;&nbsp;&nbsp; copy_moveSset "NEW1", 232, Val(copy_move.yc_3.Text), "0"<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;CopyFromOuter "预热带前段", "预热带中段"<br/>&nbsp;&nbsp; CopyFromOuter "预热带前段", "预热带后段"<br/>End Sub</p>

mccad 发表于 2009-5-23 21:21:00

<p>整个程序看过了,因为没有VB,就没有做调试,第一感觉这句有问题:<br/>CopyFromOuter "预热带前段", "预热带后段"<br/>前面已经有调用过一次CopyFromOuter ,此时"预热带后段"已经被关了,第二次调用肯定就会出错。</p><p>你是通过程序打开图库文件,最好的方法是直接把打开的文件用变量保存起来,下次调用时就直接用该变量来调用,而不要再通过文件名来指定文件,这样对于编程来说会有好处。<br/></p>
页: 1 [2]
查看完整版本: [求助]多文档时遇到的问题,想了多天解决不了,恳请高手们帮忙