当前位置: 首页> 财经> 产业 > 窗选特定对象——选择集——CAD-vba

窗选特定对象——选择集——CAD-vba

时间:2025/8/27 0:28:51来源:https://blog.csdn.net/yongshiqq/article/details/142052542 浏览次数:0次

CAD窗选时,人机交互中窗选特定类型图元,可使用选择集+过滤器实现,如下图:

Sub a()
'yngqq@2024年9月9日10:32:40
Dim mysel As AcadSelectionSet
Set mysel = ThisDrawing.SelectionSets.Add("mysel2")
Dim ftype(0) As Integer, fdata(0) As Variantftype(0) = 0: fdata(0) = "*line"
mysel.SelectOnScreen ftype, fdata
Stop
End Sub

Sub Example_SelectByPolygon()' This example adds objects to a selection set by defining a polygon.Dim ssetObj As AcadSelectionSetOn Error Resume NextThisDrawing.SelectionSets.Item("TEST_SSET3").DeleteOn Error GoTo 0Set ssetObj = ThisDrawing.SelectionSets.Add("TEST_SSET3")' Add to the selection set all the objects that lie within a fenceDim mode As IntegerDim pointsArray(0 To 11) As DoubleDim pointsArray1(0 To 7) As DoubleFor Each ent In ThisDrawing.ModelSpaceent.color = acWhiteNext entThisDrawing.Regen acActiveViewportmode = acSelectionSetFencepointsArray(0) = 28.2: pointsArray(1) = 17.2: pointsArray(2) = 0pointsArray(3) = -5: pointsArray(4) = 13: pointsArray(5) = 0pointsArray(6) = -3.3: pointsArray(7) = -3.6: pointsArray(8) = 0pointsArray(9) = 28: pointsArray(10) = -3: pointsArray(11) = 0''pointsArray1(0) = 28.2: pointsArray1(1) = 17.2:pointsArray1(2) = -5: pointsArray1(3) = 13pointsArray1(4) = -3.3: pointsArray1(5) = -3.6pointsArray1(6) = 28: pointsArray1(7) = -3Dim mypl As AcadLWPolylineSet mypl = ThisDrawing.ModelSpace.AddLightWeightPolyline(pointsArray1)mypl.Closed = TrueZoomAll'ssetObj.SelectByPolygon mode, pointsArray' Add to the selection set all the Circles that lie within fenceReDim gpCode(0 To 1) As IntegergpCode(0) = 0gpCode(1) = 8Dim pnt(0 To 2) As Doublepnt(0) = 3: pnt(1) = 6: pnt(2) = 0ReDim dataValue(0 To 1) As VariantdataValue(0) = "Circle"dataValue(1) = 0Dim groupCode As Variant, dataCode As VariantgroupCode = gpCodedataCode = dataValuessetObj.SelectOnScreen groupCode, dataCode'ssetObj.SelectByPolygon acSelectionSetFence, pointsArray, groupCode, dataCodeFor Each ent In ssetObjent.color = acRedent.UpdateNext entStop
End Sub

指定两个坐标窗口内选择:


Sub Example_SelectByPolygon()' This example adds objects to a selection set by defining a polygon.Dim ssetObj As AcadSelectionSetOn Error Resume NextThisDrawing.SelectionSets.Item("TEST_SSET3").DeleteOn Error GoTo 0Set ssetObj = ThisDrawing.SelectionSets.Add("TEST_SSET3")' Add to the selection set all the objects that lie within a fenceDim mode As IntegerDim pointsArray(0 To 11) As DoubleDim pointsArray1(0 To 2) As DoubleDim pointsArray2(0 To 2) As DoubleFor Each ent In ThisDrawing.ModelSpaceent.color = acWhiteNext entThisDrawing.Regen acActiveViewportmode = acSelectionSetFencepointsArray(0) = 0: pointsArray(1) = 17.2: pointsArray(2) = 0pointsArray(3) = -5: pointsArray(4) = 13: pointsArray(5) = 0pointsArray(6) = -3.3: pointsArray(7) = -3.6: pointsArray(8) = 0pointsArray(9) = 28: pointsArray(10) = -3: pointsArray(11) = 0''pointsArray1(0) = 0: pointsArray1(1) = 0pointsArray1(2) = 0: pointsArray2(0) = 10000pointsArray2(1) = 10000: pointsArray2(2) = 0ZoomAll'ssetObj.SelectByPolygon mode, pointsArray' Add to the selection set all the Circles that lie within fenceReDim gpCode(0 To 1) As IntegergpCode(0) = 0gpCode(1) = 8Dim pnt(0 To 2) As Doublepnt(0) = 3: pnt(1) = 6: pnt(2) = 0ReDim dataValue(0 To 1) As VariantdataValue(0) = "Circle"dataValue(1) = 0Dim groupCode As Variant, dataCode As VariantgroupCode = gpCodedataCode = dataValuessetObj.Select acSelectionSetWindow, pointsArray2, pointsArray1'ssetObj.SelectByPolygon acSelectionSetFence, pointsArray, groupCode, dataCodeFor Each ent In ssetObjent.color = acRedent.UpdateNext entStop
End Sub

 acSelectionSetCrossing为最大化框选(有交叉的都能选上),选中变红

 acSelectionSetWindow为最小化框选(全部包围才算), pointsArray2, pointsArray1

下图可知,红框左下角相交未完全包围的圆未变红,即未选中。

专注CAD二次开发、插件、代码,详情见下方 ↓

关键字:窗选特定对象——选择集——CAD-vba

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: