An old question


#6

@Serejah , Unfortunately I couldn’t find “autodesk.max.wrappers.dll” in the 3dsmax installation directory. So there is no way to use the method you provided . :disappointed_relieved:


#7

why’s that? it is located in <maxroot>/bin/assemblies folder for ages


#8

:grinning: I’m a raw recruit . I try again.


#9

ICustomMarshaler marshaler = Autodesk.Max.Wrappers.CustomMarshalerSplineShape.GetInstance(“what goes there?”);
System.IntPtr ptr = marshaler.MarshalManagedToNative(obj);
object sobj = Marshal.GetObjectForNativeVariant(ptr);
oldNode.ObjectRef = (IObject)sobj;
There still seems to be no way to convert Line to Spline Shape .


#10

no-no, you have to use NativePointer to cast from native to managed.
check out these threads for more info
https://forums.autodesk.com/t5/forums/searchpage/tab/message?advanced=false&allow_punctuation=false&filter=location&location=forum-board:area-b35&q=MarshalNativeToManaged




#11

These examples are a little esoteric for newcomers,Like how do you create a new managed instance.
Can you tell me directly that How to convert a line into Spline Shape .


#12


This article finally discussed also did not have a result, very regrettable


#13

It is funny, but I just found my own thread with same exact question.
And it turns out that you have to add an edit spline modifier to the line object to convert it to splineshape


make a line object, select it and run

(
	format "-> %\n" (classof $.baseobject)
-- uncomment and check if it works now ;)
-- 	if classof $.baseobject == line do
-- 	(
-- 		addModifier $ (edit_spline())
-- 	)

	g     = (dotNetClass "Autodesk.Max.GlobalInterface").Instance
	inode = g.coreinterface7.getselnode 0
	os    = inode.evalworldstate g.coreinterface7.time true
	obj   = os.obj

	cid    = g.class_id.create splineshape.classid[1] splineshape.classid[2] 
	convertible_to_splineshape = (obj.CanConvertToType cid) != 0

	if convertible_to_splineshape do
	(	
		converted = obj.ConvertToType currenttime.ticks cid
		inode.ObjectRef = converted
	)
	
	format "-> %\n" (classof $.baseobject)

)

#14

Your code will work for any shape(Shape Object) but line.Like you said,Casting to some other type is a dead end.
The only way to do that is to add the Edit_Spline modifier.But when it is refreshed, it disappears again. Whether to put it to Managed.If there are other modifier on the line, it is also a very tricky problem.

IInterface core = g.COREInterface;
int t = core.Time;
int modstyle = core.CommandPanelTaskMode;
IClass_ID EDITABLESPLINE_CLASS_ID = g.Class_ID.Create(96,0);
object objMod = core.CreateInstance(SClass_ID.Osm, EDITABLESPLINE_CLASS_ID);
IModifier mod = (IModifier)objMod;
IIDerivedObject dobj = g.CreateDerivedObject(obj);
dobj.AddModifier(mod, null, 0); // top of stack
oldNode.ObjectRef = dobj;
core.CollapseNode(oldNode, false);


#15

When I used OutlineMove in ISplineShape state.It goes back to a line.Miracle!!Do I have to update it, or there is an important step missing

ISplineShape spl = (ISplineShape)sobj;
spl.OutlineMove(t,10);


#16

just a quick dirty test code

static public ISplineShape GetSplineShape(uint handle)
        {

            IGlobal global = GlobalInterface.Instance;
            var node = global.COREInterface7.GetINodeByHandle(handle);
            IObject obj = node.EvalWorldState(global.COREInterface.Time, true).Obj;


            if (obj.CanConvertToType(global.SplineShapeClassID) > 0)
            {

                IInterface core = global.COREInterface;
                int t = core.Time;
                int modstyle = core.CommandPanelTaskMode;
                IClass_ID EDITABLESPLINE_CLASS_ID = global.Class_ID.Create(96, 0);
                object objMod = core.CreateInstance(SClass_ID.Osm, EDITABLESPLINE_CLASS_ID);
                IModifier mod = (IModifier)objMod;
                IIDerivedObject dobj = global.CreateDerivedObject(obj);
                dobj.AddModifier(mod, null, 0); // top of stack
                node.ObjectRef = dobj;
                core.CollapseNode(node, false);


                ISplineShape ss = (ISplineShape)node.ObjectRef.ConvertToType(global.COREInterface.Time, global.SplineShapeClassID);


                return ss;
            }

            return null;

        }

mxs test code

classof $
x = getsplineshape $.inode.handle -- c# method
setSplineSelection $ #(1)
max modify mode
x.BeginOutlineMove currenttime.ticks;
x.outlinemove currenttime.ticks 10.0
x.EndOutlineMove currenttime.ticks true
redrawViews()
classof $

o6jTXfQ29W


#17
using Autodesk.Max;
namespace SRYShapeTools
{
    public class SRYSplineShape
    {
        static public ISplineShape GetSplineShape(uint handle)
        {

            IGlobal global = GlobalInterface.Instance;
            var node = global.COREInterface7.GetINodeByHandle(handle);
            IObject obj = node.EvalWorldState(global.COREInterface.Time, true).Obj;


            if (obj.CanConvertToType(global.SplineShapeClassID) > 0)
            {

                IInterface core = global.COREInterface;
                int t = core.Time;
                int modstyle = core.CommandPanelTaskMode;
                IClass_ID EDITABLESPLINE_CLASS_ID = global.Class_ID.Create(96, 0);
                object objMod = core.CreateInstance(SClass_ID.Osm, EDITABLESPLINE_CLASS_ID);
                IModifier mod = (IModifier)objMod;
                IIDerivedObject dobj = global.CreateDerivedObject(obj);
                dobj.AddModifier(mod, null, 0); // top of stack
                node.ObjectRef = dobj;
                core.CollapseNode(node, false);


                ISplineShape ss = (ISplineShape)node.ObjectRef.ConvertToType(global.COREInterface.Time, global.SplineShapeClassID);


                return ss;
            }

            return null;

        }
    }
}

mxs code

dotNet.loadAssembly @"c:\SRYShapeTools.dll"
SRYgroupClass = dotNetObject "SRYShapeTools.SRYSplineShape"
classof $
x = SRYgroupClass.GetSplineShape $.inode.handle -- c# method
setSplineSelection $ #(1)
max modify mode
x.BeginOutlineMove currenttime.ticks;
x.outlinemove currenttime.ticks 10.0
x.EndOutlineMove currenttime.ticks true
>RedrawViews()
classof $

Same code, different results, very strange


#18

x.BeginOutlineMove currenttime.ticks;
x.outlinemove currenttime.ticks 10.0
x.EndOutlineMove currenttime.ticks true

These three lines run, and it goes back to line.


#19

maybe it is something undo related? it is worth checking with undo off
try it with different max versions and different line objects to ensure that it isn’t a bug


#20

That’s exactly what you said, I add “with undo on” in mxs code.If I don’t have to undo the function, it’s not Complete function.Do you have any good suggestions?


#21

It looks like you need two separate undo records, one for the conversion operation, second for the operation on spline like outline or similar


#22

Let me try, thank you ,@Serejah


#23

I found a new way, but it’s not stable.Don’t know what went wrong.Whether a Notify Dependents is required

//obj is Line 
int t = g.COREInterface.Time;
ISplineShape ss = g.SplineShape.Create();
IShapeObject spl = (IShapeObject)obj;
IBezierShape bz = ss.Shape;
spl.MakeBezier(t, bz);
node.ReplaceReference(1, ss, true);

#24

I think it is a bit overkill to make a bezier out of a linear shape, but if you don’t want to mess with the edit_spline modifier it might be better. In other cases simple conversion with ConvertToType should work just fine.
You previously asked about undo/redo implementation in c#. Did you manage to make it work for shapes conversion?

... https://forums.cgsociety.org/t/how-can-i-write-a-undo-redo-using-c/2069476/8
theHo.Begin();
theHo.Put(new SRYRest(handle, all3dLines));
// convert line to splineshape
theHo.Accept("SRYundo");
theHo.Suspend();

#25

The Undo/Redo is not particularly urgent for me, The Line converted to Spline shape is special importance thing for me .If a line is with other Modifiers,I can’t convert it in this state.Because the modifiers will disappear when I use to Collapse Node,That’s what I don’t want to see.Isn’t there an easier way?Why is my new method a bit overkill?Can you go into more detail?I also feel its instability, do not know what the problem is.ths