Salome HOME
Changes in the presentations of features
[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_Circ2d.h>
10 #include <GeomAPI_Pnt2d.h>
11
12 #include <GeomDataAPI_Point2D.h>
13 #include <GeomDataAPI_Dir.h>
14
15 #include <GeomAlgoAPI_PointBuilder.h>
16 #include <GeomAlgoAPI_EdgeBuilder.h>
17 #include <GeomAlgoAPI_CompoundBuilder.h>
18
19 const double tolerance = 1e-7;
20
21 SketchPlugin_Arc::SketchPlugin_Arc()
22   : SketchPlugin_Feature()
23 {
24 }
25
26 void SketchPlugin_Arc::initAttributes()
27 {
28   data()->addAttribute(ARC_ATTR_CENTER, GeomDataAPI_Point2D::type());
29   data()->addAttribute(ARC_ATTR_START,  GeomDataAPI_Point2D::type());
30   data()->addAttribute(ARC_ATTR_END,    GeomDataAPI_Point2D::type());
31 }
32
33 void SketchPlugin_Arc::execute() 
34 {
35 }
36
37 const boost::shared_ptr<GeomAPI_Shape>& SketchPlugin_Arc::preview()
38 {
39   SketchPlugin_Sketch* aSketch = sketch();
40   if (aSketch) {
41     std::list<boost::shared_ptr<GeomAPI_Shape> > aShapes;
42
43     // compute a circle point in 3D view
44     boost::shared_ptr<GeomDataAPI_Point2D> aCenterAttr = 
45       boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(data()->attribute(ARC_ATTR_CENTER));
46       // compute the arc start point
47       boost::shared_ptr<GeomDataAPI_Point2D> aStartAttr = 
48         boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(data()->attribute(ARC_ATTR_START));
49       if (aCenterAttr->isInitialized() && aStartAttr->isInitialized()) {
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       aShapes.push_back(aCenterPointShape);
54
55       // make a visible circle
56       boost::shared_ptr<GeomDataAPI_Dir> aNDir = 
57         boost::dynamic_pointer_cast<GeomDataAPI_Dir>(aSketch->data()->attribute(SKETCH_ATTR_NORM));
58       bool aHasPlane = aNDir && !(aNDir->x() == 0 && aNDir->y() == 0 && aNDir->z() == 0);
59       if (aHasPlane) {
60         boost::shared_ptr<GeomAPI_Dir> aNormal = aNDir->dir();
61         boost::shared_ptr<GeomAPI_Pnt> aStartPoint(aSketch->to3D(aStartAttr->x(), aStartAttr->y()));
62
63         // compute and change the arc end point
64         boost::shared_ptr<GeomDataAPI_Point2D> anEndAttr = 
65           boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(data()->attribute(ARC_ATTR_END));
66         if (anEndAttr->isInitialized())
67         {
68           boost::shared_ptr<GeomAPI_Circ2d> aCircleForArc(
69             new GeomAPI_Circ2d(aCenterAttr->pnt(), aStartAttr->pnt()));
70           boost::shared_ptr<GeomAPI_Pnt2d> aProjection = aCircleForArc->project(anEndAttr->pnt());
71           if (aProjection && anEndAttr->pnt()->distance(aProjection) > tolerance)
72             anEndAttr->setValue(aProjection);
73         }
74         boost::shared_ptr<GeomAPI_Pnt> aEndPoint(aSketch->to3D(anEndAttr->x(), anEndAttr->y()));
75
76         boost::shared_ptr<GeomAPI_Shape> aCircleShape = 
77                    GeomAlgoAPI_EdgeBuilder::lineCircleArc(aCenter, aStartPoint, aEndPoint, aNormal);
78         if (aCircleShape)
79           aShapes.push_back(aCircleShape);
80       }
81       boost::shared_ptr<GeomAPI_Shape> aCompound = GeomAlgoAPI_CompoundBuilder::compound(aShapes);
82       setPreview(aCompound);
83     }
84   }
85   return getPreview();
86 }
87
88 boost::shared_ptr<GeomAPI_AISObject> SketchPlugin_Arc::getAISObject(
89                                 boost::shared_ptr<GeomAPI_AISObject> thePrevious)
90 {
91   return prepareAISShape(thePrevious);
92 }
93
94 void SketchPlugin_Arc::move(double theDeltaX, double theDeltaY)
95 {
96   boost::shared_ptr<ModelAPI_Data> aData = data();
97   if (!aData->isValid())
98     return;
99
100   boost::shared_ptr<GeomDataAPI_Point2D> aPoint1 =
101         boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(ARC_ATTR_CENTER));
102   aPoint1->setValue(aPoint1->x() + theDeltaX, aPoint1->y() + theDeltaY);
103
104   boost::shared_ptr<GeomDataAPI_Point2D> aPoint2 =
105         boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(ARC_ATTR_START));
106   aPoint2->setValue(aPoint2->x() + theDeltaX, aPoint2->y() + theDeltaY);
107
108   boost::shared_ptr<GeomDataAPI_Point2D> aPoint3 =
109         boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(ARC_ATTR_END));
110   aPoint3->setValue(aPoint3->x() + theDeltaX, aPoint3->y() + theDeltaY);
111 }
112
113 double SketchPlugin_Arc::distanceToPoint(const boost::shared_ptr<GeomAPI_Pnt2d>& thePoint)
114 {
115   double aDelta = 0;
116   boost::shared_ptr<ModelAPI_Data> aData = data();
117
118   boost::shared_ptr<GeomDataAPI_Point2D> aPoint1 =
119         boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(ARC_ATTR_CENTER));
120   aDelta = aPoint1->pnt()->distance(thePoint);
121
122   boost::shared_ptr<GeomDataAPI_Point2D> aPoint2 =
123         boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(ARC_ATTR_START));
124   double aDistance = aPoint2->pnt()->distance(thePoint);
125   if (aDelta < aDistance)
126     aDelta = aDistance;
127
128   boost::shared_ptr<GeomDataAPI_Point2D> aPoint3 =
129         boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(ARC_ATTR_END));
130   aDistance = aPoint3->pnt()->distance(thePoint);
131   if (aDelta < aDistance)
132     aDelta = aDistance;
133
134   return aDelta;
135 }