Salome HOME
Adding a new type of axis creation : by 3 dimensions.
[modules/shaper.git] / src / ConstructionPlugin / ConstructionPlugin_Axis.cpp
1 // Copyright (C) 2014-2016 CEA/DEN, EDF R&D
2
3 // File:        ConstructionPlugin_Axis.cpp
4 // Created:     12 Dec 2014
5 // Author:      Vitaly Smetannikov
6
7 // Modified by CEA (delegation to Alyotech) : 29 Mar 2016 
8
9 #include "ConstructionPlugin_Axis.h"
10
11 #include <Config_PropManager.h>
12
13 #include <ModelAPI_AttributeSelection.h>
14 #include <ModelAPI_ResultConstruction.h>
15 #include <ModelAPI_AttributeString.h>
16 #include <ModelAPI_AttributeDouble.h>
17 #include <ModelAPI_Session.h>
18 #include <ModelAPI_Validator.h>
19
20 #include <GeomAPI_Edge.h>
21 #include <GeomAPI_Vertex.h>
22 #include <GeomAlgoAPI_EdgeBuilder.h>
23 #include <GeomAlgoAPI_PointBuilder.h>
24
25 #include <math.h>
26
27 #ifdef _DEBUG
28 #include <iostream>
29 #endif
30
31 using namespace std;
32
33 ConstructionPlugin_Axis::ConstructionPlugin_Axis()
34 {
35 }
36
37 void ConstructionPlugin_Axis::initAttributes()
38 {
39   data()->addAttribute(ConstructionPlugin_Axis::METHOD(),
40                        ModelAPI_AttributeString::typeId());
41   
42   // Attributes needed to build the axis using the "two points" method
43   data()->addAttribute(ConstructionPlugin_Axis::POINT_FIRST(),
44                        ModelAPI_AttributeSelection::typeId());
45   data()->addAttribute(ConstructionPlugin_Axis::POINT_SECOND(),
46                        ModelAPI_AttributeSelection::typeId());
47
48   // Attributes needed to build the axis using the "cylindrical face" method"
49   data()->addAttribute(ConstructionPlugin_Axis::CYLINDRICAL_FACE(),
50                        ModelAPI_AttributeSelection::typeId());
51   data()->addAttribute(ConstructionPlugin_Axis::X_DIRECTION(),
52                        ModelAPI_AttributeDouble::typeId());
53   data()->addAttribute(ConstructionPlugin_Axis::Y_DIRECTION(),
54                        ModelAPI_AttributeDouble::typeId());
55   data()->addAttribute(ConstructionPlugin_Axis::Z_DIRECTION(),
56                        ModelAPI_AttributeDouble::typeId());
57   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), 
58       ConstructionPlugin_Axis::X_DIRECTION());
59   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), 
60       ConstructionPlugin_Axis::Y_DIRECTION());
61   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), 
62       ConstructionPlugin_Axis::Z_DIRECTION());
63
64   //Attributes needed to build the axis using the "three dimensions" method
65   data()->addAttribute(ConstructionPlugin_Axis::DX(),
66                        ModelAPI_AttributeDouble::typeId());
67   data()->addAttribute(ConstructionPlugin_Axis::DY(),
68                        ModelAPI_AttributeDouble::typeId());
69   data()->addAttribute(ConstructionPlugin_Axis::DZ(),
70                        ModelAPI_AttributeDouble::typeId());
71 }
72
73 void ConstructionPlugin_Axis::createAxisByTwoPoints()
74 {
75   AttributeSelectionPtr aRef1 = data()->selection(ConstructionPlugin_Axis::POINT_FIRST());
76   AttributeSelectionPtr aRef2 = data()->selection(ConstructionPlugin_Axis::POINT_SECOND());
77   if ((aRef1.get() != NULL) && (aRef2.get() != NULL)) {
78     GeomShapePtr aShape1 = aRef1->value();
79     if (!aShape1.get())
80       aShape1 = aRef1->context()->shape();
81     GeomShapePtr aShape2 = aRef2->value();
82     if (!aShape2.get())
83       aShape2 = aRef2->context()->shape();
84     if (aShape1->isVertex() && aShape2->isVertex() && (!aShape1->isEqual(aShape2))) {
85       std::shared_ptr<GeomAPI_Pnt> aStart = GeomAlgoAPI_PointBuilder::point(aShape1);
86       std::shared_ptr<GeomAPI_Pnt> anEnd = GeomAlgoAPI_PointBuilder::point(aShape2);
87       if (aStart->distance(anEnd) > ConstructionPlugin_Axis::MINIMAL_LENGTH()) {
88         std::shared_ptr<GeomAPI_Edge> anEdge = GeomAlgoAPI_EdgeBuilder::line(aStart, anEnd);
89
90         ResultConstructionPtr aConstr = document()->createConstruction(data());
91         aConstr->setInfinite(true);
92         aConstr->setShape(anEdge);
93         setResult(aConstr);
94       }
95     }
96   }
97 }
98
99
100 void ConstructionPlugin_Axis::createAxisByPointAndDirection()
101 {
102   AttributeSelectionPtr aRef1 = data()->selection(ConstructionPlugin_Axis::POINT_FIRST());
103   AttributeDoublePtr aXAttr = data()->real(ConstructionPlugin_Axis::X_DIRECTION());
104   AttributeDoublePtr aYAttr = data()->real(ConstructionPlugin_Axis::Y_DIRECTION());
105   AttributeDoublePtr aZAttr = data()->real(ConstructionPlugin_Axis::Z_DIRECTION());
106   if ((aRef1.get() != NULL) && (aXAttr.get() != NULL) && 
107       (aYAttr.get() != NULL) && (aZAttr.get() != NULL)) {
108     GeomShapePtr aShape1 = aRef1->value();
109     if (!aShape1.get())
110       aShape1 = aRef1->context()->shape();
111
112     std::shared_ptr<GeomAPI_Vertex> aVertex(new GeomAPI_Vertex(aXAttr->value(), 
113                                                                aYAttr->value(),
114                                                                aZAttr->value()));
115     if (aShape1->isVertex() && (!aShape1->isEqual(aVertex))) {
116       std::shared_ptr<GeomAPI_Pnt> aStart = GeomAlgoAPI_PointBuilder::point(aShape1);
117       std::shared_ptr<GeomAPI_Pnt> anEnd = aVertex->point();
118       if (aStart->distance(anEnd) > ConstructionPlugin_Axis::MINIMAL_LENGTH()) {
119         std::shared_ptr<GeomAPI_Edge> anEdge = GeomAlgoAPI_EdgeBuilder::line(aStart, anEnd);
120
121         ResultConstructionPtr aConstr = document()->createConstruction(data());
122         aConstr->setInfinite(true);
123         aConstr->setShape(anEdge);
124         setResult(aConstr);
125       }
126     }
127   }
128 }
129
130
131 void ConstructionPlugin_Axis::createAxisByCylindricalFace()
132 {
133     std::shared_ptr<GeomAPI_Shape> aSelection = data()->selection(CYLINDRICAL_FACE())->value();
134      // update arguments due to the selection value
135     if (aSelection && !aSelection->isNull() && aSelection->isFace()) {
136       std::shared_ptr<GeomAPI_Edge> anEdge = GeomAlgoAPI_EdgeBuilder::cylinderAxis(aSelection);
137
138       ResultConstructionPtr aConstr = document()->createConstruction(data());
139       aConstr->setInfinite(true);
140       aConstr->setShape(anEdge);
141       setResult(aConstr);
142     }
143 }
144
145 void ConstructionPlugin_Axis::createAxisByDimensions()
146 {
147   // Start by getting these dimensions
148   double aDX = data()->real(DX())->value();
149   double aDY = data()->real(DY())->value();
150   double aDZ = data()->real(DZ())->value();
151
152   if (fabs(aDX) < MINIMAL_LENGTH()  && fabs(aDY) < MINIMAL_LENGTH() && fabs(aDZ) < MINIMAL_LENGTH()){
153     setError("Axis builder with dimensions  :: all dimensions are null", false);
154     return ;
155   }
156
157   // Make the axis, build the ResultConstructionPtr and write it
158   std::shared_ptr<GeomAPI_Edge> anEdge = GeomAlgoAPI_EdgeBuilder::line(aDX, aDY, aDZ);
159   ResultConstructionPtr aConstr = document()->createConstruction(data());
160   aConstr->setInfinite(true);
161   aConstr->setShape(anEdge);
162   setResult(aConstr);    
163 }
164
165 void ConstructionPlugin_Axis::execute()
166 {
167   AttributeStringPtr aMethodTypeAttr = string(ConstructionPlugin_Axis::METHOD());
168   std::string aMethodType = aMethodTypeAttr->value();
169   if (aMethodType == "AxisByPointsCase") {
170     createAxisByTwoPoints();
171   } else if (aMethodType == "AxisByCylindricalFaceCase") {
172     createAxisByCylindricalFace();
173   } else if (aMethodType == "AxisByPointAndDirection") {
174     createAxisByPointAndDirection();
175   } else if (aMethodType == "AxisByDimensionsCase") {
176     createAxisByDimensions();
177   }
178 }
179
180 bool ConstructionPlugin_Axis::customisePresentation(ResultPtr theResult, AISObjectPtr thePrs,
181   std::shared_ptr<GeomAPI_ICustomPrs> theDefaultPrs)
182 {
183   bool isCustomized = theDefaultPrs.get() != NULL &&
184                       theDefaultPrs->customisePresentation(theResult, thePrs, theDefaultPrs);
185
186   isCustomized = thePrs->setLineStyle(3) || isCustomized;
187   isCustomized = thePrs->setWidth(2) || isCustomized;
188
189   return isCustomized;
190 }