VB.net selecting sketch point problems => System.__Comobject

VB.net selecting sketch point problems => System.__Comobject

MICHAEL.JONES.AMCE
Advocate Advocate
969 Views
4 Replies
Message 1 of 5

VB.net selecting sketch point problems => System.__Comobject

MICHAEL.JONES.AMCE
Advocate
Advocate

Attempting to transition away from VBA and iLogic to a VB.net addin.

 

Using the Nifty Add-in Template by Mr. Ekins.

 

Having difficulties casting my selected object into a sketchpoint.

 

error_point_selection_a.png

 

error_point_selection_b.png

 

error_point_selection_c.png

Namespace UCSPlacementApp

    Public Module SelectPoint
        Private WithEvents m_intereaction As Inventor.InteractionEvents
        Private WithEvents m_mouse As Inventor.MouseEvents

        Public Function SelectPoint() As Inventor.SketchPoint

            'Dim MyPointSelection As ClsSelectPoint

            'If MyPointSelection Is Nothing Then
            '    MyPointSelection = New ClsSelectPoint
            'End If

            If g_DebugOn Then
                MsgBox("Getting Point From User", vbOK, "AMA ROUTINE")
            End If

            Dim tempPoint As SketchPoint

            Try
                tempPoint = g_InventorApplication.CommandManager.Pick(SelectionFilterEnum.kSketchPointFilter, "Select UCS origin point.")

                'tempPoint = MyPointSelection.GetPoint

                If tempPoint Is Nothing Then
                    Return Nothing
                End If

            Catch ex As Exception
                MsgBox("Error while having user select the origin point!" & vbCr & vbCr & ex.Message, vbOK, g_AMA_error_title)
                ' CatchErrorcatch:
                Debug.Print(ex.Message)
            End Try

            Try
                tempPoint = DirectCast(tempPoint, SketchPoint)
            Catch
                MsgBox("Error casting selected point!", g_AMA_error_title)
                Exit Function
            End Try

            If Not (tempPoint Is Nothing) Then
                myPoint = tempPoint.Geometry
            End If

            Dim oSketch As PlanarSketch = myPoint.Parent
            Dim my2DPoint As Point2d = myPoint.Geometry
            Dim my3DPoint As Point = oSketch.SketchToModelSpace(my2DPoint)

            'tempPoint = oSketch.SketchToModelSpace(my2DPoint)

            If g_DebugOn Then
                PrintXYZ(tempPoint)
            End If

            'm_intereaction.Stop()

            ' ENABLE THE LISTBOX FOR THE ANGLE
            g_UCSForm.lbAngleInput.Enabled = True
            g_UCSForm.lbAngleInput.Visible = True
            g_UCSForm.lbAngleInput.SelectedItem = ((g_UCSForm.lbAngleInput.Items.Count - 1) / 2) + 1

            Return oSketch.SketchToModelSpace(my2DPoint)

        End Function

 

Thank you in advance for your time in this matter.

 

M.

0 Likes
Accepted solutions (1)
970 Views
4 Replies
Replies (4)
Message 2 of 5

WCrihfield
Mentor
Mentor

Why are you using DirectCast to convert the variable's (tempPoint) type to be SketchPoint, when you have already defined that variable to be the type SketchPoint above that point, and used a selection filter to ensure that's the type of object you are selecting?  It seems unnecessary.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 3 of 5

MICHAEL.JONES.AMCE
Advocate
Advocate

Not sure, that's why I'm asking the question. 

 

We get the error dialog shown above and below, with or without the cast.

 

 

0 Likes
Message 4 of 5

WCrihfield
Mentor
Mentor
Accepted solution

I would eliminate the whole Try...Catch...End Try block around the DirectCast call, because it is not necessary.

I would eliminate the next If Not (tempPoint Is Nothing)....Then..End If block of code too, because you have already checked if 'tempPoint' is Nothing, so it has no use, and hides the variable 'myPoint' first appearance within that block.  Then I would just not use that 'myPoint' variable, because you don't need to.

 

{If its value would have been set successfully, it would have been assigned a Point2d object to it, and therefore set the variable's type (late binding) to Point2d.  Then below, where you were attempting to get a PlanarSketch from 'myPoint.Parent' would have thrown an error, because a Point2d doesn't have a Parent property.}

 

Just use the 'tempPoint' variable in place of the 'myPoint' variable in those next two lines, where you are setting the values of the 'oSketch' and 'my2DPoint' variables.  It is already the right object type and correct reference for use in those two places.

 

Then, there is also the question of what object type you are wanting your Function to return.  The Function's definition line, is stating that it is expecting to return a SketchPoint, but near the bottom of your code, you are using a 'Return' line, that is attempting to return a Point object.  One of the two will have to change.

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 5 of 5

MICHAEL.JONES.AMCE
Advocate
Advocate

Appreciate the assistance and advise.

 

Cleaned up my messy code and validated what exactly is needed.

 

Screencast of the testing framework/app:

https://knowledge.autodesk.com/community/screencast/09001ec5-9381-4e80-939f-321503383036

 

Thank you again.

 

0 Likes