明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
楼主: citykunan

[求助]用vb批量处理文件的通用程序

  [复制链接]
 楼主| 发表于 2004-12-8 17:32:00 | 显示全部楼层
咳,我对vb不懂,老板还是要我做真是急死人。还请版主们救救我啊。
发表于 2004-12-8 18:23:00 | 显示全部楼层
  1. 我想我给你贴的代码你没有看吧,我再贴一次好了,你在我指出的地方sendcommand加载你的lisp,请试一下,看行不行。
复制代码
  1. Private Type BrowseInfo
  2.          hWndOwner As Long
  3.          pIDLRoot As Long
  4.          pszDisplayName As Long
  5.          lpszTitle As Long
  6.          ulFlags As Long
  7.          lpfnCallback As Long
  8.          lParam As Long
  9.          iImage As Long
  10. End TypePrivate Const BIF_RETURNONLYFSDIRS = 1
  11. Private Const MAX_PATH = 260
  12. 'Public Declare function GetVersion Lib "Kernel32" () As Integer
  13. Private Declare function GetCommandLine Lib "Kernel32" Alias "GetCommandLineA" () As String
  14. Private Declare Sub CoTaskMemFree Lib "ole32.dll" (ByVal hMem As Long)
  15. Private Declare function lstrcat Lib "Kernel32" Alias "lstrcatA" (ByVal lpString1 As String, ByVal lpString2 As String) As Long
  16. Private Declare function SHBrowseForFolder Lib "shell32" (lpbi As BrowseInfo) As Long
  17. Private Declare function SHGetPathFromIDList Lib "shell32" (ByVal pidList As Long, ByVal lpBuffer As String) As LongPublic Sub qdtubl()
  18. Dim inDir As String
  19. Dim elem
  20. Dim filenom As String
  21. Dim WholeFile As String
  22. Dim newHeight As Double
  23. Dim tufu As String
  24. Dim neirong33 As String
  25. inDir = ""
  26. inDir = ThisDrawing.BrowseForFolder("请选择目录:")
  27. If inDir = "" Then Exit Sub
  28. If Right(inDir, 1) = "" Then inDir = Left(inDir, Len(inDir) - 1)
  29. filenom = Dir$(inDir & "\*.dwg")
  30. 'On Error GoTo errorcontrol
  31. Do While filenom <> ""
  32.       
  33.        WholeFile = inDir & "" & filenom
  34.        ThisDrawing.Application.Documents.Open WholeFile在这里加入你的sendcommand代码
  35.                ThisDrawing.Application.ActiveDocument.Close         filenom = Dir$
  36. Loop
  37. Exit Suberrorcontrol:
  38. MsgBox "错误,程序退出!"
  39. End SubPublic function BrowseForFolder(sPrompt As String) As String
  40. 'Public function BrowseForFolder(hWndOwner As Long, sPrompt As String) As StringDim iNull As Integer
  41. Dim lpIDList As Long
  42. Dim lResult As Long
  43. Dim sPath As String
  44. Dim udtBI As BrowseInfo
  45.        With udtBI
  46.    '           .hWndOwner = hWndOwner
  47.                .lpszTitle = lstrcat(sPrompt, "")
  48.                .ulFlags = BIF_RETURNONLYFSDIRS
  49.        End With       lpIDList = SHBrowseForFolder(udtBI)       If lpIDList Then
  50.                sPath = String$(MAX_PATH, 0)
  51.              lResult = SHGetPathFromIDList(lpIDList, sPath)
  52.                Call CoTaskMemFree(lpIDList)
  53.                iNull = InStr(sPath, vbNullChar)               If iNull Then
  54.                        sPath = Left$(sPath, iNull - 1)
  55.                End If       End If       BrowseForFolder = sPath
  56. End Function
 楼主| 发表于 2004-12-9 10:53:00 | 显示全部楼层
好像不行。我上传了lisp,好像程序不能运行。再帮我看看,谢谢。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?注册

x
发表于 2004-12-9 20:44:00 | 显示全部楼层
可以运行阿,我用你的lisp把cad/sample下的dwg全部打印成了dwf文件了,运行的很好,vba和lisp结合的很好,也没有命令顺序混乱的现象,现在我把所有的代码全都贴一次,你再试试,我这里一点问题都没有(我是2004) 下面这个是你那个lisp,我只修改了filedia的设置和dwf打印机的名称下面这个是vba的代码,你把它复制下来,粘贴在thisdrawing的code区
  1. Private Type BrowseInfo
  2.          hWndOwner As Long
  3.          pIDLRoot As Long
  4.          pszDisplayName As Long
  5.          lpszTitle As Long
  6.          ulFlags As Long
  7.          lpfnCallback As Long
  8.          lParam As Long
  9.          iImage As Long
  10. End TypePrivate Const BIF_RETURNONLYFSDIRS = 1
  11. Private Const MAX_PATH = 260
  12. 'Public Declare function GetVersion Lib "Kernel32" () As Integer
  13. Private Declare Function GetCommandLine Lib "Kernel32" Alias "GetCommandLineA" () As String
  14. Private Declare Sub CoTaskMemFree Lib "ole32.dll" (ByVal hMem As Long)
  15. Private Declare Function lstrcat Lib "Kernel32" Alias "lstrcatA" (ByVal lpString1 As String, ByVal lpString2 As String) As Long
  16. Private Declare Function SHBrowseForFolder Lib "shell32" (lpbi As BrowseInfo) As Long
  17. Private Declare Function SHGetPathFromIDList Lib "shell32" (ByVal pidList As Long, ByVal lpBuffer As String) As LongPublic Sub qdtubl()
  18. Dim inDir As String
  19. Dim elem
  20. Dim filenom As String
  21. Dim WholeFile As String
  22. Dim newHeight As Double
  23. Dim tufu As String
  24. Dim neirong33 As String
  25. inDir = ""
  26. inDir = ThisDrawing.BrowseForFolder("请选择目录:")
  27. If inDir = "" Then Exit Sub
  28. If Right(inDir, 1) = "" Then inDir = Left(inDir, Len(inDir) - 1)
  29. filenom = Dir$(inDir & "\*.dwg")
  30. On Error GoTo errorcontrolDo While filenom <> ""
  31.       
  32.        WholeFile = inDir & "" & filenom
  33.        ThisDrawing.Application.Documents.Open WholeFile'在这里加入你的sendcommand代码
  34. '输出dwf文件(运行plo.lisp文件)ThisDrawing.SendCommand "(load " & Chr(34) & "plo.lsp" & Chr(34) & ")" & vbCr               ThisDrawing.Application.ActiveDocument.Close false       filenom = Dir$Loop
  35. Exit Suberrorcontrol:
  36. MsgBox "错误,程序退出!"
  37. End SubPublic Function BrowseForFolder(sPrompt As String) As String
  38. 'Public function BrowseForFolder(hWndOwner As Long, sPrompt As String) As StringDim iNull As Integer
  39. Dim lpIDList As Long
  40. Dim lResult As Long
  41. Dim sPath As String
  42. Dim udtBI As BrowseInfo
  43.        With udtBI
  44.    '           .hWndOwner = hWndOwner
  45.                .lpszTitle = lstrcat(sPrompt, "")
  46.                .ulFlags = BIF_RETURNONLYFSDIRS
  47.        End With       lpIDList = SHBrowseForFolder(udtBI)       If lpIDList Then
  48.                sPath = String$(MAX_PATH, 0)
  49.              lResult = SHGetPathFromIDList(lpIDList, sPath)
  50.                Call CoTaskMemFree(lpIDList)
  51.                iNull = InStr(sPath, vbNullChar)               If iNull Then
  52.                        sPath = Left$(sPath, iNull - 1)
  53.                End If       End If       BrowseForFolder = sPath
  54. End Function

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?注册

x
发表于 2004-12-9 21:22:00 | 显示全部楼层
刚才又试了几次,还是可能会出现命令混乱的现象,主要表现在cad一张张打开图的时候,我同时点了几下鼠标,就可能出现命令混乱。明主说这是cad的bug,我想那就是很难解决的了,不过对你这个lisp来说,我想如果都改写为vba的话,应该不是很难的事。不过citykunan是作lisp,我想干脆plo.lsp文件就一句话一条命令好了: (command "-plot" "no" "model" "" "DWF6 ePlot.pc3"
(strcat "c:\\lhy_leo" "\\" (vl-filename-base (getvar "dwgname"))) "n" "y") 这样我试了试,点击鼠标试图干扰cad,没什么作用,程序顺利运行。 另外:好像filedia对lisp不起什么作用,上一个帖子我把你注释掉的改成语句是画蛇添足了。
 楼主| 发表于 2004-12-10 11:29:00 | 显示全部楼层
你这是vba,我是想用vb,在cad外做一个*.exe文件,双击此文件,能选择目录,选取dwg 文件,在打开cad,分别加载lisp程序,不知可以否?
发表于 2004-12-10 11:39:00 | 显示全部楼层
可以这样试试:


用Lisp做一个反应器,触发条件为Users1值为“VB-Cad”,Users2的值改变


触发处理就是调用对应路径(Users2的值)的Lisp文件,Lisp文件全部用VL改写


VB端改变Users1、Users2的值
发表于 2004-12-10 13:07:00 | 显示全部楼层
to citykunan其实把vba程序改成vb程序不是很难的。按你的要求,我已经把vba改写成了vb,程序代码如下,窗体上加个按钮(command1),点击按钮就可以打开cad,把选定的目录文件打印成dwf了。别忘记在reference中添加cad的引用。
  1. Option Explicit
  2. Private Type BrowseInfo
  3.          hWndOwner As Long
  4.          pIDLRoot As Long
  5.          pszDisplayName As Long
  6.          lpszTitle As Long
  7.          ulFlags As Long
  8.          lpfnCallback As Long
  9.          lParam As Long
  10.          iImage As Long
  11. End TypePrivate Const BIF_RETURNONLYFSDIRS = 1
  12. Private Const MAX_PATH = 260
  13. 'Public Declare function GetVersion Lib "Kernel32" () As Integer
  14. Private Declare Function GetCommandLine Lib "Kernel32" Alias "GetCommandLineA" () As String
  15. Private Declare Sub CoTaskMemFree Lib "ole32.dll" (ByVal hMem As Long)
  16. Private Declare Function lstrcat Lib "Kernel32" Alias "lstrcatA" (ByVal lpString1 As String, ByVal lpString2 As String) As Long
  17. Private Declare Function SHBrowseForFolder Lib "shell32" (lpbi As BrowseInfo) As Long
  18. Private Declare Function SHGetPathFromIDList Lib "shell32" (ByVal pidList As Long, ByVal lpBuffer As String) As Long
  19. Public Function BrowseForFolder(sPrompt As String) As String
  20. 'Public function BrowseForFolder(hWndOwner As Long, sPrompt As String) As StringDim iNull As Integer
  21. Dim lpIDList As Long
  22. Dim lResult As Long
  23. Dim sPath As String
  24. Dim udtBI As BrowseInfo
  25.        With udtBI
  26.    '           .hWndOwner = hWndOwner
  27.                .lpszTitle = lstrcat(sPrompt, "")
  28.                .ulFlags = BIF_RETURNONLYFSDIRS
  29.        End With       lpIDList = SHBrowseForFolder(udtBI)       If lpIDList Then
  30.                sPath = String$(MAX_PATH, 0)
  31.              lResult = SHGetPathFromIDList(lpIDList, sPath)
  32.                Call CoTaskMemFree(lpIDList)
  33.                iNull = InStr(sPath, vbNullChar)               If iNull Then
  34.                        sPath = Left$(sPath, iNull - 1)
  35.                End If       End If       BrowseForFolder = sPath
  36. End FunctionPrivate Sub Command1_Click()
  37. Dim inDir As String
  38. Dim elem
  39. Dim filenom As String
  40. Dim WholeFile As String
  41. Dim newHeight As Double
  42. Dim tufu As String
  43. Dim neirong33 As String
  44. inDir = ""
  45. inDir = BrowseForFolder("请选择目录:")
  46. If inDir = "" Then Exit Sub
  47. If Right(inDir, 1) = "" Then inDir = Left(inDir, Len(inDir) - 1)
  48. filenom = Dir$(inDir & "\*.dwg")Dim acadApp As AcadApplication
  49. Set acadApp = CreateObject("AutoCAD.Application")On Error GoTo errorcontrol
  50. Do While filenom <> ""
  51.       
  52.        WholeFile = inDir & "" & filenom
  53.        acadApp.Documents.Open WholeFile'在这里加入你的sendcommand代码
  54. '输出dwf文件(运行plo.lisp文件)
  55. acadApp.ActiveDocument.SendCommand "(load " & Chr(34) & "plo.lsp" & Chr(34) & ")" & vbCr
  56. acadApp.ActiveDocument.Close False
  57.        filenom = Dir$Loop
  58. acadApp.Quit
  59. Exit Suberrorcontrol:
  60. MsgBox "错误,程序退出!"
  61. End Sub  
 楼主| 发表于 2004-12-10 17:05:00 | 显示全部楼层
谢谢各位热心人,可以了。但是如果dwg文件很大的话,是不是会出现混乱的现象?不知道有没有办法解决。再次感谢。
 楼主| 发表于 2004-12-16 14:12:00 | 显示全部楼层
在前一楼的程序中acadApp.ActiveDocument.SendCommand "(load " &amp; Chr(34) &amp; "plo.lsp" &amp; Chr(34) &amp; ")"         中要将*.lsp的路径写上,有没有办法,让vb自己找到自身的*.exe的所在目录,自动加载同一目录下的*.lsp程序呢?(*.lsp程序总与*.exe程序在同一目录),这样程序复制到不同的目录就不要改程序了。谢谢。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|CAD论坛|CAD教程|CAD下载|联系我们|关于明经|明经通道 ( 粤ICP备05003914号 )  
©2000-2023 明经通道 版权所有 本站代码,在未取得本站及作者授权的情况下,不得用于商业用途

GMT+8, 2024-11-26 07:45 , Processed in 0.184964 second(s), 17 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表