- UID
- 22595
- 积分
- 118
- 精华
- 贡献
-
- 威望
-
- 活跃度
-
- D豆
-
- 在线时间
- 小时
- 注册时间
- 2002-12-29
- 最后登录
- 1970-1-1
|
楼主 |
发表于 2014-9-5 15:46:13
|
显示全部楼层
这是我之前做一个工程过程中,用VBA代码凑的一个页码程序,边做边用,出错就修改下源代码,所以,代码不优化,没有容错能力,只是为了暂时完成功能!
Sub P_Num()
'Const B_Name = "中铁A3图框"
Const DrawingName = "图名"
Dim Collection_Block_old As Collection
Dim Collection_Block_srt As Collection
'--------------------------------------------------------------------------------------------------
'写页码
Set Collection_Block_old = SertchBlockRef() '查找指定块,返回指定块的所有属性块集合(Collection)
Set Collection_Block_srt = Sort_Collection(Collection_Block_old) '把返回的属性块数组按升序排序再返回
' On Error GoTo A
For i = Collection_Block_srt.Count To 1 Step -1 '在排序后的集合里面查找当前图形的最大页码(Curent_Pags)
If Collection_Block_srt.Item(i)(4) = "页码" Then
Curent_Pags = Collection_Block_srt.Item(i)(5)
Exit For
End If
Next i
'-------------------------------------------------------------------
'代入当前最大页码,取回到当前图形的累计页码总数,并将结果写入Text文件
Prev_Pags = Get_PrevPags(Curent_Pags)
'-------------------------------------------------------------------
Call Set_Block_Str(Collection_Block_srt, Prev_Pags) '上一个的图纸数量+1即为此图开始的页码
MsgBox ("页码已更新")
Exit Sub
'A: MsgBox ("不是本工程,无法工作!")
End Sub
'按块名查找,直到找完,带回所有属性块属性的数组
Function SertchBlockRef()
Const BlockName = "中铁A3图框"
Dim EntObj As AcadEntity
Dim ins_Point()
Dim attVars As Variant
Dim arr_Pags(0 To 5)
Dim Num_Page As Integer
Dim blkRefObj As AcadBlockReference
Dim rtnCol As New Collection
On Error GoTo ErrTrap
Num_Page = 0
For Each EntObj In ThisDrawing.PaperSpace
If StrComp(EntObj.ObjectName, "AcDbBlockReference", vbTextCompare) = 0 Then
If StrComp(EntObj.Name, BlockName, vbTextCompare) = 0 Then
Set blkRefObj = EntObj
attVars = blkRefObj.GetAttributes
For k = 0 To UBound(attVars)
'--------------------------------------
'定义数组
' 0,Handle,1:x,2;y,3:z,4:块属性值,5:完整路径文件名
'ReDim Preserve arr_Pags(0 To 5, Num_Page)
'--------------------------------------
'取属性块的坐标属性循环赋给数组(1 to 3)
'把取得的坐标x,y,x赋值给数组
tmp = attVars(k).InsertionPoint '块插入点,ins_Point是数组,0:x,1:y,2:z
For j = 1 To UBound(tmp) + 1
arr_Pags(j) = tmp(j - 1)
Next j
'坐标赋值结束
'---------------------------------------
'--------------------------------------
'下面赋值 (句柄),(块属性值),(完整文件名)
arr_Pags(0) = blkRefObj.Handle '句柄
'arr_Pags(j) = attVars(k).TextString '块属性值
arr_Pags(j) = attVars(k).TagString '块属性值
'取属性结束
'--------------------------------------
If arr_Pags(j) = "页码" Then
Num_Page = Num_Page + 1
arr_Pags(j + 1) = Num_Page
Else
arr_Pags(j + 1) = ""
End If
rtnCol.Add arr_Pags 'Num_Page = Num_Page + 1
Next k
End If
End If
Next
Set SertchBlockRef = rtnCol
Set EntObj = Nothing
Set rtnCol = Nothing
Exit Function
ErrTrap:
If Not (EntObj Is Nothing) Then Set EntObj = Nothing
On Error GoTo 0
End Function
Function Sort_Collection(Collection_Block)
Dim Collection_temp As Collection
For i = 1 To Collection_Block.Count
For j = i + 1 To Collection_Block.Count
If Collection_Block.Item(j)(1) < Collection_Block.Item(i)(1) Then
Temp = Collection_Block.Item(j)
Collection_Block.Remove (j)
Collection_Block.Add Item:=Collection_Block.Item(i), after:=i
Collection_Block.Remove (i)
Collection_Block.Add Item:=Temp, before:=i
End If
Next j
Next i
Set Sort_Collection = Collection_Block
End Function
Function Set_Block_Str(Collection_Block_srt, Prev_Pags)
Dim RetVal As AcadBlockReference
On Error GoTo ErrTrap
For i = 1 To Collection_Block_srt.Count '修改图纸中属性块的值
Set RetVal = ActiveDocument.HandleToObject(Collection_Block_srt.Item(i)(0)) '(0,i)为arr_Block数组的句柄位置
attVars = RetVal.GetAttributes
For j = LBound(attVars) To UBound(attVars)
Select Case attVars(j).TagString
Case "页码"
attVars(j).TextString = Collection_Block_srt.Item(i)(5) + Prev_Pags '页码从1开始,数组从0开始,所有+1,再加上部分的页数(Last_Pags)
i = i + 1
Case "图名"
attVars(j).TextString = Split((Split(ThisDrawing.Name, " ")(1)), ".")(0)
i = i + 1
End Select
Next j
Set RetVal = Nothing
i = i - 1
Next i
ErrTrap:
If Not (RetVal Is Nothing) Then Set RetVal = Nothing
On Error GoTo 0
End Function
Function ReadSource(Curent_Pags)
'声明Excel相关
Const WorkRang = "目录"
Dim xlApp As Excel.Application
Dim xlBook As Excel.Workbook
RoadName = Right(ThisDrawing.Path, Len(ThisDrawing.Path) - InStrRev(ThisDrawing.Path, "\"))
FileName = Left(ThisDrawing.Name, InStrRev(ThisDrawing.Name, ".") - 1)
ExcelPath = Left((Left(ThisDrawing.Path, InStrRev(ThisDrawing.Path, "\") - 1)), InStrRev((Left(ThisDrawing.Path, InStrRev(ThisDrawing.Path, "\") - 1)), "\")) & "Z 数据文件(不打印)\" & "目录-数据.xlsx"
Set xlApp = New Excel.Application
'获取指定excel文件
Set xlBook = xlApp.Workbooks.Open(ExcelPath)
Dim sheet As Excel.Worksheet
'获取指定sheet
Set sheet = xlBook.Worksheets(1)
For j = 1 To sheet.UsedRange.Rows.Count
For i = 1 To sheet.UsedRange.Columns.Count
If sheet.Cells(j, i).Value = RoadName Then
If sheet.Cells(j, i).MergeCells Then
myCow = sheet.Cells(j, i).MergeArea.Rows.Count
i = i + 1
For k = 0 To myCow
If sheet.Cells(j + k, i).Value = FileName Then
lastPags = sheet.Cells(j + k - 1, i + 2).Value
If lastPags = "页数" Then
lastPags = sheet.Cells(j + k - 3, i + 2).Value
If lastPags = "" Then lastPags = 0
sheet.Cells(j + k, i + 2).Value = Curent_Pags
End If
Exit For
End If
Next k
End If
End If
If lastPags <> "" Then Exit For
Next i
If lastPags <> "" Then Exit For
Next j
ReadSource = lastPags
xlBook.Close Savechanges:=True
End Function
'该函数代入参数为一维数组(arr(0)),即'章,节,图名,页码组成的整串字符串的数组,注意是整串,数组的上标同时为0
'传入本图页数,填写Text文件页数项
'程序获取图纸相关信息后生成参数数组,代入本函数,完成排序,剔除重复,在写入文本文件
'返回上一个图纸的页码
Function Get_PrevPags(Curent_Pags)
Dim arr()
Dim arr_text(0)
图名 = Left(ThisDrawing.Name, (InStr(ThisDrawing.Name, ".") - 1))
'图名 = (Split(Split(mypath, "\")(UBound(Split(mypath, "\"))), ".")(0))
章 = Split(ThisDrawing.Path, "\")((UBound(Split(ThisDrawing.Path, "\")) - 1))
节 = Split(ThisDrawing.Path, "\")((UBound(Split(ThisDrawing.Path, "\"))))
arr_text(0) = 章 & "," & 节 & "," & 图名 & "," & Curent_Pags & ","
textFilePath = Left(ThisDrawing.Path, (InStrRev(Left(ThisDrawing.Path, (InStrRev(ThisDrawing.Path, "\") - 1)), "\"))) & "Z 数据文件(不打印)\文件页码.csv"
'---------------------------------------------------------------
'只读方式打开(文件页码)Text文件,读入数组,关闭文件
Open textFilePath For Input As #1
i = 0
Do While Not EOF(1) ' 循环至文件尾。
Line Input #1, TextLine ' 读入一行数据并将其赋予某变量。
ReDim Preserve arr(i)
arr(i) = Trim(TextLine)
i = i + 1
Loop
Close #1
'追加函数代入的参数数组,查找是否存在,有就替换旧的,没有就追加,比较条件不含页码,页数
'--------------------------------------------------------------------------------------------------
'空文件处理
'-------------------------------------------------------------------
If i = 0 Then 'i=0 为只读方式打开文件时读取的行数,0表示空文件
Get_PrevPags = 0
arr1 = Split(arr_text(0), ",")
arr1(UBound(arr1)) = Get_PrevPags + Curent_Pags
arr1(UBound(arr1) - 1) = Curent_Pags
For j = LBound(arr1) To UBound(arr1)
arrStr = arrStr & "," & arr1(j)
Next j
ReDim Preserve arr(0)
arr(i) = Right(arrStr, Len(arrStr) - 1)
GoTo CreatTextFile
End If
'空文件处理结束
'--------------------------------------------------------------------
'a = Left(arr_text(0), (InStrRev((Left(arr_text(0), InStrRev(arr_text(0), ",") - 1)), ",") - 1))
A = Split(Split(arr_text(0), ",")(0), " ")(1) & "," & Split(arr_text(0), ",")(1) & "," & Split(Split(arr_text(0), ",")(2), " ")(1)
For i = 0 To UBound(arr)
' b = Left(arr(i), (InStrRev((Left(arr(i), InStrRev(arr(i), ",") - 1)), ",") - 1))
b = Split(Split(arr(i), ",")(0), " ")(1) & "," & Split(arr(i), ",")(1) & "," & Split(Split(arr(i), ",")(2), " ")(1)
If A = b Then
arr(i) = arr_text(0)
Temp = True
Exit For
End If
Next i
'End If
If Not Temp Then
ReDim Preserve arr(UBound(arr) + 1)
arr(UBound(arr)) = arr_text(0)
End If
'---------------------------------------------------------------------------------------------------
'读入文件内容结束
'-----------------------------------------------------------------
'下面是删除数组里面重复的数据(New)
For i = 0 To UBound(arr) - 1
For j = i + 1 To UBound(arr)
If arr(i) <> "" Then
aa = Split(Split(arr(i), ",")(0), " ")(1) & "," & Split(arr(i), ",")(1) & "," & Split(Split(arr(i), ",")(2), " ")(1)
Else
aa = ""
End If
If arr(j) <> "" Then
bb = Split(Split(arr(j), ",")(0), " ")(1) & "," & Split(arr(j), ",")(1) & "," & Split(Split(arr(j), ",")(2), " ")(1)
Else
bb = ""
End If
If aa = bb Then arr(j) = ""
Next
Next
Dim tmp_arr()
j = 0
For i = 0 To UBound(arr)
If arr(i) <> "" Then
ReDim Preserve tmp_arr(j)
tmp_arr(j) = arr(i)
j = j + 1
End If
Next
arr = tmp_arr()
'删除结束
'-----------------------------------------------------------------
'下面是第一次排序,按“篇”
For i = 0 To UBound(arr) - 1
For j = i + 1 To UBound(arr)
'----------------------------------------
If Asc(arr(i)) > Asc(arr(j)) Then
Temp = arr(j)
arr(j) = arr(i)
arr(i) = Temp
End If
'-----------------------------------------
Next j
Next i
'第一次排序结束
'-----------------------------------------------------------------
'下面是按“章”排序,保持章的排序不乱
i = 0
Do While i < UBound(arr)
Do While Split(arr(i), ",")(0) = Split(arr(i + 1), ",")(0)
j = i + 1
Do While Split(arr(j), ",")(0) = Split(arr(j - 1), ",")(0)
If CInt(Split(Split(arr(i), ",")(1), " ")(0)) > CInt(Split(Split(arr(j), ",")(1), " ")(0)) Then
'If CInt(Split(Split(arr(i), ",")(1), "-")(1)) > CInt(Split(Split(arr(j), ",")(1), "-")(1)) Then
'If CInt(Split((Split(arr(i), ",")(2)), " ")(0)) > CInt(Split((Split(arr(j), ",")(2)), " ")(0)) Then
Temp = arr(j)
arr(j) = arr(i)
arr(i) = Temp
End If
j = j + 1
If j > UBound(arr) Then Exit Do
Loop
i = i + 1
If i = UBound(arr) Then Exit Do
Loop
i = i + 1
Loop
'章的排序结束
'----------------------------------------------------------------------
'下面是按“节”排序,保持章的排序不乱
i = 0
Do While i < UBound(arr)
Do While Split(arr(i), ",")(1) = Split(arr(i + 1), ",")(1)
j = i + 1
Do While Split(arr(j), ",")(1) = Split(arr(j - 1), ",")(1)
If CInt(Split((Split(arr(i), ",")(2)), " ")(0)) > CInt(Split((Split(arr(j), ",")(2)), " ")(0)) Then
Temp = arr(j)
arr(j) = arr(i)
arr(i) = Temp
End If
j = j + 1
If j > UBound(arr) Then Exit Do
Loop
i = i + 1
If i = UBound(arr) Then Exit Do
Loop
i = i + 1
Loop
'节的排序结束
'-----------------------------------------------------------------
'---------------------------------------------------------------------
'查找本图前一图纸的页数,填写本图代入的页数(Curent_Pags)
arrStr = ""
For i = 0 To UBound(arr)
'a = Left(arr_text(0), (InStrRev((Left(arr_text(0), InStrRev(arr_text(0), ",") - 1)), ",") - 1))
'b = Left(arr(i), (InStrRev((Left(arr(i), InStrRev(arr(i), ",") - 1)), ",") - 1))
A = Split(Split(arr_text(0), ",")(0), " ")(1) & "," & Split(arr_text(0), ",")(1) & "," & Split(Split(arr_text(0), ",")(2), " ")(1)
b = Split(Split(arr(i), ",")(0), " ")(1) & "," & Split(arr(i), ",")(1) & "," & Split(Split(arr(i), ",")(2), " ")(1)
If b = A Then
If i <> 0 Then
Get_PrevPags = Split(arr(i - 1), ",")(UBound(Split(arr(i - 1), ",")))
Else
Get_PrevPags = 0
End If
arr1 = Split(arr(i), ",")
arr1(UBound(arr1)) = Get_PrevPags + Curent_Pags
arr1(UBound(arr1) - 1) = Curent_Pags
For j = LBound(arr1) To UBound(arr1)
arrStr = arrStr & "," & arr1(j)
Next j
arr(i) = Right(arrStr, Len(arrStr) - 1)
Exit For
End If
Next i
'------------------------------------------------------------------------
'下面把数组写入文件
'---------------------------------------------------------------
'写入方式打开(文件页码)Text文件,读入数组,关闭文件
CreatTextFile:
Open textFilePath For Output As #1
For i = LBound(arr) To UBound(arr)
Print #1, arr(i)
Next i
Close #1
'--------------------------------------------------------------------------------------------------
End Function
|
|