如何获取在活动autocad绘图的每个图层中找到的对象数

时间:2016-05-02 10:30:34

标签: vba autocad

任何人都可以解释我让对象有层数 使用vba代码acad

2 个答案:

答案 0 :(得分:2)

我认为你应该使用SelectionSets

Public Sub SelsetByLayer()
    Dim ss As AcadSelectionSet
    Dim FilterType(0) As Integer
    Dim FilterData(0) As Variant
    FilterType(0) = 8 ' DXF code of Layer property
    For I = 0 To ThisDrawing.Layers.Count
        ' ThisDrawing.SelectionSets.Item(0).Delete
        Set ss = ThisDrawing.SelectionSets.Add("test")  ' You need to ensure if such selection set not exist yet .
        FilterData(0) = ThisDrawing.Layers.Item(I).Name
        ss.Select acSelectionSetAll, , , FilterType, FilterData
    Next
End Sub

答案 1 :(得分:1)

继原始答案之后,关于使用选择集

稍作修改:

Public Sub SelsetByLayer()
    Dim ss As AcadSelectionSet
    Dim FilterType(0) As Integer
    Dim FilterData(0) As Variant
    FilterType(0) = 8 ' DXF code of Layer property
    For I = 0 To ThisDrawing.Layers.Count
        Set ss = CreateSelectionSet("test")
        FilterData(0) = ThisDrawing.Layers.Item(I).Name
        ss.Select acSelectionSetAll, , , FilterType, FilterData
    Next
End Sub

Public Function CreateSelectionSet(SelName As String) As AcadSelectionSet

    On Error Resume Next

    ' Create a new selection set
    ' Delete any existing selection set with the specified name
    With ThisDrawing
        Set CreateSelectionSet = .SelectionSets.Add(SelName)
        If (Err.Number <> 0) Then
            Err.Clear
            .SelectionSets.Item(SelName).Delete
            Set CreateSelectionSet = .SelectionSets.Add(SelName)
        End If
    End With

End Function

我添加了缺少的方法来管理现有选择集的删除。

ss.Count将拥有找到的实体数量。但请记住,您可能在图纸中有冻结等图层,我认为这些图案将从总计排除