Salome HOME
ec9d227deec81d97d63e91b2eec985cee2d73a7b
[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 #include <ModelAPI_AttributeSelection.h>
10 #include <ModelAPI_Validator.h>
11
12 #include <GeomAPI_Circ2d.h>
13 #include <GeomAPI_Pnt2d.h>
14 #include <GeomDataAPI_Point2D.h>
15 #include <GeomDataAPI_Dir.h>
16 #include <GeomAlgoAPI_PointBuilder.h>
17 #include <GeomAlgoAPI_EdgeBuilder.h>
18 #include <GeomAlgoAPI_CompoundBuilder.h>
19
20 const double tolerance = 1e-7;
21
22 SketchPlugin_Arc::SketchPlugin_Arc()
23     : SketchPlugin_Feature()
24 {
25 }
26
27 void SketchPlugin_Arc::initAttributes()
28 {
29   data()->addAttribute(CENTER_ID(), GeomDataAPI_Point2D::type());
30   data()->addAttribute(START_ID(), GeomDataAPI_Point2D::type());
31   data()->addAttribute(END_ID(), GeomDataAPI_Point2D::type());
32   data()->addAttribute(EXTERNAL_ID(), ModelAPI_AttributeSelection::type());
33   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), EXTERNAL_ID());
34 }
35
36 void SketchPlugin_Arc::execute()
37 {
38   SketchPlugin_Sketch* aSketch = sketch();
39   if (aSketch) {
40     std::list<boost::shared_ptr<GeomAPI_Shape> > aShapes;
41
42     // compute a circle point in 3D view
43     boost::shared_ptr<GeomDataAPI_Point2D> aCenterAttr = boost::dynamic_pointer_cast<
44         GeomDataAPI_Point2D>(data()->attribute(SketchPlugin_Arc::CENTER_ID()));
45     // compute the arc start point
46     boost::shared_ptr<GeomDataAPI_Point2D> aStartAttr = boost::dynamic_pointer_cast<
47         GeomDataAPI_Point2D>(data()->attribute(SketchPlugin_Arc::START_ID()));
48     if (aCenterAttr->isInitialized() && aStartAttr->isInitialized()) {
49       boost::shared_ptr<GeomAPI_Pnt> aCenter(aSketch->to3D(aCenterAttr->x(), aCenterAttr->y()));
50       // make a visible point
51       boost::shared_ptr<GeomAPI_Shape> aCenterPointShape = GeomAlgoAPI_PointBuilder::point(aCenter);
52       //aShapes.push_back(aCenterPointShape);
53       boost::shared_ptr<ModelAPI_ResultConstruction> aConstr1 = document()->createConstruction(
54           data(), 0);
55       aConstr1->setShape(aCenterPointShape);
56       aConstr1->setIsInHistory(false);
57       setResult(aConstr1, 0);
58
59       // make a visible circle
60       boost::shared_ptr<GeomDataAPI_Dir> aNDir = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(
61           aSketch->data()->attribute(SketchPlugin_Sketch::NORM_ID()));
62       bool aHasPlane = aNDir && !(aNDir->x() == 0 && aNDir->y() == 0 && aNDir->z() == 0);
63       if (aHasPlane) {
64         boost::shared_ptr<GeomAPI_Dir> aNormal = aNDir->dir();
65         boost::shared_ptr<GeomAPI_Pnt> aStartPoint(aSketch->to3D(aStartAttr->x(), aStartAttr->y()));
66
67         // compute and change the arc end point
68         boost::shared_ptr<GeomDataAPI_Point2D> anEndAttr = boost::dynamic_pointer_cast<
69             GeomDataAPI_Point2D>(data()->attribute(SketchPlugin_Arc::END_ID()));
70         if (anEndAttr->isInitialized()) {
71           boost::shared_ptr<GeomAPI_Circ2d> aCircleForArc(
72               new GeomAPI_Circ2d(aCenterAttr->pnt(), aStartAttr->pnt()));
73           boost::shared_ptr<GeomAPI_Pnt2d> aProjection = aCircleForArc->project(anEndAttr->pnt());
74           if (aProjection && anEndAttr->pnt()->distance(aProjection) > tolerance)
75             anEndAttr->setValue(aProjection);
76         }
77         boost::shared_ptr<GeomAPI_Pnt> aEndPoint(aSketch->to3D(anEndAttr->x(), anEndAttr->y()));
78
79         boost::shared_ptr<GeomAPI_Shape> aCircleShape = GeomAlgoAPI_EdgeBuilder::lineCircleArc(
80             aCenter, aStartPoint, aEndPoint, aNormal);
81         if (aCircleShape) {
82           boost::shared_ptr<ModelAPI_ResultConstruction> aConstr2 = document()->createConstruction(
83               data(), 1);
84           aConstr2->setShape(aCircleShape);
85           aConstr2->setIsInHistory(false);
86           setResult(aConstr2, 1);
87           //aShapes.push_back(aCircleShape);
88         }
89       }
90       /*
91        boost::shared_ptr<GeomAPI_Shape> aCompound = GeomAlgoAPI_CompoundBuilder::compound(aShapes);
92        // store the result
93        boost::shared_ptr<ModelAPI_ResultConstruction> aConstr = 
94        document()->createConstruction(data());
95        aConstr->setShape(aCompound);
96        aConstr->setIsInHistory(false);
97        setResult(aConstr);
98        */
99     }
100   }
101 }
102
103 void SketchPlugin_Arc::move(double theDeltaX, double theDeltaY)
104 {
105   boost::shared_ptr<ModelAPI_Data> aData = data();
106   if (!aData->isValid())
107     return;
108
109   boost::shared_ptr<GeomDataAPI_Point2D> aPoint1 = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(
110       aData->attribute(SketchPlugin_Arc::CENTER_ID()));
111   aPoint1->setValue(aPoint1->x() + theDeltaX, aPoint1->y() + theDeltaY);
112
113   boost::shared_ptr<GeomDataAPI_Point2D> aPoint2 = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(
114       aData->attribute(SketchPlugin_Arc::START_ID()));
115   aPoint2->setValue(aPoint2->x() + theDeltaX, aPoint2->y() + theDeltaY);
116
117   boost::shared_ptr<GeomDataAPI_Point2D> aPoint3 = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(
118       aData->attribute(SketchPlugin_Arc::END_ID()));
119   aPoint3->setValue(aPoint3->x() + theDeltaX, aPoint3->y() + theDeltaY);
120 }
121
122 double SketchPlugin_Arc::distanceToPoint(const boost::shared_ptr<GeomAPI_Pnt2d>& thePoint)
123 {
124   double aDelta = 0;
125   boost::shared_ptr<ModelAPI_Data> aData = data();
126
127   boost::shared_ptr<GeomDataAPI_Point2D> aPoint1 = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(
128       aData->attribute(SketchPlugin_Arc::CENTER_ID()));
129   aDelta = aPoint1->pnt()->distance(thePoint);
130
131   boost::shared_ptr<GeomDataAPI_Point2D> aPoint2 = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(
132       aData->attribute(SketchPlugin_Arc::START_ID()));
133   double aDistance = aPoint2->pnt()->distance(thePoint);
134   if (aDelta < aDistance)
135     aDelta = aDistance;
136
137   boost::shared_ptr<GeomDataAPI_Point2D> aPoint3 = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(
138       aData->attribute(SketchPlugin_Arc::END_ID()));
139   aDistance = aPoint3->pnt()->distance(thePoint);
140   if (aDelta < aDistance)
141     aDelta = aDistance;
142
143   return aDelta;
144 }
145
146 bool SketchPlugin_Arc::isFixed() {
147   return data()->selection(EXTERNAL_ID())->context();
148 }