Hey, from time to time a get a System.InvalidCastException within the following method: public override void SetReference(int i, IReferenceTarget rtarg) { if (i == 0) { paramBlock = (IIParamBlock2)rtarg; } } This method is used when working with ParamBlock2. Actually the error says that it can not cast an Object from my PlugIn-Type (I am coding a Modifier at the moment) to an Object of Type IIParamBlock2. So I assume that the argument rtarg does sometimes contain a reference target of my PlugIn and (i==0) is valid as well. What is the reason for that? Did you ever get this error Marsel? Thx in Advance Michbeck |
| Hi Michbeck, That is strange indeed, I do not think I ever got that. However, I usually also do something like if( rtarg is IIParamBlock2 ) {...}, however, I cannot really think of a reason why it'd try to set an indexed reference not of the type you expect. There is a param block index property in the paramblock desc constructor where you can specify which reference index it gets but, unless you altered it, I can't see why it'd do that. Marsel Khadiyev (Software Developer, EPHERE Inc.) |
| Hey Marsel, I just tried now to insert a type testing like you do - but I dont know how it works. Does the class IParamblock2 have a ClassID which I can use for comparison or in what way are you doing the test: if (rtarg is IIParamBlock2) {...} . While debugging I recognized that the rtarg argument releases another exception: System.AccessViolationException. It happens only if I open up the drop down list of the Modifier Panel in Max listing all modifiers (note: I am coding a modifier plugIn at the moment). Is this exception normal or migth it be root of the problem? The exception is actually caught by the system in this moment, and I only saw it by debugging the rtarg argument. Greets, Michbeck |
| I would just check for rtarg is IIParamBlock2, if that returns true you can safely cast it. Likewise you can do something like rtarg.SuperClassID == SClass_ID.ParamBlock2 (not sure if that syntax is 100% correct but something like that). From what I know whenever you open up the list in the modifier panel 3dsmax first creates and instance of every modifier, then it checks every modifier's InputType property and only displays those modifiers that have InputType that can be cast to by the top object in the stack. I can't really say what could be causing the access exception (it has to do something with either invalid pointer being called or object being cast to a wrong type and then used), however, InputType, SetReference, and the constructor of the class is where i'd look for the source of the problem. If you're not able to resolve it still perhaps I can take a look at your code later and figure out whats going on (or try to reproduce the problem myself). Marsel Khadiyev (Software Developer, EPHERE Inc.) |
| Thank you very much Marsel! Your code is correct and I use it now to guarantee correct type of rtarg - so the problem hopefully might not evolve again :) Yes you are right - I've read the SDK documentation and an instance of every modifier is created to check the inputType and thus display the correct list of applyable modifiers! Greets, Michbeck |
| Hey Marsel - I still encounter this problem! It appears randomly, I dont know the reason! Even if I check the type of the parameter rtarg to be sure getting a Paramblock2 it doesnt solve the problem! In this case a kind of null-pointer exception is thrown as something could not be initialized properly?! Maybe you have an idea what the reason might be?! |
| Can you paste the exact text for the exception thrown? Maybe it'll ring a bell. Marsel Khadiyev (Software Developer, EPHERE Inc.) |
| System.InvalidCastException: Das Objekt des Typs "ConvertGFXModifier.ConvertGFXModifier" kann nicht in Typ "Autodesk.Max.IIParamBlock2" umgewandelt werden. bei ExtrudeModifier.ExtrudeModifier.SetReference(Int32 i, IReferenceTarget rtarg) bei Autodesk.Max.Wrappers.ManagedWrapperReferenceMaker.SetReference(ManagedWrapperReferenceMaker* , Int32 i, ReferenceTarget* rtarg) This is the error text! Sry for german - it means: Object of type ... can not be converted to ... I hope this helps. Have a nice weekend. Greets, Michbeck |