/
Command.cpp
373 lines (323 loc) · 14 KB
/
Command.cpp
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
/***************************************************************************
* Copyright (c) 2008 Jürgen Riegel <juergen.riegel@web.de> *
* *
* 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 "PreCompiled.h"
#ifndef _PreComp_
# include <QApplication>
# include <QMessageBox>
# include <BRep_Builder.hxx>
# include <BRepBuilderAPI_MakePolygon.hxx>
# include <TopoDS_Compound.hxx>
#endif
#include <sstream>
#include <Mod/Part/App/TopoShape.h>
#include <Mod/Part/App/PartFeature.h>
#include <Mod/Points/App/Structured.h>
#include <Mod/Mesh/App/MeshFeature.h>
#include <Mod/Mesh/App/Core/Approximation.h>
#include <Mod/Mesh/App/Core/Algorithm.h>
#include <App/Document.h>
#include <Gui/Application.h>
#include <Gui/Command.h>
#include <Gui/Control.h>
#include <Gui/MainWindow.h>
#include <Gui/FileDialog.h>
#include <Gui/Selection.h>
#include <Base/CoordinateSystem.h>
#include <Base/Converter.h>
#include "../App/ApproxSurface.h"
#include "FitBSplineSurface.h"
#include "Poisson.h"
#include "Segmentation.h"
using namespace std;
DEF_STD_CMD_A(CmdApproxSurface)
CmdApproxSurface::CmdApproxSurface()
: Command("Reen_ApproxSurface")
{
sAppModule = "Reen";
sGroup = QT_TR_NOOP("Reverse Engineering");
sMenuText = QT_TR_NOOP("Approximate B-spline surface...");
sToolTipText = QT_TR_NOOP("Approximate a B-spline surface");
sWhatsThis = "Reen_ApproxSurface";
sStatusTip = sToolTipText;
sPixmap = "actions/FitSurface";
}
void CmdApproxSurface::activated(int)
{
App::DocumentObjectT objT;
std::vector<App::DocumentObject*> obj = Gui::Selection().getObjectsOfType(Points::Feature::getClassTypeId());
if (obj.size() != 1) {
QMessageBox::warning(Gui::getMainWindow(),
qApp->translate("Reen_ApproxSurface", "Wrong selection"),
qApp->translate("Reen_ApproxSurface", "Please select a single point cloud.")
);
return;
}
objT = obj.front();
Gui::Control().showDialog(new ReenGui::TaskFitBSplineSurface(objT));
}
bool CmdApproxSurface::isActive(void)
{
return (hasActiveDocument() && !Gui::Control().activeDialog());
}
DEF_STD_CMD_A(CmdApproxPlane)
CmdApproxPlane::CmdApproxPlane()
: Command("Reen_ApproxPlane")
{
sAppModule = "Reen";
sGroup = QT_TR_NOOP("Reverse Engineering");
sMenuText = QT_TR_NOOP("Approximate plane...");
sToolTipText = QT_TR_NOOP("Approximate a plane");
sWhatsThis = "Reen_ApproxPlane";
sStatusTip = sToolTipText;
}
void CmdApproxPlane::activated(int)
{
std::vector<App::GeoFeature*> obj = Gui::Selection().getObjectsOfType<App::GeoFeature>();
for (std::vector<App::GeoFeature*>::iterator it = obj.begin(); it != obj.end(); ++it) {
std::vector<Base::Vector3d> aPoints;
std::vector<Base::Vector3d> aNormals;
std::vector<App::Property*> List;
(*it)->getPropertyList(List);
for (std::vector<App::Property*>::iterator jt = List.begin(); jt != List.end(); ++jt) {
if ((*jt)->getTypeId().isDerivedFrom(App::PropertyComplexGeoData::getClassTypeId())) {
const Data::ComplexGeoData* data = static_cast<App::PropertyComplexGeoData*>(*jt)->getComplexData();
if (data) {
data->getPoints(aPoints, aNormals, 0.01f);
if (!aPoints.empty())
break;
}
}
}
if (!aPoints.empty()) {
// get a reference normal for the plane fit
Base::Vector3f refNormal(0,0,0);
if (!aNormals.empty()) {
refNormal = Base::convertTo<Base::Vector3f>(aNormals.front());
}
std::vector<Base::Vector3f> aData;
aData.reserve(aPoints.size());
for (std::vector<Base::Vector3d>::iterator jt = aPoints.begin(); jt != aPoints.end(); ++jt)
aData.push_back(Base::toVector<float>(*jt));
MeshCore::PlaneFit fit;
fit.AddPoints(aData);
float sigma = fit.Fit();
Base::Vector3f base = fit.GetBase();
Base::Vector3f dirU = fit.GetDirU();
Base::Vector3f dirV = fit.GetDirV();
Base::Vector3f norm = fit.GetNormal();
// if the dot product of the reference with the plane normal is negative
// a flip must be done
if (refNormal * norm < 0) {
norm = -norm;
dirU = -dirU;
}
float length, width;
fit.Dimension(length, width);
// move to the corner point
base = base - (0.5f * length * dirU + 0.5f * width * dirV);
Base::CoordinateSystem cs;
cs.setPosition(Base::convertTo<Base::Vector3d>(base));
cs.setAxes(Base::convertTo<Base::Vector3d>(norm),
Base::convertTo<Base::Vector3d>(dirU));
Base::Placement pm = Base::CoordinateSystem().displacement(cs);
double q0, q1, q2, q3;
pm.getRotation().getValue(q0, q1, q2, q3);
Base::Console().Log("RMS value for plane fit with %lu points: %.4f\n", aData.size(), sigma);
Base::Console().Log(" Plane base(%.4f, %.4f, %.4f)\n", base.x, base.y, base.z);
Base::Console().Log(" Plane normal(%.4f, %.4f, %.4f)\n", norm.x, norm.y, norm.z);
std::stringstream str;
str << "from FreeCAD import Base" << std::endl;
str << "App.ActiveDocument.addObject('Part::Plane','Plane_fit')" << std::endl;
str << "App.ActiveDocument.ActiveObject.Length = " << length << std::endl;
str << "App.ActiveDocument.ActiveObject.Width = " << width << std::endl;
str << "App.ActiveDocument.ActiveObject.Placement = Base.Placement("
<< "Base.Vector(" << base.x << "," << base.y << "," << base.z << "),"
<< "Base.Rotation(" << q0 << "," << q1 << "," << q2 << "," << q3 << "))" << std::endl;
openCommand("Fit plane");
runCommand(Gui::Command::Doc, str.str().c_str());
commitCommand();
updateActive();
}
}
}
bool CmdApproxPlane::isActive(void)
{
if (getSelection().countObjectsOfType(App::GeoFeature::getClassTypeId()) == 1)
return true;
return false;
}
DEF_STD_CMD_A(CmdSegmentation)
CmdSegmentation::CmdSegmentation()
: Command("Reen_Segmentation")
{
sAppModule = "Reen";
sGroup = QT_TR_NOOP("Reverse Engineering");
sMenuText = QT_TR_NOOP("Create mesh segments...");
sToolTipText = QT_TR_NOOP("Create mesh segments");
sWhatsThis = "Reen_Segmentation";
sStatusTip = sToolTipText;
}
void CmdSegmentation::activated(int)
{
std::vector<Mesh::Feature*> objs = Gui::Selection().getObjectsOfType<Mesh::Feature>();
Mesh::Feature* mesh = static_cast<Mesh::Feature*>(objs.front());
Gui::TaskView::TaskDialog* dlg = Gui::Control().activeDialog();
if (!dlg) {
dlg = new ReverseEngineeringGui::TaskSegmentation(mesh);
}
Gui::Control().showDialog(dlg);
}
bool CmdSegmentation::isActive(void)
{
if (Gui::Control().activeDialog())
return false;
return Gui::Selection().countObjectsOfType
(Mesh::Feature::getClassTypeId()) == 1;
}
DEF_STD_CMD_A(CmdMeshBoundary)
CmdMeshBoundary::CmdMeshBoundary()
: Command("Reen_MeshBoundary")
{
sAppModule = "Reen";
sGroup = QT_TR_NOOP("Reverse Engineering");
sMenuText = QT_TR_NOOP("Wire from mesh...");
sToolTipText = QT_TR_NOOP("Create wire from mesh");
sWhatsThis = "Reen_Segmentation";
sStatusTip = sToolTipText;
}
void CmdMeshBoundary::activated(int)
{
std::vector<Mesh::Feature*> objs = Gui::Selection().getObjectsOfType<Mesh::Feature>();
App::Document* document = App::GetApplication().getActiveDocument();
document->openTransaction("Wire from mesh");
for (auto it : objs) {
const Mesh::MeshObject& mesh = it->Mesh.getValue();
std::list<std::vector<Base::Vector3f> > bounds;
MeshCore::MeshAlgorithm algo(mesh.getKernel());
algo.GetMeshBorders(bounds);
BRep_Builder builder;
TopoDS_Compound compound;
builder.MakeCompound(compound);
for (auto bt = bounds.begin(); bt != bounds.end(); ++bt) {
BRepBuilderAPI_MakePolygon mkPoly;
for (std::vector<Base::Vector3f>::reverse_iterator it = bt->rbegin(); it != bt->rend(); ++it) {
mkPoly.Add(gp_Pnt(it->x,it->y,it->z));
}
if (mkPoly.IsDone()) {
builder.Add(compound, mkPoly.Wire());
}
}
Part::Feature* shapeFea = static_cast<Part::Feature*>(document->addObject("Part::Feature", "Wires from mesh"));
shapeFea->Shape.setValue(compound);
}
document->commitTransaction();
}
bool CmdMeshBoundary::isActive(void)
{
return Gui::Selection().countObjectsOfType
(Mesh::Feature::getClassTypeId()) > 0;
}
DEF_STD_CMD_A(CmdPoissonReconstruction)
CmdPoissonReconstruction::CmdPoissonReconstruction()
: Command("Reen_PoissonReconstruction")
{
sAppModule = "Reen";
sGroup = QT_TR_NOOP("Reverse Engineering");
sMenuText = QT_TR_NOOP("Poisson...");
sToolTipText = QT_TR_NOOP("Poisson surface reconstruction");
sWhatsThis = "Reen_PoissonReconstruction";
sStatusTip = sToolTipText;
}
void CmdPoissonReconstruction::activated(int)
{
App::DocumentObjectT objT;
std::vector<App::DocumentObject*> obj = Gui::Selection().getObjectsOfType(Points::Feature::getClassTypeId());
if (obj.size() != 1) {
QMessageBox::warning(Gui::getMainWindow(),
qApp->translate("Reen_ApproxSurface", "Wrong selection"),
qApp->translate("Reen_ApproxSurface", "Please select a single point cloud.")
);
return;
}
objT = obj.front();
Gui::Control().showDialog(new ReenGui::TaskPoisson(objT));
}
bool CmdPoissonReconstruction::isActive(void)
{
return (hasActiveDocument() && !Gui::Control().activeDialog());
}
DEF_STD_CMD_A(CmdViewTriangulation)
CmdViewTriangulation::CmdViewTriangulation()
: Command("Reen_ViewTriangulation")
{
sAppModule = "Reen";
sGroup = QT_TR_NOOP("Reverse Engineering");
sMenuText = QT_TR_NOOP("Structured point clouds");
sToolTipText = QT_TR_NOOP("Triangulation of structured point clouds");
sStatusTip = QT_TR_NOOP("Triangulation of structured point clouds");
sWhatsThis = "Reen_ViewTriangulation";
}
void CmdViewTriangulation::activated(int)
{
std::vector<App::DocumentObject*> obj = Gui::Selection().getObjectsOfType(Points::Structured::getClassTypeId());
addModule(App,"ReverseEngineering");
openCommand("View triangulation");
try {
for (std::vector<App::DocumentObject*>::iterator it = obj.begin(); it != obj.end(); ++it) {
App::DocumentObjectT objT(*it);
QString document = QString::fromStdString(objT.getDocumentPython());
QString object = QString::fromStdString(objT.getObjectPython());
QString command = QString::fromLatin1("%1.addObject('Mesh::Feature', 'View mesh').Mesh = ReverseEngineering.viewTriangulation("
"Points=%2.Points,"
"Width=%2.Width,"
"Height=%2.Height)"
)
.arg(document)
.arg(object)
;
runCommand(Doc, command.toLatin1());
}
commitCommand();
updateActive();
}
catch (const Base::Exception& e) {
abortCommand();
QMessageBox::warning(Gui::getMainWindow(),
qApp->translate("Reen_ViewTriangulation", "View triangulation failed"),
QString::fromLatin1(e.what())
);
}
}
bool CmdViewTriangulation::isActive(void)
{
return (Gui::Selection().countObjectsOfType(Points::Structured::getClassTypeId()) > 0);
}
void CreateReverseEngineeringCommands(void)
{
Gui::CommandManager &rcCmdMgr = Gui::Application::Instance->commandManager();
rcCmdMgr.addCommand(new CmdApproxSurface());
rcCmdMgr.addCommand(new CmdApproxPlane());
rcCmdMgr.addCommand(new CmdSegmentation());
rcCmdMgr.addCommand(new CmdMeshBoundary());
rcCmdMgr.addCommand(new CmdPoissonReconstruction());
rcCmdMgr.addCommand(new CmdViewTriangulation());
}