1 #include <stdio.h> |
|
2 #include <stdlib.h> |
|
3 #include "common.h" |
|
4 #include "io.h" |
|
5 #include "gui.h" |
|
6 #include "draw.h" |
|
7 #include "bbox.h" |
|
8 |
|
9 // Clear everything from the model |
|
10 void closeModel () { |
|
11 // Remove all loaded files and the objects they contain |
|
12 for (ushort i = 0; i < g_LoadedFiles.size(); i++) { |
|
13 OpenFile* f = g_LoadedFiles[i]; |
|
14 |
|
15 for (ushort j = 0; j < f->objects.size(); ++j) |
|
16 delete (LDObject*)f->objects[j]; |
|
17 |
|
18 delete f; |
|
19 } |
|
20 |
|
21 // Clear the array |
|
22 g_LoadedFiles.clear(); |
|
23 g_CurrentFile = NULL; |
|
24 |
|
25 g_qWindow->R->hardRefresh(); |
|
26 } |
|
27 |
|
28 void newModel () { |
|
29 // Create a new anonymous file and set it to our current |
|
30 if (g_LoadedFiles.size()) |
|
31 closeModel (); // Close any open file first, though |
|
32 |
|
33 OpenFile* f = new OpenFile; |
|
34 f->zFileName = ""; |
|
35 g_LoadedFiles.push_back (f); |
|
36 g_CurrentFile = f; |
|
37 |
|
38 g_qWindow->R->hardRefresh(); |
|
39 } |
|
40 |
|
41 void openModel (str zPath) { |
|
42 if (g_CurrentFile) |
|
43 closeModel (); |
|
44 |
|
45 OpenFile* pFile = IO_OpenLDrawFile (zPath); |
|
46 g_CurrentFile = pFile; |
|
47 |
|
48 // Recalculate the bounding box |
|
49 g_BBox.calculate(); |
|
50 |
|
51 // Rebuild the object tree view now. |
|
52 g_qWindow->buildObjList (); |
|
53 g_qWindow->setTitle (); |
|
54 } |
|
55 |
|
56 void saveModel () { |
|
57 if (!g_CurrentFile) |
|
58 return; |
|
59 |
|
60 FILE* fp = fopen (g_CurrentFile->zFileName, "w"); |
|
61 if (!fp) |
|
62 return; |
|
63 |
|
64 // Write all entries now |
|
65 for (ulong i = 0; i < g_CurrentFile->objects.size(); ++i) { |
|
66 LDObject* obj = g_CurrentFile->objects[i]; |
|
67 |
|
68 // LDraw requires lines to have DOS line endings |
|
69 str zLine = str::mkfmt ("%s\r\n",obj->getContents ().chars ()); |
|
70 |
|
71 fwrite (zLine.chars(), 1, ~zLine, fp); |
|
72 } |
|
73 |
|
74 fclose (fp); |
|
75 } |
|