Salome HOME
refs #80 - Sketch base GUI: create/draw point, circle and arc
[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
9 #include <GeomAPI_Pnt2d.h>
10
11 #include <GeomDataAPI_Point2D.h>
12 #include <GeomDataAPI_Dir.h>
13
14 #include <GeomAlgoAPI_PointBuilder.h>
15 #include <GeomAlgoAPI_EdgeBuilder.h>
16 #include <GeomAlgoAPI_CompoundBuilder.h>
17
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(CIRCLE_ATTR_CENTER, GeomDataAPI_Point2D::type());
28   data()->addAttribute(CIRCLE_ATTR_RADIUS, ModelAPI_AttributeDouble::type());
29 }
30
31 void SketchPlugin_Circle::execute()
32 {
33 }
34
35 const boost::shared_ptr<GeomAPI_Shape>& SketchPlugin_Circle::preview()
36 {
37   SketchPlugin_Sketch* aSketch = sketch();
38   if (aSketch) {
39     std::list<boost::shared_ptr<GeomAPI_Shape> > aShapes;
40
41     // compute a circle point in 3D view
42     boost::shared_ptr<GeomDataAPI_Point2D> aCenterAttr = 
43       boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(data()->attribute(CIRCLE_ATTR_CENTER));
44     if (aCenterAttr->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
50       // make a visible circle
51       boost::shared_ptr<GeomDataAPI_Dir> aNDir = 
52         boost::dynamic_pointer_cast<GeomDataAPI_Dir>(aSketch->data()->attribute(SKETCH_ATTR_NORM));
53       bool aHasPlane = aNDir && !(aNDir->x() == 0 && aNDir->y() == 0 && aNDir->z() == 0);
54       if (aHasPlane) {
55         boost::shared_ptr<GeomAPI_Dir> aNormal(new GeomAPI_Dir(aNDir->x(), aNDir->y(), aNDir->z()));
56         // compute the circle radius
57         AttributeDoublePtr aRadiusAttr = 
58           boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(data()->attribute(CIRCLE_ATTR_RADIUS));
59         double aRadius = aRadiusAttr->value();
60
61         boost::shared_ptr<GeomAPI_Shape> aCircleShape = 
62                                 GeomAlgoAPI_EdgeBuilder::lineCircle(aCenter, aNormal, aRadius);
63         aShapes.push_back(aCircleShape);
64       }
65     }
66     boost::shared_ptr<GeomAPI_Shape> aCompound = GeomAlgoAPI_CompoundBuilder::compound(aShapes);
67     setPreview(aCompound);
68   }
69   return getPreview();
70 }
71
72 void SketchPlugin_Circle::move(double theDeltaX, double theDeltaY)
73 {
74   boost::shared_ptr<ModelAPI_Data> aData = data();
75   if (!aData->isValid())
76     return;
77
78   boost::shared_ptr<GeomDataAPI_Point2D> aPoint1 =
79         boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(CIRCLE_ATTR_CENTER));
80   aPoint1->setValue(aPoint1->x() + theDeltaX, aPoint1->y() + theDeltaY);
81 }
82
83 double SketchPlugin_Circle::distanceToPoint(const boost::shared_ptr<GeomAPI_Pnt2d>& thePoint)
84 {
85   boost::shared_ptr<ModelAPI_Data> aData = data();
86   boost::shared_ptr<GeomDataAPI_Point2D> aPoint =
87         boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(CIRCLE_ATTR_CENTER));
88
89   return aPoint->pnt()->distance(thePoint);
90 }