#include "stdafx.h" //////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////// // // BEGIN TestImportMeshes command // #pragma region TestImportMeshes command class CCommandTestImportMeshes : public CRhinoCommand { public: CCommandTestImportMeshes() = default; UUID CommandUUID() override { // {A3C621BE-9106-424C-B7F5-191F90420047} static const GUID TestImportMeshesCommand_UUID = { 0xA3C621BE, 0x9106, 0x424C, { 0xB7, 0xF5, 0x19, 0x1F, 0x90, 0x42, 0x00, 0x47 } }; return TestImportMeshesCommand_UUID; } const wchar_t* EnglishCommandName() override { return L"TestImportMeshes"; } CRhinoCommand::result RunCommand(const CRhinoCommandContext& context) override; }; // The one and only CCommandTestImportMeshes object static class CCommandTestImportMeshes theTestImportMeshesCommand; CRhinoCommand::result CCommandTestImportMeshes::RunCommand(const CRhinoCommandContext& context) { CRhinoGetFileDialog gf; gf.SetScriptMode(context.IsInteractive() ? FALSE : TRUE); bool rc = gf.DisplayFileDialog(CRhinoGetFileDialog::open_rhino_3dm_file_dialog, 0, RhinoApp().MainWnd()); if (!rc) return CRhinoCommand::cancel; ON_wString filename = gf.FileName(); filename.TrimLeftAndRight(); if (filename.IsEmpty()) return CRhinoCommand::nothing; if (!CRhinoFileUtilities::FileExists(filename)) { RhinoApp().Print(L"File not found\n"); return CRhinoCommand::failure; } FILE* archive_fp = ON::OpenFile(filename, L"rb"); if (0 == archive_fp) { RhinoApp().Print(L"Unable to open file\n"); return CRhinoCommand::failure; } ON_BinaryFile archive(ON::archive_mode::read3dm, archive_fp); ONX_Model model; rc = model.Read(archive); ON::CloseFile(archive_fp); if (!rc) { RhinoApp().Print(L"Error reading file\n"); return CRhinoCommand::failure; } const ON_UnitSystem& from_model_units = model.m_settings.m_ModelUnitsAndTolerances.m_unit_system; const ON_UnitSystem& to_model_units = context.m_doc.ModelUnits(); double scale = ON::UnitScale(from_model_units, to_model_units); bool bScale = (0.0 != scale || 1.0 != scale); ON_Xform xform = ON_Xform::IdentityTransformation; if (bScale) xform = ON_Xform::DiagonalTransformation(scale); int num_imported = 0; ONX_ModelComponentIterator it(model, ON_ModelComponent::Type::ModelGeometry); const ON_ModelComponent* model_component = nullptr; for (model_component = it.FirstComponent(); nullptr != model_component; model_component = it.NextComponent()) { const ON_ModelGeometryComponent* model_geometry = ON_ModelGeometryComponent::Cast(model_component); if (nullptr != model_geometry) { const ON_Mesh* const_mesh = ON_Mesh::Cast(model_geometry->Geometry(nullptr)); if (nullptr != const_mesh) { ON_Mesh* mesh = new ON_Mesh(*const_mesh); if (bScale) mesh->Transform(xform); CRhinoMeshObject* mesh_obj = new CRhinoMeshObject(); mesh_obj->SetMesh(mesh); mesh = nullptr; context.m_doc.AddObject(mesh_obj); num_imported++; } } } if (0 == num_imported) RhinoApp().Print(L"No meshes imported\n"); else if (1 == num_imported) RhinoApp().Print(L"1 mesh imported\n"); else RhinoApp().Print(L"%d meshes imported\n", num_imported); context.m_doc.Redraw(); return CRhinoCommand::success; } #pragma endregion // // END TestImportMeshes command // //////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////