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