Author Topic: Error in DirectCast??  (Read 1654 times)

0 Members and 1 Guest are viewing this topic.

xixsuse

  • Guest
Error in DirectCast??
« on: November 07, 2010, 01:40:52 PM »
Sorry for my bad English ....  :-(
I know someone indicate where the error?
VS2010 gives me error in AcadApplication
Thanks

Public Shared Function MyGetPoint(ByVal prompt As String) As Point3d
        Dim a As Array = DirectCast(DirectCast(Autodesk.AutoCAD.ApplicationServices.Application.AcadApplication, AcadApplication).ActiveDocument.Utility.GetPoint(Type.Missing, prompt), Array)
        Dim point As New Point3d()
        point.X = CDbl(a.GetValue(0))
        point.Y = CDbl(a.GetValue(1))
        point.Z = CDbl(a.GetValue(2))
        Return point
    End Function

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16727
  • Superior Stupidity at its best
Re: Error in DirectCast??
« Reply #1 on: November 07, 2010, 01:56:17 PM »
Is Autodesk.AutoCAD.ApplicationServices.Application.AcadApplication imported into the application? Does the compiler know what an AcadApplication is?

I see you are typing the application to an AcadApplication type, but declaring a variable doesn't initialize it. Since you can presume AcadApplication exists in the current context (if it is a netloaded app) you should only need the application interface to generate the point.

The problem with debugging that code is it fails on the entire line and you can't be sure which event caused the failure.

As a test, try isolating each item.

Code: [Select]
Public Shared Function MyGetPoint(ByVal prompt As String) As Point3d
        Dim app As AcadApplication = DirectCast(Autodesk.AutoCAD.ApplicationServices.Application.AcadApplication, AcadApplication)
        Dim a As Array = DirectCast(app.ActiveDocument.Utility.GetPoint(Type.Missing, prompt), Array)
        Dim point As New Point3d()
        point.X = CDbl(a.GetValue(0))
        point.Y = CDbl(a.GetValue(1))
        point.Z = CDbl(a.GetValue(2))
        Return point
    End Function

When looking at it in this context, it appears as though app is nothing therefore the function will fail with a null reference
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

sinc

  • Guest
Re: Error in DirectCast??
« Reply #2 on: November 07, 2010, 02:04:54 PM »
Rather than trying to use the COM interops to do this task, you might want to try staying in the managed world.

So rather than trying to get the AcadApplication from Application and trying to use the old Utility class, use Application.DocumentManager.MdiActivedocument.Editor and the methods on it.  This will even give you the Point3d directly (via the PromptPointResult.Value).

xixsuse

  • Guest
Re: Error in DirectCast??
« Reply #3 on: November 07, 2010, 08:17:40 PM »
is an old tool that I use, but now I realize
the concept is wrong, do not use COM interops
many thanks

xixsuse

  • Guest
Re: Error in DirectCast??
« Reply #4 on: November 07, 2010, 08:37:43 PM »
  Public Shared Function MyGetPoint(ByVal prompt As String) As Point3d
        Dim point As Point3d = Application.DocumentManager.MdiActiveDocument.Editor.GetPoint(prompt).Value
        Return point
    End Function

so it should work
thanks for the advice

sinc

  • Guest
Re: Error in DirectCast??
« Reply #5 on: November 08, 2010, 12:10:28 AM »
It is better to get the result of the GetPoint() method and make sure its Status is equal to PromptStatus.OK before you get its Value.  For example, if the user hits ESC instead of selecting a point, the Status will be PromptStatus.Cancel instead of PromptStatus.OK.

n.yuan

  • Bull Frog
  • Posts: 279
Re: Error in DirectCast??
« Reply #6 on: November 09, 2010, 10:41:14 AM »
I knew that you have already realized that you do not need to use COM API to get a point. Just like to point out why was the error from your original "DirectCast(...)":

The error in from the inner DirectCast(), here is your code:

[Dim something As Autodesk.AutoCAD.Interop.IAcadApplication] = DirectCast(Autodesk.AutoCAD.ApplicationServices.Application.AcadApplication, AcadApplication).

Note: the [...] part is what I added to simplify your original statement. Also,  the second parameter the DirectCast() "AcadApplication" must be decleared somewhere as "object" (thus the need to cast, right?), and you probably get this reference from Autodesk.AutoCAD.ApplicationServices.Application's AcadApplication property in your previous code.

Here the error is Autdesk.AutoCAD.ApplicationServices.Application.AcadApplication is an OBJECT type that points to the COM object IAcadApplication. That is, the DirectCast() actually does nothing, and yet you try to assign it to COM IAcadApplication reference, hence the error.



Sorry for my bad English ....  :-(
I know someone indicate where the error?
VS2010 gives me error in AcadApplication
Thanks

Public Shared Function MyGetPoint(ByVal prompt As String) As Point3d
        Dim a As Array = DirectCast(DirectCast(Autodesk.AutoCAD.ApplicationServices.Application.AcadApplication, AcadApplication).ActiveDocument.Utility.GetPoint(Type.Missing, prompt), Array)
        Dim point As New Point3d()
        point.X = CDbl(a.GetValue(0))
        point.Y = CDbl(a.GetValue(1))
        point.Z = CDbl(a.GetValue(2))
        Return point
    End Function