/
CommandSketcherBSpline.cpp
426 lines (337 loc) · 15 KB
/
CommandSketcherBSpline.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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
/***************************************************************************
* Copyright (c) 2017 Abdullah Tahiri <abdullah.tahiri.yo@gmail.com> *
* *
* 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 <cfloat>
# include <QMessageBox>
# include <Precision.hxx>
# include <QApplication>
#endif
# include <QMessageBox>
#include <Base/Console.h>
#include <App/Application.h>
#include <Gui/Application.h>
#include <Gui/Document.h>
#include <Gui/Selection.h>
#include <Gui/Command.h>
#include <Gui/MainWindow.h>
#include <Gui/DlgEditFileIncludeProptertyExternal.h>
#include <Gui/Action.h>
#include <Gui/BitmapFactory.h>
#include "ViewProviderSketch.h"
#include "DrawSketchHandler.h"
#include <Mod/Part/App/Geometry.h>
#include <Mod/Sketcher/App/SketchObject.h>
#include "ViewProviderSketch.h"
using namespace std;
using namespace SketcherGui;
using namespace Sketcher;
bool isSketcherBSplineActive(Gui::Document *doc, bool actsOnSelection )
{
if (doc) {
// checks if a Sketch Viewprovider is in Edit and is in no special mode
if (doc->getInEdit() && doc->getInEdit()->isDerivedFrom(SketcherGui::ViewProviderSketch::getClassTypeId())) {
if (static_cast<SketcherGui::ViewProviderSketch*>(doc->getInEdit())
->getSketchMode() == ViewProviderSketch::STATUS_NONE) {
if (!actsOnSelection)
return true;
else if (Gui::Selection().countObjectsOfType(Sketcher::SketchObject::getClassTypeId()) > 0)
return true;
}
}
}
return false;
}
void ActivateBSplineHandler(Gui::Document *doc,DrawSketchHandler *handler)
{
if (doc) {
if (doc->getInEdit() && doc->getInEdit()->isDerivedFrom
(SketcherGui::ViewProviderSketch::getClassTypeId())) {
SketcherGui::ViewProviderSketch* vp = static_cast<SketcherGui::ViewProviderSketch*> (doc->getInEdit());
vp->purgeHandler();
vp->activateHandler(handler);
}
}
}
void ShowRestoreInformationLayer(SketcherGui::ViewProviderSketch* vp, char * visibleelementname)
{
ParameterGrp::handle hGrp = App::GetApplication().GetParameterGroupByPath("User parameter:BaseApp/Preferences/Mod/Sketcher/General");
bool status = hGrp->GetBool(visibleelementname, true);
hGrp->SetBool(visibleelementname, !status);
vp->showRestoreInformationLayer();
}
// Show/Hide BSpline degree
DEF_STD_CMD_A(CmdSketcherBSplineDegree);
CmdSketcherBSplineDegree::CmdSketcherBSplineDegree()
:Command("Sketcher_BSplineDegree")
{
sAppModule = "Sketcher";
sGroup = QT_TR_NOOP("Sketcher");
sMenuText = QT_TR_NOOP("Show/Hide B-Spline degree");
sToolTipText = QT_TR_NOOP("Switches between showing and hiding the degree for all B-Splines");
sWhatsThis = "Sketcher_BSplineDegree";
sStatusTip = sToolTipText;
sPixmap = "Sketcher_BSplineDegree";
sAccel = "";
eType = ForEdit;
}
void CmdSketcherBSplineDegree::activated(int iMsg)
{
Q_UNUSED(iMsg);
Gui::Document * doc= getActiveGuiDocument();
SketcherGui::ViewProviderSketch* vp = static_cast<SketcherGui::ViewProviderSketch*>(doc->getInEdit());
ShowRestoreInformationLayer(vp, "BSplineDegreeVisible");
}
bool CmdSketcherBSplineDegree::isActive(void)
{
return isSketcherBSplineActive( getActiveGuiDocument(), false );
}
// Show/Hide BSpline polygon
DEF_STD_CMD_A(CmdSketcherBSplinePolygon);
CmdSketcherBSplinePolygon::CmdSketcherBSplinePolygon()
:Command("Sketcher_BSplinePolygon")
{
sAppModule = "Sketcher";
sGroup = QT_TR_NOOP("Sketcher");
sMenuText = QT_TR_NOOP("Show/Hide B-Spline control polygon");
sToolTipText = QT_TR_NOOP("Switches between showing and hiding the control polygons for all B-Splines");
sWhatsThis = "Sketcher_BSplinePolygon";
sStatusTip = sToolTipText;
sPixmap = "Sketcher_BSplinePolygon";
sAccel = "";
eType = ForEdit;
}
void CmdSketcherBSplinePolygon::activated(int iMsg)
{
Q_UNUSED(iMsg);
Gui::Document * doc= getActiveGuiDocument();
SketcherGui::ViewProviderSketch* vp = static_cast<SketcherGui::ViewProviderSketch*>(doc->getInEdit());
ShowRestoreInformationLayer(vp, "BSplineControlPolygonVisible");
}
bool CmdSketcherBSplinePolygon::isActive(void)
{
return isSketcherBSplineActive( getActiveGuiDocument(), false );
}
// Show/Hide BSpline comb
DEF_STD_CMD_A(CmdSketcherBSplineComb);
CmdSketcherBSplineComb::CmdSketcherBSplineComb()
:Command("Sketcher_BSplineComb")
{
sAppModule = "Sketcher";
sGroup = QT_TR_NOOP("Sketcher");
sMenuText = QT_TR_NOOP("Show/Hide B-Spline curvature comb");
sToolTipText = QT_TR_NOOP("Switches between showing and hiding the curvature comb for all B-Splines");
sWhatsThis = "Sketcher_BSplineComb";
sStatusTip = sToolTipText;
sPixmap = "Sketcher_BSplineComb";
sAccel = "";
eType = ForEdit;
}
void CmdSketcherBSplineComb::activated(int iMsg)
{
Q_UNUSED(iMsg);
Gui::Document * doc= getActiveGuiDocument();
SketcherGui::ViewProviderSketch* vp = static_cast<SketcherGui::ViewProviderSketch*>(doc->getInEdit());
ShowRestoreInformationLayer(vp, "BSplineCombVisible");
}
bool CmdSketcherBSplineComb::isActive(void)
{
return isSketcherBSplineActive( getActiveGuiDocument(), false );
}
// Composite drop down menu for show/hide geometry information layer
DEF_STD_CMD_ACLU(CmdSketcherCompBSplineShowHideGeometryInformation);
CmdSketcherCompBSplineShowHideGeometryInformation::CmdSketcherCompBSplineShowHideGeometryInformation()
: Command("Sketcher_CompBSplineShowHideGeometryInformation")
{
sAppModule = "Sketcher";
sGroup = QT_TR_NOOP("Sketcher");
sMenuText = QT_TR_NOOP("Show/hide B-Spline information layer");
sToolTipText = QT_TR_NOOP("Show/hide B-Spline information layer");
sWhatsThis = "Sketcher_CompBSplineShowHideGeometryInformation";
sStatusTip = sToolTipText;
eType = ForEdit;
}
void CmdSketcherCompBSplineShowHideGeometryInformation::activated(int iMsg)
{
Gui::CommandManager &rcCmdMgr = Gui::Application::Instance->commandManager();
Gui::Command * cmd;
if (iMsg==0)
cmd = rcCmdMgr.getCommandByName("Sketcher_BSplineDegree");
else if (iMsg==1)
cmd = rcCmdMgr.getCommandByName("Sketcher_BSplinePolygon");
else if (iMsg==2)
cmd = rcCmdMgr.getCommandByName("Sketcher_BSplineComb");
else
return;
cmd->invoke(0);
// Since the default icon is reset when enabing/disabling the command we have
// to explicitly set the icon of the used command.
Gui::ActionGroup* pcAction = qobject_cast<Gui::ActionGroup*>(_pcAction);
QList<QAction*> a = pcAction->actions();
assert(iMsg < a.size());
pcAction->setIcon(a[iMsg]->icon());
}
Gui::Action * CmdSketcherCompBSplineShowHideGeometryInformation::createAction(void)
{
Gui::ActionGroup* pcAction = new Gui::ActionGroup(this, Gui::getMainWindow());
pcAction->setDropDownMenu(true);
applyCommandData(this->className(), pcAction);
QAction* c1 = pcAction->addAction(QString());
c1->setIcon(Gui::BitmapFactory().pixmap("Sketcher_BSplineDegree"));
QAction* c2 = pcAction->addAction(QString());
c2->setIcon(Gui::BitmapFactory().pixmap("Sketcher_BSplinePolygon"));
QAction* c3 = pcAction->addAction(QString());
c3->setIcon(Gui::BitmapFactory().pixmap("Sketcher_BSplineComb"));
_pcAction = pcAction;
languageChange();
pcAction->setIcon(c2->icon());
int defaultId = 1;
pcAction->setProperty("defaultAction", QVariant(defaultId));
return pcAction;
}
void CmdSketcherCompBSplineShowHideGeometryInformation::languageChange()
{
Command::languageChange();
if (!_pcAction)
return;
Gui::ActionGroup* pcAction = qobject_cast<Gui::ActionGroup*>(_pcAction);
QList<QAction*> a = pcAction->actions();
QAction* c1 = a[0];
c1->setText(QApplication::translate("CmdSketcherCompBSplineShowHideGeometryInformation","Show/Hide B-Spline degree"));
c1->setToolTip(QApplication::translate("Sketcher_BSplineDegree","Switches between showing and hiding the degree for all B-Splines"));
c1->setStatusTip(QApplication::translate("Sketcher_BSplineDegree","Switches between showing and hiding the degree for all B-Splines"));
QAction* c2 = a[1];
c2->setText(QApplication::translate("CmdSketcherCompBSplineShowHideGeometryInformation","Show/Hide B-Spline control polygon"));
c2->setToolTip(QApplication::translate("Sketcher_BSplinePolygon","Switches between showing and hiding the control polygons for all B-Splines"));
c2->setStatusTip(QApplication::translate("Sketcher_BSplinePolygon","Switches between showing and hiding the control polygons for all B-Splines"));
QAction* c3 = a[2];
c3->setText(QApplication::translate("CmdSketcherCompBSplineShowHideGeometryInformation","Show/Hide B-Spline curvature comb"));
c3->setToolTip(QApplication::translate("Sketcher_BSplineComb","Switches between showing and hiding the curvature comb for all B-Splines"));
c3->setStatusTip(QApplication::translate("Sketcher_BSplineComb","Switches between showing and hiding the curvature comb for all B-Splines"));
}
void CmdSketcherCompBSplineShowHideGeometryInformation::updateAction(int /*mode*/)
{
}
bool CmdSketcherCompBSplineShowHideGeometryInformation::isActive(void)
{
return isSketcherBSplineActive( getActiveGuiDocument(), false );
}
// Convert to NURB
DEF_STD_CMD_A(CmdSketcherConvertToNURB);
CmdSketcherConvertToNURB::CmdSketcherConvertToNURB()
:Command("Sketcher_BSplineConvertToNURB")
{
sAppModule = "Sketcher";
sGroup = QT_TR_NOOP("Sketcher");
sMenuText = QT_TR_NOOP("Convert Geometry to B-Spline");
sToolTipText = QT_TR_NOOP("Converts the given Geometry to a B-Spline");
sWhatsThis = "Sketcher_ConvertToNURB";
sStatusTip = sToolTipText;
sPixmap = "Sketcher_BSplineApproximate";
sAccel = "";
eType = ForEdit;
}
void CmdSketcherConvertToNURB::activated(int iMsg)
{
Q_UNUSED(iMsg);
// get the selection
std::vector<Gui::SelectionObject> selection = getSelection().getSelectionEx();
// only one sketch with its subelements are allowed to be selected
if (selection.size() != 1) {
return;
}
// get the needed lists and objects
const std::vector<std::string> &SubNames = selection[0].getSubNames();
Sketcher::SketchObject* Obj = static_cast<Sketcher::SketchObject*>(selection[0].getObject());
for (unsigned int i=0; i<SubNames.size(); i++ ) {
// only handle edges
if (SubNames[i].size() > 4 && SubNames[i].substr(0,4) == "Edge") {
int GeoId = std::atoi(SubNames[i].substr(4,4000).c_str()) - 1;
Obj->ConvertToNURBS(GeoId);
}
}
ParameterGrp::handle hGrp = App::GetApplication().GetParameterGroupByPath("User parameter:BaseApp/Preferences/Mod/Sketcher");
bool autoRecompute = hGrp->GetBool("AutoRecompute",false);
if (autoRecompute)
Gui::Command::updateActive();
else
Obj->solve();
}
bool CmdSketcherConvertToNURB::isActive(void)
{
return isSketcherBSplineActive( getActiveGuiDocument(), true );
}
// Convert to NURB
DEF_STD_CMD_A(CmdSketcherIncreaseDegree);
CmdSketcherIncreaseDegree::CmdSketcherIncreaseDegree()
:Command("Sketcher_BSplineIncreaseDegree")
{
sAppModule = "Sketcher";
sGroup = QT_TR_NOOP("Sketcher");
sMenuText = QT_TR_NOOP("Increase degree");
sToolTipText = QT_TR_NOOP("Increases the degree of the B-Spline");
sWhatsThis = "Sketcher_BSplineIncreaseDegree";
sStatusTip = sToolTipText;
sPixmap = "Sketcher_BSplineIncreaseDegree";
sAccel = "";
eType = ForEdit;
}
void CmdSketcherIncreaseDegree::activated(int iMsg)
{
Q_UNUSED(iMsg);
// get the selection
std::vector<Gui::SelectionObject> selection = getSelection().getSelectionEx();
// only one sketch with its subelements are allowed to be selected
if (selection.size() != 1) {
return;
}
// get the needed lists and objects
const std::vector<std::string> &SubNames = selection[0].getSubNames();
Sketcher::SketchObject* Obj = static_cast<Sketcher::SketchObject*>(selection[0].getObject());
for (unsigned int i=0; i<SubNames.size(); i++ ) {
// only handle edges
if (SubNames[i].size() > 4 && SubNames[i].substr(0,4) == "Edge") {
int GeoId = std::atoi(SubNames[i].substr(4,4000).c_str()) - 1;
Obj->IncreaseBSplineDegree(GeoId);
}
}
ParameterGrp::handle hGrp = App::GetApplication().GetParameterGroupByPath("User parameter:BaseApp/Preferences/Mod/Sketcher");
bool autoRecompute = hGrp->GetBool("AutoRecompute",false);
if (autoRecompute)
Gui::Command::updateActive();
else
Obj->solve();
}
bool CmdSketcherIncreaseDegree::isActive(void)
{
return isSketcherBSplineActive( getActiveGuiDocument(), true );
}
void CreateSketcherCommandsBSpline(void)
{
Gui::CommandManager &rcCmdMgr = Gui::Application::Instance->commandManager();
rcCmdMgr.addCommand(new CmdSketcherBSplineDegree());
rcCmdMgr.addCommand(new CmdSketcherBSplinePolygon());
rcCmdMgr.addCommand(new CmdSketcherBSplineComb());
rcCmdMgr.addCommand(new CmdSketcherCompBSplineShowHideGeometryInformation());
rcCmdMgr.addCommand(new CmdSketcherConvertToNURB());
rcCmdMgr.addCommand(new CmdSketcherIncreaseDegree());
}