From d75c33f123ad22b8d23d79a28cabe6bf3e6967b7 Mon Sep 17 00:00:00 2001 From: Jean-Marie Verdun Date: Mon, 31 Jul 2017 22:33:09 +0200 Subject: [PATCH] Fix Placement and Color export within Hierarchical Step exporter of single shapes --- src/Mod/Import/App/ImportOCAF.cpp | 106 ++++++++++++++++++++------ src/Mod/Import/App/ImportOCAF.h | 4 +- src/Mod/Import/Gui/AppImportGuiPy.cpp | 31 +++++++- 3 files changed, 117 insertions(+), 24 deletions(-) diff --git a/src/Mod/Import/App/ImportOCAF.cpp b/src/Mod/Import/App/ImportOCAF.cpp index ba12961c2e..ca2036a19e 100644 --- a/src/Mod/Import/App/ImportOCAF.cpp +++ b/src/Mod/Import/App/ImportOCAF.cpp @@ -614,32 +614,94 @@ int ExportOCAF::saveShape(Part::Feature* part, const std::vector& co return(hierarchical_label.size()); } -// This function is re-allocating Free XCAF document shapes with absolute coordinate +// This function is scanning the OCAF doc for Free Shapes and returns the label attached to it +// If this Free Shapes are regular Part::Feature, we must use absolute coordinate instead of +// allocating a placement into the hierarchy as it is not attached to a hierarchical node -void ExportOCAF::reallocateFreeShape(std::vector & hierarchical_label,std::vector & hierarchical_loc, std::vector & hierarchical_part) +void ExportOCAF::getFreeLabels(std::vector & hierarchical_label,std::vector & labels, + std::vector & label_part_id ) { - TDF_LabelSequence FreeLabels; - aShapeTool->GetFreeShapes(FreeLabels); - int n = FreeLabels.Length(); - for (int i = 1; i <= n; i++) - { - TDF_Label label = FreeLabels.Value(i); - for ( unsigned long j = 0; j < ( hierarchical_label.size()) ; j++ ) - { - if ( label == hierarchical_label.at(j) ) - { - // hierarchical part does contain only part currently and not node I should add node - if ( hierarchical_part.at(j)->getTypeId().isDerivedFrom(Part::Feature::getClassTypeId()) ) - { - Part::Feature * part = static_cast(hierarchical_part.at(j)); - aShapeTool->SetShape(label, part->Shape.getValue()); - } - } - } - } - + TDF_LabelSequence FreeLabels; + aShapeTool->GetFreeShapes(FreeLabels); + int n = FreeLabels.Length(); + for (int i = 1; i <= n; i++) + { + TDF_Label label = FreeLabels.Value(i); + for ( unsigned long j = 0; j < ( hierarchical_label.size()) ; j++ ) + { + if ( label == hierarchical_label.at(j) ) + { + labels.push_back(label); + label_part_id.push_back(j); + } + } + } } +void ExportOCAF::reallocateFreeShape(std::vector hierarchical_part, std::vector FreeLabels, + std::vector part_id, std::vector< std::vector >& Colors) +{ + int n = FreeLabels.size(); + for (int i = 0; i < n; i++) + { + TDF_Label label = FreeLabels.at(i); + // hierarchical part does contain only part currently and not node I should add node + if ( hierarchical_part.at(part_id.at(i))->getTypeId().isDerivedFrom(Part::Feature::getClassTypeId()) ) + { + Part::Feature * part = static_cast(hierarchical_part.at(part_id.at(i))); + aShapeTool->SetShape(label, part->Shape.getValue()); + // Add color information + std::vector colors; + colors=Colors.at(i); + TopoDS_Shape baseShape = part->Shape.getValue(); + // Add color information + Quantity_Color col; + + std::set face_index; + TopTools_IndexedMapOfShape faces; + TopExp_Explorer xp(baseShape,TopAbs_FACE); + while (xp.More()) { + face_index.insert(faces.Add(xp.Current())); + xp.Next(); + } + + // define color per face? + if (colors.size() == face_index.size()) { + xp.Init(baseShape,TopAbs_FACE); + while (xp.More()) { + int index = faces.FindIndex(xp.Current()); + if (face_index.find(index) != face_index.end()) { + face_index.erase(index); + TDF_Label faceLabel = aShapeTool->AddSubShape(label, xp.Current()); + // TDF_Label faceLabel= TDF_TagSource::NewChild(label); + aShapeTool->SetShape(faceLabel, xp.Current()); + const App::Color& color = colors[index-1]; + Quantity_Parameter mat[3]; + mat[0] = color.r; + mat[1] = color.g; + mat[2] = color.b; + col.SetValues(mat[0],mat[1],mat[2],Quantity_TOC_RGB); + aColorTool->SetColor(faceLabel, col, XCAFDoc_ColorSurf); + } + xp.Next(); + } + } + else if (!colors.empty()) { + App::Color color = colors.front(); + Quantity_Parameter mat[3]; + mat[0] = color.r; + mat[1] = color.g; + mat[2] = color.b; + col.SetValues(mat[0],mat[1],mat[2],Quantity_TOC_RGB); + aColorTool->SetColor(label, col, XCAFDoc_ColorGen); + } + + } + } +} + + + // This function is moving a "standard" node into an Assembly node within an XCAF doc diff --git a/src/Mod/Import/App/ImportOCAF.h b/src/Mod/Import/App/ImportOCAF.h index 08778ec76d..b720a451ed 100644 --- a/src/Mod/Import/App/ImportOCAF.h +++ b/src/Mod/Import/App/ImportOCAF.h @@ -82,7 +82,9 @@ public: void createNode(App::Part* part, int& root_it, std::vector & hierarchical_label,std::vector & hierarchical_loc, std::vector & hierarchical_part); ExportOCAF(Handle(TDocStd_Document) h, bool explicitPlacement); int saveShape(Part::Feature* part, const std::vector&, std::vector & hierarchical_label,std::vector & hierarchical_loc,std::vector & hierarchical_part); - void reallocateFreeShape(std::vector & hierarchical_label,std::vector & hierarchical_loc, std::vector & hierarchical_part); + void reallocateFreeShape(std::vector hierarchical_part, std::vector FreeLabels, + std::vector part_id, std::vector< std::vector >& Colors); + void getFreeLabels(std::vector & hierarchical_label,std::vector & labels, std::vector & label_part_id ); void pushNode(int root, int node, std::vector & hierarchical_label,std::vector & hierarchical_loc); diff --git a/src/Mod/Import/Gui/AppImportGuiPy.cpp b/src/Mod/Import/Gui/AppImportGuiPy.cpp index 0949828460..3d500500f2 100644 --- a/src/Mod/Import/Gui/AppImportGuiPy.cpp +++ b/src/Mod/Import/Gui/AppImportGuiPy.cpp @@ -483,6 +483,27 @@ private: return(return_label); } + void get_parts_colors(std::vector hierarchical_part, std::vector FreeLabels, + std::vector part_id, std::vector< std::vector >& Colors) + { + // I am seeking for the colors of each parts + int n = FreeLabels.size(); + for (int i = 0; i < n; i++) + { + std::vector colors; + Part::Feature * part = static_cast(hierarchical_part.at(part_id.at(i))); + Gui::ViewProvider* vp = Gui::Application::Instance->getViewProvider(part); + if (vp && vp->isDerivedFrom(PartGui::ViewProviderPartExt::getClassTypeId())) { + colors = static_cast(vp)->DiffuseColor.getValues(); + if (colors.empty()) + colors.push_back(static_cast(vp)->ShapeColor.getValue()); + Colors.push_back(colors); + } + } + } + + + Py::Object exporter(const Py::Tuple& args) { PyObject* object; @@ -518,7 +539,15 @@ private: } // Free Shapes must have absolute placement and not explicit - ocaf.reallocateFreeShape(hierarchical_label, hierarchical_loc,hierarchical_part); + // Free Shapes must have absolute placement and not explicit + std::vector FreeLabels; + std::vector part_id; + ocaf.getFreeLabels(hierarchical_label,FreeLabels, part_id); + // Got issue with the colors as they are coming from the View Provider they can't be determined into + // the App Code. + std::vector< std::vector > Colors; + get_parts_colors(hierarchical_part,FreeLabels,part_id,Colors); + ocaf.reallocateFreeShape(hierarchical_part,FreeLabels,part_id,Colors); Base::FileInfo file(Utf8Name.c_str()); if (file.hasExtension("stp") || file.hasExtension("step")) {