Salome HOME
Merge remote-tracking branch 'remotes/origin/master' into SketchSolver
[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 <GeomDataAPI_Point2D.h>
10 #include <GeomDataAPI_Dir.h>
11
12 #include <GeomAlgoAPI_PointBuilder.h>
13 #include <GeomAlgoAPI_EdgeBuilder.h>
14 #include <GeomAlgoAPI_CompoundBuilder.h>
15
16 #include <ModelAPI_AttributeDouble.h>
17
18 SketchPlugin_Circle::SketchPlugin_Circle()
19   : SketchPlugin_Feature()
20 {
21 }
22
23 void SketchPlugin_Circle::initAttributes()
24 {
25   data()->addAttribute(CIRCLE_ATTR_CENTER, GeomDataAPI_Point2D::type());
26   data()->addAttribute(CIRCLE_ATTR_RADIUS, ModelAPI_AttributeDouble::type());
27 }
28
29 void SketchPlugin_Circle::execute()
30 {
31 }
32
33 const boost::shared_ptr<GeomAPI_Shape>& SketchPlugin_Circle::preview()
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 = 
41       boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(data()->attribute(CIRCLE_ATTR_CENTER));
42     boost::shared_ptr<GeomAPI_Pnt> aCenter(aSketch->to3D(aCenterAttr->x(), aCenterAttr->y()));
43     // make a visible point
44     boost::shared_ptr<GeomAPI_Shape> aCenterPointShape = GeomAlgoAPI_PointBuilder::point(aCenter);
45     aShapes.push_back(aCenterPointShape);
46
47     // make a visible circle
48     boost::shared_ptr<GeomDataAPI_Dir> aNDir = 
49       boost::dynamic_pointer_cast<GeomDataAPI_Dir>(aSketch->data()->attribute(SKETCH_ATTR_NORM));
50     bool aHasPlane = aNDir && !(aNDir->x() == 0 && aNDir->y() == 0 && aNDir->z() == 0);
51     if (aHasPlane) {
52       boost::shared_ptr<GeomAPI_Dir> aNormal(new GeomAPI_Dir(aNDir->x(), aNDir->y(), aNDir->z()));
53       // compute the circle radius
54       boost::shared_ptr<ModelAPI_AttributeDouble> aRadiusAttr = 
55         boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(data()->attribute(CIRCLE_ATTR_RADIUS));
56       double aRadius = aRadiusAttr->value();
57
58       boost::shared_ptr<GeomAPI_Shape> aCircleShape = 
59                               GeomAlgoAPI_EdgeBuilder::lineCircle(aCenter, aNormal, aRadius);
60       aShapes.push_back(aCircleShape);
61     }
62     boost::shared_ptr<GeomAPI_Shape> aCompound = GeomAlgoAPI_CompoundBuilder::compound(aShapes);
63     setPreview(aCompound);
64   }
65   return getPreview();
66 }