Salome HOME
7d25c48c3d10017323e923319605d9852c708817
[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       // compute a circle point in 3D view
97       boost::shared_ptr<GeomDataAPI_Point2D> aCenterAttr = boost::dynamic_pointer_cast<
98           GeomDataAPI_Point2D>(data()->attribute(SketchPlugin_Arc::CENTER_ID()));
99       if (aCenterAttr->isInitialized()) {
100         boost::shared_ptr<GeomAPI_Pnt> aCenter(aSketch->to3D(aCenterAttr->x(), aCenterAttr->y()));
101
102         boost::shared_ptr<GeomDataAPI_Point2D> aStartAttr = boost::dynamic_pointer_cast<
103             GeomDataAPI_Point2D>(data()->attribute(SketchPlugin_Arc::START_ID()));
104         if (aStartAttr->isInitialized()) {
105           // make a visible circle
106           boost::shared_ptr<GeomDataAPI_Dir> aNDir = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(
107               aSketch->data()->attribute(SketchPlugin_Sketch::NORM_ID()));
108           bool aHasPlane = aNDir && !(aNDir->x() == 0 && aNDir->y() == 0 && aNDir->z() == 0);
109           if (aHasPlane) {
110             boost::shared_ptr<GeomAPI_Dir> aNormal = aNDir->dir();
111             boost::shared_ptr<GeomAPI_Pnt> aStartPoint(aSketch->to3D(aStartAttr->x(), aStartAttr->y()));
112             boost::shared_ptr<GeomAPI_Shape> aCircleShape = GeomAlgoAPI_EdgeBuilder::lineCircleArc(
113                                                             aCenter, aStartPoint, aStartPoint, aNormal);
114             if (aCircleShape) {
115               std::list<boost::shared_ptr<GeomAPI_Shape> > aShapes;
116               // make a visible point
117               boost::shared_ptr<GeomAPI_Shape> aCenterPointShape = GeomAlgoAPI_PointBuilder::point(aCenter);
118               aShapes.push_back(aCenterPointShape);
119
120               aShapes.push_back(aCircleShape);
121               if (!aShapes.empty())
122               {
123                 boost::shared_ptr<GeomAPI_Shape> aCompound = GeomAlgoAPI_CompoundBuilder::compound(aShapes);
124                 AISObjectPtr anAIS = thePrevious;
125                 if (!anAIS)
126                   anAIS = AISObjectPtr(new GeomAPI_AISObject);
127                 anAIS->createShape(aCompound);
128                 return anAIS;
129               }
130             }
131           }
132         }
133       }
134     }
135   }
136   return AISObjectPtr();
137 }
138
139 void SketchPlugin_Arc::move(double theDeltaX, double theDeltaY)
140 {
141   boost::shared_ptr<ModelAPI_Data> aData = data();
142   if (!aData->isValid())
143     return;
144
145   boost::shared_ptr<GeomDataAPI_Point2D> aPoint1 = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(
146       aData->attribute(SketchPlugin_Arc::CENTER_ID()));
147   aPoint1->move(theDeltaX, theDeltaY);
148
149   boost::shared_ptr<GeomDataAPI_Point2D> aPoint2 = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(
150       aData->attribute(SketchPlugin_Arc::START_ID()));
151   aPoint2->move(theDeltaX, theDeltaY);
152
153   boost::shared_ptr<GeomDataAPI_Point2D> aPoint3 = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(
154       aData->attribute(SketchPlugin_Arc::END_ID()));
155   aPoint3->move(theDeltaX, theDeltaY);
156 }
157
158 double SketchPlugin_Arc::distanceToPoint(const boost::shared_ptr<GeomAPI_Pnt2d>& thePoint)
159 {
160   double aDelta = 0;
161   boost::shared_ptr<ModelAPI_Data> aData = data();
162
163   boost::shared_ptr<GeomDataAPI_Point2D> aPoint1 = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(
164       aData->attribute(SketchPlugin_Arc::CENTER_ID()));
165   aDelta = aPoint1->pnt()->distance(thePoint);
166
167   boost::shared_ptr<GeomDataAPI_Point2D> aPoint2 = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(
168       aData->attribute(SketchPlugin_Arc::START_ID()));
169   double aDistance = aPoint2->pnt()->distance(thePoint);
170   if (aDelta < aDistance)
171     aDelta = aDistance;
172
173   boost::shared_ptr<GeomDataAPI_Point2D> aPoint3 = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(
174       aData->attribute(SketchPlugin_Arc::END_ID()));
175   aDistance = aPoint3->pnt()->distance(thePoint);
176   if (aDelta < aDistance)
177     aDelta = aDistance;
178
179   return aDelta;
180 }
181
182 bool SketchPlugin_Arc::isFixed() {
183   return data()->selection(EXTERNAL_ID())->context();
184 }
185
186 bool SketchPlugin_Arc::isFeatureValid()
187 {
188   boost::shared_ptr<GeomDataAPI_Point2D> aCenterAttr = boost::dynamic_pointer_cast<
189       GeomDataAPI_Point2D>(data()->attribute(SketchPlugin_Arc::CENTER_ID()));
190   boost::shared_ptr<GeomDataAPI_Point2D> aStartAttr = boost::dynamic_pointer_cast<
191       GeomDataAPI_Point2D>(data()->attribute(SketchPlugin_Arc::START_ID()));
192   boost::shared_ptr<GeomDataAPI_Point2D> anEndAttr = boost::dynamic_pointer_cast<
193       GeomDataAPI_Point2D>(data()->attribute(SketchPlugin_Arc::END_ID()));
194
195   return aCenterAttr->isInitialized() && aStartAttr->isInitialized() && anEndAttr->isInitialized();
196 }