本帖最后由 zzyong00 于 2015-1-25 14:35 编辑
今天再发一下求点集凸包和最小外接矩形的代码!
首先是测试代码,在vb窗体上放一个按钮,添加如下代码(其它未定义函数详见本贴前面的代码):
- Private Sub cmd凸包_Click()
- Dim objSset As AcadSelectionSet
- Dim objDoc As AcadDocument
- Set objDoc = ThisDrawing()
- AppActivate objCad.Caption
- SelectLots "MEA~PL~TMP~123", "point"
- Set objSset = objDoc.SelectionSets("MEA~PL~TMP~123")
- If objSset.Count = 0 Then Exit Sub
- objDoc.SetVariable "MODEMACRO", "计算中,请不在Autocad中执行其它操作!"
- Dim obj1 As AcadPoint ' AcadCircle ' AcadLWPolyline
- Dim i As Long
- ReDim pt(objSset.Count - 1)
- ReDim stack(objSset.Count - 1)
- For Each obj1 In objSset
- pt(i).x = obj1.Coordinates(0)
- pt(i).y = obj1.Coordinates(1)
- i = i + 1
- Next
- Call Hull(objSset.Count - 1)
- Dim Coords() As Double
- ReDim Coords(2 * (lngTop + 1) - 1)
- For i = 0 To lngTop
- Coords(2 * i) = stack(i).x
- Coords(2 * i + 1) = stack(i).y
- Next i
- Dim objPL As AcadLWPolyline
- Set objPL = objDoc.ModelSpace.AddLightWeightPolyline(Coords)
- objPL.Closed = True
- objDoc.Regen acActiveViewport
-
- Dim Coords2() As Double
-
- calMinRect lngTop, Coords2
- Set objPL = objDoc.ModelSpace.AddLightWeightPolyline(Coords2)
- objPL.Closed = True
- objDoc.Regen acActiveViewport
- objDoc.SetVariable "MODEMACRO", ""
-
- End Sub
接下来是最主要的计算模块,编写的比较累,略收几个币,安慰一下自己:
|