05625lcc 发表于 2009-2-24 20:34:00

[求助]vb如何启动cad并用autolisp自动出图

<p>如题,用vb开发图形用户界面,将得到的数据写入文件.txt,另有一lisp程序,读取这个文件</p><p>问题是在vb中运行完后vb如何启动cad并驱动lisp程序完成出图,或者详细点vb如何得到cad的安装路径,成功启动cad后又如何让lisp程序在cad中运行呢?</p>

msdzt 发表于 2009-3-1 11:58:00

<p>我恰好写过类似的程序,见下面的VB代码。其中,cboDrawing为Combox控件,还有一个确定按钮OKButton。用VB生成的文件保存在sFileName中,并使用SetVariable方法保存于系统变量users1中,然后使用SendCommand方法加载lisp文件DwgList.vlx并运行其中的dwg_list函数。这是用VB写的一个自动提取Dwg文件中图签中的标题和图号并生成图纸目录的程序。下面的代码仅是制表部分,供参考。注意在lisp程序中使用(findfile (getvar "users1"))来获得保存的文件名称。</p><p><font style="BACKGROUND-COLOR: #ffffff;">Option Explicit<br/>Dim CadApp As AcadApplication<br/>Dim oDoc As AcadDocument<br/>Dim colFN As New Collection '图形文件的FullName集合<br/>Dim State As AcadState</font></p><p><font style="BACKGROUND-COLOR: #ffffff;">Private Sub cboDrawing_Click()<br/>cboDrawing.ToolTipText = colFN.Item(cboDrawing.ListIndex + 1)<br/>End Sub</font></p><p><font style="BACKGROUND-COLOR: #ffffff;">Private Sub Form_Load()<br/>Dim sMsg As String '错误信息<br/>On Error Resume Next<br/>Set CadApp = GetObject(, "AutoCAD.Application")<br/>If Err Then<br/>&nbsp;&nbsp;&nbsp; sMsg = sMsg &amp; "AutoCAD软件没有运行!请启动AutoCAD软件后继续!" &amp; vbCrLf<br/>Else<br/>&nbsp;&nbsp;&nbsp; Set State = GetAcadState<br/>&nbsp;&nbsp;&nbsp; If State.IsQuiescent = True Then<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cboDrawing.Clear<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; For Each oDoc In CadApp.Documents<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cboDrawing.AddItem oDoc.Name<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; colFN.Add oDoc.FullName<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Next<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; If cboDrawing.ListCount = 0 Then<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; sMsg = sMsg &amp; "AutoCAD中没有打开任何图形文件!" &amp; vbCrLf<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Else<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cboDrawing.Text = CadApp.ActiveDocument.Name<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; End If<br/>&nbsp;&nbsp;&nbsp; Else<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; sMsg = sMsg &amp; "AutoCAD 正忙!请结束AutoCAD窗口中的任何命令后继续!" &amp; vbCrLf<br/>&nbsp;&nbsp;&nbsp; End If<br/>End If<br/>If sMsg &lt;&gt; "" Then<br/>&nbsp;&nbsp;&nbsp; MsgBox "由于存在以下错误而无法进行制表!请检查相关问题后继续!" &amp; vbCrLf &amp; sMsg, vbExclamation<br/>&nbsp;&nbsp;&nbsp; OKButton.Enabled = False<br/>&nbsp;&nbsp;&nbsp; cboDrawing.Enabled = False<br/>End If<br/>End Sub</font></p><p><font style="BACKGROUND-COLOR: #ffffff;">Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)<br/>Set colFN = Nothing<br/>Set oDoc = Nothing<br/>Set CadApp = Nothing<br/>End Sub</font></p><p><font style="BACKGROUND-COLOR: #ffffff;">Private Sub OKButton_Click()<br/>Dim sFN As String<br/>sFN = colFN.Item(cboDrawing.ListIndex + 1)<br/>On Error Resume Next<br/>If cboDrawing.Text &lt;&gt; CadApp.ActiveDocument.Name Or sFN &lt;&gt; CadApp.ActiveDocument.FullName Then<br/>&nbsp;&nbsp;&nbsp; CadApp.Documents.Item(cboDrawing.ListIndex).Activate<br/>End If<br/>Set State = GetAcadState<br/>If State.IsQuiescent = False Then<br/>&nbsp;&nbsp;&nbsp; MsgBox "AutoCAD 正忙!请结束AutoCAD窗口中的任何命令后继续!", vbInformation<br/>&nbsp;&nbsp;&nbsp; OKButton.Caption = "重试(&amp;R)"<br/>&nbsp;&nbsp;&nbsp; CadApp.WindowState = acMax<br/>&nbsp;&nbsp;&nbsp; AppActivate CadApp.Caption<br/>&nbsp;&nbsp;&nbsp; Exit Sub<br/>End If<br/>'开始制表<br/>CadApp.WindowState = acMax<br/>AppActivate CadApp.Caption<br/>Set oDoc = CadApp.ActiveDocument<br/>If Err Then<br/>&nbsp;&nbsp;&nbsp; OKButton.Caption = "重试(&amp;R)"<br/>&nbsp;&nbsp;&nbsp; Exit Sub<br/>End If<br/>oDoc.SetVariable "USERS1", sFileName<br/>oDoc.SendCommand "(Load " &amp; Chr(34) &amp; "Dwglist.vlx" &amp; Chr(34) &amp; ")" &amp; vbCr &amp; "(Dwg_list)" &amp; vbCr<br/>Unload Me<br/>End Sub<br/></font></p>

05625lcc 发表于 2009-3-1 13:46:00

高手,仔细研究一下,十分感谢分享.
页: [1]
查看完整版本: [求助]vb如何启动cad并用autolisp自动出图