importexport/impexpsample/orimpexpsample_tool.cxx

importexport/impexpsample/orimpexpsample_tool.cxx
/***************************************************************************************
Autodesk(R) Open Reality(R) Samples
(C) 2009 Autodesk, Inc. and/or its licensors
All rights reserved.
AUTODESK SOFTWARE LICENSE AGREEMENT
Autodesk, Inc. licenses this Software to you only upon the condition that
you accept all of the terms contained in the Software License Agreement ("Agreement")
that is embedded in or that is delivered with this Software. By selecting
the "I ACCEPT" button at the end of the Agreement or by copying, installing,
uploading, accessing or using all or any portion of the Software you agree
to enter into the Agreement. A contract is then formed between Autodesk and
either you personally, if you acquire the Software for yourself, or the company
or other legal entity for which you are acquiring the software.
AUTODESK, INC., MAKES NO WARRANTY, EITHER EXPRESS OR IMPLIED, INCLUDING BUT
NOT LIMITED TO ANY IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR
PURPOSE REGARDING THESE MATERIALS, AND MAKES SUCH MATERIALS AVAILABLE SOLELY ON AN
"AS-IS" BASIS.
IN NO EVENT SHALL AUTODESK, INC., BE LIABLE TO ANYONE FOR SPECIAL, COLLATERAL,
INCIDENTAL, OR CONSEQUENTIAL DAMAGES IN CONNECTION WITH OR ARISING OUT OF PURCHASE
OR USE OF THESE MATERIALS. THE SOLE AND EXCLUSIVE LIABILITY TO AUTODESK, INC.,
REGARDLESS OF THE FORM OF ACTION, SHALL NOT EXCEED THE PURCHASE PRICE OF THE
MATERIALS DESCRIBED HEREIN.
Autodesk, Inc., reserves the right to revise and improve its products as it sees fit.
Autodesk and Open Reality are registered trademarks or trademarks of Autodesk, Inc.,
in the U.S.A. and/or other countries. All other brand names, product names, or
trademarks belong to their respective holders.
GOVERNMENT USE
Use, duplication, or disclosure by the U.S. Government is subject to restrictions as
set forth in FAR 12.212 (Commercial Computer Software-Restricted Rights) and
DFAR 227.7202 (Rights in Technical Data and Computer Software), as applicable.
Manufacturer is Autodesk, Inc., 10 Duke Street, Montreal, Quebec, Canada, H3C 2L7.
***************************************************************************************/
//--- Class declaration
#include "orimpexpsample_tool.h"
//--- Registration defines
#define ORTOOLSAMPLE__CLASS ORTOOLSAMPLE__CLASSNAME
#define ORTOOLSAMPLE__LABEL "Sampled data"
#define ORTOOLSAMPLE__DESC "OR - Export sampled data"
//--- FiLMBOX implementation and registration
FBToolImplementation( ORTOOLSAMPLE__CLASS );
FBRegisterTool ( ORTOOLSAMPLE__CLASS,
ORTOOLSAMPLE__LABEL,
ORTOOLSAMPLE__DESC,
FB_DEFAULT_SDK_ICON ); // Icon filename (default=Open Reality icon)
/************************************************
* FiLMBOX Creation Function.
************************************************/
bool ORToolSample::FBCreate()
{
// Tool variables
mExportFile = NULL;
mExporting = false;
mFileOpen = false;
// Initialize lock.
mCriticalSection.Init();
// UI Create & Configure
UICreate ();
UIConfigure ();
// Add idle
OnIdle.Add( this, (FBCallback) &ORToolSample::EventToolIdle );
return true;
}
/************************************************
* FiLMBOX Destruction function.
************************************************/
void ORToolSample::FBDestroy()
{
OnIdle.Remove( this, (FBCallback) &ORToolSample::EventToolIdle );
}
/************************************************
* Create the UI.
************************************************/
void ORToolSample::UICreate()
{
int lW = 200;
int lS = 5;
int lH = 25;
// Add regions
AddRegion ("EditExportFile", "EditExportFile",
lS, kFBAttachLeft, "", 1.0,
lS, kFBAttachTop, "", 1.0,
lW, kFBAttachNone, NULL, 1.0,
lH, kFBAttachNone, NULL, 1.0 );
AddRegion ("EditNumberRate", "EditNumberRate",
0, kFBAttachLeft, "EditExportFile", 1.0,
lS, kFBAttachBottom, "EditExportFile", 1.0,
0, kFBAttachWidth, "EditExportFile", 1.0,
0, kFBAttachHeight, "EditExportFile", 1.0 );
AddRegion ("ButtonExport", "ButtonExport",
0, kFBAttachLeft, "EditNumberRate", 1.0,
lS, kFBAttachBottom, "EditNumberRate", 1.0,
0, kFBAttachWidth, "EditNumberRate", 1.0,
0, kFBAttachHeight, "EditNumberRate", 1.0 );
// Assign regions
SetControl( "EditExportFile", mEditExportFile );
SetControl( "EditNumberRate", mEditNumberRate );
SetControl( "ButtonExport", mButtonExport );
}
/************************************************
* Configure the UI.
************************************************/
void ORToolSample::UIConfigure()
{
mEditNumberRate.Min = 1.0;
mEditNumberRate.Max = 100.0;
mEditNumberRate.Precision = 3.2;
mEditNumberRate.SmallStep = 0.1;
mEditNumberRate.LargeStep = 1.0;
mEditNumberRate.Value = 10.0;
mButtonExport.Caption = "Export";
mButtonExport.Style = kFB2States;
mEditExportFile.Text = "C:\\orimpexpsample.txt";
// Add callbacks
mButtonExport.OnClick.Add ( this,(FBCallback)&ORToolSample::EventButtonExportClick );
}
/************************************************
* Browse button callback.
************************************************/
void ORToolSample::EventButtonExportClick(HIRegister pSender, HKEvent pEvent)
{
// If currently exporting, stop and let the idle stop the export.
if( mButtonExport.State == 0 && mExporting )
{
ExportStop();
}
if( mButtonExport.State == 1 )
{
mExportFile = fopen( mEditExportFile.Text, "wt" );
if( mExportFile )
{
mFileOpen = true;
ExportStart();
}
else
{
FBMessageBox( "Export", "Error opening file!", "Ok", NULL, NULL );
mButtonExport.State = 0;
}
}
}
/************************************************
* UI idle callback.
************************************************/
void ORToolSample::EventToolIdle( HISender pSender, HKEvent pEvent )
{
if( mExporting )
{
// if finished (either provoked or local time beyond current take's timespan), stop
if( (((FBTime)mSystem.LocalTime) > ((FBTimeSpan)mSystem.CurrentTake->LocalTimeSpan).GetStop() ) )
{
ExportStop();
}
// else, continue export
else
{
ExportCurrentAndStep();
}
}
}
/************************************************
* Start exporting.
************************************************/
void ORToolSample::ExportStart()
{
mCriticalSection.Enter();
{
mExportPeriod.SetSecondDouble( 1.0 / ((double)mEditNumberRate.Value) );
mModelList.Clear();
FBGetSelectedModels( mModelList );
mOriginalTime = mSystem.LocalTime;
mPlayerControl.Goto( ((FBTimeSpan)mSystem.CurrentTake->LocalTimeSpan).GetStart() );
mExporting = true;
}
mCriticalSection.Leave();
}
/************************************************
* Stop exporting.
************************************************/
void ORToolSample::ExportStop()
{
mCriticalSection.Enter();
{
mExporting = false;
mButtonExport.State = 0;
mPlayerControl.Goto( mOriginalTime );
mFileOpen = false;
fclose( mExportFile );
}
mCriticalSection.Leave();
}
/************************************************
* Export information for current time and step.
************************************************/
void ORToolSample::ExportCurrentAndStep()
{
mCriticalSection.Enter();
{
ExportCurrent();
mPlayerControl.Goto( (FBTime)mSystem.LocalTime + mExportPeriod );
}
mCriticalSection.Leave();
}
/************************************************
* Export information for current time.
************************************************/
void ORToolSample::ExportCurrent()
{
if( mFileOpen )
{
FBVector3d lPos, lRot, lSca;
fprintf( mExportFile, "[%s]", (char*)(((FBTime)mSystem.LocalTime).GetTimeString()) );
// for all of the models in the model list.
for( int i=0;i<mModelList.GetCount(); i++ )
{
fprintf( mExportFile, "\t" );
// get the pos,rot,sca values
mModelList[i]->GetVector( lPos, kModelTranslation, true );
mModelList[i]->GetVector( lRot, kModelRotation, true );
mModelList[i]->GetVector( lSca, kModelScaling, true );
// print to file.
fprintf( mExportFile, "<%s> { T(%3.2f,%3.2f,%3.2f), R(%3.2f,%3.2f,%3.2f), S(%3.2f,%3.2f,%3.2f) }",
(const char*)(mModelList[i]->Name),
lPos[0],lPos[1],lPos[2],
lRot[0],lRot[1],lRot[2],
lSca[0],lSca[1],lSca[2] );
}
fprintf( mExportFile, "\n" );
}
}