]> SALOME platform Git repositories - modules/shaper.git/blob - src/SketchPlugin/SketchPlugin_Arc.cpp
Salome HOME
Projection of the last point of 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
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 #include <Precision.hxx>
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     boost::shared_ptr<GeomAPI_Pnt> aCenter(aSketch->to3D(aCenterAttr->x(), aCenterAttr->y()));
47     // make a visible point
48     boost::shared_ptr<GeomAPI_Shape> aCenterPointShape = GeomAlgoAPI_PointBuilder::point(aCenter);
49     aShapes.push_back(aCenterPointShape);
50
51     // make a visible circle
52     boost::shared_ptr<GeomDataAPI_Dir> aNDir = 
53       boost::dynamic_pointer_cast<GeomDataAPI_Dir>(aSketch->data()->attribute(SKETCH_ATTR_NORM));
54     bool aHasPlane = aNDir && !(aNDir->x() == 0 && aNDir->y() == 0 && aNDir->z() == 0);
55     if (aHasPlane) {
56       boost::shared_ptr<GeomAPI_Dir> aNormal = aNDir->dir();
57       // compute the arc start point
58       boost::shared_ptr<GeomDataAPI_Point2D> aStartAttr = 
59         boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(data()->attribute(ARC_ATTR_START));
60       boost::shared_ptr<GeomAPI_Pnt> aStartPoint(aSketch->to3D(aStartAttr->x(), aStartAttr->y()));
61
62       // compute and change the arc end point
63       boost::shared_ptr<GeomDataAPI_Point2D> anEndAttr = 
64         boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(data()->attribute(ARC_ATTR_END));
65       if (anEndAttr->isInitialized())
66       {
67         boost::shared_ptr<GeomAPI_Circ2d> aCircleForArc(
68           new GeomAPI_Circ2d(aCenterAttr->pnt(), aStartAttr->pnt()));
69         boost::shared_ptr<GeomAPI_Pnt2d> aProjection = aCircleForArc->project(anEndAttr->pnt());
70         if (aProjection && anEndAttr->pnt()->distance(aProjection) > Precision::Confusion())
71           anEndAttr->setValue(aProjection);
72       }
73       boost::shared_ptr<GeomAPI_Pnt> aEndPoint(aSketch->to3D(anEndAttr->x(), anEndAttr->y()));
74
75       boost::shared_ptr<GeomAPI_Shape> aCircleShape = 
76                  GeomAlgoAPI_EdgeBuilder::lineCircleArc(aCenter, aStartPoint, aEndPoint, aNormal);
77       if (aCircleShape)
78         aShapes.push_back(aCircleShape);
79     }
80     boost::shared_ptr<GeomAPI_Shape> aCompound = GeomAlgoAPI_CompoundBuilder::compound(aShapes);
81     setPreview(aCompound);
82   }
83   return getPreview();
84 }
85
86 void SketchPlugin_Arc::move(double theDeltaX, double theDeltaY)
87 {
88   boost::shared_ptr<ModelAPI_Data> aData = data();
89   if (!aData->isValid())
90     return;
91
92   boost::shared_ptr<GeomDataAPI_Point2D> aPoint1 =
93         boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(ARC_ATTR_CENTER));
94   aPoint1->setValue(aPoint1->x() + theDeltaX, aPoint1->y() + theDeltaY);
95
96   boost::shared_ptr<GeomDataAPI_Point2D> aPoint2 =
97         boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(ARC_ATTR_START));
98   aPoint2->setValue(aPoint2->x() + theDeltaX, aPoint2->y() + theDeltaY);
99
100   boost::shared_ptr<GeomDataAPI_Point2D> aPoint3 =
101         boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(ARC_ATTR_END));
102   aPoint3->setValue(aPoint3->x() + theDeltaX, aPoint3->y() + theDeltaY);
103 }
104
105 double SketchPlugin_Arc::distanceToPoint(const boost::shared_ptr<GeomAPI_Pnt2d>& thePoint)
106 {
107   double aDelta = 0;
108   boost::shared_ptr<ModelAPI_Data> aData = data();
109
110   boost::shared_ptr<GeomDataAPI_Point2D> aPoint1 =
111         boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(ARC_ATTR_CENTER));
112   aDelta = aPoint1->pnt()->distance(thePoint);
113
114   boost::shared_ptr<GeomDataAPI_Point2D> aPoint2 =
115         boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(ARC_ATTR_START));
116   double aDistance = aPoint2->pnt()->distance(thePoint);
117   if (aDelta < aDistance)
118     aDelta = aDistance;
119
120   boost::shared_ptr<GeomDataAPI_Point2D> aPoint3 =
121         boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(ARC_ATTR_END));
122   aDistance = aPoint3->pnt()->distance(thePoint);
123   if (aDelta < aDistance)
124     aDelta = aDistance;
125
126   return aDelta;
127 }