VBA实例:把当前图纸中符合条件的圆替换为块
huerfei008
huerfei008 Lv.12
2006年09月28日 11:07:55
只看楼主

’把当前图纸中符合条件的圆替换为块(注:块在当前图纸中已存在)Public Sub ChangeEntity(ByVal MinRadius As Double, ByVal MaxRadius As Double, _ ByVal BlockName As Variant, ByVal AutoSelect As Boolean) On Error Resume Next Dim ssobject As AcadCircle

’把当前图纸中符合条件的圆替换为块(注:块在当前图纸中已存在)
Public Sub ChangeEntity(ByVal MinRadius As Double, ByVal MaxRadius As Double, _
ByVal BlockName As Variant, ByVal AutoSelect As Boolean)
On Error Resume Next

Dim ssobject As AcadCircle
Dim InsertionPoint(0 To 2) As Double
Dim NewBlock As AcadBlockReference

’创建选择集
Dim ssetObj As AcadSelectionSet
Set ssetObj = AcadDoc.SelectionSets("BlockCount")

If Err.Number <> 0 Then
Err.Clear
Set ssetObj = AcadDoc.SelectionSets.Add("BlockCount")
End If

’清空选择集
ssetObj.Clear

’创建过滤机制
Dim fType(0 To 6) As Integer
Dim fData(0 To 6) As Variant

fType(0) = 0: fData(0) = "Circle"

fType(1) = -4: fData(1) = "<AND"
fType(2) = -4: fData(2) = ">="
fType(3) = 40: fData(3) = MinRadius
fType(4) = -4: fData(4) = "<="
fType(5) = 40: fData(5) = MaxRadius
fType(6) = -4: fData(6) = "AND>"

’选择符合条件的所有图元-圆
If AutoSelect Then
’自动选择方式
ssetObj.Select acSelectionSetAll, , , fType, fData
Else
’提示用户选择
ssetObj.SelectOnScreen fType, fData
End If

If ssetObj.Count = 0 Then Exit Sub

’替换每一个圆为指定的块对象
For Each ssobject In ssetObj
InsertionPoint(0) = ssobject.Center(0)
InsertionPoint(1) = ssobject.Center(1)
InsertionPoint(2) = ssobject.Center(2)

On Error GoTo ErrHandle

Set NewBlock = AcadDoc.ModelSpace.InsertBlock(InsertionPoint, BlockName, 1, 1, 1, 0)

ssobject.Delete
Set NewBlock = Nothing
Next

’删除数组
Erase fType: Erase fData

’刷新视图
’AcadDoc.Regen acActiveViewport

MsgBox "当前图纸中有 " & ssetObj.Count & " 个符合条件的圆被替换为块 “" & BlockName & "”。", vbInformation, "提示:"

’删除选择集
ssetObj.Clear
ssetObj.Delete

Set ssetObj = Nothing
Exit Sub
ErrHandle:
Select Case Err.Number
Case -2147418113
MsgBox "在当前图纸中找不到名称为: “" & BlockName & "” 的块参照,请确认块名!", vbCritical, "错误:"
Case Else
MsgBox Err.Number & Chr(13) & Err.Descri ption, vbCritical, "产生了以下错误:"
End Select
Err.Clear
End Sub

免费打赏
huerfei008
2006年09月28日 11:15:38
2楼
坐标注记程序
Sub zzb()
On Error GoTo ERR
Dim ver(0 To 5) As Double ’多段线顶点坐标
Dim plineobj As AcadLWPolyline ’多段线
Dim text_x As AcadText ’X坐标
Dim text_y As AcadText ’Y坐标
Dim xins(0 To 2) As Double ’X坐标插入点
Dim yins(0 To 2) As Double ’Y坐标插入点
Dim zjlayer As AcadLayer ’注记层
Dim ltxt As Single ’坐标文本长度
Dim lint As Integer ’坐标文本长度
Dim us1 As String ’比例尺
Dim us2 As String ’左下角X坐标
Dim us3 As String ’’左下角Y坐标



Set zjlayer = ThisDrawing.Layers.Add("ZJ_NEW")


zjlayer.Color = acCyan

Dim x As String
Dim y As String

Dim p1 As Variant
Dim p2 As Variant
Dim p3(0 To 1) As Double
’ ThisDrawing.SetVariable "OSMODE", 1
p1 = ThisDrawing.Utility.GetPoint(, vbCrLf & "选择注记点:")


p2 = ThisDrawing.Utility.GetPoint(p1, vbCr & "注记坐标 ")



ltxt = 17


If p2(0) > p1(0) And p2(1) > p1(1) Then
GoTo 1 ’第一象限
ElseIf p2(0) > p1(0) And p2(1) < p1(1) Then
GoTo 1 ’第二象限
ElseIf p2(0) < p1(0) And p2(1) < p1(1) Then
GoTo 2 ’第三象限
ElseIf p2(0) < p1(0) And p2(1) > p1(1) Then
GoTo 2 ’第四象限
End If

1:
p3(0) = p2(0) + ltxt
p3(1) = p2(1)
xins(0) = p2(0) + 1
xins(1) = p2(1) + 1
yins(2) = 0
yins(0) = p2(0) + 1
yins(1) = p2(1) - 3
yins(2) = 0
GoTo zj

2:

p3(0) = p2(0) - ltxt
p3(1) = p2(1)
xins(0) = p3(0) + 1
xins(1) = p3(1) + 1
yins(2) = 0
yins(0) = p3(0) + 1
yins(1) = p3(1) - 3
yins(2) = 0

zj:
ver(0) = p1(0)
ver(1) = p1(1)
ver(2) = p2(0)
ver(3) = p2(1)
ver(4) = p3(0)
ver(5) = p3(1)





p1(0) = p1(0): p1(1) = p1(1)

x = Format(p1(0), "####0.000")
y = Format(p1(1), "####0.000")

Set plineobj = ThisDrawing.ModelSpace.AddLightWeightPolyline(ver) ’二维轻量多段线
plineobj.Layer = "ZJ_NEW"


Set text_x = ThisDrawing.ModelSpace.AddText(" X" & " " & x, xins, 2)
Set text_y = ThisDrawing.ModelSpace.AddText("NY" & " " & y, yins, 2)
text_x.Layer = "ZJ_NEW"
text_y.Layer = "ZJ_NEW"


Exit Sub

ERR:
Resume
End Sub
回复
sunlee168
2006年10月18日 16:36:06
3楼
大哥,能不能给各画四边形的例子?
参数是内接圆半径,圆心。
回复
ai361210
2009年10月17日 08:21:14
4楼
小弟在VB编辑器中编辑“把当前图纸中符合条件的圆替换为块“的源代码,
但无法在cad宏中找到,小弟不知该怎么运行,哪位大哥告诉小弟下!谢谢啦!!!

:L :L

[ 本帖最后由 ai361210 于 2009-10-17 08:24 编辑 ]
回复

相关推荐

APP内打开