JIG error

#1
This code generates a uncatchable exception and crashes ...

Code: Select all

#Region "Namespaces"

Imports System.Text
Imports System.Linq
Imports System.Xml
Imports System.Reflection
Imports System.ComponentModel
Imports System.Collections
Imports System.Collections.Generic
Imports System.Windows

Imports System.Windows.Forms
Imports System.Drawing
Imports System.IO


Imports MgdAcApplication = IntelliCAD.ApplicationServices.Application
Imports MgdAcDocument = IntelliCAD.ApplicationServices.Document


#End Region

Namespace AcadNetAddinCS
    Public Class MoveRotationScaleJig
        Inherits DrawJig
#Region "Fields"

        Private mBase As Teigha.Geometry.Point3d

        Private mEntities As New List(Of Entity)()

        Private mTotalJigFactorCount As Integer = 3
        Private mCurJigFactorIndex As Integer = 1
        ' Jig Factor Index
        Public mLocation As Teigha.Geometry.Point3d
        ' Jig Factor #1
        Public mAngle As [Double]
        ' Jig Factor #2
        Public mScaleFactor As [Double]
        ' Jig Factor #3
#End Region

#Region "Constructors"

        Public Sub New(basePt As Point3d)
            mBase = basePt.TransformBy(UCS)

            'TODO: Initialize jig factors and transform them if necessary.
            mLocation = mBase
            mAngle = 0
            mScaleFactor = 1
        End Sub

#End Region

#Region "Properties"

        Public Property Location() As Teigha.Geometry.Point3d
            Get
                Return mLocation
            End Get
            Set
                mLocation = Value
            End Set
        End Property

        Public Property Angle() As [Double]
            Get
                Return mAngle
            End Get
            Set
                mAngle = Value
            End Set
        End Property

        Public Property ScaleFactor() As [Double]
            Get
                Return mScaleFactor
            End Get
            Set
                mScaleFactor = Value
            End Set
        End Property

        Public Property Base() As Teigha.Geometry.Point3d
            Get
                Return mBase
            End Get
            Set
                mBase = Value
            End Set
        End Property

        Public Property TotalJigFactor As Integer
            Get
                Return mTotalJigFactorCount
            End Get
            Set(value As Integer)
                mTotalJigFactorCount = value
            End Set
        End Property
        Public ReadOnly Property AcEditor() As Editor
            Get
                Return MgdAcApplication.DocumentManager.MdiActiveDocument.Editor
            End Get
        End Property

        Public ReadOnly Property UCS() As Matrix3d
            Get
                Return AcEditor.CurrentUserCoordinateSystem
            End Get
        End Property

        Public ReadOnly Property Transformation() As Matrix3d
            Get
                'return Matrix3d.Identity; //* Change it to anything else meaningful.
                Return Matrix3d.Scaling(mScaleFactor, mLocation).PostMultiplyBy(Matrix3d.Rotation(mAngle, Vector3d.ZAxis.TransformBy(UCS), mLocation)).PostMultiplyBy(Matrix3d.Displacement(mBase.GetVectorTo(mLocation)))
            End Get
        End Property

        Public Property Entities() As List(Of Entity)
            Get
                Return mEntities
            End Get
            Set(value As List(Of Entity))
                mEntities = value
            End Set
        End Property
#End Region

#Region "Methods"

        Public Sub AddEntity(ent As Entity)
            mEntities.Add(ent)
        End Sub

        Public Sub TransformEntities()
            Dim mat As Matrix3d = Transformation

            For Each ent As Entity In mEntities
                ent.TransformBy(mat)
            Next
        End Sub

#End Region

#Region "Overrides"

        Protected Overrides Function WorldDraw(draw As Teigha.GraphicsInterface.WorldDraw) As Boolean
            Dim mat As Matrix3d = Transformation

            Dim geo As WorldGeometry = draw.Geometry
            If geo IsNot Nothing Then
                geo.PushModelTransform(mat)

                For Each ent As Entity In mEntities
                    geo.Draw(ent)
                Next

                geo.PopModelTransform()
            End If

            Return True
        End Function

        Protected Overrides Function Sampler(prompts As JigPrompts) As SamplerStatus
            Try

                Select Case mCurJigFactorIndex
                    Case 1
                        Dim prOptions1 As New JigPromptPointOptions(vbLf & "Move:")
                        ' Set properties such as UseBasePoint and BasePoint of the prompt options object if necessary here.
                        prOptions1.UserInputControls = UserInputControls.GovernedByOrthoMode Or UserInputControls.GovernedByUCSDetect

                        Dim prResult1 As PromptPointResult = prompts.AcquirePoint(prOptions1)

                        If prResult1.Status = PromptStatus.Cancel OrElse prResult1.Status = PromptStatus.Error OrElse prResult1.Status = PromptStatus.Keyword Then
                            Return SamplerStatus.Cancel
                        End If

                        If prResult1.Value.Equals(mLocation) Then
                            'Use better comparison method if necessary.
                            Return SamplerStatus.NoChange
                        Else
                            mLocation = prResult1.Value
                            Return SamplerStatus.OK
                        End If
                    Case 2
                        Dim prOptions2 As New JigPromptAngleOptions(vbLf & "Rotate:")
                        prOptions2.UseBasePoint = True
                        prOptions2.BasePoint = mLocation
                        prOptions2.UserInputControls = UserInputControls.GovernedByOrthoMode Or UserInputControls.GovernedByUCSDetect
                        Dim prResult2 As PromptDoubleResult = prompts.AcquireAngle(prOptions2)

                        If prResult2.Status = PromptStatus.Cancel OrElse prResult2.Status = PromptStatus.Error OrElse prResult2.Status = PromptStatus.Keyword Then
                            Return SamplerStatus.Cancel
                        End If

                        If prResult2.Value.Equals(mAngle) Then
                            'Use better comparison method if necessary.
                            Return SamplerStatus.NoChange
                        Else
                            mAngle = prResult2.Value
                            Return SamplerStatus.OK
                        End If
                    Case 3
                        'Dim prOptions3 As New JigPromptDistanceOptions(vbLf & "Scale:")
                        'prOptions3.UseBasePoint = True
                        'prOptions3.BasePoint = mLocation
                        'prOptions3.UserInputControls = UserInputControls.GovernedByOrthoMode Or UserInputControls.GovernedByUCSDetect
                        'Dim prResult3 As PromptDoubleResult = prompts.AcquireDistance(prOptions3)
                        'If prResult3.Status = PromptStatus.Cancel AndAlso prResult3.Status = PromptStatus.[Error] Then
                        '    Return SamplerStatus.Cancel
                        'End If

                        'If prResult3.Value.Equals(mScaleFactor) Then
                        '    'Use better comparison method if necessary.
                        '    Return SamplerStatus.NoChange
                        'Else
                        '    mScaleFactor = prResult3.Value
                        '    Return SamplerStatus.OK
                        'End If
                        Return SamplerStatus.OK
                    Case Else

                        Exit Select
                End Select
            Catch ex As system.Exception
                MsgBox(ex.Message, MsgBoxStyle.Critical, "Erro")
            End Try

            Return SamplerStatus.OK
        End Function

#End Region

#Region "Method to Call"

        Public Function Jig() As Boolean
            Try
                Dim pr As PromptResult
                Do
                    pr = AcEditor.Drag(Me)
                    ' Keyword handling code

                    If pr.Status = PromptStatus.Keyword Then

                    Else
                        Me.mCurJigFactorIndex += 1
                    End If
                Loop While (pr.Status <> PromptStatus.Cancel AndAlso pr.Status <> PromptStatus.Error AndAlso pr.Status <> PromptStatus.Keyword) AndAlso Me.mCurJigFactorIndex <= Me.mTotalJigFactorCount

                If pr.Status = PromptStatus.Cancel Then
                    Return False
                End If

                If Me.mCurJigFactorIndex = Me.mTotalJigFactorCount + 1 Then
                    Return True
                Else
                    Return False
                End If
            Catch ex As Teigha.Runtime.Exception
                Return False
            Catch EX As System.Exception
                Return False
            End Try
        End Function

#End Region

#Region "Commands"

        <CommandMethod("TestMoveRotationScaleJig")>
        Public Shared Sub TestMoveRotationScaleJig_Method()
            Dim db As Database = HostApplicationServices.WorkingDatabase
            Dim ed As Editor = MgdAcApplication.DocumentManager.MdiActiveDocument.Editor
            Try
                Dim selRes As PromptSelectionResult = ed.GetSelection()
                If selRes.Status <> PromptStatus.OK Then
                    Return
                End If

                Dim prOpt As New PromptPointOptions(vbLf & "Base point:")
                Dim pr As PromptPointResult = ed.GetPoint(prOpt)
                If pr.Status <> PromptStatus.OK Then
                    Return
                End If

                Dim jigger As New MoveRotationScaleJig(pr.Value)

                Using tr As Transaction = db.TransactionManager.StartTransaction()
                    For Each id As ObjectId In selRes.Value.GetObjectIds()
                        Dim ent As Entity = DirectCast(tr.GetObject(id, Teigha.DatabaseServices.OpenMode.ForWrite), Entity)
                        jigger.AddEntity(ent)
                    Next

                    If jigger.Jig() Then
                        jigger.TransformEntities()

                    End If

                    tr.Commit()
                End Using
            Catch ex As System.Exception
                ed.WriteMessage(ex.ToString())
            End Try
        End Sub


        Public Shared Sub MoveRotationScaleJig_Method(BasePoint As Point3d, Entities As List(Of ObjectId))
            Dim db As Database = HostApplicationServices.WorkingDatabase
            Dim ed As Editor = MgdAcApplication.DocumentManager.MdiActiveDocument.Editor
            Try


                Dim jigger As New MoveRotationScaleJig(BasePoint)
                Using tr As Transaction = db.TransactionManager.StartTransaction()
                    For Each id As ObjectId In Entities
                        Dim ent As Entity = DirectCast(tr.GetObject(id, Teigha.DatabaseServices.OpenMode.ForWrite), Entity)
                        jigger.AddEntity(ent)
                    Next

                    If jigger.Jig() Then
                        jigger.TransformEntities()
                        tr.Commit()
                    Else
                        tr.Abort()
                    End If
                End Using
            Catch ex As System.Exception
                ed.WriteMessage(ex.ToString())
            End Try
        End Sub
#End Region

    End Class
End Namespace

Re: JIG error

#2
Hi,
Please use TRY .... CATCH to catch errors if any.
Not sure what you want achievement but this can run without any error.

Code: Select all

#Region "Namespaces"

'Imports System.Text
'Imports System.Linq
'Imports System.Xml
'Imports System.Reflection
'Imports System.ComponentModel
'Imports System.Collections
'Imports System.Collections.Generic
'Imports System.Windows

'Imports System.Windows.Forms
'Imports System.Drawing
'Imports System.IO

Imports IntelliCAD.EditorInput
Imports Teigha.Runtime
Imports Teigha.DatabaseServices
Imports Teigha.Geometry

Imports MgdAcApplication = IntelliCAD.ApplicationServices.Application
'Imports MgdAcDocument = IntelliCAD.ApplicationServices.Document


#End Region

Namespace AcadNetAddinCS
    Public Class MoveRotationScaleJig
        Inherits DrawJig
#Region "Fields"

        Private mBase As Teigha.Geometry.Point3d

        Private mEntities As New List(Of Entity)()

        Private mTotalJigFactorCount As Integer = 3
        Private mCurJigFactorIndex As Integer = 1
        ' Jig Factor Index
        Public mLocation As Teigha.Geometry.Point3d
        ' Jig Factor #1
        Public mAngle As [Double]
        ' Jig Factor #2
        Public mScaleFactor As [Double]
        ' Jig Factor #3
#End Region

#Region "Constructors"

        Public Sub New(basePt As Point3d)
            mBase = basePt.TransformBy(UCS)

            'TODO: Initialize jig factors and transform them if necessary.
            mLocation = mBase
            mAngle = 0
            mScaleFactor = 1
        End Sub

#End Region

#Region "Properties"

        Public Property Location() As Teigha.Geometry.Point3d
            Get
                Return mLocation
            End Get
            Set
                mLocation = Value
            End Set
        End Property

        Public Property Angle() As [Double]
            Get
                Return mAngle
            End Get
            Set
                mAngle = Value
            End Set
        End Property

        Public Property ScaleFactor() As [Double]
            Get
                Return mScaleFactor
            End Get
            Set
                mScaleFactor = Value
            End Set
        End Property

        Public Property Base() As Teigha.Geometry.Point3d
            Get
                Return mBase
            End Get
            Set
                mBase = Value
            End Set
        End Property

        Public Property TotalJigFactor As Integer
            Get
                Return mTotalJigFactorCount
            End Get
            Set(value As Integer)
                mTotalJigFactorCount = value
            End Set
        End Property
        Public ReadOnly Property AcEditor() As Editor
            Get
                Return MgdAcApplication.DocumentManager.MdiActiveDocument.Editor
            End Get
        End Property

        Public ReadOnly Property UCS() As Matrix3d
            Get
                Return AcEditor.CurrentUserCoordinateSystem
            End Get
        End Property

        Public ReadOnly Property Transformation() As Matrix3d
            Get
                'return Matrix3d.Identity; //* Change it to anything else meaningful.
                Return Matrix3d.Scaling(mScaleFactor, mLocation).PostMultiplyBy(Matrix3d.Rotation(mAngle, Vector3d.ZAxis.TransformBy(UCS), mLocation)).PostMultiplyBy(Matrix3d.Displacement(mBase.GetVectorTo(mLocation)))
            End Get
        End Property

        Public Property Entities() As List(Of Entity)
            Get
                Return mEntities
            End Get
            Set(value As List(Of Entity))
                mEntities = value
            End Set
        End Property
#End Region

#Region "Methods"

        Public Sub AddEntity(ent As Entity)
            mEntities.Add(ent)
        End Sub

        Public Sub TransformEntities()
            Dim mat As Matrix3d = Transformation

            For Each ent As Entity In mEntities
                ent.TransformBy(mat)
            Next
        End Sub

#End Region

#Region "Overrides"

        Protected Overrides Function WorldDraw(draw As Teigha.GraphicsInterface.WorldDraw) As Boolean
            Dim mat As Matrix3d = Transformation

            Dim geo As Teigha.GraphicsInterface.WorldGeometry = draw.Geometry
            If geo IsNot Nothing Then
                geo.PushModelTransform(mat)

                For Each ent As Entity In mEntities
                    geo.Draw(ent)
                Next

                geo.PopModelTransform()
            End If

            Return True
        End Function

        Protected Overrides Function Sampler(prompts As JigPrompts) As SamplerStatus
            Try

                Select Case mCurJigFactorIndex
                    Case 1
                        Dim prOptions1 As New JigPromptPointOptions(vbLf & "Move:")
                        ' Set properties such as UseBasePoint and BasePoint of the prompt options object if necessary here.
                        prOptions1.UserInputControls = UserInputControls.GovernedByOrthoMode Or UserInputControls.GovernedByUCSDetect

                        Dim prResult1 As PromptPointResult = prompts.AcquirePoint(prOptions1)

                        If prResult1.Status = PromptStatus.Cancel OrElse prResult1.Status = PromptStatus.Error OrElse prResult1.Status = PromptStatus.Keyword Then
                            Return SamplerStatus.Cancel
                        End If

                        If prResult1.Value.Equals(mLocation) Then
                            'Use better comparison method if necessary.
                            Return SamplerStatus.NoChange
                        Else
                            mLocation = prResult1.Value
                            Return SamplerStatus.OK
                        End If
                    Case 2
                        Dim prOptions2 As New JigPromptAngleOptions(vbLf & "Rotate:")
                        prOptions2.UseBasePoint = True
                        prOptions2.BasePoint = mLocation
                        prOptions2.UserInputControls = UserInputControls.GovernedByOrthoMode Or UserInputControls.GovernedByUCSDetect
                        Dim prResult2 As PromptDoubleResult = prompts.AcquireAngle(prOptions2)

                        If prResult2.Status = PromptStatus.Cancel OrElse prResult2.Status = PromptStatus.Error OrElse prResult2.Status = PromptStatus.Keyword Then
                            Return SamplerStatus.Cancel
                        End If

                        If prResult2.Value.Equals(mAngle) Then
                            'Use better comparison method if necessary.
                            Return SamplerStatus.NoChange
                        Else
                            mAngle = prResult2.Value
                            Return SamplerStatus.OK
                        End If
                    Case 3
                        'Dim prOptions3 As New JigPromptDistanceOptions(vbLf & "Scale:")
                        'prOptions3.UseBasePoint = True
                        'prOptions3.BasePoint = mLocation
                        'prOptions3.UserInputControls = UserInputControls.GovernedByOrthoMode Or UserInputControls.GovernedByUCSDetect
                        'Dim prResult3 As PromptDoubleResult = prompts.AcquireDistance(prOptions3)
                        'If prResult3.Status = PromptStatus.Cancel AndAlso prResult3.Status = PromptStatus.[Error] Then
                        '    Return SamplerStatus.Cancel
                        'End If

                        'If prResult3.Value.Equals(mScaleFactor) Then
                        '    'Use better comparison method if necessary.
                        '    Return SamplerStatus.NoChange
                        'Else
                        '    mScaleFactor = prResult3.Value
                        '    Return SamplerStatus.OK
                        'End If
                        Return SamplerStatus.OK
                    Case Else

                        Exit Select
                End Select
            Catch ex As System.Exception
                MsgBox(ex.Message, MsgBoxStyle.Critical, "Erro")
            End Try

            Return SamplerStatus.OK
        End Function

#End Region

#Region "Method to Call"

        Public Function Jig() As Boolean
            Try
                Dim pr As PromptResult
                Do
                    pr = AcEditor.Drag(Me)
                    ' Keyword handling code

                    If pr.Status = PromptStatus.Keyword Then

                    Else
                        Me.mCurJigFactorIndex += 1
                    End If
                Loop While (pr.Status <> PromptStatus.Cancel AndAlso pr.Status <> PromptStatus.Error AndAlso pr.Status <> PromptStatus.Keyword) AndAlso Me.mCurJigFactorIndex <= Me.mTotalJigFactorCount

                If pr.Status = PromptStatus.Cancel Then
                    Return False
                End If

                If Me.mCurJigFactorIndex = Me.mTotalJigFactorCount + 1 Then
                    Return True
                Else
                    Return False
                End If
            Catch ex As Teigha.Runtime.Exception
                Return False
            Catch EX As System.Exception
                Return False
            End Try
        End Function

#End Region

#Region "Commands"

        <CommandMethod("TestMoveRotationScaleJig")>
        Public Shared Sub TestMoveRotationScaleJig_Method()
            Dim db As Database = HostApplicationServices.WorkingDatabase
            Dim ed As Editor = MgdAcApplication.DocumentManager.MdiActiveDocument.Editor
            Try
                Dim selRes As PromptSelectionResult = ed.GetSelection()
                If selRes.Status <> PromptStatus.OK Then
                    Return
                End If

                Dim prOpt As New PromptPointOptions(vbLf & "Base point:")
                Dim pr As PromptPointResult = ed.GetPoint(prOpt)
                If pr.Status <> PromptStatus.OK Then
                    Return
                End If

                Dim jigger As New MoveRotationScaleJig(pr.Value)

                Using tr As Transaction = db.TransactionManager.StartTransaction()
                    For Each id As ObjectId In selRes.Value.GetObjectIds()
                        Dim ent As Entity = DirectCast(tr.GetObject(id, Teigha.DatabaseServices.OpenMode.ForWrite), Entity)
                        jigger.AddEntity(ent)
                    Next

                    If jigger.Jig() Then
                        jigger.TransformEntities()

                    End If

                    tr.Commit()
                End Using
            Catch ex As System.Exception
                ed.WriteMessage(ex.ToString())
            End Try
        End Sub


        Public Shared Sub MoveRotationScaleJig_Method(BasePoint As Point3d, Entities As List(Of ObjectId))
            Dim db As Database = HostApplicationServices.WorkingDatabase
            Dim ed As Editor = MgdAcApplication.DocumentManager.MdiActiveDocument.Editor
            Try


                Dim jigger As New MoveRotationScaleJig(BasePoint)
                Using tr As Transaction = db.TransactionManager.StartTransaction()
                    For Each id As ObjectId In Entities
                        Dim ent As Entity = DirectCast(tr.GetObject(id, Teigha.DatabaseServices.OpenMode.ForWrite), Entity)
                        jigger.AddEntity(ent)
                    Next

                    If jigger.Jig() Then
                        jigger.TransformEntities()
                        tr.Commit()
                    Else
                        tr.Abort()
                    End If
                End Using
            Catch ex As System.Exception
                ed.WriteMessage(ex.ToString())
            End Try
        End Sub
#End Region

    End Class
End Namespace

Re: JIG error

#3
It crashes after some operations ...

Try ... Catch doesn't get anything, but crashes report says it's "Memory violation error - trying to access (read/wrtie) a protected memory" but I can't find which point it is.

My guess is about to have a keyword inside overrides Sampler ...

I also cannot get the ENTRE press, which gives "Invalid Input" ...

Any idea to rewrite Sampler is welcome.