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