Imports Autodesk.AutoCAD.Geometry
Imports Autodesk.AutoCAD.EditorInput
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
Public Class 切线
'以点P到圆C圆心的线段为直径的圆NewC与圆C的两个交点即为点P到圆C的两个切点
<CommandMethod("qieXian")> _
Public Sub test()
Dim acDoc As Document = Application.DocumentManager.MdiActiveDocument
Dim acDb As Database = acDoc.Database
Dim acEd As Editor = acDoc.Editor
Dim acTrans As Transaction = acDb.TransactionManager.StartTransaction()
Dim Bt As BlockTable = TryCast(acTrans.GetObject(acDb.BlockTableId, OpenMode.ForRead), BlockTable)
Dim Btr As BlockTableRecord = TryCast(acTrans.GetObject(Bt(BlockTableRecord.ModelSpace), OpenMode.ForWrite), BlockTableRecord)
Dim C As New Circle()
Dim P As New Point3d()
'选取一个圆
Dim acTypedValue As TypedValue() = New TypedValue(0) {}
acTypedValue.SetValue(New TypedValue(0, "CIRCLE"), 0)
Dim acSelFtr As New SelectionFilter(acTypedValue)
Dim acPromptSelectionResult As PromptSelectionResult = acEd.GetSelection(acSelFtr)
If acPromptSelectionResult.Status = PromptStatus.OK Then
Dim Ss As SelectionSet = acPromptSelectionResult.Value
C = TryCast(acTrans.GetObject(Ss(0).ObjectId, OpenMode.ForRead), Circle)
End If
'选取一个点
Dim acPromptPointResult As PromptPointResult
Dim acPromptPointOptions As PromptPointOptions = New PromptPointOptions("圆外选取一点:")
acPromptPointResult = acDoc.Editor.GetPoint(acPromptPointOptions)
If acPromptPointResult.Status = PromptStatus.OK Then
P = acPromptPointResult.Value
End If
Dim P2PCenter As New Point3d((P.X + C.Center.X) / 2, (P.Y + C.Center.Y) / 2, 0) ' 点到圆心的中点
Dim V As Vector3d = P.GetVectorTo(P2PCenter)
Dim R As Double = V.Length
If R <= C.Radius Then
acEd.WriteMessage(vbLf & "点在圆内,不存在圆切线,请重新选择点:")
End If
Dim NewC As New Circle()
NewC.SetDatabaseDefaults()
NewC.Radius = R
NewC.Center = P2PCenter
'切点
Dim QieDians As New Point3dCollection()
NewC.IntersectWith(C, Intersect.OnBothOperands, QieDians, 0, 0)
'画切线
For Each QD As Point3d In QieDians
Dim L As New Line(QD, P)
Btr.AppendEntity(L)
acTrans.AddNewlyCreatedDBObject(L, True)
Next
acTrans.Commit()
acTrans.Dispose()
End Sub
End Class
Imports Autodesk.AutoCAD.EditorInput
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
Public Class 切线
'以点P到圆C圆心的线段为直径的圆NewC与圆C的两个交点即为点P到圆C的两个切点
<CommandMethod("qieXian")> _
Public Sub test()
Dim acDoc As Document = Application.DocumentManager.MdiActiveDocument
Dim acDb As Database = acDoc.Database
Dim acEd As Editor = acDoc.Editor
Dim acTrans As Transaction = acDb.TransactionManager.StartTransaction()
Dim Bt As BlockTable = TryCast(acTrans.GetObject(acDb.BlockTableId, OpenMode.ForRead), BlockTable)
Dim Btr As BlockTableRecord = TryCast(acTrans.GetObject(Bt(BlockTableRecord.ModelSpace), OpenMode.ForWrite), BlockTableRecord)
Dim C As New Circle()
Dim P As New Point3d()
'选取一个圆
Dim acTypedValue As TypedValue() = New TypedValue(0) {}
acTypedValue.SetValue(New TypedValue(0, "CIRCLE"), 0)
Dim acSelFtr As New SelectionFilter(acTypedValue)
Dim acPromptSelectionResult As PromptSelectionResult = acEd.GetSelection(acSelFtr)
If acPromptSelectionResult.Status = PromptStatus.OK Then
Dim Ss As SelectionSet = acPromptSelectionResult.Value
C = TryCast(acTrans.GetObject(Ss(0).ObjectId, OpenMode.ForRead), Circle)
End If
'选取一个点
Dim acPromptPointResult As PromptPointResult
Dim acPromptPointOptions As PromptPointOptions = New PromptPointOptions("圆外选取一点:")
acPromptPointResult = acDoc.Editor.GetPoint(acPromptPointOptions)
If acPromptPointResult.Status = PromptStatus.OK Then
P = acPromptPointResult.Value
End If
Dim P2PCenter As New Point3d((P.X + C.Center.X) / 2, (P.Y + C.Center.Y) / 2, 0) ' 点到圆心的中点
Dim V As Vector3d = P.GetVectorTo(P2PCenter)
Dim R As Double = V.Length
If R <= C.Radius Then
acEd.WriteMessage(vbLf & "点在圆内,不存在圆切线,请重新选择点:")
End If
Dim NewC As New Circle()
NewC.SetDatabaseDefaults()
NewC.Radius = R
NewC.Center = P2PCenter
'切点
Dim QieDians As New Point3dCollection()
NewC.IntersectWith(C, Intersect.OnBothOperands, QieDians, 0, 0)
'画切线
For Each QD As Point3d In QieDians
Dim L As New Line(QD, P)
Btr.AppendEntity(L)
acTrans.AddNewlyCreatedDBObject(L, True)
Next
acTrans.Commit()
acTrans.Dispose()
End Sub
End Class
【圆外一点到圆的切线.rar】点击下载此文件
|
暂时没有评论
发表评论 - 不要忘了输入验证码哦! |