Is SendModelingCommand allowed in Message?[SOLVED]

On 12/05/2015 at 05:18, xxxxxxxx wrote:

User Information:
Cinema 4D Version:   R16 
Platform:      Mac OSX  ; 
Language(s) :     C++  ;

---------
Hi,

I want to call MCOMMAND_CURRENTSTATETOOBJECT when a button is pressed.
I should do this inside Message()'s MSG_DESCRIPTION_COMMAND, I guess, but if I try it my program crashes.
Is this not possible inside Message?

Here is my code:

  
if (type == MSG_DESCRIPTION_COMMAND)  
{  
  DescriptionCommand* dc = (DescriptionCommand* )data;  
  if (dc->id[0].id == MY_BUTTON)  
  {  
      BaseDocument* doc = static_cast<BaseDocument*>(data);  
      if (!doc)  
          return false;  
      BaseObject* myObject = doc->GetFirstObject();  
      if (!myObject)  
          return false;  
      ModelingCommandData cd;  
      cd.doc = doc;  
      cd.op = myObject;  
      SendModelingCommand(MCOMMAND_CURRENTSTATETOOBJECT, cd);  
  }  
}  

Thanks in advance for your help and time!
Greetings,
Casimir Smets

On 12/05/2015 at 06:03, xxxxxxxx wrote:

It is valid and safe. But you cannot simply cast data (which is a DescriptionCommand) into a basedocument coz it does not hold a basedocument pointer.

You should use node->GetDocument() instead (or GetActiveDocument() which should be valid due to a button click most of the time only being triggered interactively..though this is no guarantee so node->GetDocument() is the better alternative).

Also you might need to do this on a copy of the scene.

On 12/05/2015 at 06:22, xxxxxxxx wrote:

Hi,

If I change that, I get no error, and my program doesn't quit.
The only problem is, it is not making it editable..

Greetings,
Casimir Smets

On 12/05/2015 at 06:32, xxxxxxxx wrote:

Hi,

Ignore my previous post ;-)
I forgot to cast it and add it to the scene, feeling stupid now yeeeuy :D

Greetings,
Casimir Smets

On 12/05/2015 at 07:50, xxxxxxxx wrote:

Hi Casimir,

Katachi is right with his answer.
I sense some problems with the concept of casting datatypes here. It is certainly one of the more advanced concepts of C and C++ and probably one which is cause for many (if not most) bugs. As casts are needed in several places in the C4D SDK, I'd recommend to have a read on this topic.
For example here or here or here. Especially the parts about casting pointers are crucial.

On 13/05/2015 at 08:17, xxxxxxxx wrote:

Hi Andreas,

Thanks for your answer!
I shall definately look more into casting!

Greetings,
Casimir Smets