1
/***************************************************************************
2
* Copyright (c) 2005 Werner Mayer <wmayer[at]users.sourceforge.net> *
4
* This file is part of the FreeCAD CAx development system. *
6
* This library is free software; you can redistribute it and/or *
7
* modify it under the terms of the GNU Library General Public *
8
* License as published by the Free Software Foundation; either *
9
* version 2 of the License, or (at your option) any later version. *
11
* This library is distributed in the hope that it will be useful, *
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14
* GNU Library General Public License for more details. *
16
* You should have received a copy of the GNU Library General Public *
17
* License along with this library; see the file COPYING.LIB. If not, *
18
* write to the Free Software Foundation, Inc., 59 Temple Place, *
19
* Suite 330, Boston, MA 02111-1307, USA *
21
***************************************************************************/
23
#include "PreCompiled.h"
25
#include <Base/Converter.h>
27
#include "Core/Algorithm.h"
28
#include "Core/Evaluation.h"
30
#include "FeatureMeshSegmentByMesh.h"
34
using namespace MeshCore;
36
PROPERTY_SOURCE(Mesh::SegmentByMesh, Mesh::Feature)
39
SegmentByMesh::SegmentByMesh()
41
ADD_PROPERTY(Source, (nullptr));
42
ADD_PROPERTY(Tool, (nullptr));
43
ADD_PROPERTY(Base, (0.0, 0.0, 0.0));
44
ADD_PROPERTY(Normal, (0.0, 0.0, 1.0));
47
short SegmentByMesh::mustExecute() const
49
if (Source.isTouched() || Tool.isTouched()) {
52
if (Source.getValue() && Source.getValue()->isTouched()) {
55
if (Tool.getValue() && Tool.getValue()->isTouched()) {
61
App::DocumentObjectExecReturn* SegmentByMesh::execute()
63
Mesh::PropertyMeshKernel* kernel = nullptr;
64
App::DocumentObject* mesh = Source.getValue();
66
App::Property* prop = mesh->getPropertyByName("Mesh");
67
if (prop && prop->is<Mesh::PropertyMeshKernel>()) {
68
kernel = static_cast<Mesh::PropertyMeshKernel*>(prop);
72
return new App::DocumentObjectExecReturn("No mesh specified.\n");
74
else if (mesh->isError()) {
75
return new App::DocumentObjectExecReturn("No valid mesh.\n");
78
Mesh::PropertyMeshKernel* toolmesh = nullptr;
79
App::DocumentObject* tool = Tool.getValue();
81
App::Property* prop = tool->getPropertyByName("Mesh");
82
if (prop && prop->is<Mesh::PropertyMeshKernel>()) {
83
toolmesh = static_cast<Mesh::PropertyMeshKernel*>(prop);
87
return new App::DocumentObjectExecReturn("No toolmesh specified.\n");
89
else if (tool->isError()) {
90
return new App::DocumentObjectExecReturn("No valid toolmesh.\n");
94
Base::Vector3f cBase, cNormal;
95
cBase = Base::convertTo<Base::Vector3f>(Base.getValue());
96
cNormal = Base::convertTo<Base::Vector3f>(Normal.getValue());
99
const MeshKernel& rMeshKernel = kernel->getValue().getKernel();
100
const MeshKernel& rToolMesh = toolmesh->getValue().getKernel();
102
// check if the toolmesh is a solid
103
if (!MeshEvalSolid(rToolMesh).Evaluate()) {
104
return new App::DocumentObjectExecReturn("Toolmesh is not solid.\n");
107
std::vector<MeshCore::FacetIndex> faces;
108
std::vector<MeshGeomFacet> aFaces;
110
MeshAlgorithm cAlg(rMeshKernel);
111
if (cNormal.Length() > 0.1f) { // not a null vector
112
cAlg.GetFacetsFromToolMesh(rToolMesh, cNormal, faces);
115
cAlg.GetFacetsFromToolMesh(rToolMesh, Base::Vector3f(0.0, 1.0f, 0.0f), faces);
118
// if the clipping plane was set then we want only the visible facets
119
if (cNormal.Length() > 0.1f) { // not a null vector
120
// now we have too many facets since we have (invisible) facets near to the back clipping
121
// plane, so we need the nearest facet to the front clipping plane
123
float fDist = FLOAT_MAX;
124
MeshCore::FacetIndex uIdx = MeshCore::FACET_INDEX_MAX;
125
MeshFacetIterator cFIt(rMeshKernel);
127
// get the nearest facet to the user (front clipping plane)
128
for (MeshCore::FacetIndex it : faces) {
130
float dist = (float)fabs(cFIt->GetGravityPoint().DistanceToPlane(cBase, cNormal));
138
if (uIdx != MeshCore::FACET_INDEX_MAX) {
139
// set VISIT-Flag to all outer facets
140
cAlg.SetFacetFlag(MeshFacet::VISIT);
141
cAlg.ResetFacetsFlag(faces, MeshFacet::VISIT);
144
MeshTopFacetVisitor clVisitor(faces);
145
rMeshKernel.VisitNeighbourFacets(clVisitor, uIdx);
147
// append also the start facet
148
faces.push_back(uIdx);
152
for (MeshCore::FacetIndex it : faces) {
153
aFaces.push_back(rMeshKernel.GetFacet(it));
156
std::unique_ptr<MeshObject> pcKernel(new MeshObject);
157
pcKernel->addFacets(aFaces);
158
Mesh.setValuePtr(pcKernel.release());
160
return App::DocumentObject::StdReturn;