Visio:如何获取一个形状中包含的形状?

Visio: How to get the shapes that are contained in one shape?

我无法从 "Package (expanded)" 形状中获取方法 SpartialNeighbors 的信息。

通常我使用这个代码:

 Dim s As Shape, vsoShapeOnPage As Shape
 Dim vsoReturnedSelection As Visio.Selection  

 's contains the current shape
 Set vsoReturnedSelection = s.SpatialNeighbors(visSpatialContain, 0, visSpatialIncludeContainerShapes)
        If vsoReturnedSelection.Count = 0 Then
            'No Shapes contained
        Else
            For Each vsoShapeOnPage In vsoReturnedSelection
               'Code
            Next
        End If

这对于像默认 UML 模板 (nameU = "Overview")

中的形状非常有效

我知道我可以对形状进行分组,但这会增加工作量。

还有一点,当我分析其他形状时,我看到 "MemberOfContainers" 该形状包含在 "Package (expanded)" 中。因此,一定有可能在不遍历所有形状的情况下从相反的方向获取信息。

在这里你可以看到 "Package" 和 "interface" 的形状

如果一个形状是一个容器,它的 ContainerProperties 属性 将被填充(即不为空)。然后您可以对其进行查询以检索成员形状 ID 数组。

以下是在 SDK 下载中找到的一些示例代码的略微改编版本 - 基于如下所示的文档:

您可以像这样获取成员形状:

Sub CheckMyPackageContainer()
    'Assumes container is selected shape in active drawing window
    Call ReportContainerShape(ActiveWindow.Selection.PrimaryItem)
End Sub


Sub ReportContainerShape(ByRef contShp As Visio.Shape)
    If Not contShp Is Nothing Then
        Dim containerProps As ContainerProperties
        Set containerProps = contShp.ContainerProperties
        If Not containerProps Is Nothing Then
            Dim lngContainerMembers() As Long
            lngContainerMembers = containerProps.GetMemberShapes(Visio.VisContainerFlags.visContainerFlagsDefault)

            Dim hostingPage As Visio.Page
            Set hostingPage = contShp.ContainingPage
            For Each varMember In lngContainerMembers
                Dim shpItem As Visio.Shape
                Set shpItem = hostingPage.Shapes.ItemFromID(varMember)
                Debug.Print shpItem.NameU, "Text = " & shpItem.Text
            Next
        End If
    End If
End Sub

这将导致以下输出(注意不包括 'InterfaceThree'):

Interface     Text = InterfaceOne
Interface.30  Text = InterfaceTwo