|
发表于 2017-7-29 15:13:00
|
显示全部楼层
‘圆弧段转成多边形的函数’,英文很差,中文写的顺手
Public Shared Function Curve2Polyline(_curve As Curve, precise As Double) As Polyline
Dim pl As Polyline = New Polyline
Dim dm As DocumentCollection = Application.DocumentManager
Dim ed As Editor = dm.MdiActiveDocument.Editor
'获取当前数据库作为目标数据库
Dim Db As Database = dm.MdiActiveDocument.Database
Using trans As Transaction = Db.TransactionManager.StartTransaction()
If TypeOf trans.GetObject(_curve.ObjectId, OpenMode.ForRead) Is Circle Then
Dim c1 As Circle = New Circle
c1 = CType(trans.GetObject(_curve.ObjectId, OpenMode.ForRead), Circle)
' Dim 分割数 As Integer = c1.Circumference / precise
Dim 棱边数 As Integer = 1024
Dim 对角距 As Double = 函数库.求对角距(c1.Radius, 棱边数)
Dim 角度 As Double = PI * 2 / 棱边数
Dim pts(棱边数 - 1) As Point2d
For i As Integer = 0 To 棱边数 - 1
pts(i) = New Point2d(c1.Center.X + 对角距 * Cos(角度 * i), c1.Center.Y + 对角距 * Sin(角度 * i))
pl.AddVertexAt(i, pts(i), 0, 0, 0)
Next
pl.Closed = True
ElseIf TypeOf trans.GetObject(_curve.ObjectId, OpenMode.ForRead) Is Polyline Then
Dim PLL As Polyline = New Polyline
PLL = CType(trans.GetObject(_curve.ObjectId, OpenMode.ForRead), Polyline)
'---------------判断多边形方向
Dim points As New List(Of Point2d)
Dim PPP As Point2d
For K As Integer = 0 To PLL.NumberOfVertices - 1
PPP = PLL.GetPoint2dAt(K)
points.Add(PPP)
Next
Dim 多边形方向 As CsharpClass.ClockDirection
多边形方向 = CsharpClass.Polygon.CalculateClockDirection(points, False)
'--------------判断多边形方向
Dim 凸度(-1) As Double
Dim 凸起位置(-1) As Integer
Dim 圆心(-1) As Point3d
Dim N As Integer = -1
Dim PLS(-1) As Polyline
For i As Integer = 0 To PLL.NumberOfVertices - 1
pl.AddVertexAt(i, PLL.GetPoint2dAt(i), 0, 0, 0)
If PLL.GetBulgeAt(i) 0 Then
N = N + 1
ReDim Preserve 凸度(N)
ReDim Preserve 凸起位置(N)
ReDim Preserve 圆心(N)
ReDim Preserve PLS(N)
凸度(N) = PLL.GetBulgeAt(i)
凸起位置(N) = i
End If
Next
For J As Integer = N To 0 Step -1
Dim 起点 As Point2d '起点坐标
Dim 终点 As Point2d '终点坐标
Dim pC As Point3d '圆心坐标
' Dim b As Double 'b=Bulge 凸度值
Dim L As Double 'L为弦长
Dim Lc As Double '弦心距(弦中心到圆弧中心的距离)
Dim R As Double '弧半径
起点 = PLL.GetPoint2dAt(凸起位置(J))
If 凸起位置(J) = PLL.NumberOfVertices - 1 Then
终点 = PLL.GetPoint2dAt(0)
Else
终点 = PLL.GetPoint2dAt(凸起位置(J) + 1)
End If
' MsgBox(凸起位置(J).ToString & vbCrLf & 起点.ToString)
' MsgBox(终点.ToString)
L = Sqrt((起点.X - 终点.X) ^ 2 + (起点.Y - 终点.Y) ^ 2)
R = 0.25 * L * (1 + 凸度(J) ^ 2) / 凸度(J)
Lc = 0.25 * L * (1 - 凸度(J) ^ 2) / 凸度(J)
' pC.X = (起点.X + 终点.X) / 2 + Lc / L * (起点.Y - 终点.Y)
' pC.Y = (起点.Y + 终点.Y) / 2 + Lc / L * (终点.X - 起点.X))
pC = New Point3d((起点.X + 终点.X) / 2 + Lc / L * (起点.Y - 终点.Y), _
(起点.Y + 终点.Y) / 2 + Lc / L * (终点.X - 起点.X), 0)
Dim pts(precise - 1) As Point2d
Dim 总弧度 As Double = 4 * Atan(凸度(J))
Dim 角度 As Double = 总弧度 / precise
Dim 对角距 As Double = R / Cos(角度 / 2)
Dim 起点角度 As Double
起点角度 = 函数库.求线段绝对角度(函数库.p32d(pC), 起点)
PLS(J) = New Polyline
For K As Integer = 0 To precise - 1
'顺时针方向
If 多边形方向 = ClockDirection.Clockwise Then
pts(K) = New Point2d(pC.X + R * Cos(起点角度 + 角度 * K), pC.Y + R * Sin(起点角度 + 角度 * K))
If 凸度(J) < 0 Then
pts(K) = New Point2d(pC.X + 对角距 * Cos(起点角度 + 角度 / 2 + 角度 * K + PI), pC.Y + 对角距 * Sin(起点角度 + 角度 / 2 + 角度 * K + PI))
End If
'逆时针方向
ElseIf 多边形方向 = ClockDirection.Counterclockwise Then
pts(K) = New Point2d(pC.X + 对角距 * Cos(起点角度 + 角度 / 2 + 角度 * K), pC.Y + 对角距 * Sin(起点角度 + 角度 / 2 + 角度 * K))
If 凸度(J) < 0 Then
'pts(K) = New Point2d(pC.X + 对角距 * Cos(起点角度 + 角度 / 2 + 角度 * K + PI), pC.Y + 对角距 * Sin(起点角度 + 角度 / 2 + 角度 * K + PI))
pts(K) = New Point2d(pC.X + R * Cos(起点角度 + 角度 * K + PI), pC.Y + R * Sin(起点角度 + 角度 * K + PI))
End If
ElseIf 多边形方向 = ClockDirection.None Then
'按外接多边形考虑
pts(K) = New Point2d(pC.X + 对角距 * Cos(起点角度 + 角度 / 2 + 角度 * K), pC.Y + 对角距 * Sin(起点角度 + 角度 / 2 + 角度 * K))
If 凸度(J) < 0 Then
pts(K) = New Point2d(pC.X + 对角距 * Cos(起点角度 + 角度 / 2 + 角度 * K + PI), pC.Y + 对角距 * Sin(起点角度 + 角度 / 2 + 角度 * K + PI))
'pts(K) = New Point2d(pC.X + R * Cos(起点角度 + 角度 * K + PI), pC.Y + R * Sin(起点角度 + 角度 * K + PI))
End If
End If
PLS(J).AddVertexAt(K, pts(K), 0, 0, 0)
Next
PLS(J).ReverseCurve()
For k As Integer = 0 To precise - 1
pl.AddVertexAt(凸起位置(J) + 1, PLS(J).GetPoint2dAt(k), 0, 0, 0)
Next
ReMove_PlSaPt(pl)
Next
pl.Closed = True
End If
trans.Commit()
End Using
Return pl
End Function
Public Shared Function ReMove_PlSaPt(PLL As Polyline) As Polyline
Dim PL As Polyline = New Polyline
PL = PLL
Dim 相同点(-1) As Integer
Dim 计数器 As Integer = -1
For M As Integer = 0 To PL.NumberOfVertices - 2 '移除相同点
If PL.GetPoint3dAt(M) = PL.GetPoint3dAt(M + 1) Then
计数器 = 计数器 + 1
ReDim Preserve 相同点(计数器)
相同点(计数器) = M
'MsgBox(" SS" & 计数器)
End If
Next M
For N As Integer = 计数器 To 0 Step -1
PL.RemoveVertexAt(相同点(N))
' MsgBox(相同点(N))
Next
Return PL
End Function |
|