summaryrefslogtreecommitdiffstats
path: root/tools/trackeditor/code/commands/export.cpp
blob: 77b73288f1d51031541f4711235db0d0a24d831f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#include "precompiled/PCH.h"

#include "export.h"
#include "main/constants.h"
#include "nodes/walllocator.h"
#include "nodes/fenceline.h"
#include "nodes/intersection.h"
#include "nodes/road.h"
#include "nodes/pedpath.h"
#include "utility/mui.h"
#include "utility/mext.h"

#include <toollib.hpp>

const char*  ExportCommand::stringId = "TE_Export";
bool ExportCommand::sRegisteredChunks = false;


ExportCommand::ExportCommand() {};
ExportCommand::~ExportCommand() {};

//==============================================================================
// ExportCommand::creator
//==============================================================================
// Description: Comment
//
// Parameters:  ()
//
// Return:      void
//
//==============================================================================
void* ExportCommand::creator()
{
    return new ExportCommand();
}

//==============================================================================
// ExportCommand::doIt
//==============================================================================
// Description: Comment
//
// Parameters:  ( const MArgList& args )
//
// Return:      MStatus 
//
//==============================================================================
MStatus ExportCommand::doIt( const MArgList& args )
{
    if ( !sRegisteredChunks )
    {
        tlDataChunk::RegisterDefaultChunks();
        sRegisteredChunks = true;
    }

    //Go through all the chunks looking for the types we want and exporting them.
    //Alternatively, we could go thtough all the chunks and attempt to access the
    //IExportable interface and then export if the interface exists...

    //Args are all, or selected.

    const unsigned char FILE_NAME_SIZE = 255;
    char filePath[FILE_NAME_SIZE];
    filePath[0] = '\0';

    if ( MUI::FileDialog( filePath,  
                          FILE_NAME_SIZE, 
                          "Track Editor Export", 
                          "Pure3D(*.p3d)|*.p3d|All Files(*.*)|*.*||", 
                          "p3d", 
                          MUI::SAVE ) )
    {
        //TODO: add selected.
        MItDag dagIt( MItDag::kBreadthFirst, MFn::kLocator );

        tlDataChunk* outChunk = new tlDataChunk;

        //Put in a history chunk.
        tlHistory history;
        
//        char hist[256];
//        sprintf(hist, "Track Editor version: 2.0, toollib version: %s", tlversion);
//
//        history.AddLine( hist );

//        outChunk->AppendSubChunk( history.Chunk(), 0 );

        bool deleteLast = false;
        MFnDependencyNode fnNode;
        MObject lastObj;
        MTypeId id;

        while ( !dagIt.isDone() )
        { 
            fnNode.setObject( dagIt.item() );
            id = fnNode.typeId();

            if ( id == FenceLineNode::id )
            {
                //Export a wall locator;
                tlDataChunk* newChunk = FenceLineNode::Export( dagIt.item(), history );

                if ( newChunk )
                {
                    //Append this to the output file.
                    outChunk->AppendSubChunk( newChunk );
                }
                else
                {
                    //Time to go away.
                    deleteLast = true;
                }
            }
            else if ( id == IntersectionLocatorNode::id )
            {
                tlDataChunk* newChunk = IntersectionLocatorNode::Export( dagIt.item(), history );

                if ( newChunk )
                {
                    //Append this to the output file.
                    outChunk->AppendSubChunk( newChunk );
                }
                else
                {
                    //Time to go away.
                    deleteLast = true;
                }
            }
            else if ( id == RoadNode::id )
            {
                tlDataChunk* newChunk = RoadNode::Export( dagIt.item(), history, outChunk );

                if ( newChunk )
                {
                    //Append this to the output file.
                    outChunk->AppendSubChunk( newChunk );
                }
                else
                {
                    //Time to go away.
                    deleteLast = true;
                }
            }
            else if ( id == PedPathNode::id )
            {
                tlDataChunk* newChunk = PedPathNode::Export( dagIt.item(), history );

                if ( newChunk )
                {
                    //Append this to the output file.
                    outChunk->AppendSubChunk( newChunk );
                }
                else
                {
                    //Time to go away.
                    deleteLast = true;
                }
            }

            if ( deleteLast )
            {
                lastObj = dagIt.item();
            }
            
            dagIt.next();

            if ( deleteLast )
            {
                MExt::DisplayWarning( "Deleting useless node: %s", fnNode.name().asChar() );
                MExt::DeleteNode( lastObj, true );
                deleteLast = false;
            }
        }

        tlFile output(new tlFileByteStream(filePath, omWRITE), tlFile::CHUNK32);

        if(!output.IsOpen()) 
        {

            MGlobal::displayError("Unable to write file!");

            delete outChunk;
            return MS::kFailure;
        }

        //Sort it out..
        outChunk->SortSubChunks();
        outChunk->Write(&output);

        delete outChunk;
    }

    return MS::kSuccess;
}