]> SALOME platform Git repositories - modules/shaper.git/blob - src/SketchPlugin/SketchPlugin_Circle.cpp
Salome HOME
35371ff9dea7dd90994a7f78f0d83393224532e1
[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
10 #include <GeomAPI_Pnt2d.h>
11 #include <GeomDataAPI_Point2D.h>
12 #include <GeomDataAPI_Dir.h>
13 #include <GeomAlgoAPI_PointBuilder.h>
14 #include <GeomAlgoAPI_EdgeBuilder.h>
15 #include <GeomAlgoAPI_CompoundBuilder.h>
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(SketchPlugin_Circle::CENTER_ID(), GeomDataAPI_Point2D::type());
26   data()->addAttribute(SketchPlugin_Circle::RADIUS_ID(), ModelAPI_AttributeDouble::type());
27 }
28
29 void SketchPlugin_Circle::execute()
30 {
31   SketchPlugin_Sketch* aSketch = sketch();
32   if (aSketch) {
33     std::list<boost::shared_ptr<GeomAPI_Shape> > aShapes;
34
35     // compute a circle point in 3D view
36     boost::shared_ptr<GeomDataAPI_Point2D> aCenterAttr = 
37       boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(
38       data()->attribute(SketchPlugin_Circle::CENTER_ID()));
39     AttributeDoublePtr aRadiusAttr = 
40       boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
41       data()->attribute(SketchPlugin_Circle::RADIUS_ID()));
42     if (aCenterAttr->isInitialized() && aRadiusAttr->isInitialized()) {
43       boost::shared_ptr<GeomAPI_Pnt> aCenter(aSketch->to3D(aCenterAttr->x(), aCenterAttr->y()));
44       // make a visible point
45       boost::shared_ptr<GeomAPI_Shape> aCenterPointShape = GeomAlgoAPI_PointBuilder::point(aCenter);
46       aShapes.push_back(aCenterPointShape);
47
48       // make a visible circle
49       boost::shared_ptr<GeomDataAPI_Dir> aNDir = 
50         boost::dynamic_pointer_cast<GeomDataAPI_Dir>(aSketch->data()->attribute(
51         SketchPlugin_Sketch::NORM_ID()));
52       bool aHasPlane = aNDir && !(aNDir->x() == 0 && aNDir->y() == 0 && aNDir->z() == 0);
53       if (aHasPlane) {
54         boost::shared_ptr<GeomAPI_Dir> aNormal(new GeomAPI_Dir(aNDir->x(), aNDir->y(), aNDir->z()));
55         // compute the circle 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     }
63     boost::shared_ptr<GeomAPI_Shape> aCompound = GeomAlgoAPI_CompoundBuilder::compound(aShapes);
64       // store the result
65       boost::shared_ptr<ModelAPI_ResultConstruction> aConstr = 
66         document()->createConstruction(data());
67       aConstr->setShape(aCompound);
68       aConstr->setIsInHistory(false);
69       setResult(aConstr);
70   }
71 }
72
73 void SketchPlugin_Circle::move(double theDeltaX, double theDeltaY)
74 {
75   boost::shared_ptr<ModelAPI_Data> aData = data();
76   if (!aData->isValid())
77     return;
78
79   boost::shared_ptr<GeomDataAPI_Point2D> aPoint1 =
80         boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(
81         aData->attribute(SketchPlugin_Circle::CENTER_ID()));
82   aPoint1->setValue(aPoint1->x() + theDeltaX, aPoint1->y() + theDeltaY);
83 }
84
85 double SketchPlugin_Circle::distanceToPoint(const boost::shared_ptr<GeomAPI_Pnt2d>& thePoint)
86 {
87   boost::shared_ptr<ModelAPI_Data> aData = data();
88   boost::shared_ptr<GeomDataAPI_Point2D> aPoint =
89         boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(
90         aData->attribute(SketchPlugin_Circle::CENTER_ID()));
91
92   return aPoint->pnt()->distance(thePoint);
93 }