Salome HOME
Added option to create Construction Point by intersection of two lines.
[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
54 //==================================================================================================
55 void ConstructionPlugin_Point::execute()
56 {
57   GeomShapePtr aShape;
58
59   std::string aCreationMethod = string(CREATION_METHOD())->value();
60   if(aCreationMethod == CREATION_METHOD_BY_XYZ()) {
61     aShape = createByXYZ();
62   } else if(aCreationMethod == CREATION_METHOD_BY_DISTANCE_ON_EDGE()) {
63     aShape = createByDistanceOnEdge();
64   } else if(aCreationMethod == CREATION_METHOD_BY_PROJECTION()) {
65     aShape = createByProjection();
66   } else if(aCreationMethod == CREATION_METHOD_BY_LINES_INTERSECTION()) {
67     aShape = createByIntersection();
68   }
69
70   if(aShape.get()) {
71     std::shared_ptr<ModelAPI_ResultConstruction> aConstr = document()->createConstruction(data());
72     aConstr->setShape(aShape);
73     setResult(aConstr);
74   }
75 }
76
77 //==================================================================================================
78 bool ConstructionPlugin_Point::customisePresentation(ResultPtr theResult,
79                                                      AISObjectPtr thePrs,
80                                                      std::shared_ptr<GeomAPI_ICustomPrs> theDefaultPrs)
81 {
82   bool isCustomized = theDefaultPrs.get() != NULL &&
83                       theDefaultPrs->customisePresentation(theResult, thePrs, theDefaultPrs);
84   //thePrs->setPointMarker(1, 1.); // Set point as a '+' symbol
85   return true;
86 }
87
88 //==================================================================================================
89 std::shared_ptr<GeomAPI_Vertex> ConstructionPlugin_Point::createByXYZ()
90 {
91   return GeomAlgoAPI_PointBuilder::vertex(real(X())->value(),
92                                           real(Y())->value(),
93                                           real(Z())->value());
94 }
95
96 //==================================================================================================
97 std::shared_ptr<GeomAPI_Vertex> ConstructionPlugin_Point::createByDistanceOnEdge()
98 {
99   // Get edge.
100   AttributeSelectionPtr anEdgeSelection = selection(EDGE());
101   GeomShapePtr aShape = anEdgeSelection->value();
102   if(!aShape.get()) {
103     aShape = anEdgeSelection->context()->shape();
104   }
105   std::shared_ptr<GeomAPI_Edge> anEdge(new GeomAPI_Edge(aShape));
106
107   // Get distance value and percent flag.
108   double aValue = real(DISTANCE_VALUE())->value();
109   bool anIsPercent = boolean(DISTANCE_PERCENT())->value();
110
111   // Get reverse flag.
112   bool anIsReverse = boolean(REVERSE())->value();
113
114   return GeomAlgoAPI_PointBuilder::vertexOnEdge(anEdge, aValue, anIsPercent, anIsReverse);
115 }
116
117 //==================================================================================================
118 std::shared_ptr<GeomAPI_Vertex> ConstructionPlugin_Point::createByProjection()
119 {
120   // Get point.
121   AttributeSelectionPtr aPointSelection = selection(POINT());
122   GeomShapePtr aPointShape = aPointSelection->value();
123   if(!aPointShape.get()) {
124     aPointShape = aPointSelection->context()->shape();
125   }
126   std::shared_ptr<GeomAPI_Vertex> aVertex(new GeomAPI_Vertex(aPointShape));
127
128   // Get plane.
129   AttributeSelectionPtr aPlaneSelection = selection(PLANE());
130   GeomShapePtr aPlaneShape = aPlaneSelection->value();
131   if(!aPlaneShape.get()) {
132     aPlaneShape = aPlaneSelection->context()->shape();
133   }
134   std::shared_ptr<GeomAPI_Face> aFace(new GeomAPI_Face(aPlaneShape));
135
136   return GeomAlgoAPI_PointBuilder::vertexByProjection(aVertex, aFace);
137 }
138
139 //==================================================================================================
140 std::shared_ptr<GeomAPI_Vertex> ConstructionPlugin_Point::createByIntersection()
141 {
142   // Get first line.
143   AttributeSelectionPtr aFirstLineSelection= selection(FIRST_LINE());
144   GeomShapePtr aFirstLineShape = aFirstLineSelection->value();
145   if(!aFirstLineShape.get()) {
146     aFirstLineShape = aFirstLineSelection->context()->shape();
147   }
148   std::shared_ptr<GeomAPI_Edge> aFirstEdge(new GeomAPI_Edge(aFirstLineShape));
149
150   // Get first line.
151   AttributeSelectionPtr aSecondLineSelection= selection(SECOND_LINE());
152   GeomShapePtr aSecondLineShape = aSecondLineSelection->value();
153   if(!aSecondLineShape.get()) {
154     aSecondLineShape = aSecondLineSelection->context()->shape();
155   }
156   std::shared_ptr<GeomAPI_Edge> aSecondEdge(new GeomAPI_Edge(aSecondLineShape));
157
158   return GeomAlgoAPI_PointBuilder::vertexByIntersection(aFirstEdge, aSecondEdge);
159 }