Chief , Noram wrote this. it is working . But too long. Anyways I am using yours in my codes. Just for your info and all help I got from you. Thanks.
Public Class DragCommand
<CommandMethod("AngledDrag")> _
Public Sub RunThisMethod()
Dim dwg As Document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument
Dim drag As New AngledDrag(dwg)
Try
If drag.DragAtAngle() Then
GenerateLine(dwg, drag.StartPoint, drag.EndPoint)
dwg.Editor.WriteMessage(vbLf & "MyCommand executed.")
End If
Catch ex As Autodesk.AutoCAD.Runtime.Exception
dwg.Editor.WriteMessage(vbLf & "Error: {0}" & vbLf, ex.Message)
End Try
End Sub
Private Shared Sub GenerateLine(ByVal dwg As Document, ByVal startPt As Point3d, ByVal endPt As Point3d)
Using tran As Transaction = dwg.Database.TransactionManager.StartTransaction()
Dim br As BlockTableRecord = DirectCast(tran.GetObject(dwg.Database.CurrentSpaceId, OpenMode.ForWrite), BlockTableRecord)
Dim line As New Line(startPt, endPt)
line.SetDatabaseDefaults(dwg.Database)
br.AppendEntity(line)
tran.AddNewlyCreatedDBObject(line, True)
tran.Commit()
End Using
End Sub
End Class
Public Class AngledDrag
Private _dwg As Document
Private _db As Database
Private _editor As Editor
Private _startPoint As New Point3d(0.0, 0.0, 0.0)
Private _endPoint As New Point3d(0.0, 0.0, 0.0)
Private _dragAngle As Double = 45.0
Private _dragLine As Line = Nothing
Private _colorIndex As Integer = 1
Public Sub New(ByVal dwg As Document)
_dwg = dwg
_db = dwg.Database
_editor = dwg.Editor
End Sub
Public ReadOnly Property StartPoint() As Point3d
Get
Return _startPoint
End Get
End Property
Public ReadOnly Property EndPoint() As Point3d
Get
Return _endPoint
End Get
End Property
Public Function DragAtAngle() As Boolean
_endPoint = _startPoint
AddHandler _editor.PointMonitor, New PointMonitorEventHandler(AddressOf Editor_PointMonitor)
Try
'Get end point
If GetEndPoint() Then
Return True
Else
Return False
End If
Finally
ClearTransientGraphics()
RemoveHandler _editor.PointMonitor, AddressOf Editor_PointMonitor
End Try
End Function
Private Sub Editor_PointMonitor(ByVal sender As Object, ByVal e As PointMonitorEventArgs)
DrawDragLine(e.Context.RawPoint)
If _dragLine IsNot Nothing Then
e.AppendToolTipText("Angle: " + _dragAngle.ToString() + vbLf & "Length: " + _dragLine.Length.ToString())
Else
e.AppendToolTipText("")
End If
End Sub
Private Sub DrawDragLine(ByVal mousePoint As Point3d)
ClearTransientGraphics()
Dim pt As Point3d = CalculateEndPoint(mousePoint)
_dragLine = New Line(_startPoint, pt)
_dragLine.SetDatabaseDefaults(_db)
_dragLine.ColorIndex = _colorIndex
Dim col As New IntegerCollection()
TransientManager.CurrentTransientManager.AddTransient(_dragLine, TransientDrawingMode.Highlight, 128, col)
'whenever the dragged line updated, reset _endPoint
_endPoint = pt
End Sub
Private Sub ClearTransientGraphics()
If _dragLine IsNot Nothing Then
Dim col As New IntegerCollection()
TransientManager.CurrentTransientManager.EraseTransient(_dragLine, col)
_dragLine.Dispose()
_dragLine = Nothing
End If
End Sub
Private Function CalculateEndPoint(ByVal mousePoint As Point3d) As Point3d
Dim pt As Point3d = mousePoint
If _dragAngle <= 90.0 OrElse _dragAngle >= 270.0 Then
If mousePoint.X <= _startPoint.X Then
pt = _startPoint
Else
If _dragAngle <= 45.0 OrElse _dragAngle >= 315.0 Then
Dim y As Double = (mousePoint.X - _startPoint.X) * Math.Tan(_dragAngle * Math.PI / 180)
pt = New Point3d(mousePoint.X, _startPoint.Y + y, 0.0)
Else
If _dragAngle > 45.0 AndAlso _dragAngle <= 90.0 Then
If mousePoint.Y < _startPoint.Y Then
pt = _startPoint
Else
Dim x As Double = (mousePoint.Y - _startPoint.Y) / Math.Tan(_dragAngle * Math.PI / 180)
pt = New Point3d(_startPoint.X + x, mousePoint.Y, 0.0)
End If
Else
If mousePoint.Y > _startPoint.Y Then
pt = _startPoint
Else
Dim x As Double = (mousePoint.Y - _startPoint.Y) / Math.Tan(_dragAngle * Math.PI / 180)
pt = New Point3d(_startPoint.X + x, mousePoint.Y, 0.0)
End If
End If
End If
Return pt
End If
End If
If _dragAngle >= 90.0 AndAlso _dragAngle <= 270.0 Then
If mousePoint.X >= _startPoint.X Then
pt = _startPoint
Else
If _dragAngle >= 135.0 AndAlso _dragAngle <= 225.0 Then
Dim y As Double = (mousePoint.X - _startPoint.X) * Math.Tan(_dragAngle * Math.PI / 180)
pt = New Point3d(mousePoint.X, _startPoint.Y + y, 0.0)
Else
If _dragAngle >= 90.0 AndAlso _dragAngle < 135.0 Then
If mousePoint.Y <= _startPoint.Y Then
pt = _startPoint
Else
Dim x As Double = (mousePoint.Y - _startPoint.Y) / Math.Tan(_dragAngle * Math.PI / 180)
pt = New Point3d(_startPoint.X + x, mousePoint.Y, 0.0)
End If
Else
If mousePoint.Y >= _startPoint.Y Then
pt = _startPoint
Else
Dim x As Double = (mousePoint.Y - _startPoint.Y) / Math.Tan(_dragAngle * Math.PI / 180)
pt = New Point3d(_startPoint.X + x, mousePoint.Y, 0.0)
End If
End If
End If
Return pt
End If
End If
Return pt
End Function
Private Function GetEndPoint() As Boolean
'endPoint = new Point3d();
Dim go As Boolean = True
Dim picked As Boolean = False
While go
Dim opt As New PromptPointOptions(vbLf & "Pick point:")
'opt.BasePoint = _startPoint;
'opt.UseBasePoint = true;
opt.Keywords.Add("Start point")
opt.Keywords.Add("Angle")
opt.Keywords.Add("End point")
opt.Keywords.Add("eXit")
opt.Keywords.[Default] = "End point"
opt.AppendKeywordsToMessage = True
opt.AllowArbitraryInput = False
opt.AllowNone = False
Dim res As PromptPointResult = _editor.GetPoint(opt)
If res.Status = PromptStatus.Cancel Then
go = False
Else
Select Case res.Status
Case PromptStatus.Keyword
'_editor.WriteMessage("\n" + res.StringResult);
If res.StringResult.StartsWith("Start") Then
SetStartPoint()
go = True
End If
If res.StringResult.StartsWith("Angle") Then
SetAngle()
go = True
End If
If res.StringResult.StartsWith("eXit") Then
go = False
End If
Exit Select
Case PromptStatus.OK
'endPoint = res.Value;
picked = True
go = False
Exit Select
Case Else
go = True
Exit Select
End Select
End If
End While
Return picked
End Function
Private Sub SetStartPoint()
ClearTransientGraphics()
RemoveHandler _editor.PointMonitor, AddressOf Editor_PointMonitor
Dim opt As New PromptPointOptions(vbLf & "Start point:")
Dim res As PromptPointResult = _editor.GetPoint(opt)
If res.Status = PromptStatus.OK Then
_startPoint = res.Value
End If
AddHandler _editor.PointMonitor, New PointMonitorEventHandler(AddressOf Editor_PointMonitor)
End Sub
Private Sub SetAngle()
ClearTransientGraphics()
RemoveHandler _editor.PointMonitor, AddressOf Editor_PointMonitor
Dim opt As New PromptDoubleOptions(vbLf & "Enter drag-angle in degree [" + _dragAngle.ToString() + "]: ")
opt.AllowNegative = False
opt.AllowZero = True
opt.AllowNone = True
Dim res As PromptDoubleResult = _editor.GetDouble(opt)
If res.Status = PromptStatus.OK Then
_dragAngle = res.Value
If _dragAngle > 360.0 Then
_dragAngle -= 360.0
End If
End If
AddHandler _editor.PointMonitor, New PointMonitorEventHandler(AddressOf Editor_PointMonitor)
End Sub
End Class