Community
3ds Max Programming
Welcome to Autodesk’s 3ds Max Forums. Share your knowledge, ask questions, and explore popular 3ds Max SDK, Maxscript and Python topics.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

C# issue get interface IIViewportViewSetting

8 REPLIES 8
SOLVED
Reply
Message 1 of 9
Mr.Drm
982 Views, 8 Replies

C# issue get interface IIViewportViewSetting

Hi guys!

Please, help.

 

Win 10 1906 / 3ds max 2019 / VS2019

I am writing this:

private static readonly IInterface_ID IVIEWPORT_SETTINGS_INTERFACE_ID = GlobalInterface.Instance.Interface_ID.Create(0x761840a9, 0x437f4090);
//...//
var
vpt = GlobalInterface.Instance.COREInterface19.ActiveViewExp; var vset = (IIViewportViewSetting) vpt.GetInterface(IVIEWPORT_SETTINGS_INTERFACE_ID);

I get an error:

System.InvalidCastException: Unable to cast object of type 'Autodesk.Max.Wrappers.BaseInterface' to type 'Autodesk.Max.MaxSDK.Graphics.IIViewportViewSetting'

 

Maybe I'm doing something wrong?

And how then to access this interface?

 

Thanks for any help!

8 REPLIES 8
Message 2 of 9
denisT.MaxDoctor
in reply to: Mr.Drm

try to access it from unmanaged memory using GCHandleGCHandle.ToIntPtr, and GCHandle.FromIntPtr...

it's just an idea. I didn't try it but don't see a problem.

 

Message 3 of 9
Mr.Drm
in reply to: denisT.MaxDoctor

I could not access using GCHandle, because I do not fully understand how this can be done. Maybe there is something more obvious?

Message 4 of 9
denisT.MaxDoctor
in reply to: Mr.Drm

this is an example of 'downcasting' ... in c++ you can do "dynamic_cast" but in c# it's not such easy. because IIViewportViewSetting is an abstract class, I don't see any other way than to do it unsafe using a pointer... I might be wrong and maybe anyone else with better c# knowledge can give you the better solution.

 

the situation we have:

    class BaseInterface
    {
           // the things are here
    };
    class IIViewportViewSetting : BaseInterface
    {
           // the things are here
    };
        

so my guess of what can work is:

    BaseInterface bi = // vpt.GetInterface(IVIEWPORT_SETTINGS_INTERFACE_ID);
    GCHandle gch = GCHandle.Alloc(bi);
    IntPtr ptr = GCHandle.ToIntPtr(gch);
    gch.Free();
    IIViewportViewSetting vs = (IIViewportViewSetting)GCHandle.FromIntPtr(ptr).Target;

again.. it's never tested and just my guess...

 

Message 5 of 9
Mr.Drm
in reply to: denisT.MaxDoctor

Yes, that's exactly what I tried.
and in this line of code:

IIViewportViewSetting vs = (IIViewportViewSetting) GCHandle.FromIntPtr(ptr).Target;

same error:

Unable to cast object of type 'Autodesk.Max.Wrappers.BaseInterface' to type Autodesk.Max.MaxSDK.Graphics.IIViewportViewSetting'.

I'm at a dead end.

Спасибо Денис, что помогаете мне.

 

Message 6 of 9
Swordslayer
in reply to: Mr.Drm

There're marshallers specifically for that purpose:

 

using Wrappers = Autodesk.Max.Wrappers;
using Graphics = Autodesk.Max.MaxSDK.Graphics;

...

var vset = (Graphics.IIViewportViewSetting)Graphics.Wrappers.CustomMarshalerIViewportViewSetting.GetInstance(string.Empty).MarshalNativeToManaged((vpt.GetInterface(IVIEWPORT_SETTINGS_INTERFACE_ID) as Wrappers.BaseInterface).INativeObject__NativePointer);

 

Message 7 of 9
Mr.Drm
in reply to: Swordslayer

Sorry, but I don’t understand how to use this.

Clip2net_190815172234.jpeg

Message 8 of 9
Swordslayer
in reply to: Mr.Drm

Copy the parts that I posted, don't rewrite it how you think it should look. The marshaller is not part of the general wrappers, it's part of the Autodesk.Max.MaxSDK namespace. And it looks like you also haven't added a reference to the Autodesk.Max.Wrappers.dll (it's in the %MAXROOT%/bin/assemblies/ folder).

Message 9 of 9
Mr.Drm
in reply to: Swordslayer

Oh, thanks a lot.
I did not know about the existence of Autodesk.Max.Wrappers.dll separately.
Thank you again, everything works like a charm!

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report