]> SALOME platform Git repositories - modules/shaper.git/blob - src/SketchPlugin/SketchPlugin_Circle.cpp
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 <GeomDataAPI_Point2D.h>
9 #include <GeomAlgoAPI_PointBuilder.h>
10 #include <GeomAlgoAPI_EdgeBuilder.h>
11 #include <GeomAlgoAPI_CompoundBuilder.h>
12 #include <GeomDataAPI_Dir.h>
13
14 #include <ModelAPI_AttributeDouble.h>
15
16 SketchPlugin_Circle::SketchPlugin_Circle()
17   : SketchPlugin_Feature()
18 {
19 }
20
21 void SketchPlugin_Circle::initAttributes()
22 {
23   data()->addAttribute(CIRCLE_ATTR_CENTER, GeomDataAPI_Point2D::type());
24   data()->addAttribute(CIRCLE_ATTR_RADIUS, ModelAPI_AttributeDouble::type());
25 }
26
27 void SketchPlugin_Circle::execute()
28 {
29 }
30
31 const boost::shared_ptr<GeomAPI_Shape>& SketchPlugin_Circle::preview()
32 {
33   SketchPlugin_Sketch* aSketch = sketch();
34   if (aSketch) {
35     // compute a circle point in 3D view
36     boost::shared_ptr<GeomDataAPI_Point2D> aCenterAttr = 
37       boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(data()->attribute(CIRCLE_ATTR_CENTER));
38     boost::shared_ptr<GeomAPI_Pnt> aCenter(aSketch->to3D(aCenterAttr->x(), aCenterAttr->y()));
39
40     // compute the circle radius
41     boost::shared_ptr<ModelAPI_AttributeDouble> aRadiusAttr = 
42       boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(data()->attribute(CIRCLE_ATTR_RADIUS));
43     double aRadius = aRadiusAttr->value();
44
45     std::list<boost::shared_ptr<GeomAPI_Shape> > aShapes;
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
57       boost::shared_ptr<GeomAPI_Shape> aCircleShape = 
58                               GeomAlgoAPI_EdgeBuilder::lineCircle(aCenter, aNormal, aRadius);
59       aShapes.push_back(aCircleShape);
60
61       boost::shared_ptr<GeomAPI_Shape> aCompound = GeomAlgoAPI_CompoundBuilder::compound(aShapes);
62       setPreview(aCompound);
63     }
64   }
65   /// \todo Implement preview for the circle
66   return getPreview();
67 }