Salome HOME
Merge branch 'master' of newgeom:newgeom.git into BR_PYTHON_PLUGIN
[modules/shaper.git] / src / SketchPlugin / SketchPlugin_Circle.cpp
1 // File:        SketchPlugin_Circle.cpp
2 // Created:     26 May 2014
3 // Author:      Artem ZHIDKOV
4
5 #include "SketchPlugin_Circle.h"
6 #include "SketchPlugin_Sketch.h"
7 #include <ModelAPI_Data.h>
8 #include <ModelAPI_ResultConstruction.h>
9 #include <ModelAPI_AttributeSelection.h>
10 #include <ModelAPI_Validator.h>
11 #include <ModelAPI_AttributeDouble.h>
12 #include <ModelAPI_Session.h>
13
14 #include <GeomAPI_Pnt2d.h>
15 #include <GeomDataAPI_Point2D.h>
16 #include <GeomDataAPI_Dir.h>
17 #include <GeomAlgoAPI_PointBuilder.h>
18 #include <GeomAlgoAPI_EdgeBuilder.h>
19 #include <GeomAlgoAPI_CompoundBuilder.h>
20
21 SketchPlugin_Circle::SketchPlugin_Circle()
22     : SketchPlugin_Feature()
23 {
24 }
25
26 void SketchPlugin_Circle::initAttributes()
27 {
28   data()->addAttribute(SketchPlugin_Circle::CENTER_ID(), GeomDataAPI_Point2D::type());
29   data()->addAttribute(SketchPlugin_Circle::RADIUS_ID(), ModelAPI_AttributeDouble::type());
30   data()->addAttribute(EXTERNAL_ID(), ModelAPI_AttributeSelection::type());
31   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), EXTERNAL_ID());
32 }
33
34 void SketchPlugin_Circle::execute()
35 {
36   SketchPlugin_Sketch* aSketch = sketch();
37   if (aSketch) {
38     std::list<boost::shared_ptr<GeomAPI_Shape> > aShapes;
39
40     // compute a circle point in 3D view
41     boost::shared_ptr<GeomDataAPI_Point2D> aCenterAttr = boost::dynamic_pointer_cast<
42         GeomDataAPI_Point2D>(data()->attribute(SketchPlugin_Circle::CENTER_ID()));
43     AttributeDoublePtr aRadiusAttr = boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
44         data()->attribute(SketchPlugin_Circle::RADIUS_ID()));
45     if (aCenterAttr->isInitialized() && aRadiusAttr->isInitialized()) {
46       boost::shared_ptr<GeomAPI_Pnt> aCenter(aSketch->to3D(aCenterAttr->x(), aCenterAttr->y()));
47       // make a visible point
48       boost::shared_ptr<GeomAPI_Shape> aCenterPointShape = GeomAlgoAPI_PointBuilder::point(aCenter);
49       //aShapes.push_back(aCenterPointShape);
50       boost::shared_ptr<ModelAPI_ResultConstruction> aConstr1 = document()->createConstruction(
51           data(), 0);
52       aConstr1->setShape(aCenterPointShape);
53       aConstr1->setIsInHistory(false);
54       setResult(aConstr1, 0);
55
56       // make a visible circle
57       boost::shared_ptr<GeomDataAPI_Dir> aNDir = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(
58           aSketch->data()->attribute(SketchPlugin_Sketch::NORM_ID()));
59       bool aHasPlane = aNDir && !(aNDir->x() == 0 && aNDir->y() == 0 && aNDir->z() == 0);
60       if (aHasPlane) {
61         boost::shared_ptr<GeomAPI_Dir> aNormal(new GeomAPI_Dir(aNDir->x(), aNDir->y(), aNDir->z()));
62         // compute the circle radius
63         double aRadius = aRadiusAttr->value();
64
65         boost::shared_ptr<GeomAPI_Shape> aCircleShape = GeomAlgoAPI_EdgeBuilder::lineCircle(
66             aCenter, aNormal, aRadius);
67         aShapes.push_back(aCircleShape);
68         boost::shared_ptr<ModelAPI_ResultConstruction> aConstr2 = document()->createConstruction(
69             data(), 1);
70         aConstr2->setShape(aCircleShape);
71         aConstr2->setIsInHistory(false);
72         setResult(aConstr2, 1);
73       }
74     }
75     /*
76      boost::shared_ptr<GeomAPI_Shape> aCompound = GeomAlgoAPI_CompoundBuilder::compound(aShapes);
77      // store the result
78      boost::shared_ptr<ModelAPI_ResultConstruction> aConstr = 
79      document()->createConstruction(data());
80      aConstr->setShape(aCompound);
81      aConstr->setIsInHistory(false);
82      setResult(aConstr);
83      */
84   }
85 }
86
87 void SketchPlugin_Circle::move(double theDeltaX, double theDeltaY)
88 {
89   boost::shared_ptr<ModelAPI_Data> aData = data();
90   if (!aData->isValid())
91     return;
92
93   boost::shared_ptr<GeomDataAPI_Point2D> aPoint1 = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(
94       aData->attribute(SketchPlugin_Circle::CENTER_ID()));
95   aPoint1->setValue(aPoint1->x() + theDeltaX, aPoint1->y() + theDeltaY);
96 }
97
98 double SketchPlugin_Circle::distanceToPoint(const boost::shared_ptr<GeomAPI_Pnt2d>& thePoint)
99 {
100   boost::shared_ptr<ModelAPI_Data> aData = data();
101   boost::shared_ptr<GeomDataAPI_Point2D> aPoint = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(
102       aData->attribute(SketchPlugin_Circle::CENTER_ID()));
103
104   return aPoint->pnt()->distance(thePoint);
105 }
106
107 bool SketchPlugin_Circle::isFixed() {
108   return data()->selection(EXTERNAL_ID())->context();
109 }