接上期的内容,此次读取view的下一层几subview
主要用到下面的方法获取view的第一个子级
一个封装的类
Public Class DrawingExpolrerEx
Public Shared Function DrawingHasViews(draftApp As MarDrafting) As Boolean
Dim vh As MarElementHandle
Try
vh = draftApp.ElementChildFirstGet()
Return True
Catch ex As Exception
Return False
End Try
End Function
''' <summary>
''' 获取当前视图的全部的子视图的句柄
''' </summary>
''' <param name="draftApp">MarDrafting对象</param>
''' <param name="viewHandle">视图的句柄</param>
''' <returns>子视图句柄的list集合</returns>
Public Shared Function ViewSubViews(draftApp As MarDrafting, viewHandle As MarElementHandle) As List(Of MarElementHandle)
If Not draftApp.ElementIsView(viewHandle) Then
Return Nothing
End If
Dim rtns As New List(Of MarElementHandle)
Try
Dim subvh As MarElementHandle
subvh = draftApp.ElementChildFirstGet(viewHandle)
rtns.Add(subvh)
'读取第2个view
Dim nextsubVh As MarElementHandle
Try
nextsubVh = draftApp.ElementSiblingNextGet(subvh)
rtns.Add(nextsubVh)
Catch ex As Exception
End Try
Dim errFlag As Boolean = True
Do
Try
nextsubVh = draftApp.ElementSiblingNextGet(nextsubVh)
rtns.Add(nextsubVh)
Catch ex As Exception
errFlag = False
End Try
Loop While errFlag
Catch ex As Exception
MsgBox(ex.StackTrace)
End Try
Return rtns
End Function
''' <summary>
''' 获取当前子视图的全部的组件的句柄
''' </summary>
''' <param name="draftApp">MarDrafting对象</param>
''' <param name="subviewHandle">子视图的句柄</param>
''' <returns>组件句柄的list集合</returns>
Public Shared Function SubViewComponents(draftApp As MarDrafting, subviewHandle As MarElementHandle) As List(Of MarElementHandle)
If Not draftApp.ElementIsSubview(subviewHandle) Then
Return Nothing
End If
Dim rtns As New List(Of MarElementHandle)
Try
Dim compovh As MarElementHandle
compovh = draftApp.ElementChildFirstGet(subviewHandle)
rtns.Add(compovh)
'读取第2个view
Dim nextcompobVh As MarElementHandle
Try
nextcompobVh = draftApp.ElementSiblingNextGet(compovh)
rtns.Add(nextcompobVh)
Catch ex As Exception
End Try
Dim errFlag As Boolean = True
Do
Try
nextcompobVh = draftApp.ElementSiblingNextGet(nextcompobVh)
rtns.Add(nextcompobVh)
Catch ex As Exception
errFlag = False
End Try
Loop While errFlag
Catch ex As Exception
MsgBox(ex.StackTrace)
End Try
Return rtns
End Function
''' <summary>
''' 获取当前图纸的全部的视图的句柄
''' </summary>
''' <param name="draftApp">MarDrafting对象</param>
''' <returns>视图句柄的list集合</returns>
Public Shared Function DrawingViews(draftApp As MarDrafting) As List(Of MarElementHandle)
Dim rtns As New List(Of MarElementHandle)
Try
If Not DrawingHasViews(draftApp) Then MsgBox("当前未开启任何图纸,结束命令!", MsgBoxStyle.Critical) : Return Nothing
'读取第一个view
Dim vh As MarElementHandle
vh = draftApp.ElementChildFirstGet()
rtns.Add(vh)
'读取第2个view
Dim nextVh As MarElementHandle
Try
nextVh = draftApp.ElementSiblingNextGet(vh)
rtns.Add(nextVh)
Catch ex As Exception
End Try
Dim errFlag As Boolean = True
Do
Try
nextVh = draftApp.ElementSiblingNextGet(nextVh)
rtns.Add(nextVh)
Catch ex As Exception
errFlag = False
End Try
Loop While errFlag
Catch ex As Exception
MsgBox(ex.StackTrace)
End Try
Return rtns
End Function
End Class
主函数,也就文章刚开始看到的结果
<MyAmFunctionAtt(NameOf(读取DrawingExplorer), NameOf(读取子视图名称))>
Public Sub 读取子视图名称(wm As WindowManager)
Try
Dim amEnv As New AmEnvironment()
If Not amEnv.DraftApp.DwgCurrent() Then MsgBox("当前未开启任何图纸,结束命令!", MsgBoxStyle.Critical) : Exit Sub
Dim views = DrawingExpolrerEx.DrawingViews(amEnv.DraftApp)
If views Is Nothing Then MsgBox("图纸中无任何内容!", MsgBoxStyle.Critical) : Exit Sub
Dim i As Integer = 1
For Each item As MarElementHandle In views
amEnv.MarUI.MessageNoConfirm($"<------第 {i} 个view的名称是:{amEnv.DraftApp.SubpictureNameGet(item)},开始读取其子视图------> ")
Dim subViews = DrawingExpolrerEx.ViewSubViews(amEnv.DraftApp, item)
If subViews IsNot Nothing Then
Dim j As Integer = 1
For Each sv As MarElementHandle In subViews
amEnv.MarUI.MessageNoConfirm($"第 {j} 个subview的名称是:{amEnv.DraftApp.SubpictureNameGet(sv)} ")
j += 1
Next
amEnv.MarUI.MessageNoConfirm($"<-------第 {i} 个view的读取其子视图完成------>")
End If
i += 1
Next
Catch ex As Exception
MsgBox(ex.StackTrace)
End Try
End Sub