How to end command 1 before run command 2


#1

There are two commands “C1” & “C2”. The two commands inherits from CRhinoScriptCommand. I run C1 command and run C2 command when C1 still running. How can I end command C1 before running C2?

There is no problem when the two commands inherits from CRhinoCommand. How to do when commands inherited from CRhinoScriptCommand?

Best Regards,
Vaker


(Dale Fugier) #2

Hi Vaker,

When you press the Esc key while a CRhinoScriptCommand command is running, only the currently running scripted command is canceled, not the entire command.

The workaround for this is for you plug-in to have a CRhinoEventWatcher object that overrides OnEndCommand(). When commands are ended, cache the CRhinoCommand::result code returned by the command. Then, check this code after calling RhinoApp().RunScript() to see if your command should continue.

Let me know if you need an example.


#3

Hi Dale,

I want to enforce end C1 when C2 is beginning. I overrides ONBeginCommand(). When C2 is beginning, program cache the event and enforce end C1. Is is right?

Please give me an example.
Thank you.


(Dale Fugier) #4

Hi Vaker,

See if this example helps:

https://github.com/mcneel/Rhino5Samples_CPP/blob/master/SampleCommands/cmdSampleScripter.cpp

Dale Fugier
Rhinoceros Development
Robert McNeel & Associates


#5

Hi Dale,

Thank you for example. But this is not I wanted.

Command 1 and command 2 are not called in the same RunCommand()…I do not know when command 1 or command 2 begin running.
I use CRhinoEventWatcher:: OnBeginCommand() to cache the event. If the last command is still running, I will stop the last command. I do not know how to end command.(use what function?)
See the sample code:

class CSampleScriptCancelHelper : public CRhinoEventWatcher
{
public:
  CSampleScriptCancelHelper();
  ~CSampleScriptCancelHelper();

  CRhinoCommand::result LastCommandResult() const;

public:
    void OnBeginCommand( const CRhinoCommand& command, const CRhinoCommandContext& context );
    void OnEndCommand( const CRhinoCommand& command, const CRhinoCommandContext& context, CRhinoCommand::result rc );

private:
    int m_iCallCommand;
    const CRhinoCommand* m_pLastCommand;
};

CSampleScriptCancelHelper::CSampleScriptCancelHelper()
{
  m_iCallCommand = 0;
  m_pLastCommand = NULL;
}

CSampleScriptCancelHelper::~CSampleScriptCancelHelper()
{
  m_iCallCommand = 0;
  m_pLastCommand = NULL;
}

void CSampleScriptCancelHelper::OnBeginCommand( const CRhinoCommand& command, const CRhinoCommandContext& context )
{
  m_iCallCommand++;
  if ( m_iCallCommand > 1 )
  {
     **How to stop m_pLastCommand??**
  }
  else
    m_pLastCommand = &command;
}

void CSampleScriptCancelHelper::OnEndCommand( const CRhinoCommand& command, const CRhinoCommandContext& context, CRhinoCommand::result rc )
{
  m_iCallCommand = 0;
}

(Dale Fugier) #6

About the only think you can do programatically to cancel a running command is post a “!” character using CRhinoApp::RunScript. For example:

RhinoApp().RunScript( L"!", 0 );

There are no guarantees this will work, as the command might not be in a state where it can be canceled.


(Dale Fugier) #7

About the only think you can do programatically to cancel a running command
is post a “!” character using CRhinoApp::RunScript. For example:

RhinoApp().RunScript( L"!", 0 );

There are no guarantees this will work, as the command might not be in a
state where it can be canceled.

Dale Fugier
Rhinoceros Development
Robert McNeel & Associates


#8

OK
Thank you.