]> SALOME platform Git repositories - modules/shaper.git/blob - src/SketchPlugin/SketchPlugin_Circle.cpp
Salome HOME
Merge branch 'Dev_1.1.0' of newgeom:newgeom into Dev_1.1.0
[modules/shaper.git] / src / SketchPlugin / SketchPlugin_Circle.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
2
3 // File:        SketchPlugin_Circle.cpp
4 // Created:     26 May 2014
5 // Author:      Artem ZHIDKOV
6
7 #include "SketchPlugin_Circle.h"
8 #include "SketchPlugin_Sketch.h"
9 #include <ModelAPI_Data.h>
10 #include <ModelAPI_ResultConstruction.h>
11 #include <ModelAPI_AttributeSelection.h>
12 #include <ModelAPI_Validator.h>
13 #include <ModelAPI_AttributeDouble.h>
14 #include <ModelAPI_Session.h>
15
16 #include <GeomAPI_Pnt2d.h>
17 #include <GeomAPI_Circ.h>
18 #include <GeomDataAPI_Point2D.h>
19 #include <GeomDataAPI_Dir.h>
20 #include <GeomAlgoAPI_PointBuilder.h>
21 #include <GeomAlgoAPI_EdgeBuilder.h>
22 #include <GeomAlgoAPI_CompoundBuilder.h>
23
24 SketchPlugin_Circle::SketchPlugin_Circle()
25     : SketchPlugin_SketchEntity()
26 {
27 }
28
29 void SketchPlugin_Circle::initAttributes()
30 {
31   SketchPlugin_SketchEntity::initAttributes();
32
33   data()->addAttribute(CENTER_ID(), GeomDataAPI_Point2D::typeId());
34   data()->addAttribute(RADIUS_ID(), ModelAPI_AttributeDouble::typeId());
35   data()->addAttribute(EXTERNAL_ID(), ModelAPI_AttributeSelection::typeId());
36   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), EXTERNAL_ID());
37 }
38
39 void SketchPlugin_Circle::execute()
40 {
41   SketchPlugin_Sketch* aSketch = sketch();
42   if (aSketch) {
43     std::list<std::shared_ptr<GeomAPI_Shape> > aShapes;
44
45     // compute a circle point in 3D view
46     std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr = std::dynamic_pointer_cast<
47         GeomDataAPI_Point2D>(data()->attribute(CENTER_ID()));
48     AttributeDoublePtr aRadiusAttr = 
49       std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(data()->attribute(RADIUS_ID()));
50     if (aCenterAttr->isInitialized() && aRadiusAttr->isInitialized()) {
51       std::shared_ptr<GeomAPI_Pnt> aCenter(aSketch->to3D(aCenterAttr->x(), aCenterAttr->y()));
52       //std::cout<<"Execute circle "<<aCenter->x()<<" "<<aCenter->y()<<" "<<aCenter->z()<<std::endl;
53       // make a visible point
54       std::shared_ptr<GeomAPI_Shape> aCenterPointShape = GeomAlgoAPI_PointBuilder::point(aCenter);
55       std::shared_ptr<ModelAPI_ResultConstruction> aConstr1 = document()->createConstruction(
56           data(), 0);
57       aConstr1->setShape(aCenterPointShape);
58       aConstr1->setIsInHistory(false);
59       setResult(aConstr1, 0);
60
61       // make a visible circle
62       std::shared_ptr<GeomDataAPI_Dir> aNDir = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
63           aSketch->data()->attribute(SketchPlugin_Sketch::NORM_ID()));
64       bool aHasPlane = aNDir && !(aNDir->x() == 0 && aNDir->y() == 0 && aNDir->z() == 0);
65       if (aHasPlane) {
66         std::shared_ptr<GeomAPI_Dir> aNormal(new GeomAPI_Dir(aNDir->x(), aNDir->y(), aNDir->z()));
67         // compute the circle radius
68         double aRadius = aRadiusAttr->value();
69
70         std::shared_ptr<GeomAPI_Shape> aCircleShape = GeomAlgoAPI_EdgeBuilder::lineCircle(
71             aCenter, aNormal, aRadius);
72         aShapes.push_back(aCircleShape);
73         std::shared_ptr<ModelAPI_ResultConstruction> aConstr2 = document()->createConstruction(
74             data(), 1);
75         aConstr2->setShape(aCircleShape);
76         aConstr2->setIsInHistory(false);
77         setResult(aConstr2, 1);
78       }
79     }
80   }
81 }
82
83 void SketchPlugin_Circle::move(double theDeltaX, double theDeltaY)
84 {
85   std::shared_ptr<ModelAPI_Data> aData = data();
86   if (!aData->isValid())
87     return;
88
89   std::shared_ptr<GeomDataAPI_Point2D> aPoint1 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
90       aData->attribute(CENTER_ID()));
91   aPoint1->move(theDeltaX, theDeltaY);
92 }
93
94 double SketchPlugin_Circle::distanceToPoint(const std::shared_ptr<GeomAPI_Pnt2d>& thePoint)
95 {
96   std::shared_ptr<ModelAPI_Data> aData = data();
97   std::shared_ptr<GeomDataAPI_Point2D> aPoint = 
98     std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(CENTER_ID()));
99
100   return aPoint->pnt()->distance(thePoint);
101 }
102
103 bool SketchPlugin_Circle::isFixed() {
104   return data()->selection(EXTERNAL_ID())->context().get() != NULL;
105 }
106
107 void SketchPlugin_Circle::attributeChanged(const std::string& theID) {
108   if (theID == EXTERNAL_ID()) {
109     std::shared_ptr<GeomAPI_Shape> aSelection = data()->selection(EXTERNAL_ID())->value();
110     // update arguments due to the selection value
111     if (aSelection && !aSelection->isNull() && aSelection->isEdge()) {
112       std::shared_ptr<GeomAPI_Edge> anEdge( new GeomAPI_Edge(aSelection));
113       std::shared_ptr<GeomAPI_Circ> aCirc = anEdge->circle();
114       std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr = 
115         std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(CENTER_ID()));
116       aCenterAttr->setValue(sketch()->to2D(aCirc->center()));
117       real(RADIUS_ID())->setValue(aCirc->radius());
118     }
119   }
120 }