BaseObject from PolygonObject?
-
THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED
On 20/07/2007 at 11:16, xxxxxxxx wrote:
User Information:
Cinema 4D Version: R9-r10
Platform: Windows ; Mac ; Mac OSX ;
Language(s) : C++ ;---------
Howdy,OK, this is probably very simple, but I just can't get my head around it.
in a CommandData plugin, I have a function that creates a PolygonObject and inserts it into the document. The function is based on this example code from the SDK docs in the Modeling library:
AutoAlloc<PolygonObject> obj(0,0); if (!obj) return FALSE; AutoAlloc<Modeling> mod; if (!mod || !mod->InitObject(obj)) return FALSE; LONG a = mod->AddPoint(obj, Vector(0,0,0)); LONG b = mod->AddPoint(obj, Vector(100,0,0)); LONG c = mod->AddPoint(obj, Vector(150,50,0)); LONG d = mod->AddPoint(obj, Vector(100,100,0)); LONG e = mod->AddPoint(obj, Vector(0,100,0)); if (!a || !b || !c || !d || !e) return FALSE; LONG padr[] = {a,b,c,d,e}; LONG cnt = sizeof(padr)/sizeof(padr[0]); LONG i = mod->CreateNgon(obj, padr, cnt); if (!i) return FALSE; if (!mod->Commit()) return FALSE; doc->InsertObject(obj.Release(), NULL, NULL); EventAdd();
The problem is, that the function needs to return a BaseObject pointer to the Execute() function in the CommandData:
BaseObject *polyOp = CreatePolygonObject(doc,op); if(!polyOp) { GePrint("no polyOp"); return TRUE; } .... perform other operations on polyOp ...
The function is setup like this:
BaseObject* CreatePolygonObject(BaseDocument *doc,BaseObject *op) { AutoAlloc<PolygonObject> obj(0,0); if (!obj) return NULL; ..... code to create polygon object .... return static_cast<BaseObject*>(obj); }
The function creates the object alright and inserts it into the document, but upon returning to the Execute() function, the console prints "no polyOp" and the Execute() function returns without continuing the rest of the operations on the object.
What is the proper way to return a BaseObject pointer?
I've tried several different things on the return. The above example is just the latest thing I tried.Adios,
Cactus Dan -
THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED
On 20/07/2007 at 12:44, xxxxxxxx wrote:
AutoAlloc<> is your problem. With AutoAlloc<>, the scope is within the current method or block within. As soon as you exit the method, obj is freed!
You'll have to dig deep and do the BaseObject* obj = BaseObject::Alloc(Opolygon); call and either free it later or insert it into the document. If you just need it temporarily, you'll need to keep obj within scope until you are completely done with it - read: AutoAlloc and set it up in the method where it will be used.
-
THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED
On 20/07/2007 at 13:34, xxxxxxxx wrote:
Howdy,
Well, the object is inserted in the document with the line:
doc->InsertObject(obj.Release(), NULL, NULL);
..... in the function before returning, but I can't seem to get a BaseObject pointer from it.
Or am I missing something?
Adios,
Cactus Dan -
THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED
On 20/07/2007 at 14:33, xxxxxxxx wrote:
Ah, I see. So Release() transfers ownership - but note that it also sets the pointer to NULL.
Note also that in the SDK Doc example, the auto allocated object is done in the same method/scope to guarantee that it isn't released prematurely. I'd say that you'll have to remove the CreatePolygonObject() method and integrate it into the Execute() method where everything occurs. There is just no way that you'll get that obj out of scope without it being freed - even the transfer of ownership leaves you with a NULL pointer.
-
THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED
On 20/07/2007 at 17:32, xxxxxxxx wrote:
Howdy,
Well,this seems to work:
BaseObject *opRet = (BaseObject* )obj; doc->InsertObject(obj.Release(),NULL, NULL); EventAdd(); return opRet; }
... if I cast the pointer before inserting the object it returns the pointer correctly.
Adios,
Cactus Dan -
THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED
On 20/07/2007 at 20:57, xxxxxxxx wrote:
Howdy,
Hmmmm, it creates the polgons ok, but I can't select the object in the view port. It's like Cinema doesn't see the object as a selectable object.
Why is that? Did I forget to do something?
Adios,
Cactus Dan -
THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED
On 20/07/2007 at 21:39, xxxxxxxx wrote:
Howdy,
DOH! DOH! DOH! the polygon selection filter was disabled. So never mind, everything is working now.
Adios,
Cactus Dan -
THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED
On 21/07/2007 at 06:10, xxxxxxxx wrote:
Good to here. Wasn't certain if using another pointer would still retain validity but if it works, it works.