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 #include <ModelAPI_AttributeSelection.h>
10 #include <ModelAPI_Validator.h>
11 #include <ModelAPI_Session.h>
12
13 #include <GeomAPI_Circ2d.h>
14 #include <GeomAPI_Pnt2d.h>
15 #include <GeomDataAPI_Point2D.h>
16 #include <GeomDataAPI_Dir.h>
17 #include <GeomAlgoAPI_PointBuilder.h>
18 #include <GeomAlgoAPI_EdgeBuilder.h>
19 #include <GeomAlgoAPI_CompoundBuilder.h>
20
21 const double tolerance = 1e-7;
22
23 SketchPlugin_Arc::SketchPlugin_Arc()
24     : SketchPlugin_Feature()
25 {
26 }
27
28 void SketchPlugin_Arc::initAttributes()
29 {
30   data()->addAttribute(CENTER_ID(), GeomDataAPI_Point2D::type());
31   data()->addAttribute(START_ID(), GeomDataAPI_Point2D::type());
32   data()->addAttribute(END_ID(), GeomDataAPI_Point2D::type());
33   data()->addAttribute(EXTERNAL_ID(), ModelAPI_AttributeSelection::type());
34   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), EXTERNAL_ID());
35 }
36
37 void SketchPlugin_Arc::execute()
38 {
39   SketchPlugin_Sketch* aSketch = sketch();
40   // result for the arc is set only when all obligatory attributes are initialized,
41   // otherwise AIS object is used to visualize the arc's preview
42   if (aSketch && isFeatureValid()) {
43     // compute a circle point in 3D view
44     boost::shared_ptr<GeomDataAPI_Point2D> aCenterAttr = boost::dynamic_pointer_cast<
45         GeomDataAPI_Point2D>(data()->attribute(SketchPlugin_Arc::CENTER_ID()));
46     // compute the arc start point
47     boost::shared_ptr<GeomDataAPI_Point2D> aStartAttr = boost::dynamic_pointer_cast<
48         GeomDataAPI_Point2D>(data()->attribute(SketchPlugin_Arc::START_ID()));
49
50     boost::shared_ptr<GeomAPI_Pnt> aCenter(aSketch->to3D(aCenterAttr->x(), aCenterAttr->y()));
51     // make a visible point
52     boost::shared_ptr<GeomAPI_Shape> aCenterPointShape = GeomAlgoAPI_PointBuilder::point(aCenter);
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       boost::shared_ptr<GeomAPI_Circ2d> aCircleForArc(
71           new GeomAPI_Circ2d(aCenterAttr->pnt(), aStartAttr->pnt()));
72       boost::shared_ptr<GeomAPI_Pnt2d> aProjection = aCircleForArc->project(anEndAttr->pnt());
73       if (aProjection && anEndAttr->pnt()->distance(aProjection) > tolerance)
74         anEndAttr->setValue(aProjection);
75       boost::shared_ptr<GeomAPI_Pnt> aEndPoint(aSketch->to3D(anEndAttr->x(), anEndAttr->y()));
76
77       boost::shared_ptr<GeomAPI_Shape> aCircleShape = GeomAlgoAPI_EdgeBuilder::lineCircleArc(
78           aCenter, aStartPoint, aEndPoint, aNormal);
79       if (aCircleShape) {
80         boost::shared_ptr<ModelAPI_ResultConstruction> aConstr2 = document()->createConstruction(
81             data(), 1);
82         aConstr2->setShape(aCircleShape);
83         aConstr2->setIsInHistory(false);
84         setResult(aConstr2, 1);
85       }
86     }
87   }
88 }
89
90 AISObjectPtr SketchPlugin_Arc::getAISObject(AISObjectPtr thePrevious)
91 {
92   SketchPlugin_Sketch* aSketch = sketch();
93   if (aSketch) {
94     // if the feature is valid, the execute() method should be performed, AIS object is empty
95     if (!isFeatureValid()) {
96       std::list<boost::shared_ptr<GeomAPI_Shape> > aShapes;
97
98       // compute a circle point in 3D view
99       boost::shared_ptr<GeomDataAPI_Point2D> aCenterAttr = boost::dynamic_pointer_cast<
100           GeomDataAPI_Point2D>(data()->attribute(SketchPlugin_Arc::CENTER_ID()));
101       if (aCenterAttr->isInitialized()) {
102         boost::shared_ptr<GeomAPI_Pnt> aCenter(aSketch->to3D(aCenterAttr->x(), aCenterAttr->y()));
103         // make a visible point
104         boost::shared_ptr<GeomAPI_Shape> aCenterPointShape = GeomAlgoAPI_PointBuilder::point(aCenter);
105         aShapes.push_back(aCenterPointShape);
106
107         boost::shared_ptr<GeomDataAPI_Point2D> aStartAttr = boost::dynamic_pointer_cast<
108             GeomDataAPI_Point2D>(data()->attribute(SketchPlugin_Arc::START_ID()));
109         if (aStartAttr->isInitialized()) {
110           // make a visible circle
111           boost::shared_ptr<GeomDataAPI_Dir> aNDir = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(
112               aSketch->data()->attribute(SketchPlugin_Sketch::NORM_ID()));
113           bool aHasPlane = aNDir && !(aNDir->x() == 0 && aNDir->y() == 0 && aNDir->z() == 0);
114           if (aHasPlane) {
115             boost::shared_ptr<GeomAPI_Dir> aNormal = aNDir->dir();
116             boost::shared_ptr<GeomAPI_Pnt> aStartPoint(aSketch->to3D(aStartAttr->x(), aStartAttr->y()));
117             boost::shared_ptr<GeomAPI_Shape> aCircleShape = GeomAlgoAPI_EdgeBuilder::lineCircleArc(
118                                                             aCenter, aStartPoint, aStartPoint, aNormal);
119             if (aCircleShape) {
120               aShapes.push_back(aCircleShape);
121             }
122           }
123         }
124       }
125       boost::shared_ptr<GeomAPI_Shape> aCompound = GeomAlgoAPI_CompoundBuilder::compound(aShapes);
126       AISObjectPtr anAIS = thePrevious;
127       if (!anAIS)
128         anAIS = AISObjectPtr(new GeomAPI_AISObject);
129       anAIS->createShape(aCompound);
130       return anAIS;
131     }
132   }
133   return AISObjectPtr();
134 }
135
136 void SketchPlugin_Arc::move(double theDeltaX, double theDeltaY)
137 {
138   boost::shared_ptr<ModelAPI_Data> aData = data();
139   if (!aData->isValid())
140     return;
141
142   boost::shared_ptr<GeomDataAPI_Point2D> aPoint1 = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(
143       aData->attribute(SketchPlugin_Arc::CENTER_ID()));
144   aPoint1->move(theDeltaX, theDeltaY);
145
146   boost::shared_ptr<GeomDataAPI_Point2D> aPoint2 = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(
147       aData->attribute(SketchPlugin_Arc::START_ID()));
148   aPoint2->move(theDeltaX, theDeltaY);
149
150   boost::shared_ptr<GeomDataAPI_Point2D> aPoint3 = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(
151       aData->attribute(SketchPlugin_Arc::END_ID()));
152   aPoint3->move(theDeltaX, theDeltaY);
153 }
154
155 double SketchPlugin_Arc::distanceToPoint(const boost::shared_ptr<GeomAPI_Pnt2d>& thePoint)
156 {
157   double aDelta = 0;
158   boost::shared_ptr<ModelAPI_Data> aData = data();
159
160   boost::shared_ptr<GeomDataAPI_Point2D> aPoint1 = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(
161       aData->attribute(SketchPlugin_Arc::CENTER_ID()));
162   aDelta = aPoint1->pnt()->distance(thePoint);
163
164   boost::shared_ptr<GeomDataAPI_Point2D> aPoint2 = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(
165       aData->attribute(SketchPlugin_Arc::START_ID()));
166   double aDistance = aPoint2->pnt()->distance(thePoint);
167   if (aDelta < aDistance)
168     aDelta = aDistance;
169
170   boost::shared_ptr<GeomDataAPI_Point2D> aPoint3 = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(
171       aData->attribute(SketchPlugin_Arc::END_ID()));
172   aDistance = aPoint3->pnt()->distance(thePoint);
173   if (aDelta < aDistance)
174     aDelta = aDistance;
175
176   return aDelta;
177 }
178
179 bool SketchPlugin_Arc::isFixed() {
180   return data()->selection(EXTERNAL_ID())->context();
181 }
182
183 bool SketchPlugin_Arc::isFeatureValid()
184 {
185   boost::shared_ptr<GeomDataAPI_Point2D> aCenterAttr = boost::dynamic_pointer_cast<
186       GeomDataAPI_Point2D>(data()->attribute(SketchPlugin_Arc::CENTER_ID()));
187   boost::shared_ptr<GeomDataAPI_Point2D> aStartAttr = boost::dynamic_pointer_cast<
188       GeomDataAPI_Point2D>(data()->attribute(SketchPlugin_Arc::START_ID()));
189   boost::shared_ptr<GeomDataAPI_Point2D> anEndAttr = boost::dynamic_pointer_cast<
190       GeomDataAPI_Point2D>(data()->attribute(SketchPlugin_Arc::END_ID()));
191
192   return aCenterAttr->isInitialized() && aStartAttr->isInitialized() && anEndAttr->isInitialized();
193 }