批量删除CAD中块(block)(vba实现,上百个CAD文件一键完成)

         当我们需要删除大量cad文件中的块时,一个一个打开删除关闭,费时又费力。此代码实现了一键删除功能,只需把所有dwg文件放入同一个文件夹,打开cad,alt+F11打开vba开发环境,复制代码,F5运行。(或加载此插件命令栏输入: appload  ,然后加载此dvb文件,输入 vbarun 运行宏即可)。

代码如下:

'2024年3月5日09:26:00 @qq443440204 使用前把首行和末行的print删除,运行即可
#If VBA7 Then
Private Declare PtrSafe Function ts_apiGetOpenFileName Lib "comdlg32.dll" Alias "GetOpenFileNameA" (tsFN As tsFileName) As Boolean
Private Declare PtrSafe Function ts_apiGetSaveFileName Lib "comdlg32.dll" Alias "GetSaveFileNameA" (tsFN As tsFileName) As Boolean
Private Declare PtrSafe Function CommDlgExtendedError Lib "comdlg32.dll" () As Long
Private Declare PtrSafe Function SHGetPathFromIDList Lib "shell32.dll" Alias _
"SHGetPathFromIDListA" (ByVal pidl As LongPtr, ByVal pszPath As String) As LongPtr
Private Declare PtrSafe Function SHBrowseForFolder Lib "shell32.dll" Alias _
"SHBrowseForFolderA" (lpBrowseInfo As BROWSEINFO) As LongPtr
Private Const BIF_RETURNONLYFSDIRS = &H1
Private Type BROWSEINFO
            hOwner As LongPtr
            pidlRoot As LongPtr
            pszDisplayName As String
            lpszTitle As String
            ulFlags As LongPtr
            lpfn As LongPtr
            lParam As LongPtr
            iImage As LongPtr
End Type
Private Type tsFileName
   lStructSize As Long
   hwndOwner As LongPtr
   hInstance As LongPtr
   strFilter As String
   strCustomFilter As String
   nMaxCustFilter As Long
   nFilterIndex As Long
   strFile As String
   nMaxFile As Long
   strFileTitle As String
   nMaxFileTitle As Long
   strInitialDir As String
   strTitle As String
   flags As Long
   nFileOffset As Integer
   nFileExtension As Integer
   strDefExt As String
   lCustData As Long
   lpfnHook As LongPtr
   lpTemplateName As String
End Type
 
' Flag Constants
Private Const tscFNAllowMultiSelect = &H200
Private Const tscFNCreatePrompt = &H2000
Private Const tscFNExplorer = &H80000
Private Const tscFNExtensionDifferent = &H400
Private Const tscFNFileMustExist = &H1000
Private Const tscFNPathMustExist = &H800
Private Const tscFNNoValidate = &H100
Private Const tscFNHelpButton = &H10
Private Const tscFNHideReadOnly = &H4
Private Const tscFNLongNames = &H200000
Private Const tscFNNoLongNames = &H40000
Private Const tscFNNoChangeDir = &H8
Private Const tscFNReadOnly = &H1
Private Const tscFNOverwritePrompt = &H2
Private Const tscFNShareAware = &H4000
Private Const tscFNNoReadOnlyReturn = &H8000
Private Const tscFNNoDereferenceLinks = &H100000
 
Public Function GOFN( _
    Optional ByRef rlngflags As Long = 0&, _
    Optional ByVal strInitialDir As String = "", _
    Optional ByVal strFilter As String = "dwg文件 (*.dwg)" & vbNullChar & "*.dwg" _
    & vbNullChar & "All Files (*.*)" & vbNullChar & "*.*", _
    Optional ByVal lngFilterIndex As Long = 1, _
    Optional ByVal strDefaultExt As String = "", _
    Optional ByVal strFileName As String = "", _
    Optional ByVal strDialogTitle As String = "", _
    Optional ByVal fOpenFile As Boolean = True) As Variant
'On Error GoTo GOFN_Err
    
    Dim tsFN As tsFileName
    Dim strFileTitle As String
    Dim fResult As Boolean
 
    ' Allocate string space for the returned strings.
    strFileName = Left(strFileName & String(256, 0), 256)
    strFileTitle = String(256, 0)
 
    ' Set up the data structure before you call the function
    With tsFN
        .lStructSize = LenB(tsFN)
        '.hwndOwner = Application.hWndAccessApp
        .strFilter = strFilter
        .nFilterIndex = lngFilterIndex
        .strFile = strFileName
        .nMaxFile = Len(strFileName)
        .strFileTitle = strFileTitle
        .nMaxFileTitle = Len(strFileTitle)
        .strTitle = strDialogTitle
        .flags = rlngflags
        .strDefExt = strDefaultExt
        .strInitialDir = strInitialDir
        .hInstance = 0
        .strCustomFilter = String(255, 0)
        .nMaxCustFilter = 255
        .lpfnHook = 0
    End With
   
    ' Call the function in the windows API
   
        fResult = ts_apiGetOpenFileName(tsFN)
    If fResult Then
        rlngflags = tsFN.flags
        GOFN = tsTrimNull(tsFN.strFile)
    Else
        GOFN = Null
        MsgBox "您未选择"
        End
    End If
 
End Function
Public Function GSFN( _
    Optional ByRef rlngflags As Long = 0&, _
    Optional ByVal strInitialDir As String = "", _
    Optional ByVal strFilter As String = "dwg文件 (*.dwg)" & vbNullChar & "*.dwg" _
    & vbNullChar & "All Files (*.*)" & vbNullChar & "*.*", _
    Optional ByVal lngFilterIndex As Long = 1, _
    Optional ByVal strDefaultExt As String = "", _
    Optional ByVal strFileName As String = "", _
    Optional ByVal strDialogTitle As String = "", _
    Optional ByVal fOpenFile As Boolean = False) As Variant
'On Error GoTo tsGetFileFromUser_Err
    
    Dim tsFN As tsFileName
    Dim strFileTitle As String
    Dim fResult As Boolean
 
    ' Allocate string space for the returned strings.
    strFileName = Left(strFileName & String(256, 0), 256)
    strFileTitle = String(256, 0)
 
    ' Set up the data structure before you call the function
    With tsFN
        .lStructSize = LenB(tsFN)
        '.hwndOwner = Application.hWndAccessApp
        .strFilter = strFilter
        .nFilterIndex = lngFilterIndex
        .strFile = strFileName
        .nMaxFile = Len(strFileName)
        .strFileTitle = strFileTitle
        .nMaxFileTitle = Len(strFileTitle)
        .strTitle = strDialogTitle
        .flags = rlngflags
        .strDefExt = strDefaultExt
        .strInitialDir = strInitialDir
        .hInstance = 0
        .strCustomFilter = String(255, 0)
        .nMaxCustFilter = 255
        .lpfnHook = 0
    End With
   
        fResult = ts_apiGetSaveFileName(tsFN)
    If fResult Then
        rlngflags = tsFN.flags
        GSFN = tsTrimNull(tsFN.strFile)
    Else
        GSFN = Null
        MsgBox "您未保存"
        End
    End If
 
End Function
 
' Trim Nulls from a string returned by an API call.
Private Function tsTrimNull(ByVal strItem As String) As String
On Error GoTo tsTrimNull_Err
    Dim I As Integer
   
    I = InStr(strItem, vbNullChar)
    If I > 0 Then
        tsTrimNull = Left(strItem, I - 1)
    Else
        tsTrimNull = strItem
    End If
    
tsTrimNull_End:
    On Error GoTo 0
    Exit Function
 
 
tsTrimNull_Err:
    Beep
    MsgBox Err.Description, , "Error: " & Err.Number _
    & " in function basBrowseFiles.tsTrimNull"
    Resume tsTrimNull_End
 
End Function

Public Function GOFOLDER() As String
On Error GoTo Err_GOFOLDER
    Dim x As LongPtr, bi As BROWSEINFO, dwIList As LongPtr
    Dim szPath As String, wPos As Integer
   
    With bi
        '.hOwner = hWndAccessApp
        .lpszTitle = "请选择文件夹"
        .ulFlags = BIF_RETURNONLYFSDIRS
    End With
   
    dwIList = SHBrowseForFolder(bi)
    szPath = Space$(512)
    x = SHGetPathFromIDList(ByVal dwIList, ByVal szPath)
   
    If x Then
        wPos = InStr(szPath, Chr(0))
        GOFOLDER = Left$(szPath, wPos - 1)
    Else
        GOFOLDER = ""
        MsgBox "您未选择"
        End
    End If
Exit_GOFOLDER:
    Exit Function
Err_GOFOLDER:
    MsgBox Err.Number & " - " & Err.Description
    Resume Exit_GOFOLDER
End Function
#Else
Declare Function GetOpenFileName Lib "comdlg32.dll" Alias "GetOpenFileNameA" (lpofn As OPENFILENAME) As Long
Declare Function GetSaveFileName Lib "comdlg32.dll" Alias "GetSaveFileNameA" (lpofn As OPENFILENAME) As Long
Declare Function SHBrowseForFolder Lib "SHELL32.DLL" (lpBrowseInfo As BROWSEINFO) As Long
Declare Function SHGetPathFromIDList Lib "SHELL32.DLL" (ByVal pidl As Long, ByVal pszPath As String) As Long
Public choice As String
Type OPENFILENAME
     lStructSize As Long
     hwndOwner As Long
     hInstance As Long
     lpstrFilter As String
     lpstrCustomFilter As String
     nMaxCustFilter As Long
     nFilterIndex As Long
     lpstrFile As String
     nMaxFile As Long
     lpstrFileTitle As String
     nMaxFileTitle As Long
     lpstrInitialDir As String
     lpstrTitle As String
     flags As Long
     nFileOffset As Integer
     nFileExtension As Integer
     lpstrDefExt As String
     lCustData As Long
     lpfnHook As Long
     lpTemplateName As String
End Type
Public Type BROWSEINFO
    hOwner As Long
    pidlRoot As Long
    pszDisplayName As String
    lpszTitle As String
    ulFlags As Long
    lpfn As Long
    lParam As Long
    iImage As Long
End Type

Function GOFOLDER(Optional message) As String
Dim bInfo As BROWSEINFO
Dim path As String
Dim r As Long, x As Long, pos As Integer
bInfo.pidlRoot = 0
bInfo.lpszTitle = ""
bInfo.ulFlags = &H1
x = SHBrowseForFolder(bInfo)
path = Space$(256)
r = SHGetPathFromIDList(ByVal x, ByVal path)
If r Then
    pos = InStr(path, Chr(0))
    GOFOLDER = Left(path, pos - 1)
Else
    GOFOLDER = ""
    MsgBox "您未选择"
    End
End If
End Function
Function GOFN() As String
    Dim sOFN As OPENFILENAME
    With sOFN
        .lStructSize = Len(sOFN)
        
       .lpstrFilter = "dwg文件(*.dwg)" & Chr(0) & "*.dwg" & Chr(0) & "dxf文件(*.dxf)" & Chr(0) & "*.dxf" & Chr(0) & "Excel文件(*.xl*)" & Chr(0) & "*.xl*" & Chr(0) & "Word文件(*.do*)" _
        & Chr(0) & "*.do*" & Chr(0) & "PPT文件(*.pp*)" & Chr(0) & "*.pp*" & Chr(0) & "所有文件(*.*)" & Chr(0) & "*.*" _
        & Chr(0) & Chr(0)
        .lpstrFile = Space(1024)
        .nMaxFile = 1025
    End With
    Dim sFileName As String

    If GetOpenFileName(sOFN) <> 0 Then
        With sOFN
            sFileName = Trim(.lpstrFile)
            GOFN = Left(sFileName, Len(sFileName) - 1)
        End With
    Else
        GOFN = ""
          MsgBox "您已取消,请重新选择"
        End
    End If
End Function
Function GSFN() As String
    Dim sSFN As OPENFILENAME
    With sSFN
        .lStructSize = Len(sSFN)
        '设置保存文件对话框中的文件筛选字符串对
       .lpstrFilter = "dwg文件(*.dwg)" & Chr(0) & "*.dwg" & Chr(0) & "dxf文件(*.dxf)" & Chr(0) & "*.dxf" & Chr(0) & "Excel文件(*.xl*)" & Chr(0) & "*.xl*" & Chr(0) & "Word文件(*.do*)" _
        & Chr(0) & "*.do*" & Chr(0) & "PPT文件(*.pp*)" & Chr(0) & "*.pp*" & Chr(0) & "所有文件(*.*)" & Chr(0) & "*.*" _
        & Chr(0) & Chr(0)
        '设置文件完整路径和文件名的缓冲区
        .lpstrFile = Space(1024)
        '设置文件完整路径和文件名的最大字符数,一定要比lpstrFile参数指定的字符数多1,用于存储结尾Null字符
        .nMaxFile = 1025
    End With
     
    Dim sFileName As String
    If GetSaveFileName(sSFN) <> 0 Then
        With sSFN
            sFileName = Trim(.lpstrFile)
            GSFN = Left(sFileName, Len(sFileName) - 1)
        End With
    Else
        GSFN = ""
        MsgBox "您已取消,请重新选择"
        End
       
    End If
'    Debug.Print GSFN, Len(GSFN)

End Function
#End If


Sub 批量删除块block()
Dim folderPath As String
Dim path As String
Dim counter As Integer
counter = 0 '计数器,记录替换的文字数量
Dim fileName As String
Dim acadDoc As AcadDocument
folderPath = GOFOLDER
fileName = Dir(folderPath & "\*.dwg")  '获取文件夹中的DWG文件
Dim ent As AcadEntity
Do While fileName <> ""
    Set acadDoc = Documents.Open(folderPath & "\" & fileName)
    For Each ent In ThisDrawing.ModelSpace
    If ent.ObjectName = "AcDbBlockReference" Then
        ent.Delete
        counter = counter + 1
    End If
    Next ent
    ThisDrawing.Close
    fileName = Dir()
Loop
MsgBox "共删除了 " & counter & " 个块 " & Space(20) & vbCr & "qq:443440204", , "版权所有qq:443440204"
End Sub



由下图可知,共删除了文件夹内cad文件3个块。

  • 19
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 批量合并文件夹里的CAD文件到当前工作图纸涉及到VBA编程技巧。下面是一个300字的VBA程序示例代码: ```vba Sub 合并CAD文件() Dim 文件夹路径 As String 文件夹路径 = "C:\要合并的文件夹路径\" Dim 文件名 As String 文件名 = Dir(文件夹路径 & "*.dwg") Do While 文件名 <> "" Dim CAD文件 As Object Set CAD文件 = Documents.Open(文件夹路径 & 文件名) ' 将CAD文件中的所有图纸复制到当前工作图纸中 Dim 图纸 As Object For Each 图纸 In CAD文件.ModelSpace 图纸.Copy ThisDrawing.Paste Next 图纸 ' 关闭并保存CAD文件 CAD文件.Close True ' 继续下一个文件 文件名 = Dir Loop ' 清除剪贴板 Application.CutCopyMode = False ' 提示合并完成 MsgBox "所有CAD文件已成功合并到当前工作图纸。" End Sub ``` 请替换 `"C:\要合并的文件夹路径\"` 为你要合并的文件夹路径。接着,将此代码复制到CADVBA编辑器中的一个新模块中。运行该代码时,它会遍历指定文件夹中的所有DWG文件,并将其中的所有图纸都复制到当前工作图纸。最后,它会关闭并保存每个CAD文件,并提示合并完成。 请注意,该程序仅适用于AutoCAD软件,并且需要在AutoCAD中执行。 ### 回答2: 编写一个 VBA 程序,实现批量合并文件夹里的 CAD 文件到当前工作图纸的功能。具体步骤如下: 1. 首先,需要在 VBA 编辑器中引用 AutoCAD 的库文件,以便于操作 CAD 文件。在 VBA 编辑器的“工具”菜单下选择“引用”,然后勾选 AutoCAD文件。 2. 接下来,需要为程序添加一个按钮或快捷键,便于运行程序。在 AutoCAD 的命令窗口中输入“cui”,打开用户界面编辑器。选择“自定义命令”页签,在“原型”列表中选择“命令”,然后点击“新建命令”按钮。在“大纲”窗口中选择“图标”子级,然后点击“新建按钮”按钮。在右侧面板中,可以设置按钮的名称、图标等属性。设置好后,点击确定保存设置。 3. 在 VBA 编辑器中,编写一个宏程序用于合并文件夹里的 CAD 文件到当前工作图纸。具体步骤如下: - 首先,获取目标文件夹的路径,可以使用 `Application.FileDialog` 对话框选择文件夹。 - 然后,获取当前工作图纸的模型空间或布局空间对象,可以使用 `ThisDrawing.ModelSpace` 或 `ThisDrawing.ActiveLayout` 属性。 - 遍历目标文件夹中的所有文件,可以使用 `FileSystemObject` 对象的 `GetFolder` 和 `Files` 属性,以及 `For Each` 语句。 - 对于每个 CAD 文件,可以使用 `ThisDrawing.ModelSpace.InsertBlock` 或 `ThisDrawing.ActiveLayout.InsertBlock` 方法将其插入到当前工作图纸中。可以使用文件的路径和文件名作为参数。 - 最后,保存并关闭文件夹中的所有 CAD 文件。 4. 修改 AutoCAD 的配置,使得程序在启动时自动加载 VBA 程序。在 AutoCAD 的命令窗口中输入“vbaide”,打开 VBA 编辑器。选择“工具”菜单中的“选项”,然后在“启动”页签中,确保勾选“以注释加载项目”选项,然后单击“确定”按钮保存设置。 通过以上步骤,我们可以编写一个 VBA 程序,实现批量合并文件夹里的 CAD 文件到当前工作图纸的功能。此程序能够提高工作效率,简化操作步骤,同时可以根据实际需求进行进一步的优化和扩展。 ### 回答3: 批量合并文件夹里CAD文件到当前工作图纸可以使用VBA程序来实现。下面是一个大致的思路: 1. 首先,创建一个VBA程序,打开CAD软件并激活其中一个图纸。 2. 然后,通过对话框或者直接指定文件夹路径,获取需要合并的CAD文件所在的文件夹路径。 3. 使用文件夹路径来列出该文件夹下的所有CAD文件。 4. 遍历每个CAD文件,打开它们,并将其内容复制到当前工作图纸中。 5. 完成所有文件的复制后,保存当前工作图纸。 6. 如果还有其他需要合并的文件夹,重复上述步骤。 下面是一个简单的示例代码: ```vba Sub MergeCADFiles() Dim FolderPath As String Dim FileName As String Dim CADFile As AcadDocument ' 获取文件夹路径 FolderPath = "C:\CADFiles" ' 可以根据需要自行修改 ' 列出文件夹中的所有CAD文件 FileName = Dir(FolderPath & "\*.dwg") ' 遍历每个CAD文件 Do While FileName <> "" ' 打开CAD文件 Set CADFile = Documents.Open(FolderPath & "\" & FileName) ' 复制CAD文件内容到当前工作图纸中 ThisDrawing.CopyObjects CADFile.ModelSpace, ThisDrawing.ModelSpace ' 关闭CAD文件,不保存修改 CADFile.Close False ' 查找下一个CAD文件 FileName = Dir Loop ' 保存当前工作图纸 ThisDrawing.Save End Sub ``` 需要注意的是,以上代码是基于CAD软件自带的VBA功能,具体代码可能需要根据CAD软件的版本和接口进行调整。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值