Salome HOME
refs #80 - Sketch base GUI: create/draw point, circle 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
9 #include <GeomDataAPI_Point2D.h>
10 #include <GeomDataAPI_Dir.h>
11
12 #include <GeomAlgoAPI_PointBuilder.h>
13 #include <GeomAlgoAPI_EdgeBuilder.h>
14 #include <GeomAlgoAPI_CompoundBuilder.h>
15
16 SketchPlugin_Arc::SketchPlugin_Arc()
17   : SketchPlugin_Feature()
18 {
19 }
20
21 void SketchPlugin_Arc::initAttributes()
22 {
23   data()->addAttribute(ARC_ATTR_CENTER, GeomDataAPI_Point2D::type());
24   data()->addAttribute(ARC_ATTR_START,  GeomDataAPI_Point2D::type());
25   data()->addAttribute(ARC_ATTR_END,    GeomDataAPI_Point2D::type());
26 }
27
28 void SketchPlugin_Arc::execute() 
29 {
30 }
31
32 const boost::shared_ptr<GeomAPI_Shape>& SketchPlugin_Arc::preview()
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>(data()->attribute(ARC_ATTR_CENTER));
41     boost::shared_ptr<GeomAPI_Pnt> aCenter(aSketch->to3D(aCenterAttr->x(), aCenterAttr->y()));
42     // make a visible point
43     boost::shared_ptr<GeomAPI_Shape> aCenterPointShape = GeomAlgoAPI_PointBuilder::point(aCenter);
44     aShapes.push_back(aCenterPointShape);
45
46     // make a visible circle
47     boost::shared_ptr<GeomDataAPI_Dir> aNDir = 
48       boost::dynamic_pointer_cast<GeomDataAPI_Dir>(aSketch->data()->attribute(SKETCH_ATTR_NORM));
49     bool aHasPlane = aNDir && !(aNDir->x() == 0 && aNDir->y() == 0 && aNDir->z() == 0);
50     if (aHasPlane) {
51       boost::shared_ptr<GeomAPI_Dir> aNormal(new GeomAPI_Dir(aNDir->x(), aNDir->y(), aNDir->z()));
52       // compute the arc start point
53       boost::shared_ptr<GeomDataAPI_Point2D> aStartAttr = 
54         boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(data()->attribute(ARC_ATTR_START));
55       boost::shared_ptr<GeomAPI_Pnt> aStartPoint(aSketch->to3D(aStartAttr->x(), aStartAttr->y()));
56
57       // compute the arc end point
58       boost::shared_ptr<GeomDataAPI_Point2D> anEndAttr = 
59         boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(data()->attribute(ARC_ATTR_END));
60       boost::shared_ptr<GeomAPI_Pnt> aEndPoint(aSketch->to3D(anEndAttr->x(), anEndAttr->y()));
61
62       boost::shared_ptr<GeomAPI_Shape> aCircleShape = 
63                  GeomAlgoAPI_EdgeBuilder::lineCircleArc(aCenter, aStartPoint, aEndPoint, aNormal);
64       if (aCircleShape)
65         aShapes.push_back(aCircleShape);
66     }
67     boost::shared_ptr<GeomAPI_Shape> aCompound = GeomAlgoAPI_CompoundBuilder::compound(aShapes);
68     setPreview(aCompound);
69   }
70   return getPreview();
71 }
72
73 void SketchPlugin_Arc::move(double theDeltaX, double theDeltaY)
74 {
75   boost::shared_ptr<ModelAPI_Data> aData = data();
76   if (!aData->isValid())
77     return;
78
79   boost::shared_ptr<GeomDataAPI_Point2D> aPoint1 =
80         boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(ARC_ATTR_CENTER));
81   aPoint1->setValue(aPoint1->x() + theDeltaX, aPoint1->y() + theDeltaY);
82
83   boost::shared_ptr<GeomDataAPI_Point2D> aPoint2 =
84         boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(ARC_ATTR_START));
85   aPoint2->setValue(aPoint2->x() + theDeltaX, aPoint2->y() + theDeltaY);
86
87   boost::shared_ptr<GeomDataAPI_Point2D> aPoint3 =
88         boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(ARC_ATTR_END));
89   aPoint3->setValue(aPoint3->x() + theDeltaX, aPoint3->y() + theDeltaY);
90 }