Salome HOME
Merge branch 'master' of newgeom:newgeom.git
[modules/shaper.git] / src / SketchPlugin / SketchPlugin_Arc.cpp
1 // File:        SketchPlugin_Arc.cpp
2 // Created:     26 Apr 2014
3 // Author:      Artem ZHIDKOV
4
5 #include "SketchPlugin_Arc.h"
6 #include "SketchPlugin_Sketch.h"
7 #include <ModelAPI_Data.h>
8 #include <ModelAPI_ResultConstruction.h>
9
10 #include <GeomAPI_Circ2d.h>
11 #include <GeomAPI_Pnt2d.h>
12 #include <GeomDataAPI_Point2D.h>
13 #include <GeomDataAPI_Dir.h>
14 #include <GeomAlgoAPI_PointBuilder.h>
15 #include <GeomAlgoAPI_EdgeBuilder.h>
16 #include <GeomAlgoAPI_CompoundBuilder.h>
17
18 const double tolerance = 1e-7;
19
20 SketchPlugin_Arc::SketchPlugin_Arc()
21   : SketchPlugin_Feature()
22 {
23 }
24
25 void SketchPlugin_Arc::initAttributes()
26 {
27   data()->addAttribute(SketchPlugin_Arc::CENTER_ID(), GeomDataAPI_Point2D::type());
28   data()->addAttribute(SketchPlugin_Arc::START_ID(),  GeomDataAPI_Point2D::type());
29   data()->addAttribute(SketchPlugin_Arc::END_ID(),    GeomDataAPI_Point2D::type());
30 }
31
32 void SketchPlugin_Arc::execute() 
33 {
34   SketchPlugin_Sketch* aSketch = sketch();
35   if (aSketch) {
36     std::list<boost::shared_ptr<GeomAPI_Shape> > aShapes;
37
38     // compute a circle point in 3D view
39     boost::shared_ptr<GeomDataAPI_Point2D> aCenterAttr = 
40       boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(data()->attribute(SketchPlugin_Arc::CENTER_ID()));
41       // compute the arc start point
42       boost::shared_ptr<GeomDataAPI_Point2D> aStartAttr = 
43         boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(data()->attribute(SketchPlugin_Arc::START_ID()));
44       if (aCenterAttr->isInitialized() && aStartAttr->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(SketchPlugin_Sketch::NORM_ID()));
53       bool aHasPlane = aNDir && !(aNDir->x() == 0 && aNDir->y() == 0 && aNDir->z() == 0);
54       if (aHasPlane) {
55         boost::shared_ptr<GeomAPI_Dir> aNormal = aNDir->dir();
56         boost::shared_ptr<GeomAPI_Pnt> aStartPoint(aSketch->to3D(aStartAttr->x(), aStartAttr->y()));
57
58         // compute and change the arc end point
59         boost::shared_ptr<GeomDataAPI_Point2D> anEndAttr = 
60           boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(data()->attribute(SketchPlugin_Arc::END_ID()));
61         if (anEndAttr->isInitialized())
62         {
63           boost::shared_ptr<GeomAPI_Circ2d> aCircleForArc(
64             new GeomAPI_Circ2d(aCenterAttr->pnt(), aStartAttr->pnt()));
65           boost::shared_ptr<GeomAPI_Pnt2d> aProjection = aCircleForArc->project(anEndAttr->pnt());
66           if (aProjection && anEndAttr->pnt()->distance(aProjection) > tolerance)
67             anEndAttr->setValue(aProjection);
68         }
69         boost::shared_ptr<GeomAPI_Pnt> aEndPoint(aSketch->to3D(anEndAttr->x(), anEndAttr->y()));
70
71         boost::shared_ptr<GeomAPI_Shape> aCircleShape = 
72                    GeomAlgoAPI_EdgeBuilder::lineCircleArc(aCenter, aStartPoint, aEndPoint, aNormal);
73         if (aCircleShape)
74           aShapes.push_back(aCircleShape);
75       }
76       boost::shared_ptr<GeomAPI_Shape> aCompound = GeomAlgoAPI_CompoundBuilder::compound(aShapes);
77       // store the result
78       boost::shared_ptr<ModelAPI_ResultConstruction> aConstr = 
79         document()->createConstruction(data());
80       aConstr->setShape(aCompound);
81       results().push_back(aConstr);
82     }
83   }
84 }
85
86 void SketchPlugin_Arc::move(double theDeltaX, double theDeltaY)
87 {
88   boost::shared_ptr<ModelAPI_Data> aData = data();
89   if (!aData->isValid())
90     return;
91
92   boost::shared_ptr<GeomDataAPI_Point2D> aPoint1 =
93         boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Arc::CENTER_ID()));
94   aPoint1->setValue(aPoint1->x() + theDeltaX, aPoint1->y() + theDeltaY);
95
96   boost::shared_ptr<GeomDataAPI_Point2D> aPoint2 =
97         boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Arc::START_ID()));
98   aPoint2->setValue(aPoint2->x() + theDeltaX, aPoint2->y() + theDeltaY);
99
100   boost::shared_ptr<GeomDataAPI_Point2D> aPoint3 =
101         boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Arc::END_ID()));
102   aPoint3->setValue(aPoint3->x() + theDeltaX, aPoint3->y() + theDeltaY);
103 }
104
105 double SketchPlugin_Arc::distanceToPoint(const boost::shared_ptr<GeomAPI_Pnt2d>& thePoint)
106 {
107   double aDelta = 0;
108   boost::shared_ptr<ModelAPI_Data> aData = data();
109
110   boost::shared_ptr<GeomDataAPI_Point2D> aPoint1 =
111         boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Arc::CENTER_ID()));
112   aDelta = aPoint1->pnt()->distance(thePoint);
113
114   boost::shared_ptr<GeomDataAPI_Point2D> aPoint2 =
115         boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Arc::START_ID()));
116   double aDistance = aPoint2->pnt()->distance(thePoint);
117   if (aDelta < aDistance)
118     aDelta = aDistance;
119
120   boost::shared_ptr<GeomDataAPI_Point2D> aPoint3 =
121         boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Arc::END_ID()));
122   aDistance = aPoint3->pnt()->distance(thePoint);
123   if (aDelta < aDistance)
124     aDelta = aDistance;
125
126   return aDelta;
127 }