不久前,我编写了一些代码来处理块,但后来不需要它并将其删除。我在动态创建块并插入它。我建议您在尝试插入块之前先查看它的实例是否存在
- Public Function BlockExists(BlockName As String) As Boolean
- Dim oBlock As AcadBlock
- Dim thisdrawing As AcadDocument
- 'iterate through the Block collection object
- Set entity = thisdrawing.Application.ActiveDocument
- For Each oBlock In thisdrawing.Blocks
- If oBlock.Name Like BlockName & "*" Then
- 'found a match, so it exist
- BlockExists = True
- 'so, exit the function with True
- Exit Function
- End If
- Next oBlock
- 'lblock does not exist
- BlockExists = False
- End Function
小心这行:
如果对象锁定。名称类似于BlockName&“*”,然后<br>要获得精确匹配,请将其修改为:<br>If oBlock。像BlockName这样的名称,然后是“&”*”
上面的代码最初是由Bell,R.Robert在Autodesk论坛上发布的,用于查看是否存在层。我现在为集合集提供了多种风格的代码
一个重要的注意事项是,如果多次使用add方法,如果该方法存在,它将返回当前值,因此您根本不需要检查。我在那里也读到了,但直到我写了一些代码才明白,这样做并没有因为添加了这样存在的层而崩溃
以下是我使用的一些其他方法:
- Public Function TextStyleExists(TextStyle As String) As Boolean
- Dim oStyle As ACADTextStyle
- Dim entity As AcadDocument
- 'iterate through the Layers collection object
- Set entity = thisdrawing.Application.ActiveDocument
- For Each oStyle In entity.TextStyles
- If LCase(oStyle.Name) Like LCase(TextStyle) Then '& "*" Then
- 'found a match, so it exist
- TextStyleExists = True
- 'so, exit the function with True
- Exit Function
- End If
- Next oStyle
- 'layer does not exist
- TextStyleExists = False
- End Function
- Public Function DimStyleExists(DimStyle As String) As Boolean
- Dim oStyle As ACADDimStyle
- Dim entity As AcadDocument
- 'iterate through the Layers collection object
- Set entity = thisdrawing.Application.ActiveDocument
- For Each oStyle In entity.DimStyles
- If LCase(oStyle.Name) Like LCase(DimStyle) Then '& "*" Then
- 'found a match, so it exist
- DimStyleExists = True
- 'so, exit the function with True
- Exit Function
- End If
- Next oStyle
- 'layer does not exist
- DimStyleExists = False
- End Function
- Public Function GroupExists(grpName As String) As Boolean
- Dim oGroup As AcadGroup
- Dim thisdrawing As AcadDocument
- 'Dim entity As AcadDocument
- 'iterate through the Layers collection object
- Set thisdrawing = AutoCAD_Application.ActiveDocument
- For Each oGroup In thisdrawing.Groups
- If oGroup.Name Like grpName & "*" Then
- 'found a match, so it exist
- GroupExists = True
- 'so, exit the function with True
- Exit Function
- End If
- Next oGroup
- 'layer does not exist
- GroupExists = False
- End Function
- Public Function LayerExists(LayerName As String) As Boolean
- Dim oLayer As AcadLayer
- Dim entity As AcadDocument
- 'iterate through the Layers collection object
- Set entity = thisdrawing.Application.ActiveDocument
- For Each oLayer In entity.Layers
- If oLayer.Name Like LayerName & "*" Then
- 'found a match, so it exist
- LayerExists = True
- 'so, exit the function with True
- Exit Function
- End If
- Next oLayer
- 'layer does not exist
- LayerExists = False
- End Function
- Public Function LayerExists2(LayerName As String) As Boolean
- Dim oLayer As AcadLayer
- Dim entity As AcadDocument
- 'iterate through the Layers collection object
- Set entity = thisdrawing.Application.ActiveDocument
- For Each oLayer In entity.Layers
- If oLayer.Name Like LayerName Then '& "*" Then
- 'found a match, so it exist
- LayerExists2 = True
- 'so, exit the function with True
- Exit Function
- End If
- Next oLayer
- 'layer does not exist
- LayerExists2 = False
- End Function
|