File size: 6,160 Bytes
985c397 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 | // SPDX-License-Identifier: LGPL-2.1-or-later
/***************************************************************************
* Copyright (c) 2005 Werner Mayer <wmayer[at]users.sourceforge.net> *
* *
* This file is part of the FreeCAD CAx development system. *
* *
* This library is free software; you can redistribute it and/or *
* modify it under the terms of the GNU Library General Public *
* License as published by the Free Software Foundation; either *
* version 2 of the License, or (at your option) any later version. *
* *
* This library is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU Library General Public License for more details. *
* *
* You should have received a copy of the GNU Library General Public *
* License along with this library; see the file COPYING.LIB. If not, *
* write to the Free Software Foundation, Inc., 59 Temple Place, *
* Suite 330, Boston, MA 02111-1307, USA *
* *
***************************************************************************/
#include <limits>
#include <Base/Converter.h>
#include "Core/Algorithm.h"
#include "Core/Evaluation.h"
#include "FeatureMeshSegmentByMesh.h"
using namespace Mesh;
using namespace MeshCore;
PROPERTY_SOURCE(Mesh::SegmentByMesh, Mesh::Feature)
SegmentByMesh::SegmentByMesh()
{
ADD_PROPERTY(Source, (nullptr));
ADD_PROPERTY(Tool, (nullptr));
ADD_PROPERTY(Base, (0.0, 0.0, 0.0));
ADD_PROPERTY(Normal, (0.0, 0.0, 1.0));
}
short SegmentByMesh::mustExecute() const
{
if (Source.isTouched() || Tool.isTouched()) {
return 1;
}
if (Source.getValue() && Source.getValue()->isTouched()) {
return 1;
}
if (Tool.getValue() && Tool.getValue()->isTouched()) {
return 1;
}
return 0;
}
App::DocumentObjectExecReturn* SegmentByMesh::execute()
{
Mesh::PropertyMeshKernel* kernel = nullptr;
App::DocumentObject* mesh = Source.getValue();
if (mesh) {
App::Property* prop = mesh->getPropertyByName("Mesh");
if (prop && prop->is<Mesh::PropertyMeshKernel>()) {
kernel = static_cast<Mesh::PropertyMeshKernel*>(prop);
}
}
if (!kernel) {
return new App::DocumentObjectExecReturn("No mesh specified.\n");
}
if (mesh->isError()) {
return new App::DocumentObjectExecReturn("No valid mesh.\n");
}
Mesh::PropertyMeshKernel* toolmesh = nullptr;
App::DocumentObject* tool = Tool.getValue();
if (tool) {
App::Property* prop = tool->getPropertyByName("Mesh");
if (prop && prop->is<Mesh::PropertyMeshKernel>()) {
toolmesh = static_cast<Mesh::PropertyMeshKernel*>(prop);
}
}
if (!toolmesh) {
return new App::DocumentObjectExecReturn("No toolmesh specified.\n");
}
if (tool->isError()) {
return new App::DocumentObjectExecReturn("No valid toolmesh.\n");
}
// the clipping plane
Base::Vector3f cBase, cNormal;
cBase = Base::convertTo<Base::Vector3f>(Base.getValue());
cNormal = Base::convertTo<Base::Vector3f>(Normal.getValue());
const MeshKernel& rMeshKernel = kernel->getValue().getKernel();
const MeshKernel& rToolMesh = toolmesh->getValue().getKernel();
// check if the toolmesh is a solid
if (!MeshEvalSolid(rToolMesh).Evaluate()) {
return new App::DocumentObjectExecReturn("Toolmesh is not solid.\n");
}
std::vector<MeshCore::FacetIndex> faces;
MeshAlgorithm cAlg(rMeshKernel);
if (cNormal.Length() > 0.1F) { // not a null vector
cAlg.GetFacetsFromToolMesh(rToolMesh, cNormal, faces);
}
else {
cAlg.GetFacetsFromToolMesh(rToolMesh, Base::Vector3f(0.0, 1.0F, 0.0F), faces);
}
// if the clipping plane was set then we want only the visible facets
if (cNormal.Length() > 0.1F) { // not a null vector
// now we have too many facets since we have (invisible) facets near to the back clipping
// plane, so we need the nearest facet to the front clipping plane
//
float fDist = std::numeric_limits<float>::max();
MeshCore::FacetIndex uIdx = MeshCore::FACET_INDEX_MAX;
MeshFacetIterator cFIt(rMeshKernel);
// get the nearest facet to the user (front clipping plane)
for (MeshCore::FacetIndex it : faces) {
cFIt.Set(it);
float dist = (float)fabs(cFIt->GetGravityPoint().DistanceToPlane(cBase, cNormal));
if (dist < fDist) {
fDist = dist;
uIdx = it;
}
}
// succeeded
if (uIdx != MeshCore::FACET_INDEX_MAX) {
// set VISIT-Flag to all outer facets
cAlg.SetFacetFlag(MeshFacet::VISIT);
cAlg.ResetFacetsFlag(faces, MeshFacet::VISIT);
faces.clear();
MeshTopFacetVisitor clVisitor(faces);
rMeshKernel.VisitNeighbourFacets(clVisitor, uIdx);
// append also the start facet
faces.push_back(uIdx);
}
}
std::vector<MeshGeomFacet> aFaces;
aFaces.reserve(faces.size());
for (MeshCore::FacetIndex it : faces) {
aFaces.push_back(rMeshKernel.GetFacet(it));
}
std::unique_ptr<MeshObject> pcKernel(new MeshObject);
pcKernel->addFacets(aFaces);
Mesh.setValuePtr(pcKernel.release());
return App::DocumentObject::StdReturn;
}
|