Salome HOME
Added option to create Construction Point by intersection of line and plane.
[modules/shaper.git] / src / ConstructionPlugin / ConstructionPlugin_Point.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        ConstructionPlugin_Point.cxx
4 // Created:     27 Mar 2014
5 // Author:      Mikhail PONIKAROV
6
7 #include "ConstructionPlugin_Point.h"
8
9 #include <ModelAPI_AttributeBoolean.h>
10 #include <ModelAPI_AttributeDouble.h>
11 #include <ModelAPI_AttributeSelection.h>
12 #include <ModelAPI_AttributeString.h>
13 #include <ModelAPI_ResultConstruction.h>
14
15 #include <GeomAlgoAPI_PointBuilder.h>
16
17 #include <GeomAPI_Edge.h>
18 #include <GeomAPI_Pnt.h>
19 #include <GeomAPI_Vertex.h>
20
21 //==================================================================================================
22 ConstructionPlugin_Point::ConstructionPlugin_Point()
23 {
24 }
25
26 //==================================================================================================
27 const std::string& ConstructionPlugin_Point::getKind()
28 {
29   static std::string MY_KIND = ConstructionPlugin_Point::ID();
30   return MY_KIND;
31 }
32
33 //==================================================================================================
34 void ConstructionPlugin_Point::initAttributes()
35 {
36   data()->addAttribute(X(), ModelAPI_AttributeDouble::typeId());
37   data()->addAttribute(Y(), ModelAPI_AttributeDouble::typeId());
38   data()->addAttribute(Z(), ModelAPI_AttributeDouble::typeId());
39
40   data()->addAttribute(CREATION_METHOD(), ModelAPI_AttributeString::typeId());
41
42   data()->addAttribute(EDGE(), ModelAPI_AttributeSelection::typeId());
43   data()->addAttribute(DISTANCE_VALUE(), ModelAPI_AttributeDouble::typeId());
44   data()->addAttribute(DISTANCE_PERCENT(), ModelAPI_AttributeBoolean::typeId());
45   data()->addAttribute(REVERSE(), ModelAPI_AttributeBoolean::typeId());
46
47   data()->addAttribute(POINT(), ModelAPI_AttributeSelection::typeId());
48   data()->addAttribute(PLANE(), ModelAPI_AttributeSelection::typeId());
49
50   data()->addAttribute(FIRST_LINE(), ModelAPI_AttributeSelection::typeId());
51   data()->addAttribute(SECOND_LINE(), ModelAPI_AttributeSelection::typeId());
52
53   data()->addAttribute(INTERSECTION_LINE(), ModelAPI_AttributeSelection::typeId());
54   data()->addAttribute(INTERSECTION_PLANE(), ModelAPI_AttributeSelection::typeId());
55 }
56
57 //==================================================================================================
58 void ConstructionPlugin_Point::execute()
59 {
60   GeomShapePtr aShape;
61
62   std::string aCreationMethod = string(CREATION_METHOD())->value();
63   if(aCreationMethod == CREATION_METHOD_BY_XYZ()) {
64     aShape = createByXYZ();
65   } else if(aCreationMethod == CREATION_METHOD_BY_DISTANCE_ON_EDGE()) {
66     aShape = createByDistanceOnEdge();
67   } else if(aCreationMethod == CREATION_METHOD_BY_PROJECTION()) {
68     aShape = createByProjection();
69   } else if(aCreationMethod == CREATION_METHOD_BY_LINES_INTERSECTION()) {
70     aShape = createByLinesIntersection();
71   } else if(aCreationMethod == CREATION_METHOD_BY_LINE_AND_PLANE_INTERSECTION()) {
72     aShape = createByLineAndPlaneIntersection();
73   }
74
75   if(aShape.get()) {
76     std::shared_ptr<ModelAPI_ResultConstruction> aConstr = document()->createConstruction(data());
77     aConstr->setShape(aShape);
78     setResult(aConstr);
79   }
80 }
81
82 //==================================================================================================
83 bool ConstructionPlugin_Point::customisePresentation(ResultPtr theResult,
84                                                      AISObjectPtr thePrs,
85                                                      std::shared_ptr<GeomAPI_ICustomPrs> theDefaultPrs)
86 {
87   bool isCustomized = theDefaultPrs.get() != NULL &&
88                       theDefaultPrs->customisePresentation(theResult, thePrs, theDefaultPrs);
89   //thePrs->setPointMarker(1, 1.); // Set point as a '+' symbol
90   return true;
91 }
92
93 //==================================================================================================
94 std::shared_ptr<GeomAPI_Vertex> ConstructionPlugin_Point::createByXYZ()
95 {
96   return GeomAlgoAPI_PointBuilder::vertex(real(X())->value(),
97                                           real(Y())->value(),
98                                           real(Z())->value());
99 }
100
101 //==================================================================================================
102 std::shared_ptr<GeomAPI_Vertex> ConstructionPlugin_Point::createByDistanceOnEdge()
103 {
104   // Get edge.
105   AttributeSelectionPtr anEdgeSelection = selection(EDGE());
106   GeomShapePtr aShape = anEdgeSelection->value();
107   if(!aShape.get()) {
108     aShape = anEdgeSelection->context()->shape();
109   }
110   std::shared_ptr<GeomAPI_Edge> anEdge(new GeomAPI_Edge(aShape));
111
112   // Get distance value and percent flag.
113   double aValue = real(DISTANCE_VALUE())->value();
114   bool anIsPercent = boolean(DISTANCE_PERCENT())->value();
115
116   // Get reverse flag.
117   bool anIsReverse = boolean(REVERSE())->value();
118
119   return GeomAlgoAPI_PointBuilder::vertexOnEdge(anEdge, aValue, anIsPercent, anIsReverse);
120 }
121
122 //==================================================================================================
123 std::shared_ptr<GeomAPI_Vertex> ConstructionPlugin_Point::createByProjection()
124 {
125   // Get point.
126   AttributeSelectionPtr aPointSelection = selection(POINT());
127   GeomShapePtr aPointShape = aPointSelection->value();
128   if(!aPointShape.get()) {
129     aPointShape = aPointSelection->context()->shape();
130   }
131   std::shared_ptr<GeomAPI_Vertex> aVertex(new GeomAPI_Vertex(aPointShape));
132
133   // Get plane.
134   AttributeSelectionPtr aPlaneSelection = selection(PLANE());
135   GeomShapePtr aPlaneShape = aPlaneSelection->value();
136   if(!aPlaneShape.get()) {
137     aPlaneShape = aPlaneSelection->context()->shape();
138   }
139   std::shared_ptr<GeomAPI_Face> aFace(new GeomAPI_Face(aPlaneShape));
140
141   return GeomAlgoAPI_PointBuilder::vertexByProjection(aVertex, aFace);
142 }
143
144 //==================================================================================================
145 std::shared_ptr<GeomAPI_Vertex> ConstructionPlugin_Point::createByLinesIntersection()
146 {
147   // Get first line.
148   AttributeSelectionPtr aFirstLineSelection= selection(FIRST_LINE());
149   GeomShapePtr aFirstLineShape = aFirstLineSelection->value();
150   if(!aFirstLineShape.get()) {
151     aFirstLineShape = aFirstLineSelection->context()->shape();
152   }
153   std::shared_ptr<GeomAPI_Edge> aFirstEdge(new GeomAPI_Edge(aFirstLineShape));
154
155   // Get first line.
156   AttributeSelectionPtr aSecondLineSelection= selection(SECOND_LINE());
157   GeomShapePtr aSecondLineShape = aSecondLineSelection->value();
158   if(!aSecondLineShape.get()) {
159     aSecondLineShape = aSecondLineSelection->context()->shape();
160   }
161   std::shared_ptr<GeomAPI_Edge> aSecondEdge(new GeomAPI_Edge(aSecondLineShape));
162
163   return GeomAlgoAPI_PointBuilder::vertexByIntersection(aFirstEdge, aSecondEdge);
164 }
165
166 //==================================================================================================
167 std::shared_ptr<GeomAPI_Vertex> ConstructionPlugin_Point::createByLineAndPlaneIntersection()
168 {
169   // Get line.
170   AttributeSelectionPtr aLineSelection= selection(INTERSECTION_LINE());
171   GeomShapePtr aLineShape = aLineSelection->value();
172   if(!aLineShape.get()) {
173     aLineShape = aLineSelection->context()->shape();
174   }
175   std::shared_ptr<GeomAPI_Edge> anEdge(new GeomAPI_Edge(aLineShape));
176
177   // Get plane.
178   AttributeSelectionPtr aPlaneSelection= selection(INTERSECTION_PLANE());
179   GeomShapePtr aPlaneShape = aPlaneSelection->value();
180   if(!aPlaneShape.get()) {
181     aPlaneShape = aPlaneSelection->context()->shape();
182   }
183   std::shared_ptr<GeomAPI_Face> aFace(new GeomAPI_Face(aPlaneShape));
184
185   return GeomAlgoAPI_PointBuilder::vertexByIntersection(anEdge, aFace);
186 }