Salome HOME
Merge branch 'master' of newgeom:newgeom.git
[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 = boost::dynamic_pointer_cast<
37         GeomDataAPI_Point2D>(data()->attribute(SketchPlugin_Circle::CENTER_ID()));
38     AttributeDoublePtr aRadiusAttr = boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
39         data()->attribute(SketchPlugin_Circle::RADIUS_ID()));
40     if (aCenterAttr->isInitialized() && aRadiusAttr->isInitialized()) {
41       boost::shared_ptr<GeomAPI_Pnt> aCenter(aSketch->to3D(aCenterAttr->x(), aCenterAttr->y()));
42       // make a visible point
43       boost::shared_ptr<GeomAPI_Shape> aCenterPointShape = GeomAlgoAPI_PointBuilder::point(aCenter);
44       //aShapes.push_back(aCenterPointShape);
45       boost::shared_ptr<ModelAPI_ResultConstruction> aConstr1 = document()->createConstruction(
46           data(), 0);
47       aConstr1->setShape(aCenterPointShape);
48       aConstr1->setIsInHistory(false);
49       setResult(aConstr1, 0);
50
51       // make a visible circle
52       boost::shared_ptr<GeomDataAPI_Dir> aNDir = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(
53           aSketch->data()->attribute(SketchPlugin_Sketch::NORM_ID()));
54       bool aHasPlane = aNDir && !(aNDir->x() == 0 && aNDir->y() == 0 && aNDir->z() == 0);
55       if (aHasPlane) {
56         boost::shared_ptr<GeomAPI_Dir> aNormal(new GeomAPI_Dir(aNDir->x(), aNDir->y(), aNDir->z()));
57         // compute the circle radius
58         double aRadius = aRadiusAttr->value();
59
60         boost::shared_ptr<GeomAPI_Shape> aCircleShape = GeomAlgoAPI_EdgeBuilder::lineCircle(
61             aCenter, aNormal, aRadius);
62         aShapes.push_back(aCircleShape);
63         boost::shared_ptr<ModelAPI_ResultConstruction> aConstr2 = document()->createConstruction(
64             data(), 1);
65         aConstr2->setShape(aCircleShape);
66         aConstr2->setIsInHistory(false);
67         setResult(aConstr2, 1);
68       }
69     }
70     /*
71      boost::shared_ptr<GeomAPI_Shape> aCompound = GeomAlgoAPI_CompoundBuilder::compound(aShapes);
72      // store the result
73      boost::shared_ptr<ModelAPI_ResultConstruction> aConstr = 
74      document()->createConstruction(data());
75      aConstr->setShape(aCompound);
76      aConstr->setIsInHistory(false);
77      setResult(aConstr);
78      */
79   }
80 }
81
82 void SketchPlugin_Circle::move(double theDeltaX, double theDeltaY)
83 {
84   boost::shared_ptr<ModelAPI_Data> aData = data();
85   if (!aData->isValid())
86     return;
87
88   boost::shared_ptr<GeomDataAPI_Point2D> aPoint1 = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(
89       aData->attribute(SketchPlugin_Circle::CENTER_ID()));
90   aPoint1->setValue(aPoint1->x() + theDeltaX, aPoint1->y() + theDeltaY);
91 }
92
93 double SketchPlugin_Circle::distanceToPoint(const boost::shared_ptr<GeomAPI_Pnt2d>& thePoint)
94 {
95   boost::shared_ptr<ModelAPI_Data> aData = data();
96   boost::shared_ptr<GeomDataAPI_Point2D> aPoint = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(
97       aData->attribute(SketchPlugin_Circle::CENTER_ID()));
98
99   return aPoint->pnt()->distance(thePoint);
100 }