1
/***************************************************************************
2
* Copyright (c) 2009 J�rgen Riegel <juergen.riegel@web.de> *
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
***************************************************************************/
24
#include "PreCompiled.h"
27
# include <Standard_math.hxx>
28
# include <Inventor/SoPath.h>
29
# include <Inventor/SoPickedPoint.h>
30
# include <Inventor/details/SoLineDetail.h>
31
# include <Inventor/details/SoPointDetail.h>
32
# include <Inventor/nodes/SoBaseColor.h>
33
# include <Inventor/nodes/SoCoordinate3.h>
34
# include <Inventor/nodes/SoDrawStyle.h>
35
# include <Inventor/nodes/SoLineSet.h>
36
# include <Inventor/nodes/SoPointSet.h>
37
# include <Inventor/nodes/SoMaterial.h>
38
# include <Inventor/nodes/SoSeparator.h>
39
# include <Inventor/nodes/SoVertexProperty.h>
40
# include <QMessageBox>
43
/// Here the FreeCAD includes sorted by Base,App,Gui......
44
#include <Base/Parameter.h>
45
#include <Base/Console.h>
46
#include <Gui/Application.h>
47
#include <Gui/Document.h>
48
#include <Gui/MainWindow.h>
49
#include <Gui/DlgEditFileIncludeProptertyExternal.h>
51
#include <Mod/Sketcher/App/SketchFlatInterface.h>
52
#include <Mod/Sketcher/App/SketchObjectSF.h>
55
#include "ViewProviderSketchSF.h"
58
using namespace SketcherGui;
59
using namespace Sketcher;
61
const float fCurveColor[] = {1.0f,1.0f,1.0f};
62
const float fCurveConstructionColor[] = {0.2f,1.0f,0.2f};
63
const float fPointColor[] = {0.9f,0.9f,0.9f};
64
const float fPreselectColor[] = {0.8f,0.0f,0.0f};
65
const float fSelectColor[] = {1.0f,0.0f,0.0f};
66
const float fDatumLineColor[] = {0.0f,0.8f,0.0f};
68
//**************************************************************************
69
// Construction/Destruction
71
PROPERTY_SOURCE(SketcherGui::ViewProviderSketchSF, PartGui::ViewProvider2DObject)
74
ViewProviderSketchSF::ViewProviderSketchSF()
75
: Mode(STATUS_NONE),DragPoint(-1),EditRoot(0)
90
sPixmap = "Sketcher_NewSketch";
93
ViewProviderSketchSF::~ViewProviderSketchSF()
99
// **********************************************************************************
101
void ViewProviderSketchSF::setSketchMode(int mode)
106
bool ViewProviderSketchSF::setConstrainOnSelected(int Constrain)
111
bool ViewProviderSketchSF::keyPressed(int key)
116
void ViewProviderSketchSF::getCoordsOnSketchPlane(double &u, double &v,const SbVec3f &point, const SbVec3f &normal)
119
Base::Vector3d R0(0,0,0),RN(0,0,1),RX(1,0,0),RY(0,1,0);
121
// move to position of Sketch
122
Base::Placement Plz = getSketchObjectSF()->Placement.getValue();
123
R0 = Plz.getPosition() ;
124
Base::Rotation tmp(Plz.getRotation());
128
Plz.setRotation(tmp);
131
Base::Vector3d R1(point[0],point[1],point[2]),RA(normal[0],normal[1],normal[2]);
132
// intersection point on plane
133
Base::Vector3d S = R1 + ((RN * (R0-R1))/(RN*RA))*RA;
135
// distance to x Axle of the sketch
136
S.TransformToCoordinateSystem(R0,RX,RY);
142
bool ViewProviderSketchSF::mouseButtonPressed(int Button, bool pressed, const SbVec3f &point,
143
const SbVec3f &normal, const SoPickedPoint* pp)
148
const SoDetail* detail = pp->getDetail();
149
if (detail && detail->getTypeId() == SoPointDetail::getClassTypeId()) {
150
pos = pp->getPoint();
154
getCoordsOnSketchPlane(x,y,pos,normal);
156
// Left Mouse button ****************************************************
159
// Do things depending on the mode of the user interaction
162
if (PreselectPoint >=0) {
163
this->DragPoint = SketchFlat->getPoint(PreselectPoint);
164
Base::Console().Log("start dragging, point:%d\n",this->DragPoint);
165
SketchFlat->forcePoint(this->DragPoint,x,y);
166
Mode = STATUS_SKETCH_DragPoint;
171
case STATUS_SKETCH_CreateArc:
172
case STATUS_SKETCH_CreateCircle:
173
case STATUS_SKETCH_CreateRectangle:
174
case STATUS_SKETCH_CreateText:
176
case STATUS_SKETCH_CreateLine:
177
this->DragPoint = SketchFlat->addLine(x,y);
178
SketchFlat->forcePoint(this->DragPoint,x,y);
179
Mode = STATUS_SKETCH_DoLine;
182
case STATUS_SKETCH_DoLine:
183
SketchFlat->forcePoint(this->DragPoint,x,y);
186
Base::Console().Log("Finish line, point:%d\n",this->DragPoint);
187
this->DragPoint = -1;
190
case STATUS_SKETCH_CreatePolyline:
191
this->DragPoint = SketchFlat->addLine(x,y);
192
SketchFlat->forcePoint(this->DragPoint,x,y);
193
Mode = STATUS_SKETCH_DoPolyline;
196
case STATUS_SKETCH_DoPolyline:
197
SketchFlat->forcePoint(this->DragPoint,x,y);
199
this->DragPoint = SketchFlat->addLine(x,y);
200
SketchFlat->forcePoint(this->DragPoint,x,y);
209
// Do things depending on the mode of the user interaction
211
case STATUS_SKETCH_DragPoint:
212
SketchFlat->forcePoint(this->DragPoint,x,y);
215
this->DragPoint = -1;
223
// Right mouse button ****************************************************
224
else if (Button == 2) {
227
case STATUS_SKETCH_DoPolyline:
228
SketchFlat->forcePoint(this->DragPoint,x,y);
231
Base::Console().Log("Finish polyline, point:%d\n",this->DragPoint);
232
this->DragPoint = -1;
244
bool ViewProviderSketchSF::mouseMove(const SbVec3f &point, const SbVec3f &normal, const SoPickedPoint* pp)
247
getCoordsOnSketchPlane(x,y,point,normal);
249
handlePreselection(pp);
254
case STATUS_SKETCH_CreateArc:
255
case STATUS_SKETCH_CreateCircle:
256
case STATUS_SKETCH_CreatePolyline:
257
case STATUS_SKETCH_CreateRectangle:
258
case STATUS_SKETCH_CreateText:
259
case STATUS_SKETCH_CreateLine:
261
case STATUS_SKETCH_DoLine:
262
case STATUS_SKETCH_DoPolyline:
263
case STATUS_SKETCH_DragPoint:
264
SketchFlat->forcePoint(this->DragPoint,x,y);
273
bool ViewProviderSketchSF::handlePreselection(const SoPickedPoint* Point)
276
Base::Console().Log("Point pick\n");
277
const SoDetail* point_detail = Point->getDetail(this->PointSet);
278
if (point_detail && point_detail->getTypeId() == SoPointDetail::getClassTypeId()) {
280
int idx = static_cast<const SoPointDetail*>(point_detail)->getCoordinateIndex();
281
if (PreselectPoint != idx) {
282
PointsMaterials->diffuseColor.set1Value(idx,fPreselectColor);
283
if (PreselectPoint >= 0)
284
PointsMaterials->diffuseColor.set1Value(PreselectPoint,fPointColor);
285
PreselectPoint = idx;
286
if (PreselectCurve >= 0)
287
CurvesMaterials->diffuseColor.set1Value(PreselectCurve,PreselectOldColor);
291
Base::Console().Log("Point pick%d\n",idx);
295
const SoDetail* curve_detail = Point->getDetail(this->CurveSet);
296
if (curve_detail && curve_detail->getTypeId() == SoLineDetail::getClassTypeId()) {
298
int idx = static_cast<const SoLineDetail*>(curve_detail)->getLineIndex();
299
if (PreselectCurve != idx) {
300
if (PreselectCurve >= 0)
301
CurvesMaterials->diffuseColor.set1Value(PreselectCurve,PreselectOldColor);
302
PreselectOldColor = CurvesMaterials->diffuseColor[idx];
303
CurvesMaterials->diffuseColor.set1Value(idx,fPreselectColor);
304
PreselectCurve = idx;
305
if (PreselectPoint >= 0)
306
PointsMaterials->diffuseColor.set1Value(PreselectPoint,fPointColor);
310
Base::Console().Log("Curve pick%d\n",idx);
314
const SoDetail* datum_detail = Point->getDetail(this->LineSet);
315
if (datum_detail && datum_detail->getTypeId() == SoLineDetail::getClassTypeId()) {
317
unsigned long idx = static_cast<const SoLineDetail*>(datum_detail)->getPartIndex();
318
Base::Console().Log("Datum pick%d\n",idx);
323
if (PreselectCurve >= 0)
324
CurvesMaterials->diffuseColor.set1Value(PreselectCurve,PreselectOldColor);
326
if (PreselectPoint >= 0)
327
PointsMaterials->diffuseColor.set1Value(PreselectPoint,fPointColor);
333
bool ViewProviderSketchSF::isPointOnSketch(const SoPickedPoint* pp) const
335
// checks if we picked a point on the sketch or any other nodes like the grid
336
SoPath* path = pp->getPath();
337
return path->containsNode(EditRoot);
340
bool ViewProviderSketchSF::doubleClicked(void)
343
Sketcher::SketchObjectSF *obj = static_cast<Sketcher::SketchObjectSF *>(getObject());
345
Gui::Dialog::DlgEditFileIncludePropertyExternal dlg((obj->SketchFlatFile),Gui::getMainWindow());
347
dlg.ProcName = QString::fromUtf8((App::Application::Config()["AppHomePath"] + "bin/sketchflat.exe").c_str());
350
App::GetApplication().getActiveDocument()->recompute();
352
//Gui::Application::Instance->activeDocument()->setEdit(this);
356
void ViewProviderSketchSF::draw(void)
359
//double x0, y0, dx, dy;
362
// sketchflat generate curves out of entities:
363
//SketchFlat->setUpRendering();
365
// go through the curves and collect the points
366
//int NbrCrv = SketchFlat->nbrOfCurves();
368
//std::vector<std::vector<Base::Vector3d> > coords(NbrCrv);
369
//for (i=0 ; i<NbrCrv;++i) {
370
// SketchFlat->getCurvePoints(coords[i],Construction,i);
371
// totalPts += coords[i].size();
374
std::vector<Base::Vector3d> Coords;
375
std::vector<unsigned int> Index;
376
std::vector<unsigned int> Color;
378
SketchFlat->getLineSet(Coords,Index,Color);
380
CurveSet->numVertices.setNum(Index.size());
381
CurvesCoordinate->point.setNum(Coords.size());
382
CurvesMaterials->diffuseColor.setNum(Color.size());
384
SbVec3f* verts = CurvesCoordinate->point.startEditing();
385
int32_t* index = CurveSet->numVertices.startEditing();
386
SbColor* color = CurvesMaterials->diffuseColor.startEditing();
389
for(std::vector<Base::Vector3d>::const_iterator it=Coords.begin();it!=Coords.end();++it,i++){
390
verts[i].setValue(it->x,it->y,0.1f);
393
for(std::vector<unsigned int>::const_iterator it=Index.begin();it!=Index.end();++it,i++){
397
for(std::vector<unsigned int>::const_iterator it=Color.begin();it!=Color.end();++it,i++){
398
color[i].setValue((*it==1?fCurveConstructionColor:fCurveColor));
401
CurvesCoordinate->point.finishEditing();
402
CurveSet->numVertices.finishEditing();
403
CurvesMaterials->diffuseColor.finishEditing();
406
// // use the start/finish editing facility for optimization
407
// SbVec3f* verts = CurvesCoordinate->point.startEditing();
408
// int32_t* index = CurveSet->numVertices.startEditing();
409
// SbColor* color = CurvesMaterials->diffuseColor.startEditing();
411
// for (i=0 ; i<NbrCrv;++i) {
412
// const std::vector<Base::Vector3d>& c = coords[i];
413
// for (std::vector<Base::Vector3d>::const_iterator it = c.begin(); it != c.end(); ++it)
414
// verts[r++].setValue(it->x,it->y,0.1f);
415
// index[i] = c.size();
416
// color[i].setValue((Construction?fCurveConstructionColor:fCurveColor));
418
// CurvesCoordinate->point.finishEditing();
419
// CurveSet->numVertices.finishEditing();
420
// CurvesMaterials->diffuseColor.finishEditing();
423
//// set up datum lines
424
//int NbrLns = SketchFlat->nbrOfLines();
425
//LinesCoordinate->point.setNum(2*NbrLns);
426
//LinesMaterials->diffuseColor.setNum(NbrLns);
427
//LineSet->numVertices.setNum(NbrLns);
429
// // use the start/finish editing facility for optimization
430
// SbVec3f* verts = LinesCoordinate->point.startEditing();
431
// int32_t* index = LineSet->numVertices.startEditing();
432
// SbColor* color = LinesMaterials->diffuseColor.startEditing();
433
// for (i=0; i<NbrLns; ++i) {
434
// SketchFlat->getLine(i, x0, y0, dx, dy);
435
// verts[i*2 ].setValue(x0-dx*50,y0-dy*50,0.1f);
436
// verts[i*2+1].setValue(x0+dx*50,y0+dy*50,0.1f);
438
// color[i].setValue(fDatumLineColor);
440
// LinesCoordinate->point.finishEditing();
441
// LineSet->numVertices.finishEditing();
442
// LinesMaterials->diffuseColor.finishEditing();
446
int NbrPts = SketchFlat->nbrOfPoints();
447
PointsCoordinate->point.setNum(NbrPts);
448
PointsMaterials->diffuseColor.setNum(NbrPts);
450
// use the start/finish editing facility for optimization
451
SbVec3f* verts = PointsCoordinate->point.startEditing();
452
SbColor* color = PointsMaterials->diffuseColor.startEditing();
453
for (i=0; i<NbrPts; ++i) {
454
SketchFlat->getPoint(i,x,y);
455
verts[i].setValue(x,y,0.2f);
456
color[i].setValue(fPointColor);
458
if (PreselectPoint >= 0 && PreselectPoint < NbrPts)
459
color[PreselectPoint].setValue(fPreselectColor);
460
PointsCoordinate->point.finishEditing();
461
PointsMaterials->diffuseColor.finishEditing();
465
void ViewProviderSketchSF::updateData(const App::Property* prop)
467
ViewProvider2DObject::updateData(prop);
470
void ViewProviderSketchSF::onChanged(const App::Property* prop)
473
PartGui::ViewProvider2DObject::onChanged(prop);
476
void ViewProviderSketchSF::attach(App::DocumentObject *pcFeat)
478
ViewProviderPart::attach(pcFeat);
481
bool ViewProviderSketchSF::setEdit(int ModNum)
485
// if (SketchFlatInterface::isAlive()) {
486
// QMessageBox::warning(Gui::getMainWindow(), QObject::tr("Cannot edit sketch"),
487
// QObject::tr("Sketch cannot be edited because there is already another sketch in edit mode.\n"
488
// "Please leave the edit mode for the other sketch to enter edit mode for this sketch."));
492
// // interface to the solver
493
//SketchFlat = new SketchFlatInterface();
495
// // insert the SketchFlat file
496
// SketchFlat->load(getSketchObjectSF()->SketchFlatFile.getValue());
498
// createEditInventorNodes();
499
// this->hide(); // avoid that the wires interfere with the edit lines
501
//ShowGrid.setValue(true);
503
// SketchFlat->solve();
509
void ViewProviderSketchSF::createEditInventorNodes(void)
512
EditRoot = new SoSeparator;
513
pcRoot->addChild(EditRoot);
514
EditRoot->renderCaching = SoSeparator::OFF ;
517
// stuff for the points ++++++++++++++++++++++++++++++++++++++
518
PointsMaterials = new SoMaterial;
519
EditRoot->addChild(PointsMaterials);
521
SoMaterialBinding *MtlBind = new SoMaterialBinding;
522
MtlBind->value = SoMaterialBinding::PER_VERTEX;
523
EditRoot->addChild(MtlBind);
525
PointsCoordinate = new SoCoordinate3;
526
EditRoot->addChild(PointsCoordinate);
528
SoDrawStyle *DrawStyle = new SoDrawStyle;
529
DrawStyle->pointSize = 6;
530
EditRoot->addChild( DrawStyle );
531
PointSet = new SoPointSet;
532
EditRoot->addChild( PointSet );
534
// stuff for the lines +++++++++++++++++++++++++++++++++++++++
535
LinesMaterials = new SoMaterial;
536
EditRoot->addChild(LinesMaterials);
538
MtlBind = new SoMaterialBinding;
539
MtlBind->value = SoMaterialBinding::PER_PART;
540
EditRoot->addChild(MtlBind);
542
LinesCoordinate = new SoCoordinate3;
543
EditRoot->addChild(LinesCoordinate);
545
DrawStyle = new SoDrawStyle;
546
DrawStyle->lineWidth = 3;
547
DrawStyle->linePattern = 0x0fff;
548
EditRoot->addChild(DrawStyle);
550
LineSet = new SoLineSet;
552
EditRoot->addChild(LineSet);
554
// stuff for the Curves +++++++++++++++++++++++++++++++++++++++
555
CurvesMaterials = new SoMaterial;
556
EditRoot->addChild(CurvesMaterials);
558
MtlBind = new SoMaterialBinding;
559
MtlBind->value = SoMaterialBinding::PER_FACE;
560
EditRoot->addChild(MtlBind);
562
CurvesCoordinate = new SoCoordinate3;
563
EditRoot->addChild(CurvesCoordinate);
565
DrawStyle = new SoDrawStyle;
566
DrawStyle->lineWidth = 3;
567
EditRoot->addChild( DrawStyle );
569
CurveSet = new SoLineSet;
571
EditRoot->addChild( CurveSet );
574
void ViewProviderSketchSF::unsetEdit(void)
576
//ShowGrid.setValue(false);
580
// // save the result of editing
581
// if (std::string(getSketchObjectSF()->SketchFlatFile.getValue())=="") {
582
// // make a meaningfull name
583
// Base::FileInfo temp(getSketchObjectSF()->SketchFlatFile.getDocTransientPath()
584
// + "/" + getSketchObjectSF()->getNameInDocument() + ".skf");
585
// if (temp.exists())
586
// // save under save name
587
// file = Base::FileInfo::getTempFileName("Sketch.skf",getSketchObjectSF()
588
// ->SketchFlatFile.getDocTransientPath().c_str());
590
// file = temp.filePath();
593
// // save under old name
594
// file = getSketchObjectSF()->SketchFlatFile.getExchangeTempFile();
597
// // save the sketch and set the property
598
// SketchFlat->save(file.c_str());
599
// getSketchObjectSF()->SketchFlatFile.setValue(file.c_str());
600
// getSketchObjectSF()->touch();
602
//// close the solver
603
//delete(SketchFlat);
605
// // recompute the part
606
// getSketchObjectSF()->getDocument()->recompute();
609
//EditRoot->removeAllChildren();
610
//PointsMaterials = 0;
611
//LinesMaterials = 0;
612
//CurvesMaterials = 0;
613
//PointsCoordinate = 0;
614
//LinesCoordinate = 0;
615
//CurvesCoordinate = 0;
620
// PreselectCurve = -1;
621
// PreselectPoint = -1;
625
Sketcher::SketchObjectSF* ViewProviderSketchSF::getSketchObjectSF(void)
627
return dynamic_cast<Sketcher::SketchObjectSF*>(pcObject);