明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 1158|回复: 3

创建匿名块

[复制链接]
发表于 2022-11-29 16:04:28 | 显示全部楼层 |阅读模式
Option Explicit

' 创建匿名块
Public Sub CreateAnonymousBlk()
    Dim ptBase(0 To 2) As Double
    ptBase(0) = 0: ptBase(1) = 0: ptBase(2) = 0

    ' 添加块定义
    Dim objBlkDef As AcadBlock
    Set objBlkDef = ThisDrawing.Blocks.Add(ptBase, "*U")

    ' 向块定义中添加图形对象
    Dim pt1(0 To 2) As Double
    Dim pt2(0 To 2) As Double
    pt1(0) = -10: pt1(1) = 0: pt1(2) = 0
    pt2(0) = 10: pt2(1) = 0: pt2(2) = 0
    objBlkDef.AddLine pt1, pt2

    pt1(0) = 0: pt1(1) = -10: pt1(2) = 0
    pt2(0) = 0: pt2(1) = 10: pt2(2) = 0
    objBlkDef.AddLine pt1, pt2

    objBlkDef.AddCircle ptBase, 6
End Sub

' 获得最后创建的匿名块
Public Function GetLastAnonymousBlk() As AcadBlock
    Dim objBlkDef As AcadBlock
    Dim n As Integer

    For Each objBlkDef In ThisDrawing.Blocks
        ' 匿名块以*为起始字符
        If Left(objBlkDef.Name, 1) = "*" Then
            ' 消除布局块的影响
            If objBlkDef.Name <> "*Model_Space" And Left(objBlkDef.Name, 12) <> "*Paper_Space" Then
                ' 返回名称编号最大的一个块
                If Mid(objBlkDef.Name, 3) >= n Then
                    n = Mid(objBlkDef.Name, 3)
                    Set GetLastAnonymousBlk = objBlkDef
                End If
            End If
        End If
    Next

    Set objBlkDef = Nothing
End Function

' 插入一个匿名块
Public Sub InsertAnonymousBlkRef()
    Dim ptInsert(0 To 2) As Double
    ptInsert(0) = 100: ptInsert(1) = 100: ptInsert(2) = 0

    Dim objBlk As AcadBlock
    ' 获得图形中最后一个创建的匿名块
    Set objBlk = GetLastAnonymousBlk
    ThisDrawing.ModelSpace.InsertBlock ptInsert, objBlk.Name, 1, 1, 1, 0
End Sub

' 获得图形中匿名块的数量和名称
Public Sub GetAnonymousBlkNumber()
    Dim objBlkDef As AcadBlock
    Dim n As Integer                    ' 匿名块的数量

    For Each objBlkDef In ThisDrawing.Blocks
        ' 匿名块以*为起始字符
        If Left(objBlkDef.Name, 1) = "*" Then
            ' 消除布局块的影响
            If objBlkDef.Name <> "*Model_Space" And Left(objBlkDef.Name, 12) <> "*Paper_Space" Then
                n = n + 1
                Debug.Print objBlkDef.Name
            End If
        End If
    Next

    MsgBox "当前图形中匿名块的数量是: " & CStr(n)
End Sub


 楼主| 发表于 2022-11-29 19:54:45 | 显示全部楼层
需要的复制哈
发表于 2023-9-21 20:33:08 | 显示全部楼层
怎么在你的代码上面修改获取块的外框呢
您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|CAD论坛|CAD教程|CAD下载|联系我们|关于明经|明经通道 ( 粤ICP备05003914号 )  
©2000-2023 明经通道 版权所有 本站代码,在未取得本站及作者授权的情况下,不得用于商业用途

GMT+8, 2024-11-17 21:31 , Processed in 0.164815 second(s), 22 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表