明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 2691|回复: 0

VB6.0使用ObjectDBX实现批量文字替换,帮忙修改一下

[复制链接]
发表于 2009-5-22 20:58:00 | 显示全部楼层 |阅读模式

Option Explicit

Dim objDBX As Object

Private Sub Form_Activate()
If Left(Version, 2) = "15" Then
    Set objDBX = CreateObject("ObjectDBX.AxDbDocument.1")
End If
End Sub

Private Sub Command1_Click()

If ListView1.ListItems.Count = 0 Then
  MsgBox "请先选择图纸!"
 Exit Sub
 
Else
    Dim adText As AcadText
    Dim adMText As AcadMText
    Dim adSS As AcadSelectionSet
    Dim fType(0 To 1) As Integer, fData(0 To 1)
    Dim i As Integer

    If txtfind.Text = "" Or txtreplace.Text = "" Then
        MsgBox "输入所要替换的字符串内容!"
        Exit Sub
    End If

    Dim strFind As String
    Dim strReplace As String
    strFind = txtfind.Text
    strReplace = txtreplace.Text

    ' 打开图形进行操作
For i = 1 To Form1.ListView1.ListItems.Count + 1
Call ReplaceTextInDwg(Form1.ListView1.ListItems(i).SubItems(1) & "\" & ListView1.ListItems.Item(i), strFind,strReplace)
    Next i
End If
        MsgBox "OK!  ^_^"
End Sub

' 对某个图形进行文字替换
Private Sub ReplaceTextInDwg(ByVal strDwgName As String, ByVal strFind As String, _
                            ByVal strReplace As String)
    ' 打开指定的图形
objDBX.Open strDwgName

    Dim ent As AcadEntity
    For Each ent In objDBX.ModelSpace
        If TypeOf ent Is AcadText Or TypeOf ent Is AcadMText Then
            With ent
                If InStr(.TextString, strFind) Then .TextString = ReplaceStr(.TextString, strFind, strReplace, False)
            End With
        End If
    Next ent
   
    objDBX.SaveAs strDwgName
End Sub

' 对字符串中指定的字符进行替换
Public Function ReplaceStr(ByVal searchStr As String, ByVal oldStr As String, _
        ByVal newStr As String, ByVal firstOnly As Boolean) As String
    '对错误操作的处理
    If searchStr = "" Then Exit Function
    If oldStr = "" Then Exit Function

    ReplaceStr = ""
    Dim i As Integer, oldStrLen As Integer, holdStr As String, StrLoc As Integer
   
    '计算原来字符串的长度
    oldStrLen = Len(oldStr)
    StrLoc = InStr(searchStr, oldStr)
   
    While StrLoc > 0
        '获得图形中文字对象位于查找字符串之前的字符串
        holdStr = holdStr & Left(searchStr, StrLoc - 1) & newStr
        '获得文字对象位于查找字符串之后的字符串
        searchStr = Mid(searchStr, StrLoc + oldStrLen)
        StrLoc = InStr(searchStr, oldStr)
        If firstOnly Then ReplaceStr = holdStr & searchStr: Exit Function
    Wend
   
    ReplaceStr = holdStr & searchStr
End Function

' 列表框中是否存在指定名称的项目
Private Function HasItem(ByVal strDwgName As String) As Boolean
    HasItem = False
   
    Dim i As Integer
    For i = 1 To Form1.ListView1.ListItems.Count + 1
        If StrComp(Form1.ListView1.ListItems(i).SubItems(1) & "\" & ListView1.ListItems.Item(i), strDwgName, vbTextCompare) = 0 Then
            HasItem = True
            Exit Function
        End If
    Next i
End Function

您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2024-11-29 04:38 , Processed in 0.173164 second(s), 24 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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