/
ViewProviderSpline.cpp
301 lines (261 loc) · 10.9 KB
/
ViewProviderSpline.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
/***************************************************************************
* Copyright (c) 2013 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 "PreCompiled.h"
#ifndef _PreComp_
# include <BRepAdaptor_Curve.hxx>
# include <BRepAdaptor_Surface.hxx>
# include <GeomAbs_CurveType.hxx>
# include <GeomAbs_SurfaceType.hxx>
# include <Geom_BezierCurve.hxx>
# include <Geom_BSplineCurve.hxx>
# include <Geom_BezierSurface.hxx>
# include <Geom_BSplineSurface.hxx>
# include <gp_Pnt.hxx>
# include <TopoDS.hxx>
# include <TopoDS_Edge.hxx>
# include <TopoDS_Wire.hxx>
# include <TopoDS_Face.hxx>
# include <TopoDS_Shape.hxx>
# include <TopoDS_Shell.hxx>
# include <TopExp_Explorer.hxx>
# include <Inventor/nodes/SoCoordinate3.h>
# include <Inventor/nodes/SoSeparator.h>
# include <Inventor/nodes/SoSwitch.h>
# include <QAction>
# include <QMenu>
#endif
#include <boost/bind.hpp>
#include <App/PropertyStandard.h>
#include <Mod/Part/App/PartFeature.h>
#include <Gui/ActionFunction.h>
#include "SoFCShapeObject.h"
#include "ViewProviderSpline.h"
using namespace PartGui;
PROPERTY_SOURCE(PartGui::ViewProviderSpline, PartGui::ViewProviderPartExt)
ViewProviderSpline::ViewProviderSpline()
: pcControlPoints(0)
{
ADD_PROPERTY(ControlPoints,(false));
}
ViewProviderSpline::~ViewProviderSpline()
{
}
void ViewProviderSpline::setupContextMenu(QMenu* menu, QObject* receiver, const char* member)
{
ViewProviderPartExt::setupContextMenu(menu, receiver, member);
// toggle command to display components
Gui::ActionFunction* func = new Gui::ActionFunction(menu);
QAction* act = menu->addAction(QObject::tr("Show control points"));
act->setCheckable(true);
act->setChecked(ControlPoints.getValue());
func->toggle(act, boost::bind(&ViewProviderSpline::toggleControlPoints, this, _1));
}
void ViewProviderSpline::toggleControlPoints(bool on)
{
ControlPoints.setValue(on);
}
void ViewProviderSpline::updateData(const App::Property* prop)
{
ViewProviderPartExt::updateData(prop);
if (prop->getTypeId() == Part::PropertyPartShape::getClassTypeId() && strcmp(prop->getName(), "Shape") == 0) {
// update control points if there
if (pcControlPoints) {
pcControlPoints->removeAllChildren();
showControlPoints(this->ControlPoints.getValue(), prop);
}
}
}
void ViewProviderSpline::onChanged(const App::Property* prop)
{
if (prop == &ControlPoints) {
App::DocumentObject* obj = this->pcObject;
App::Property* shape = obj->getPropertyByName("Shape");
showControlPoints(ControlPoints.getValue(), shape);
}
else {
ViewProviderPartExt::onChanged(prop);
}
}
void ViewProviderSpline::showControlPoints(bool show, const App::Property* prop)
{
if (!pcControlPoints && show) {
pcControlPoints = new SoSwitch();
pcRoot->addChild(pcControlPoints);
}
if (pcControlPoints) {
pcControlPoints->whichChild = (show ? SO_SWITCH_ALL : SO_SWITCH_NONE);
}
if (!show || !pcControlPoints || pcControlPoints->getNumChildren() > 0)
return;
// ask for the property we are interested in
if (prop && prop->getTypeId() == Part::PropertyPartShape::getClassTypeId()) {
const TopoDS_Shape& shape = static_cast<const Part::PropertyPartShape*>(prop)->getValue();
if (shape.IsNull())
return; // empty shape
for (TopExp_Explorer xp(shape, TopAbs_SHELL); xp.More(); xp.Next()) {
const TopoDS_Shell& shell = TopoDS::Shell(xp.Current());
for (TopExp_Explorer xp2(shell, TopAbs_FACE); xp2.More(); xp2.Next()) {
const TopoDS_Face& face = TopoDS::Face(xp2.Current());
showControlPointsOfFace(face);
}
}
for (TopExp_Explorer xp(shape, TopAbs_FACE, TopAbs_SHELL); xp.More(); xp.Next()) {
const TopoDS_Face& face = TopoDS::Face(xp.Current());
showControlPointsOfFace(face);
}
for (TopExp_Explorer xp(shape, TopAbs_WIRE, TopAbs_FACE); xp.More(); xp.Next()) {
const TopoDS_Wire& wire = TopoDS::Wire(xp.Current());
for (TopExp_Explorer xp2(wire, TopAbs_EDGE); xp2.More(); xp2.Next()) {
const TopoDS_Edge& edge = TopoDS::Edge(xp2.Current());
showControlPointsOfEdge(edge);
}
}
for (TopExp_Explorer xp(shape, TopAbs_EDGE, TopAbs_WIRE); xp.More(); xp.Next()) {
const TopoDS_Edge& edge = TopoDS::Edge(xp.Current());
showControlPointsOfEdge(edge);
}
}
}
void ViewProviderSpline::showControlPointsOfEdge(const TopoDS_Edge& edge)
{
std::list<gp_Pnt> poles, knots;
Standard_Integer nCt=0;
BRepAdaptor_Curve curve(edge);
switch (curve.GetType())
{
case GeomAbs_BezierCurve:
{
Handle(Geom_BezierCurve) hBezier = curve.Bezier();
nCt = hBezier->NbPoles();
for (Standard_Integer i = 1; i <= nCt; i++)
poles.push_back(hBezier->Pole(i));
if (hBezier->IsClosed()) {
nCt++;
poles.push_back(hBezier->Pole(1));
}
} break;
case GeomAbs_BSplineCurve:
{
Handle(Geom_BSplineCurve) hBSpline = curve.BSpline();
nCt = hBSpline->NbPoles();
for (Standard_Integer i = 1; i <= nCt; i++)
poles.push_back(hBSpline->Pole(i));
if (hBSpline->IsClosed()) {
nCt++;
poles.push_back(hBSpline->Pole(1));
}
for (Standard_Integer i = hBSpline->FirstUKnotIndex()+1; i <= hBSpline->LastUKnotIndex()-1; i++)
knots.push_back(hBSpline->Value(hBSpline->Knot(i)));
} break;
default:
break;
}
if (poles.empty())
return; // nothing to do
SoCoordinate3 * coords = new SoCoordinate3;
coords->point.setNum(nCt + knots.size());
int index=0;
SbVec3f* verts = coords->point.startEditing();
for (std::list<gp_Pnt>::iterator p = poles.begin(); p != poles.end(); ++p) {
verts[index++].setValue((float)p->X(), (float)p->Y(), (float)p->Z());
}
for (std::list<gp_Pnt>::iterator k = knots.begin(); k != knots.end(); ++k) {
verts[index++].setValue((float)k->X(), (float)k->Y(), (float)k->Z());
}
coords->point.finishEditing();
SoFCControlPoints* control = new SoFCControlPoints();
control->numPolesU = nCt;
control->numPolesV = 1;
SoSeparator* nodes = new SoSeparator();
nodes->addChild(coords);
nodes->addChild(control);
pcControlPoints->addChild(nodes);
}
void ViewProviderSpline::showControlPointsOfFace(const TopoDS_Face& face)
{
std::list<gp_Pnt> knots;
std::vector<std::vector<gp_Pnt> > poles;
Standard_Integer nCtU=0, nCtV=0;
BRepAdaptor_Surface surface(face);
BRepAdaptor_Surface clSurface(face);
switch (clSurface.GetType())
{
case GeomAbs_BezierSurface:
{
Handle(Geom_BezierSurface) hBezier = surface.Bezier();
nCtU = hBezier->NbUPoles();
nCtV = hBezier->NbVPoles();
poles.resize(nCtU);
for (Standard_Integer u = 1; u <= nCtU; u++) {
poles[u-1].resize(nCtV);
for (Standard_Integer v = 1; v <= nCtV; v++)
poles[u-1][v-1] = hBezier->Pole(u, v);
}
} break;
case GeomAbs_BSplineSurface:
{
Handle(Geom_BSplineSurface) hBSpline = surface.BSpline();
nCtU = hBSpline->NbUPoles();
nCtV = hBSpline->NbVPoles();
poles.resize(nCtU);
for (Standard_Integer u = 1; u <= nCtU; u++) {
poles[u-1].resize(nCtV);
for (Standard_Integer v = 1; v <= nCtV; v++)
poles[u-1][v-1] = hBSpline->Pole(u, v);
}
//Standard_Integer nKnU = hBSpline->NbUKnots();
//Standard_Integer nKnV = hBSpline->NbVKnots();
for (Standard_Integer u = 1; u <= hBSpline->NbUKnots(); u++) {
for (Standard_Integer v = 1; v <= hBSpline->NbVKnots(); v++)
knots.push_back(hBSpline->Value(hBSpline->UKnot(u), hBSpline->VKnot(v)));
}
} break;
default:
break;
}
if (poles.empty())
return; // nothing to do
SoCoordinate3 * coords = new SoCoordinate3;
coords->point.setNum(nCtU * nCtV + knots.size());
int index=0;
SbVec3f* verts = coords->point.startEditing();
for (std::vector<std::vector<gp_Pnt> >::iterator u = poles.begin(); u != poles.end(); ++u) {
for (std::vector<gp_Pnt>::iterator v = u->begin(); v != u->end(); ++v) {
verts[index++].setValue((float)v->X(), (float)v->Y(), (float)v->Z());
}
}
for (std::list<gp_Pnt>::iterator k = knots.begin(); k != knots.end(); ++k) {
verts[index++].setValue((float)k->X(), (float)k->Y(), (float)k->Z());
}
coords->point.finishEditing();
SoFCControlPoints* control = new SoFCControlPoints();
control->numPolesU = nCtU;
control->numPolesV = nCtV;
//if (knots.size() > 0) {
// control->numKnotsU = nKnU;
// control->numKnotsV = nKnV;
//}
SoSeparator* nodes = new SoSeparator();
nodes->addChild(coords);
nodes->addChild(control);
pcControlPoints->addChild(nodes);
}