]> SALOME platform Git repositories - modules/shaper.git/blob - src/SketchPlugin/SketchPlugin_Arc.cpp
Salome HOME
Merge branch 'master' of newgeom:newgeom
[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
9 #include <GeomAPI_Pnt2d.h>
10
11 #include <GeomDataAPI_Point2D.h>
12 #include <GeomDataAPI_Dir.h>
13
14 #include <GeomAlgoAPI_PointBuilder.h>
15 #include <GeomAlgoAPI_EdgeBuilder.h>
16 #include <GeomAlgoAPI_CompoundBuilder.h>
17
18 SketchPlugin_Arc::SketchPlugin_Arc()
19   : SketchPlugin_Feature()
20 {
21 }
22
23 void SketchPlugin_Arc::initAttributes()
24 {
25   data()->addAttribute(ARC_ATTR_CENTER, GeomDataAPI_Point2D::type());
26   data()->addAttribute(ARC_ATTR_START,  GeomDataAPI_Point2D::type());
27   data()->addAttribute(ARC_ATTR_END,    GeomDataAPI_Point2D::type());
28 }
29
30 void SketchPlugin_Arc::execute() 
31 {
32 }
33
34 const boost::shared_ptr<GeomAPI_Shape>& SketchPlugin_Arc::preview()
35 {
36   SketchPlugin_Sketch* aSketch = sketch();
37   if (aSketch) {
38     std::list<boost::shared_ptr<GeomAPI_Shape> > aShapes;
39
40     // compute a circle point in 3D view
41     boost::shared_ptr<GeomDataAPI_Point2D> aCenterAttr = 
42       boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(data()->attribute(ARC_ATTR_CENTER));
43     boost::shared_ptr<GeomAPI_Pnt> aCenter(aSketch->to3D(aCenterAttr->x(), aCenterAttr->y()));
44     // make a visible point
45     boost::shared_ptr<GeomAPI_Shape> aCenterPointShape = GeomAlgoAPI_PointBuilder::point(aCenter);
46     aShapes.push_back(aCenterPointShape);
47
48     // make a visible circle
49     boost::shared_ptr<GeomDataAPI_Dir> aNDir = 
50       boost::dynamic_pointer_cast<GeomDataAPI_Dir>(aSketch->data()->attribute(SKETCH_ATTR_NORM));
51     bool aHasPlane = aNDir && !(aNDir->x() == 0 && aNDir->y() == 0 && aNDir->z() == 0);
52     if (aHasPlane) {
53       boost::shared_ptr<GeomAPI_Dir> aNormal(new GeomAPI_Dir(aNDir->x(), aNDir->y(), aNDir->z()));
54       // compute the arc start point
55       boost::shared_ptr<GeomDataAPI_Point2D> aStartAttr = 
56         boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(data()->attribute(ARC_ATTR_START));
57       boost::shared_ptr<GeomAPI_Pnt> aStartPoint(aSketch->to3D(aStartAttr->x(), aStartAttr->y()));
58
59       // compute the arc end point
60       boost::shared_ptr<GeomDataAPI_Point2D> anEndAttr = 
61         boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(data()->attribute(ARC_ATTR_END));
62       boost::shared_ptr<GeomAPI_Pnt> aEndPoint(aSketch->to3D(anEndAttr->x(), anEndAttr->y()));
63
64       boost::shared_ptr<GeomAPI_Shape> aCircleShape = 
65                  GeomAlgoAPI_EdgeBuilder::lineCircleArc(aCenter, aStartPoint, aEndPoint, aNormal);
66       if (aCircleShape)
67         aShapes.push_back(aCircleShape);
68     }
69     boost::shared_ptr<GeomAPI_Shape> aCompound = GeomAlgoAPI_CompoundBuilder::compound(aShapes);
70     setPreview(aCompound);
71   }
72   return getPreview();
73 }
74
75 void SketchPlugin_Arc::move(double theDeltaX, double theDeltaY)
76 {
77   boost::shared_ptr<ModelAPI_Data> aData = data();
78   if (!aData->isValid())
79     return;
80
81   boost::shared_ptr<GeomDataAPI_Point2D> aPoint1 =
82         boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(ARC_ATTR_CENTER));
83   aPoint1->setValue(aPoint1->x() + theDeltaX, aPoint1->y() + theDeltaY);
84
85   boost::shared_ptr<GeomDataAPI_Point2D> aPoint2 =
86         boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(ARC_ATTR_START));
87   aPoint2->setValue(aPoint2->x() + theDeltaX, aPoint2->y() + theDeltaY);
88
89   boost::shared_ptr<GeomDataAPI_Point2D> aPoint3 =
90         boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(ARC_ATTR_END));
91   aPoint3->setValue(aPoint3->x() + theDeltaX, aPoint3->y() + theDeltaY);
92 }
93
94 double SketchPlugin_Arc::distanceToPoint(const boost::shared_ptr<GeomAPI_Pnt2d>& thePoint)
95 {
96   double aDelta = 0;
97   boost::shared_ptr<ModelAPI_Data> aData = data();
98
99   boost::shared_ptr<GeomDataAPI_Point2D> aPoint1 =
100         boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(ARC_ATTR_CENTER));
101   aDelta = aPoint1->pnt()->distance(thePoint);
102
103   boost::shared_ptr<GeomDataAPI_Point2D> aPoint2 =
104         boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(ARC_ATTR_START));
105   double aDistance = aPoint2->pnt()->distance(thePoint);
106   if (aDelta < aDistance)
107     aDelta = aDistance;
108
109   boost::shared_ptr<GeomDataAPI_Point2D> aPoint3 =
110         boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(ARC_ATTR_END));
111   aDistance = aPoint3->pnt()->distance(thePoint);
112   if (aDelta < aDistance)
113     aDelta = aDistance;
114
115   return aDelta;
116 }