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