Salome HOME
Merge branch 'master' of salome:modules/shaper
[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 #include <ModelAPI_AttributeDouble.h>
15
16 #include <GeomAPI_Edge.h>
17 #include <GeomAPI_Vertex.h>
18 #include <GeomAlgoAPI_EdgeBuilder.h>
19 #include <GeomAlgoAPI_PointBuilder.h>
20
21 #ifdef _DEBUG
22 #include <iostream>
23 #endif
24
25 using namespace std;
26
27 ConstructionPlugin_Axis::ConstructionPlugin_Axis()
28 {
29 }
30
31 void ConstructionPlugin_Axis::initAttributes()
32 {
33   data()->addAttribute(ConstructionPlugin_Axis::METHOD(),
34                        ModelAPI_AttributeString::typeId());
35   data()->addAttribute(ConstructionPlugin_Axis::POINT_FIRST(),
36                        ModelAPI_AttributeSelection::typeId());
37   data()->addAttribute(ConstructionPlugin_Axis::POINT_SECOND(),
38                        ModelAPI_AttributeSelection::typeId());
39   data()->addAttribute(ConstructionPlugin_Axis::CYLINDRICAL_FACE(),
40                        ModelAPI_AttributeSelection::typeId());
41   data()->addAttribute(ConstructionPlugin_Axis::X_DIRECTION(),
42                        ModelAPI_AttributeDouble::typeId());
43   data()->addAttribute(ConstructionPlugin_Axis::Y_DIRECTION(),
44                        ModelAPI_AttributeDouble::typeId());
45   data()->addAttribute(ConstructionPlugin_Axis::Z_DIRECTION(),
46                        ModelAPI_AttributeDouble::typeId());
47 }
48
49 void ConstructionPlugin_Axis::createAxisByTwoPoints()
50 {
51   AttributeSelectionPtr aRef1 = data()->selection(ConstructionPlugin_Axis::POINT_FIRST());
52   AttributeSelectionPtr aRef2 = data()->selection(ConstructionPlugin_Axis::POINT_SECOND());
53   if ((aRef1.get() != NULL) && (aRef2.get() != NULL)) {
54     GeomShapePtr aShape1 = aRef1->value();
55     if (!aShape1.get())
56       aShape1 = aRef1->context()->shape();
57     GeomShapePtr aShape2 = aRef2->value();
58     if (!aShape2.get())
59       aShape2 = aRef2->context()->shape();
60     if (aShape1->isVertex() && aShape2->isVertex() && (!aShape1->isEqual(aShape2))) {
61       std::shared_ptr<GeomAPI_Pnt> aStart = GeomAlgoAPI_PointBuilder::point(aShape1);
62       std::shared_ptr<GeomAPI_Pnt> anEnd = GeomAlgoAPI_PointBuilder::point(aShape2);
63       if (aStart->distance(anEnd) > ConstructionPlugin_Axis::MINIMAL_LENGTH()) {
64         std::shared_ptr<GeomAPI_Edge> anEdge = GeomAlgoAPI_EdgeBuilder::line(aStart, anEnd);
65
66         ResultConstructionPtr aConstr = document()->createConstruction(data());
67         aConstr->setInfinite(true);
68         aConstr->setShape(anEdge);
69         setResult(aConstr);
70       }
71     }
72   }
73 }
74
75
76 void ConstructionPlugin_Axis::createAxisByPointAndDirection()
77 {
78   AttributeSelectionPtr aRef1 = data()->selection(ConstructionPlugin_Axis::POINT_FIRST());
79   AttributeDoublePtr aXAttr = data()->real(ConstructionPlugin_Axis::X_DIRECTION());
80   AttributeDoublePtr aYAttr = data()->real(ConstructionPlugin_Axis::Y_DIRECTION());
81   AttributeDoublePtr aZAttr = data()->real(ConstructionPlugin_Axis::Z_DIRECTION());
82   if ((aRef1.get() != NULL) && (aXAttr.get() != NULL) && 
83       (aYAttr.get() != NULL) && (aZAttr.get() != NULL)) {
84     GeomShapePtr aShape1 = aRef1->value();
85     if (!aShape1.get())
86       aShape1 = aRef1->context()->shape();
87
88     std::shared_ptr<GeomAPI_Vertex> aVertex(new GeomAPI_Vertex(aXAttr->value(), 
89                                                                aYAttr->value(),
90                                                                aZAttr->value()));
91     if (aShape1->isVertex() && (!aShape1->isEqual(aVertex))) {
92       std::shared_ptr<GeomAPI_Pnt> aStart = GeomAlgoAPI_PointBuilder::point(aShape1);
93       std::shared_ptr<GeomAPI_Pnt> anEnd = aVertex->point();
94       if (aStart->distance(anEnd) > ConstructionPlugin_Axis::MINIMAL_LENGTH()) {
95         std::shared_ptr<GeomAPI_Edge> anEdge = GeomAlgoAPI_EdgeBuilder::line(aStart, anEnd);
96
97         ResultConstructionPtr aConstr = document()->createConstruction(data());
98         aConstr->setInfinite(true);
99         aConstr->setShape(anEdge);
100         setResult(aConstr);
101       }
102     }
103   }
104 }
105
106
107 void ConstructionPlugin_Axis::createAxisByCylindricalFace()
108 {
109     std::shared_ptr<GeomAPI_Shape> aSelection = data()->selection(CYLINDRICAL_FACE())->value();
110      // update arguments due to the selection value
111     if (aSelection && !aSelection->isNull() && aSelection->isFace()) {
112       std::shared_ptr<GeomAPI_Edge> anEdge = GeomAlgoAPI_EdgeBuilder::cylinderAxis(aSelection);
113
114       ResultConstructionPtr aConstr = document()->createConstruction(data());
115       aConstr->setInfinite(true);
116       aConstr->setShape(anEdge);
117       setResult(aConstr);
118     }
119 }
120
121
122
123 void ConstructionPlugin_Axis::execute()
124 {
125   AttributeStringPtr aMethodTypeAttr = string(ConstructionPlugin_Axis::METHOD());
126   std::string aMethodType = aMethodTypeAttr->value();
127   if (aMethodType == "AxisByPointsCase") {
128     createAxisByTwoPoints();
129   } else if (aMethodType == "AxisByCylindricalFaceCase") {
130     createAxisByCylindricalFace();
131   } else if (aMethodType == "AxisByPointAndDirection") {
132     createAxisByPointAndDirection();
133   }
134 }
135
136 bool ConstructionPlugin_Axis::customisePresentation(ResultPtr theResult, AISObjectPtr thePrs,
137   std::shared_ptr<GeomAPI_ICustomPrs> theDefaultPrs)
138 {
139   bool isCustomized = theDefaultPrs.get() != NULL &&
140                       theDefaultPrs->customisePresentation(theResult, thePrs, theDefaultPrs);
141
142   isCustomized = thePrs->setLineStyle(3) || isCustomized;
143   isCustomized = thePrs->setWidth(2) || isCustomized;
144
145   return isCustomized;
146 }