[转帖]用VBA创建匿名块
在AutoCAD中,可以用VBA创建匿名块,那么如何创建呢?其实制作匿名块的方法和制作普通块的方法是一样的。如:<P class=txt14>Dim iPt(0 To 2) As Double
<P class=txt14>iPt(0) = 0: iPt(1) = 0: iPt(2) = 0
<P class=txt14>Dim BlockObj As AcadBlock
<P class=txt14>Set BlockObj = ThisDrawing.Blocks.Add(iPt, "*U")
<P class=txt14>上面的代码中在块集合生成了一个匿名块。看了上面的代码,是不是觉得制作匿名块其实很简单。但是如何获取刚创建的块的名称呢?其实匿名块的名称是以*+字母开头的,后面是从1开始递增的数字,那么如果是第一次创建匿名块,那名称就是“*U1”。下面的代码返回最后创建的匿名块:
<P class=txt14>Public Function GetUNBlock() As AcadBlock
<P class=txt14>Dim BlockObj As AcadBlock
<P class=txt14>Dim n As Integer
<P class=txt14>For Each BlockObj In ThisDrawing.Blocks
<P class=txt14>If Left(BlockObj.Name, 1) = "*" Then
<P class=txt14>If BlockObj.Name <> "*Model_Space" And Left(BlockObj.Name, 12) <> "*Paper_Space" Then
<P class=txt14>If Mid(BlockObj.Name, 3) >= n Then
<P class=txt14>n = Mid(BlockObj.Name, 3)
<P class=txt14>Set GetUNBlock = BlockObj
<P class=txt14>End If
<P class=txt14>End If
<P class=txt14>End If
<P class=txt14>Next
<P class=txt14>Set BlockObj = Nothing
<P class=txt14>End Function 要插入该<B>匿名块</B><BR>Set BlkObj = ThisDrawing.ModelSpace.InsertBlock(P, GetUNBlock, 1, 1, 1, 0)
程序修改如下:
Public Function GetUNBlock() As String <FONT color=#ee6911>'此处修改了,该为获取块名</FONT>
<BR>Dim BlockObj As AcadBlock
<BR>Dim n As Integer
<BR>For Each BlockObj In ThisDrawing.Blocks
<BR>If Left(BlockObj.Name, 1) = "*" Then
<BR>If BlockObj.Name <> "*Model_Space" And Left(BlockObj.Name, 12) <> "*Paper_Space" Then
<BR>If Mid(BlockObj.Name, 3) >= n Then
<BR>n = Mid(BlockObj.Name, 3)
<BR>GetUNBlock = BlockObj.Name <FONT color=#ee6911> '此处修改了</FONT>
<BR>End If
<BR>End If
<BR>End If
<BR>Next
<BR>Set BlockObj = Nothing
<BR>End Function 创建匿名块确实与创建普通块在AX方法中是一样的。<BR>匿名块的特殊性:<BR>1.创建时名称由系统定。也就是在系统中还是有名称的。<BR>2.如果匿名块没有被插入图中,在第二次打开图形时匿名块会被自动删除掉。<BR>3.匿名块只能通过程序插入,不能直接在界面操作插入。原因有2:A在界面对话框中看不到匿名块,B块名前加*是表示插入块的同时炸开块。<BR><BR>因为匿名块在系统中还是有名称的,所以在插入块后可以通过.Name属性来取得匿名块的名称,而且可以通过InsertBlock方法来插入。
页:
[1]