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