我想获得一个已存在圆的圆心坐标和半径数据
写了下面的一段:
Private Sub Cmd_getcircle_Click()
Dim set_circle As AcadSelectionSet
If Not IsNull(ThisDrawing.SelectionSets.Item("newcircle")) Then Set set_circle = ThisDrawing.SelectionSets.Item("newcircle") set_circle.Delete End If Set set_circle = ThisDrawing.SelectionSets.Add("newcircle")
set_circle.SelectOnScreen
Dim objcircle As AcadCircle Set objcircle = set_circle.Item(0)
Dim circle_cen As Variant Dim circle_radius As Variant
circle_cen = objcircle.Center circle_radius = objcircle.Radius cen_x = circle_cen(0) cen_y = circle_cen(1) cen_z = circle_cen(2) circle_r = circle_radius
set_circle.Delete End Sub
但是运行的时候出现"没有主键",不知道怎么回事
但是我去掉下面一段后:
If Not IsNull(ThisDrawing.SelectionSets.Item("newcircle")) Then Set set_circle = ThisDrawing.SelectionSets.Item("newcircle") set_circle.Delete End If 再运行的时候就没有问题,请高手帮忙解答一下
我本来想安全创建选择集的,现在反而有问题了
给你一个创建选择集的函数:
'--------------------------------------------------------------------- ' '[函数] 创建选择集, 返回选择集对象 ' '---------------------------------------------------------------------
Private Function CreateSSet(ByVal name As String) As AcadSelectionSet On Error GoTo ERR_HANDLER
Dim ssetObj As AcadSelectionSet Dim SSetColl As AcadSelectionSets Set SSetColl = ThisDrawing.SelectionSets
Dim index As Integer Dim found As Boolean
found = False
For index = 0 To SSetColl.count - 1 Set ssetObj = SSetColl.Item(index) If StrComp(ssetObj.name, name, 1) = 0 Then found = True Exit For End If Next
If Not (found) Then Set ssetObj = SSetColl.Add(name) Else ssetObj.Clear End If
Set CreateSSet = ssetObj
Exit Function ERR_HANDLER: '----------------------------------------------- ' just print the error the the debug window. Debug.Print "Error in sub CreateSSet: " & Err.Number & " -- "; Err.Description Resume ERR_END
ERR_END: End Function
调用方法:
Dim ssetObj1 As AcadSelectionSet Set ssetObj1 = CreateSSet("MySet")
试试
Sub tttt() Dim ss As AcadSelectionSet Set ss = ThisDrawing.ActiveSelectionSet ss.Clear ss.SelectOnScreen End Sub
先运行一遍,选择几个实体
在图形中点击文件菜单->打开,显示打开对话框,然后点击取消回到原来的图形中。
再运行一遍