Salome HOME
Update copyrights
[modules/shaper.git] / src / SketchPlugin / SketchPlugin_Circle.cpp
1 // Copyright (C) 2014-2019  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 email : webmaster.salome@opencascade.com
18 //
19
20 #include "SketchPlugin_Circle.h"
21 #include "SketchPlugin_Sketch.h"
22 #include <ModelAPI_Data.h>
23 #include <ModelAPI_ResultConstruction.h>
24 #include <ModelAPI_AttributeSelection.h>
25 #include <ModelAPI_Validator.h>
26 #include <ModelAPI_AttributeDouble.h>
27 #include <ModelAPI_AttributeString.h>
28 #include <ModelAPI_Session.h>
29
30 #include <GeomAPI_Pnt2d.h>
31 #include <GeomAPI_Circ.h>
32 #include <GeomAPI_Circ2d.h>
33 #include <GeomAPI_Vertex.h>
34 #include <GeomAPI_XY.h>
35 #include <GeomDataAPI_Point2D.h>
36 #include <GeomDataAPI_Dir.h>
37 #include <GeomAlgoAPI_PointBuilder.h>
38 #include <GeomAlgoAPI_EdgeBuilder.h>
39 #include <GeomAlgoAPI_CompoundBuilder.h>
40
41 #include <cmath>
42
43 const double tolerance = 1e-7;
44
45
46 SketchPlugin_Circle::SketchPlugin_Circle()
47 : SketchPlugin_SketchEntity()
48 {
49 }
50
51 void SketchPlugin_Circle::initDerivedClassAttributes()
52 {
53   data()->addAttribute(CENTER_ID(), GeomDataAPI_Point2D::typeId());
54   data()->addAttribute(RADIUS_ID(), ModelAPI_AttributeDouble::typeId());
55
56   data()->addAttribute(EXTERNAL_ID(), ModelAPI_AttributeSelection::typeId());
57   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), EXTERNAL_ID());
58 }
59
60 void SketchPlugin_Circle::execute()
61 {
62   SketchPlugin_Sketch* aSketch = sketch();
63   if(!aSketch) {
64     return;
65   }
66
67   // Compute a circle in 3D view.
68   std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr =
69       std::dynamic_pointer_cast<GeomDataAPI_Point2D>(data()->attribute(CENTER_ID()));
70   AttributeDoublePtr aRadiusAttr = real(RADIUS_ID());
71   if(!aCenterAttr->isInitialized() || !aRadiusAttr->isInitialized()) {
72     return;
73   }
74
75   double aRadius = aRadiusAttr->value();
76   if(aRadius < tolerance) {
77     return;
78   }
79
80   // Make a visible point.
81   SketchPlugin_Sketch::createPoint2DResult(this, sketch(), CENTER_ID(), 0);
82
83   // Make a visible circle.
84   std::shared_ptr<GeomAPI_Pnt> aCenter(aSketch->to3D(aCenterAttr->x(), aCenterAttr->y()));
85   std::shared_ptr<GeomDataAPI_Dir> aNDir = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
86       aSketch->data()->attribute(SketchPlugin_Sketch::NORM_ID()));
87   std::shared_ptr<GeomAPI_Dir> aNormal(new GeomAPI_Dir(aNDir->x(), aNDir->y(), aNDir->z()));
88
89   std::shared_ptr<GeomAPI_Shape> aCircleShape =
90       GeomAlgoAPI_EdgeBuilder::lineCircle(aCenter, aNormal, aRadius);
91
92   std::shared_ptr<ModelAPI_ResultConstruction> aResult = document()->createConstruction(data(), 1);
93   aResult->setShape(aCircleShape);
94   aResult->setIsInHistory(false);
95   setResult(aResult, 1);
96 }
97
98 bool SketchPlugin_Circle::isFixed() {
99   return data()->selection(EXTERNAL_ID())->context().get() != NULL;
100 }
101
102 void SketchPlugin_Circle::attributeChanged(const std::string& theID) {
103   // the second condition for unability to move external segments anywhere
104   if (theID == EXTERNAL_ID() || isFixed()) {
105     std::shared_ptr<GeomAPI_Shape> aSelection = data()->selection(EXTERNAL_ID())->value();
106     if (!aSelection) {
107       // empty shape in selection shows that the shape is equal to context
108       ResultPtr anExtRes = selection(EXTERNAL_ID())->context();
109       if (anExtRes)
110         aSelection = anExtRes->shape();
111     }
112     // update arguments due to the selection value
113     if (aSelection && !aSelection->isNull() && aSelection->isEdge()) {
114       std::shared_ptr<GeomAPI_Edge> anEdge( new GeomAPI_Edge(aSelection));
115       std::shared_ptr<GeomAPI_Circ> aCirc = anEdge->circle();
116       std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr =
117         std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(CENTER_ID()));
118       aCenterAttr->setValue(sketch()->to2D(aCirc->center()));
119       real(RADIUS_ID())->setValue(aCirc->radius());
120     }
121   }
122 }