choose1和choose2可以写成一个单独的函数,因为内容是一样的。如以下函数,除可取得图元外,同样把图元的两个端点均取得,而且端点已经进行了排序,这样可以更方便,也更清晰。- Function GetLine(PromptTxt As String, ByRef Point1 As Variant, ByRef Point2 As Variant) As AcadEntity
- Dim ent As AcadEntity
- Dim pnt As Variant
- Dim p1(2) As Double
- Dim p2(2) As Double
- On Error Resume Next
- ThisDrawing.Utility.GetEntity ent, pnt, PromptTxt
- Do
- Select Case ent.ObjectName
- Case "AcDbLine"
- Set GetLine = ent
- Point1 = ent.StartPoint
- Point2 = ent.EndPoint
- If ThisDrawing.Utility.AngleFromXAxis(Point1, Point2) >= PI Then
- Point1 = ent.EndPoint
- Point2 = ent.StartPoint
- End If
- Exit Do
- Case "AcDbPolyline"
- If UBound(ent.Coordinates) = 3 Then
- p1(0) = ent.Coordinates(0): p1(1) = ent.Coordinates(1)
- p2(0) = ent.Coordinates(2): p2(1) = ent.Coordinates(3)
- If ThisDrawing.Utility.AngleFromXAxis(p1, p2) >= PI Then
- p2(0) = ent.Coordinates(0): p2(1) = ent.Coordinates(1)
- p1(0) = ent.Coordinates(2): p1(1) = ent.Coordinates(3)
- End If
- Set GetLine = ent
- Point1 = p1: Point2 = p2
- Exit Do
- End If
- End Select
- ThisDrawing.Utility.Prompt vbCr & "所选对象不符合要求,请重新"
- Loop
- End Function
- Public Function PI() As Double
- PI = Atn(1) * 4
- End Function
另外,为什么要判断是否垂直,其它对于线来说,如第1条线是P1和P2点,第2条线是P3和P4点,这样如果第1线与第2线的角度是一样,而且第1线的角度与P1P3点的角度一样的话,则可判断两条线是在同一线上。
这样的话,就剩下4个点的排序了,因为两组点已经排好序,所以也就简单。 |