From fbe5f352ff0f238266bc690a0b750674f80b1f02 Mon Sep 17 00:00:00 2001 From: Aki Date: Sat, 6 Apr 2024 02:34:19 +0200 Subject: Added obj2mag and mag2obj utilities --- Magic2/MagicDoc.cpp | 153 ++++++---------------------------------------------- 1 file changed, 17 insertions(+), 136 deletions(-) (limited to 'Magic2') diff --git a/Magic2/MagicDoc.cpp b/Magic2/MagicDoc.cpp index 55ecd8d..c57d922 100644 --- a/Magic2/MagicDoc.cpp +++ b/Magic2/MagicDoc.cpp @@ -15,6 +15,7 @@ #include "Magic.h" #include "MagicDoc.h" +#include "MagicLoad.h" #include "ModelFileMAG.h" #include "ModelFileOBJ.h" #include "ModelFile3DS.h" @@ -22,12 +23,8 @@ #include "Selector.h" #include "Editor.h" #include "Command.h" - -#include "Bitmap.h" -#include "Color.h" #include "D3DXImage.h" #include "Geometry.h" -#include "Pcx.h" #include "Polygon.h" #include "Solid.h" @@ -236,146 +233,30 @@ BOOL MagicDoc::OnOpenDocument(LPCTSTR path_name) return TRUE; } + bool MagicDoc::ImportFile(LPCTSTR path_name) { if (strstr(path_name, ".obj") || strstr(path_name, ".OBJ")) { - ModelFileOBJ obj_file(path_name); - - if (solid->GetModel()) { - Solid* s = new Solid; - - if (s->Load(&obj_file)) { - // todo: insert command here - Model* orig = solid->GetModel(); - Model* imported = s->GetModel(); - - orig->GetMaterials().append(imported->GetMaterials()); - orig->GetSurfaces().append(imported->GetSurfaces()); - orig->OptimizeMaterials(); - - imported->GetMaterials().clear(); - imported->GetSurfaces().clear(); - - SetModifiedFlag(FALSE); - UpdateAllViews(NULL); - delete s; - return true; - } - - delete s; - } - else { - if (solid->Load(&obj_file)) { - SetModifiedFlag(FALSE); - UpdateAllViews(NULL); - return true; - } - } - - return false; + if (!ImportInto(path_name, solid)) + return false; + SetModifiedFlag(FALSE); + UpdateAllViews(NULL); + return true; } - if (strstr(path_name, ".3ds") || strstr(path_name, ".3DS")) { - ModelFile3DS model_file(path_name); - - if (solid->GetModel()) { - Solid* s = new Solid; - - if (s->Load(&model_file)) { - // todo: insert command here - Model* orig = solid->GetModel(); - Model* imported = s->GetModel(); - - orig->GetMaterials().append(imported->GetMaterials()); - orig->GetSurfaces().append(imported->GetSurfaces()); - orig->OptimizeMaterials(); - - imported->GetMaterials().clear(); - imported->GetSurfaces().clear(); - - SetModifiedFlag(FALSE); - UpdateAllViews(NULL); - delete s; - return true; - } - - delete s; - } - else { - if (solid->Load(&model_file)) { - SetModifiedFlag(FALSE); - UpdateAllViews(NULL); - return true; - } - } - - return false; - } - - FILE* fp = fopen(path_name, "rb"); - if (!fp) { - ::MessageBox(0, "Import Failed: could not open file", "ERROR", MB_OK); - return false; - } - - int version = 1; - char file_id[5]; - fread(file_id, 4, 1, fp); - file_id[4] = '\0'; - fclose(fp); - - if (strncmp(file_id, "MAG", 3)) { - ::MessageBox(0, "Open Failed: Invalid file type", "ERROR", MB_OK); - return false; - } - - switch (file_id[3]) { - case '6': version = 6; break; - case '5': version = 5; break; - default: version = 0; break; + if (!ImportInto(path_name, solid)) + return false; + SetModifiedFlag(FALSE); + UpdateAllViews(NULL); + return true; } - - if (version < 5 || version > 6) { - ::MessageBox(0, "Open Failed: Unsupported version", "ERROR", MB_OK); + if (!ImportInto(path_name, solid)) return false; - } - - ModelFileMAG mag_file(path_name); - - if (solid->GetModel()) { - Solid* s = new Solid; - if (s->Load(&mag_file)) { - // todo: insert command here - Model* orig = solid->GetModel(); - Model* imported = s->GetModel(); - - orig->GetMaterials().append(imported->GetMaterials()); - orig->GetSurfaces().append(imported->GetSurfaces()); - orig->OptimizeMaterials(); - - imported->GetMaterials().clear(); - imported->GetSurfaces().clear(); - - SetModifiedFlag(FALSE); - UpdateAllViews(NULL); - delete s; - return true; - } - - delete s; - } - else { - InitCommandStack(); - - if (solid->Load(&mag_file)) { - SetModifiedFlag(FALSE); - UpdateAllViews(NULL); - return true; - } - } - - return false; + InitCommandStack(); + SetModifiedFlag(FALSE); + UpdateAllViews(NULL); + return true; } bool -- cgit v1.1