马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
×
CAD中规定PL线在Offset时,逆时针时偏移量正值时为放大。那么程序首先需要判断该PL线的方向,网上有一个算法如下:
- Public Function GE_WhatPoly(ByVal oPLine As Object) As Integer
- ' 1 顺时针
- ' -1 逆时针
- ' 0 无法判断
- Dim i As Integer
- Dim intHigh As Integer
- Dim dblArea As Double
- Dim arrPts() As Double
- arrPts = oPLine.Coordinates
- intHigh = UBound(arrPts)
- For i = 0 To intHigh - 2 Step 2
- dblArea = dblArea + (arrPts((i + 2) Mod (intHigh + 1)) - arrPts(i)) * (arrPts((i + 3) Mod (intHigh + 1)) + arrPts(i + 1))
- Next i
- GE_WhatPoly = IIf(dblArea > 0, 1, -1)
- If dblArea = 0 Then GE_WhatPoly = 0
- End Function
经过测试大部分时候可以有效判断,但也有个别例外失效了。如附件:
测试代码:
- Public Function GE_WhatPoly(ByVal oPLine As Object) As Integer
- ' 1 顺时针
- ' -1 逆时针
- ' 0 无法判断
- Dim i As Integer
- Dim intHigh As Integer
- Dim dblArea As Double
- Dim arrPts() As Double
- arrPts = oPLine.Coordinates
- intHigh = UBound(arrPts)
- For i = 0 To intHigh - 2 Step 2
- dblArea = dblArea + (arrPts((i + 2) Mod (intHigh + 1)) - arrPts(i)) * (arrPts((i + 3) Mod (intHigh + 1)) + arrPts(i + 1))
- Next i
- GE_WhatPoly = IIf(dblArea > 0, 1, -1)
- If dblArea = 0 Then GE_WhatPoly = 0
- End Function
- Public Sub a1()
- Dim GetEnt As Object
- Dim picPnt
- ThisDrawing.Utility.GetEntity GetEnt, picPnt, vbCr & _
- " >>请拾取PL线:"
- If Err.Number = "-2147352567" Then 'ESC or 没选中 or Enter
- ThisDrawing.Utility.Prompt vbCr & _
- " >>结束!"
- Exit Sub
- Else
- Dim Temp As String
- Temp = IIf(GE_WhatPoly(GetEnt), "顺时针", "逆时针")
- MsgBox Temp
- End If
- End Sub
- Public Sub a2() 'PL线偏移(放大)300
- Dim GetEnt As Object
- Dim picPnt
- ThisDrawing.Utility.GetEntity GetEnt, picPnt, vbCr & _
- " >>请拾取PL线:"
- If Err.Number = "-2147352567" Then 'ESC or 没选中 or Enter
- ThisDrawing.Utility.Prompt vbCr & _
- " >>结束!"
- Exit Sub
- Else
- GetEnt.Offset GE_WhatPoly(GetEnt) * -300
- End If
- End Sub
图中两根线实际测试时出错了。请高手来完善一下算法。
我现在用偏移之后的面积比较来区分实际偏移方向。
|