Salome HOME
Fix for issue #461
[modules/shaper.git] / src / ConstructionPlugin / ConstructionPlugin_Axis.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        ConstructionPlugin_Axis.cpp
4 // Created:     12 Dec 2014
5 // Author:      Vitaly Smetannikov
6
7 #include "ConstructionPlugin_Axis.h"
8
9 #include <Config_PropManager.h>
10
11 #include <ModelAPI_AttributeSelection.h>
12 #include <ModelAPI_ResultConstruction.h>
13 #include <ModelAPI_AttributeString.h>
14
15 #include <GeomAPI_Edge.h>
16 #include <GeomAlgoAPI_EdgeBuilder.h>
17 #include <GeomAlgoAPI_PointBuilder.h>
18
19 #ifdef _DEBUG
20 #include <iostream>
21 #endif
22
23 using namespace std;
24
25 ConstructionPlugin_Axis::ConstructionPlugin_Axis()
26 {
27 }
28
29 void ConstructionPlugin_Axis::initAttributes()
30 {
31   data()->addAttribute(ConstructionPlugin_Axis::METHOD(),
32                        ModelAPI_AttributeString::typeId());
33   data()->addAttribute(ConstructionPlugin_Axis::POINT_FIRST(),
34                        ModelAPI_AttributeSelection::typeId());
35   data()->addAttribute(ConstructionPlugin_Axis::POINT_SECOND(),
36                        ModelAPI_AttributeSelection::typeId());
37   data()->addAttribute(ConstructionPlugin_Axis::CYLINDRICAL_FACE(),
38                        ModelAPI_AttributeSelection::typeId());
39 }
40
41 void ConstructionPlugin_Axis::createAxisByTwoPoints()
42 {
43   AttributeSelectionPtr aRef1 = data()->selection(ConstructionPlugin_Axis::POINT_FIRST());
44   AttributeSelectionPtr aRef2 = data()->selection(ConstructionPlugin_Axis::POINT_SECOND());
45   if ((aRef1.get() != NULL) && (aRef2.get() != NULL)) {
46     GeomShapePtr aShape1 = aRef1->value();
47     GeomShapePtr aShape2 = aRef2->value();
48     if (aShape1->isVertex() && aShape2->isVertex() && (!aShape1->isEqual(aShape2))) {
49       std::shared_ptr<GeomAPI_Pnt> aStart = GeomAlgoAPI_PointBuilder::point(aShape1);
50       std::shared_ptr<GeomAPI_Pnt> anEnd = GeomAlgoAPI_PointBuilder::point(aShape2);
51       if (aStart->distance(anEnd) > ConstructionPlugin_Axis::MINIMAL_LENGTH()) {
52         std::shared_ptr<GeomAPI_Edge> anEdge = GeomAlgoAPI_EdgeBuilder::line(aStart, anEnd);
53
54         ResultConstructionPtr aConstr = document()->createConstruction(data());
55         aConstr->setShape(anEdge);
56         setResult(aConstr);
57       }
58     }
59   }
60 }
61
62 void ConstructionPlugin_Axis::createAxisByCylindricalFace()
63 {
64     std::shared_ptr<GeomAPI_Shape> aSelection = data()->selection(CYLINDRICAL_FACE())->value();
65      // update arguments due to the selection value
66     if (aSelection && !aSelection->isNull() && aSelection->isFace()) {
67       std::shared_ptr<GeomAPI_Edge> anEdge = GeomAlgoAPI_EdgeBuilder::cylinderAxis(aSelection);
68
69       ResultConstructionPtr aConstr = document()->createConstruction(data());
70       aConstr->setShape(anEdge);
71       setResult(aConstr);
72     }
73 }
74
75 void ConstructionPlugin_Axis::execute()
76 {
77   AttributeStringPtr aMethodTypeAttr = string(ConstructionPlugin_Axis::METHOD());
78   std::string aMethodType = aMethodTypeAttr->value();
79   if (aMethodType == "AxisByPointsCase") {
80     createAxisByTwoPoints();
81   } else if (aMethodType == "AxisByCylindricalFaceCase") {
82     createAxisByCylindricalFace();
83   }
84 }
85
86 bool ConstructionPlugin_Axis::customisePresentation(ResultPtr theResult, AISObjectPtr thePrs,
87   std::shared_ptr<GeomAPI_ICustomPrs> theDefaultPrs)
88 {
89   bool isCustomized = theDefaultPrs.get() != NULL &&
90                       theDefaultPrs->customisePresentation(theResult, thePrs, theDefaultPrs);
91
92   isCustomized = thePrs->setLineStyle(3);
93
94   return isCustomized;
95 }