Q:
Is there an event or series of events that I can use to determine that the user has drawn an object such as a line? And find the exact parameters of the object, start point, end point, length, etc.
A:
You can implement the requirement using SketchEvents and DocumentEvents. Use DocumentEvents.OnChange to know why the event fired such as Create Line, then use SketchEvents. OnSketchChange to know the sketch object. DocumentEvents.OnChange will be fired before SketchEvents. OnSketchChange is fired.
Here is the VB.Net code:
Private _ctx As NameValueMap
Private _docEvents As DocumentEvents
Private _sketchEvents As SketchEvents
Sub ListenEvents(ByVal app As Inventor.Application)
If (Not app.ActiveDocument Is Nothing) Then
_docEvents = app.ActiveDocument.DocumentEvents
_sketchEvents = app.SketchEvents
AddHandler _docEvents.OnChange, AddressOf OnDocChange
AddHandler _sketchEvents.OnSketchChange, AddressOf OnSketchChange
End If
End Sub
Sub OnDocChange(ByVal ReasonsForChange As CommandTypesEnum,
ByVal BeforeOrAfter As EventTimingEnum,
ByVal Context As NameValueMap,
ByRef HandlingCode As HandlingCodeEnum)
If (BeforeOrAfter = EventTimingEnum.kBefore) Then
_ctx = Context
End If
End Sub
Sub OnSketchChange(ByVal DocumentObject As Document,
ByVal Sketch As Sketch,
ByVal BeforeOrAfter As EventTimingEnum,
ByVal Context As NameValueMap,
ByRef HandlingCode As HandlingCodeEnum)
If (BeforeOrAfter = EventTimingEnum.kAfter) Then
If Sketch.Type = ObjectTypeEnum.kPlanarSketchObject Then
If _ctx.Item(1) = "Create Line" Then
Debug.Print("a line is created")
' The newest line should be the last item of the
' sketchlines, so use Sketch.SketchLines.count
Dim oLine As SketchLine =
Sketch.SketchLines.Item(Sketch.SketchLines.Count)
Debug.Print("startPt.x = " & oLine.Geometry.StartPoint.X &
" startPt.y = " & oLine.Geometry.StartPoint.Y)
Debug.Print("EndPt.x = " & oLine.Geometry.EndPoint.X &
" startPt.y = " & oLine.Geometry.EndPoint.Y)
End If
End If
End If
End Sub