Salome HOME
Sources formated according to the codeing standards
[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 = boost::dynamic_pointer_cast<
40         GeomDataAPI_Point2D>(data()->attribute(SketchPlugin_Arc::CENTER_ID()));
41     // compute the arc start point
42     boost::shared_ptr<GeomDataAPI_Point2D> aStartAttr = boost::dynamic_pointer_cast<
43         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       boost::shared_ptr<ModelAPI_ResultConstruction> aConstr1 = document()->createConstruction(
50           data(), 0);
51       aConstr1->setShape(aCenterPointShape);
52       aConstr1->setIsInHistory(false);
53       setResult(aConstr1, 0);
54
55       // make a visible circle
56       boost::shared_ptr<GeomDataAPI_Dir> aNDir = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(
57           aSketch->data()->attribute(SketchPlugin_Sketch::NORM_ID()));
58       bool aHasPlane = aNDir && !(aNDir->x() == 0 && aNDir->y() == 0 && aNDir->z() == 0);
59       if (aHasPlane) {
60         boost::shared_ptr<GeomAPI_Dir> aNormal = aNDir->dir();
61         boost::shared_ptr<GeomAPI_Pnt> aStartPoint(aSketch->to3D(aStartAttr->x(), aStartAttr->y()));
62
63         // compute and change the arc end point
64         boost::shared_ptr<GeomDataAPI_Point2D> anEndAttr = boost::dynamic_pointer_cast<
65             GeomDataAPI_Point2D>(data()->attribute(SketchPlugin_Arc::END_ID()));
66         if (anEndAttr->isInitialized()) {
67           boost::shared_ptr<GeomAPI_Circ2d> aCircleForArc(
68               new GeomAPI_Circ2d(aCenterAttr->pnt(), aStartAttr->pnt()));
69           boost::shared_ptr<GeomAPI_Pnt2d> aProjection = aCircleForArc->project(anEndAttr->pnt());
70           if (aProjection && anEndAttr->pnt()->distance(aProjection) > tolerance)
71             anEndAttr->setValue(aProjection);
72         }
73         boost::shared_ptr<GeomAPI_Pnt> aEndPoint(aSketch->to3D(anEndAttr->x(), anEndAttr->y()));
74
75         boost::shared_ptr<GeomAPI_Shape> aCircleShape = GeomAlgoAPI_EdgeBuilder::lineCircleArc(
76             aCenter, aStartPoint, aEndPoint, aNormal);
77         if (aCircleShape) {
78           boost::shared_ptr<ModelAPI_ResultConstruction> aConstr2 = document()->createConstruction(
79               data(), 1);
80           aConstr2->setShape(aCircleShape);
81           aConstr2->setIsInHistory(false);
82           setResult(aConstr2, 1);
83           //aShapes.push_back(aCircleShape);
84         }
85       }
86       /*
87        boost::shared_ptr<GeomAPI_Shape> aCompound = GeomAlgoAPI_CompoundBuilder::compound(aShapes);
88        // store the result
89        boost::shared_ptr<ModelAPI_ResultConstruction> aConstr = 
90        document()->createConstruction(data());
91        aConstr->setShape(aCompound);
92        aConstr->setIsInHistory(false);
93        setResult(aConstr);
94        */
95     }
96   }
97 }
98
99 void SketchPlugin_Arc::move(double theDeltaX, double theDeltaY)
100 {
101   boost::shared_ptr<ModelAPI_Data> aData = data();
102   if (!aData->isValid())
103     return;
104
105   boost::shared_ptr<GeomDataAPI_Point2D> aPoint1 = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(
106       aData->attribute(SketchPlugin_Arc::CENTER_ID()));
107   aPoint1->setValue(aPoint1->x() + theDeltaX, aPoint1->y() + theDeltaY);
108
109   boost::shared_ptr<GeomDataAPI_Point2D> aPoint2 = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(
110       aData->attribute(SketchPlugin_Arc::START_ID()));
111   aPoint2->setValue(aPoint2->x() + theDeltaX, aPoint2->y() + theDeltaY);
112
113   boost::shared_ptr<GeomDataAPI_Point2D> aPoint3 = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(
114       aData->attribute(SketchPlugin_Arc::END_ID()));
115   aPoint3->setValue(aPoint3->x() + theDeltaX, aPoint3->y() + theDeltaY);
116 }
117
118 double SketchPlugin_Arc::distanceToPoint(const boost::shared_ptr<GeomAPI_Pnt2d>& thePoint)
119 {
120   double aDelta = 0;
121   boost::shared_ptr<ModelAPI_Data> aData = data();
122
123   boost::shared_ptr<GeomDataAPI_Point2D> aPoint1 = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(
124       aData->attribute(SketchPlugin_Arc::CENTER_ID()));
125   aDelta = aPoint1->pnt()->distance(thePoint);
126
127   boost::shared_ptr<GeomDataAPI_Point2D> aPoint2 = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(
128       aData->attribute(SketchPlugin_Arc::START_ID()));
129   double aDistance = aPoint2->pnt()->distance(thePoint);
130   if (aDelta < aDistance)
131     aDelta = aDistance;
132
133   boost::shared_ptr<GeomDataAPI_Point2D> aPoint3 = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(
134       aData->attribute(SketchPlugin_Arc::END_ID()));
135   aDistance = aPoint3->pnt()->distance(thePoint);
136   if (aDelta < aDistance)
137     aDelta = aDistance;
138
139   return aDelta;
140 }