From 361951eac3969b35cc0dec8040c4eddc313f5183 Mon Sep 17 00:00:00 2001 From: wmayer Date: Wed, 7 Sep 2016 19:20:33 +0200 Subject: [PATCH] create mesh segments by face colors --- src/Mod/MeshPart/App/AppMeshPartPy.cpp | 27 ++++++++++++++++--- src/Mod/MeshPart/App/Mesher.cpp | 37 +++++++++++++++++++++----- src/Mod/MeshPart/App/Mesher.h | 8 ++++++ src/Mod/MeshPart/Gui/Tessellation.cpp | 15 ++++++++--- src/Mod/MeshPart/Gui/Tessellation.ui | 34 ++++++++++++++++------- 5 files changed, 97 insertions(+), 24 deletions(-) diff --git a/src/Mod/MeshPart/App/AppMeshPartPy.cpp b/src/Mod/MeshPart/App/AppMeshPartPy.cpp index c32c5e1c9..12ecec6b0 100644 --- a/src/Mod/MeshPart/App/AppMeshPartPy.cpp +++ b/src/Mod/MeshPart/App/AppMeshPartPy.cpp @@ -177,17 +177,38 @@ private: { PyObject *shape; - static char* kwds_lindeflection[] = {"Shape", "LinearDeflection", "AngularDeflection", NULL}; + static char* kwds_lindeflection[] = {"Shape", "LinearDeflection", "AngularDeflection", + "Segments", "GroupColors", NULL}; PyErr_Clear(); double lindeflection=0; double angdeflection=0.5; - if (PyArg_ParseTupleAndKeywords(args.ptr(), kwds.ptr(), "O!d|d", kwds_lindeflection, - &(Part::TopoShapePy::Type), &shape, &lindeflection, &angdeflection)) { + PyObject* segment = Py_False; + PyObject* groupColors = 0; + if (PyArg_ParseTupleAndKeywords(args.ptr(), kwds.ptr(), "O!d|dO!O", kwds_lindeflection, + &(Part::TopoShapePy::Type), &shape, &lindeflection, &angdeflection, + &(PyBool_Type), &segment, &groupColors)) { MeshPart::Mesher mesher(static_cast(shape)->getTopoShapePtr()->getShape()); mesher.setMethod(MeshPart::Mesher::Standard); mesher.setDeflection(lindeflection); mesher.setAngularDeflection(angdeflection); mesher.setRegular(true); + mesher.setSegments(PyObject_IsTrue(segment) ? true : false); + if (groupColors) { + Py::Sequence list(groupColors); + std::vector colors; + colors.reserve(list.size()); + for (Py::Sequence::iterator it = list.begin(); it != list.end(); ++it) { + Py::Tuple t(*it); + Py::Float r(t[0]); + Py::Float g(t[1]); + Py::Float b(t[2]); + App::Color c(static_cast(r), + static_cast(g), + static_cast(b)); + colors.push_back(c.getPackedValue()); + } + mesher.setColors(colors); + } return Py::asObject(new Mesh::MeshPy(mesher.createMesh())); } diff --git a/src/Mod/MeshPart/App/Mesher.cpp b/src/Mod/MeshPart/App/Mesher.cpp index 6737f90b6..b70273eef 100644 --- a/src/Mod/MeshPart/App/Mesher.cpp +++ b/src/Mod/MeshPart/App/Mesher.cpp @@ -144,6 +144,7 @@ Mesher::Mesher(const TopoDS_Shape& s) , minLen(0) , maxLen(0) , regular(false) + , segments(false) #if defined (HAVE_NETGEN) , fineness(5) , growthRate(0) @@ -180,6 +181,13 @@ Mesh::MeshObject* Mesher::createMesh() const #endif } + std::map > colorMap; + for (std::size_t i=0; iNbDomains()); + MeshCore::MeshFacetArray faces; faces.reserve(aMesh->NbTriangles()); @@ -188,7 +196,7 @@ Mesh::MeshObject* Mesher::createMesh() const Standard_Real x2, y2, z2; Standard_Real x3, y3, z3; - std::list< std::vector > meshSegments; + std::vector< std::vector > meshSegments; std::size_t numMeshFaces = 0; StlMesh_MeshExplorer xp(aMesh); for (Standard_Integer nbd=1;nbd<=aMesh->NbDomains();nbd++) { @@ -244,10 +252,12 @@ Mesh::MeshObject* Mesher::createMesh() const } // add a segment for the face - std::vector segment(numDomainFaces); - std::generate(segment.begin(), segment.end(), Base::iotaGen(numMeshFaces)); - numMeshFaces += numDomainFaces; - meshSegments.push_back(segment); + if (createSegm || this->segments) { + std::vector segment(numDomainFaces); + std::generate(segment.begin(), segment.end(), Base::iotaGen(numMeshFaces)); + numMeshFaces += numDomainFaces; + meshSegments.push_back(segment); + } } MeshCore::MeshPointArray verts; @@ -260,8 +270,21 @@ Mesh::MeshObject* Mesher::createMesh() const Mesh::MeshObject* meshdata = new Mesh::MeshObject(); meshdata->swap(kernel); - for (auto it : meshSegments) { - meshdata->addSegment(it); + if (createSegm) { + for (auto it : colorMap) { + Mesh::Segment segm(meshdata, false); + for (auto jt : it.second) { + segm.addIndices(meshSegments[jt]); + } + segm.save(true); + segm.setName("Group"); + meshdata->addSegment(segm); + } + } + else { + for (auto it : meshSegments) { + meshdata->addSegment(it); + } } return meshdata; } diff --git a/src/Mod/MeshPart/App/Mesher.h b/src/Mod/MeshPart/App/Mesher.h index f9bcfc750..700677fa9 100644 --- a/src/Mod/MeshPart/App/Mesher.h +++ b/src/Mod/MeshPart/App/Mesher.h @@ -81,6 +81,12 @@ public: { regular = s; } bool isRegular() const { return regular; } + void setSegments(bool s) + { segments = s; } + bool isSegments() const + { return segments; } + void setColors(const std::vector& c) + { colors = c; } //@} #if defined (HAVE_NETGEN) @@ -129,6 +135,7 @@ private: double angularDeflection; double minLen, maxLen; bool regular; + bool segments; #if defined (HAVE_NETGEN) int fineness; double growthRate; @@ -138,6 +145,7 @@ private: bool optimize; bool allowquad; #endif + std::vector colors; struct Vertex; }; diff --git a/src/Mod/MeshPart/Gui/Tessellation.cpp b/src/Mod/MeshPart/Gui/Tessellation.cpp index ddbb4c822..108c21d87 100644 --- a/src/Mod/MeshPart/Gui/Tessellation.cpp +++ b/src/Mod/MeshPart/Gui/Tessellation.cpp @@ -246,16 +246,23 @@ bool Tessellation::accept() QString cmd; if (method == 0) { // Standard double devFace = ui->spinSurfaceDeviation->value().getValue(); + QString param = QString::fromLatin1("Shape=__doc__.getObject(\"%1\").Shape,LinearDeflection=%2") + .arg(shape) + .arg(devFace); + if (ui->meshShapeColors->isChecked()) + param += QString::fromLatin1(",Segments=True"); + if (ui->groupsFaceColors->isChecked()) + param += QString::fromLatin1(",GroupColors=__doc__.getObject(\"%1\").ViewObject.DiffuseColor") + .arg(shape); cmd = QString::fromLatin1( "__doc__=FreeCAD.getDocument(\"%1\")\n" "__mesh__=__doc__.addObject(\"Mesh::Feature\",\"Mesh\")\n" - "__mesh__.Mesh=MeshPart.meshFromShape(Shape=__doc__.getObject(\"%2\").Shape,LinearDeflection=%3)\n" - "__mesh__.Label=\"%4 (Meshed)\"\n" + "__mesh__.Mesh=MeshPart.meshFromShape(%2)\n" + "__mesh__.Label=\"%3 (Meshed)\"\n" "__mesh__.ViewObject.CreaseAngle=25.0\n" "del __doc__, __mesh__\n") .arg(this->document) - .arg(shape) - .arg(devFace) + .arg(param) .arg(label); } else if (method == 1) { // Mefisto diff --git a/src/Mod/MeshPart/Gui/Tessellation.ui b/src/Mod/MeshPart/Gui/Tessellation.ui index 2071f60af..7d2614eff 100644 --- a/src/Mod/MeshPart/Gui/Tessellation.ui +++ b/src/Mod/MeshPart/Gui/Tessellation.ui @@ -60,6 +60,19 @@ + + + + Qt::Vertical + + + + 20 + 189 + + + + @@ -87,18 +100,19 @@ + + + + Define segments by face colors + + + - - - Qt::Vertical + + + Apply face colors to mesh - - - 20 - 189 - - - +