Salome HOME
Merge branch 'master' into cgt/devCEA
[modules/shaper.git] / src / SketchPlugin / SketchPlugin_Circle.cpp
1 // Copyright (C) 2014-2017  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
19 //
20
21 #include "SketchPlugin_Circle.h"
22 #include "SketchPlugin_Sketch.h"
23 #include <ModelAPI_Data.h>
24 #include <ModelAPI_ResultConstruction.h>
25 #include <ModelAPI_AttributeSelection.h>
26 #include <ModelAPI_Validator.h>
27 #include <ModelAPI_AttributeDouble.h>
28 #include <ModelAPI_AttributeString.h>
29 #include <ModelAPI_Session.h>
30
31 #include <GeomAPI_Pnt2d.h>
32 #include <GeomAPI_Circ.h>
33 #include <GeomAPI_Circ2d.h>
34 #include <GeomAPI_Vertex.h>
35 #include <GeomAPI_XY.h>
36 #include <GeomDataAPI_Point2D.h>
37 #include <GeomDataAPI_Dir.h>
38 #include <GeomAlgoAPI_PointBuilder.h>
39 #include <GeomAlgoAPI_EdgeBuilder.h>
40 #include <GeomAlgoAPI_CompoundBuilder.h>
41
42 #include <cmath>
43
44 const double tolerance = 1e-7;
45
46
47 SketchPlugin_Circle::SketchPlugin_Circle()
48 : SketchPlugin_SketchEntity()
49 {
50 }
51
52 void SketchPlugin_Circle::initDerivedClassAttributes()
53 {
54   data()->addAttribute(CENTER_ID(), GeomDataAPI_Point2D::typeId());
55   data()->addAttribute(RADIUS_ID(), ModelAPI_AttributeDouble::typeId());
56
57   data()->addAttribute(EXTERNAL_ID(), ModelAPI_AttributeSelection::typeId());
58   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), EXTERNAL_ID());
59 }
60
61 void SketchPlugin_Circle::execute()
62 {
63   SketchPlugin_Sketch* aSketch = sketch();
64   if(!aSketch) {
65     return;
66   }
67
68   // Compute a circle in 3D view.
69   std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr =
70       std::dynamic_pointer_cast<GeomDataAPI_Point2D>(data()->attribute(CENTER_ID()));
71   AttributeDoublePtr aRadiusAttr = real(RADIUS_ID());
72   if(!aCenterAttr->isInitialized() || !aRadiusAttr->isInitialized()) {
73     return;
74   }
75
76   double aRadius = aRadiusAttr->value();
77   if(aRadius < tolerance) {
78     return;
79   }
80
81   // Make a visible point.
82   SketchPlugin_Sketch::createPoint2DResult(this, sketch(), CENTER_ID(), 0);
83
84   // Make a visible circle.
85   std::shared_ptr<GeomAPI_Pnt> aCenter(aSketch->to3D(aCenterAttr->x(), aCenterAttr->y()));
86   std::shared_ptr<GeomDataAPI_Dir> aNDir = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
87       aSketch->data()->attribute(SketchPlugin_Sketch::NORM_ID()));
88   std::shared_ptr<GeomAPI_Dir> aNormal(new GeomAPI_Dir(aNDir->x(), aNDir->y(), aNDir->z()));
89
90   std::shared_ptr<GeomAPI_Shape> aCircleShape =
91       GeomAlgoAPI_EdgeBuilder::lineCircle(aCenter, aNormal, aRadius);
92
93   std::shared_ptr<ModelAPI_ResultConstruction> aResult = document()->createConstruction(data(), 1);
94   aResult->setShape(aCircleShape);
95   aResult->setIsInHistory(false);
96   setResult(aResult, 1);
97 }
98
99 void SketchPlugin_Circle::move(double theDeltaX, double theDeltaY)
100 {
101   std::shared_ptr<ModelAPI_Data> aData = data();
102   if(!aData->isValid()) {
103     return;
104   }
105
106   std::shared_ptr<GeomDataAPI_Point2D> aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
107       aData->attribute(CENTER_ID()));
108   if(aPoint->isInitialized()) {
109     aPoint->move(theDeltaX, theDeltaY);
110   }
111 }
112
113 bool SketchPlugin_Circle::isFixed() {
114   return data()->selection(EXTERNAL_ID())->context().get() != NULL;
115 }
116
117 void SketchPlugin_Circle::attributeChanged(const std::string& theID) {
118   // the second condition for unability to move external segments anywhere
119   if (theID == EXTERNAL_ID() || isFixed()) {
120     std::shared_ptr<GeomAPI_Shape> aSelection = data()->selection(EXTERNAL_ID())->value();
121     if (!aSelection) {
122       // empty shape in selection shows that the shape is equal to context
123       ResultPtr anExtRes = selection(EXTERNAL_ID())->context();
124       if (anExtRes)
125         aSelection = anExtRes->shape();
126     }
127     // update arguments due to the selection value
128     if (aSelection && !aSelection->isNull() && aSelection->isEdge()) {
129       std::shared_ptr<GeomAPI_Edge> anEdge( new GeomAPI_Edge(aSelection));
130       std::shared_ptr<GeomAPI_Circ> aCirc = anEdge->circle();
131       std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr =
132         std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(CENTER_ID()));
133       aCenterAttr->setValue(sketch()->to2D(aCirc->center()));
134       real(RADIUS_ID())->setValue(aCirc->radius());
135     }
136   }
137 }