Skip to content

Commit

Permalink
generation default mesh is moved outside from constructor (#1373)
Browse files Browse the repository at this point in the history
* generation default mesh is moved outside from constructor

* some fix and update gcode tools library ui
  • Loading branch information
ABSitf authored Jun 29, 2023
1 parent f1f8c91 commit 20f169b
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 23 deletions.
56 changes: 35 additions & 21 deletions source/MRViewer/MRToolsLibrary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,11 @@ GcodeToolsLibrary::GcodeToolsLibrary( const std::string& libraryName )
assert( !libraryName.empty() );
libraryName_ = libraryName;

defaultToolMesh_ = std::make_shared<ObjectMesh>();
defaultToolMesh_->setName( "DefaultToolMesh" );
auto meshPtr = std::make_shared<Mesh>( makeCylinder( 1.f, 8.f, 50 ) );
defaultToolMesh_->setMesh( meshPtr );

toolMesh_ = defaultToolMesh_;
selectedIndex_ = 0;
selectedFileName_ = defaultName;
}

bool GcodeToolsLibrary::drawInterface()
{
bool openSelectMeshPopup = false;
bool result = false;

if ( UI::beginCombo( "Tool Mesh", selectedFileName_ ) )
Expand All @@ -49,7 +41,6 @@ bool GcodeToolsLibrary::drawInterface()
toolMesh_ = defaultToolMesh_;
selectedFileName_ = defaultName;
result = true;
ImGui::CloseCurrentPopup();
}

updateFilesList_();
Expand All @@ -60,7 +51,6 @@ bool GcodeToolsLibrary::drawInterface()
if ( ImGui::Selectable( filesList_[i].c_str(), &selected ) && selected )
{
result = loadMeshFromFile_( filesList_[i] );
ImGui::CloseCurrentPopup();
}
}

Expand All @@ -72,20 +62,29 @@ bool GcodeToolsLibrary::drawInterface()
{
addNewToolFromFile_();
result = true;
ImGui::CloseCurrentPopup();
}

const bool anyMeshExist = bool( getDepthFirstObject<ObjectMesh>( &SceneRoot::get(), ObjectSelectivityType::Selectable ) );
if ( !anyMeshExist )
{
ImGui::PushStyleColor( ImGuiCol_Text, ImGui::GetStyleColorVec4( ImGuiCol_TextDisabled ) );
if ( ImGui::Selectable( "<New Tool from exist Mesh>", &selected ) && anyMeshExist )
ImGui::Text( "%s", "<New Tool from exist Mesh>" );
ImGui::PopStyleColor();
} else if ( ImGui::BeginMenu( "<New Tool from exist Mesh>" ) )
{
openSelectMeshPopup = true;
result = true;
ImGui::CloseCurrentPopup();
auto objsMesh = getAllObjectsInTree<ObjectMesh>( &SceneRoot::get(), ObjectSelectivityType::Selectable );
for ( int i = 0; i < objsMesh.size(); ++i )
{
selected = false;
if ( ImGui::Selectable( objsMesh[i]->name().c_str(), &selected ) )
{
result = true;
addNewToolFromMesh_( objsMesh[i] );
}
}

ImGui::EndMenu();
}
if ( !anyMeshExist )
ImGui::PopStyleColor();
}
UI::endCombo();
}
Expand All @@ -107,12 +106,27 @@ bool GcodeToolsLibrary::drawInterface()
}
}

if ( openSelectMeshPopup )
ImGui::OpenPopup( "SelectMesh" );
drawSelectMeshPopup_();
return result;
}

const std::shared_ptr<MR::ObjectMesh>& GcodeToolsLibrary::getToolObject()
{
if ( selectedFileName_ == defaultName )
{
if ( !defaultToolMesh_ )
{
defaultToolMesh_ = std::make_shared<ObjectMesh>();
defaultToolMesh_->setName( "DefaultToolMesh" );
auto meshPtr = std::make_shared<Mesh>( makeCylinder( 1.f, 8.f, 50 ) );
defaultToolMesh_->setMesh( meshPtr );
}

if ( toolMesh_ != defaultToolMesh_ )
toolMesh_ = defaultToolMesh_;
}
return toolMesh_;
}

std::filesystem::path GcodeToolsLibrary::getFolder_()
{
const std::filesystem::path path = getUserConfigDir() / libraryName_;
Expand Down Expand Up @@ -172,7 +186,7 @@ void GcodeToolsLibrary::addNewToolFromMesh_( const std::shared_ptr<ObjectMesh>&
return;
toolMesh_ = std::dynamic_pointer_cast< ObjectMesh >( objMesh->clone() );
MeshSave::toMrmesh( *toolMesh_->mesh(), folderPath / ( toolMesh_->name() + ".mrmesh" ) );
updateFilesList_();
selectedFileName_ = toolMesh_->name();
}

void GcodeToolsLibrary::drawSelectMeshPopup_()
Expand Down
3 changes: 1 addition & 2 deletions source/MRViewer/MRToolsLibrary.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class MRVIEWER_CLASS GcodeToolsLibrary
MRVIEWER_API bool drawInterface();

// get selected tool as ObjectMesh
const std::shared_ptr<ObjectMesh>& getToolObject() { return toolMesh_; }
MRVIEWER_API const std::shared_ptr<ObjectMesh>& getToolObject();
private:

// storage folder
Expand All @@ -36,7 +36,6 @@ class MRVIEWER_CLASS GcodeToolsLibrary

std::string libraryName_;
std::vector<std::string> filesList_;
int selectedIndex_;
std::string selectedFileName_;
std::shared_ptr<ObjectMesh> toolMesh_;
std::shared_ptr<ObjectMesh> defaultToolMesh_; // default mesh
Expand Down

0 comments on commit 20f169b

Please sign in to comment.