- UID
- 4508
- 积分
- 2485
- 精华
- 贡献
-
- 威望
-
- 活跃度
-
- D豆
-
- 在线时间
- 小时
- 注册时间
- 2002-4-29
- 最后登录
- 1970-1-1
|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
×
本帖最后由 snsj 于 2013-8-1 22:20 编辑
- Option Explicit Off '隐式声明变量
- Imports Autodesk.AutoCAD.ApplicationServices
- Imports Autodesk.AutoCAD.DatabaseServices
- Imports Autodesk.AutoCAD.EditorInput
- Imports Autodesk.AutoCAD.Geometry
- Imports Autodesk.AutoCAD.Runtime
- Public Class AU_DrawJig
- Inherits Autodesk.AutoCAD.EditorInput.DrawJig
- Dim ed As Editor = Application.DocumentManager.MdiActiveDocument.Editor
- Dim db As Database = HostApplicationServices.WorkingDatabase() '将数据库作为公用函数
- Dim el_pt1, el_pt2, elcen As Point3d : Dim normal As Vector3d = Vector3d.ZAxis '椭圆的轴的两点和Z轴矢量
- Dim elent As Ellipse '在内存中创建椭圆
- Dim ratio As Double '短轴与长轴的长度比例
- Dim czdst As Double '长半轴距离
- Dim vec As Vector2d : Dim ddd As Double : Dim elmaj, elmaj1 As Vector3d : Dim ang As Double
- Public Shared Function apple_polar3d(ByVal pPt As Point3d, ByVal dAng As Double, ByVal dDist As Double)
- Return New Point3d(pPt.X + dDist * Math.Cos(dAng), pPt.Y + dDist * Math.Sin(dAng), pPt.Z)
- End Function
- <CommandMethod("cc")> _
- Public Sub DoJigA()
- '设置轴端点1
- Dim el_ptopt As PromptPointOptions = New PromptPointOptions(vbCrLf & "点取椭圆的轴端点<第一点>:")
- el_ptopt.AllowNone = True '允许回车/空格响应
- Dim respt As PromptPointResult = ed.GetPoint(el_ptopt) '开始动态取值
- If respt.Status = PromptStatus.Cancel Then '如果取消了就退出
- Return
- End If
- el_pt1 = respt.Value '设置椭圆轴端点1
- '设置轴端点2
- Dim el_ptopt1 As PromptPointOptions = New PromptPointOptions(vbCrLf & "点取椭圆的轴端点<第二点>:")
- el_ptopt1.AllowNone = True '允许回车/空格响应
- el_ptopt1.UseBasePoint = True
- el_ptopt1.BasePoint = el_pt1 '使用上一个点作为基点
- Dim respt1 As PromptPointResult = ed.GetPoint(el_ptopt1) '开始动态取值
- If respt1.Status = PromptStatus.Cancel Or el_pt1.DistanceTo(el_pt2) = 0 Then '如果取消了或两点距离为0就退出
- Return
- End If
- el_pt2 = respt1.Value '设置椭圆轴端点2
- elcen = New Point3d((el_pt1.X + el_pt2.X) / 2, (el_pt1.Y + el_pt2.Y) / 2, 0) '得到椭圆圆心,只取平面点,两点的中点
- elmaj = elcen.GetVectorTo(el_pt2) '长轴矢量,起点为圆心,中点为长轴的一个端点
- ratio = 0.0001 '短轴与长轴的长度比例
- czdst = el_pt1.DistanceTo(elcen) '长轴一半的长度
- ddd = 0.000001
- el_pt12d = New Point2d(el_pt1.X, el_pt1.Y) : el_pt22d = New Point2d(el_pt2.X, el_pt2.Y)
- vec = el_pt12d - el_pt22d '从A到B的向量
- ang = vec.Angle + Math.PI * 0.5 '角度
- elent = New Ellipse(Point3d.Origin, Vector3d.ZAxis, New Vector3d(0.000001, 0.0, 0.0), 1, 0.0, 0.0) '初始化椭圆
- 'If SamplerStatus.Cancel Then
- Using trans As Transaction = db.TransactionManager.StartTransaction()
- Dim bt As BlockTable = DirectCast(trans.GetObject(db.BlockTableId, OpenMode.ForRead), BlockTable)
- Dim btr As BlockTableRecord = DirectCast(trans.GetObject(db.CurrentSpaceId, OpenMode.ForWrite), BlockTableRecord)
- Dim jigRes As PromptResult = ed.Drag(Me) ' 开始拖动。
- If jigRes.Status = PromptStatus.OK Then
- ' 将圆对象加入到图形数据库中。
- btr.AppendEntity(elent)
- trans.AddNewlyCreatedDBObject(elent, True)
- trans.Commit()
- End If
- End Using
- 'End If
- End Sub
- Protected Overrides Function Sampler(ByVal prompts As JigPrompts) As SamplerStatus
- Dim prOptions2 As New JigPromptDistanceOptions(vbLf & "点取生成椭圆/回车结束:")
- prOptions2.UseBasePoint = True
- prOptions2.BasePoint = elcen '以椭圆圆心为基点
- prOptions2.Cursor = CursorType.NoSpecialCursor
- myPR = prompts.AcquireDistance(prOptions2) '得到一个距离DOUBLE
- curRat = myPR.Value
- If myPR.Status <> PromptStatus.Cancel Then
- If ddd = curRat Or ddd = 0 Then '数值不能为0
- Return SamplerStatus.NoChange
- Else
- If curRat <> 0 Then
- ddd = curRat
- ratio = Math.Abs(ddd / czdst) '一定要取绝对值,因为有可能出现负数
- If ratio > 1 Then
- elmaj1 = elcen.GetVectorTo(apple_polar3d(elcen, ang, ddd))
- ratio = 1 / ratio
- elent.Set(elcen, normal, elmaj1, ratio, 0, 2 * Math.PI) '更新椭圆参数
- Else
- elent.Set(elcen, normal, elmaj, ratio, 0, 2 * Math.PI) '更新椭圆参数
- End If
- Return SamplerStatus.OK
- Else
- Return SamplerStatus.NoChange
- End If
- End If
- Else
- Return SamplerStatus.Cancel '如果用户按ESC拖拽取消
- End If
- End Function
- Protected Overrides Function WorldDraw(ByVal draw As Autodesk.AutoCAD.GraphicsInterface.WorldDraw) As Boolean
- draw.Geometry.Draw(elent)
- Return True
- End Function
- End Class
|
|