Salome HOME
2.3.3.1 Point creation by projection of another point on a line
[modules/shaper.git] / src / ConstructionAPI / ConstructionAPI_Point.cpp
1 // Copyright (C) 2014-2017  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
19 //
20
21 #include "ConstructionAPI_Point.h"
22
23 #include <GeomAPI_Shape.h>
24
25 #include <ModelHighAPI_Dumper.h>
26 #include <ModelHighAPI_Selection.h>
27 #include <ModelHighAPI_Tools.h>
28
29 //==================================================================================================
30 ConstructionAPI_Point::ConstructionAPI_Point(const std::shared_ptr<ModelAPI_Feature>& theFeature)
31 : ModelHighAPI_Interface(theFeature)
32 {
33   initialize();
34 }
35
36 //==================================================================================================
37 ConstructionAPI_Point::ConstructionAPI_Point(const std::shared_ptr<ModelAPI_Feature>& theFeature,
38                                              const ModelHighAPI_Double& theX,
39                                              const ModelHighAPI_Double& theY,
40                                              const ModelHighAPI_Double& theZ)
41 : ModelHighAPI_Interface(theFeature)
42 {
43   if(initialize()) {
44     setByXYZ(theX, theY, theZ);
45   }
46 }
47
48 //==================================================================================================
49 ConstructionAPI_Point::ConstructionAPI_Point(const std::shared_ptr<ModelAPI_Feature>& theFeature,
50                                              const ModelHighAPI_Selection& theEdge,
51                                              const ModelHighAPI_Double& theOffset,
52                                              const bool theUseRatio,
53                                              const bool theReverse)
54 : ModelHighAPI_Interface(theFeature)
55 {
56   if(initialize()) {
57     setByOffsetOnEdge(theEdge, theOffset, theUseRatio, theReverse);
58   }
59 }
60
61 //==================================================================================================
62 ConstructionAPI_Point::ConstructionAPI_Point(const std::shared_ptr<ModelAPI_Feature>& theFeature,
63                                              const ModelHighAPI_Selection& theObject1,
64                                              const ModelHighAPI_Selection& theObject2)
65 : ModelHighAPI_Interface(theFeature)
66 {
67   if(initialize()) {
68     GeomAPI_Shape::ShapeType aType1 = getShapeType(theObject1);
69     GeomAPI_Shape::ShapeType aType2 = getShapeType(theObject2);
70     if(aType1 == GeomAPI_Shape::VERTEX && aType2 == GeomAPI_Shape::FACE) {
71       // If first object is vertex and second object is face then set by projection.
72       setByProjectionOnFace(theObject1, theObject2);
73     } else if (aType1 == GeomAPI_Shape::VERTEX && aType2 == GeomAPI_Shape::EDGE) {
74       // If first object is vertex and second object is edge then set by projection.
75       setByProjectionOnEdge(theObject1, theObject2);
76     } /* else if(aType1 == GeomAPI_Shape::EDGE && aType2 == GeomAPI_Shape::EDGE) {
77       // If both objects are edges then set by lines intersection.
78       setByLinesIntersection(theObject1, theObject2);
79     } */ else if(aType1 == GeomAPI_Shape::EDGE && aType2 == GeomAPI_Shape::FACE) {
80       // If first object is edge and second object is face then set by line and plane intersection.
81       setByLineAndPlaneIntersection(theObject1, theObject2);
82     }
83   }
84 }
85
86 //==================================================================================================
87 ConstructionAPI_Point::~ConstructionAPI_Point()
88 {
89
90 }
91
92 //==================================================================================================
93 void ConstructionAPI_Point::setByXYZ(const ModelHighAPI_Double& theX,
94                                      const ModelHighAPI_Double& theY,
95                                      const ModelHighAPI_Double& theZ)
96 {
97   //fillAttribute(ConstructionPlugin_Point::CREATION_METHOD_BY_XYZ(), mycreationMethod);
98   fillAttribute(theX, myx);
99   fillAttribute(theY, myy);
100   fillAttribute(theZ, myz);
101   fillAttribute(ConstructionPlugin_Point::CREATION_METHOD_BY_XYZ(), mycreationMethod);
102
103   execute(false);
104 }
105
106 //==================================================================================================
107 void ConstructionAPI_Point::setByOffsetOnEdge(const ModelHighAPI_Selection& theEdge,
108                                               const ModelHighAPI_Double& theOffset,
109                                               const bool theUseRatio,
110                                               const bool theReverse)
111 {
112   fillAttribute(ConstructionPlugin_Point::CREATION_METHOD_BY_DISTANCE_ON_EDGE(), mycreationMethod);
113   fillAttribute(theEdge, myedge);
114   if (theUseRatio) {
115     fillAttribute(ConstructionPlugin_Point::OFFSET_TYPE_BY_RATIO(), myoffsetType);
116     fillAttribute(theOffset, myratio);
117   }
118   else {
119     fillAttribute(ConstructionPlugin_Point::OFFSET_TYPE_BY_DISTANCE(), myoffsetType);
120     fillAttribute(theOffset, mydistance);
121   }
122   fillAttribute(theReverse, myreverse);
123
124   execute();
125 }
126
127 //==================================================================================================
128 void ConstructionAPI_Point::setByProjectionOnEdge(const ModelHighAPI_Selection& theVertex,
129                                                   const ModelHighAPI_Selection& theEdge)
130 {
131   fillAttribute(ConstructionPlugin_Point::CREATION_METHOD_BY_PROJECTION_ON_EDGE(),
132                 mycreationMethod);
133   fillAttribute(theVertex, mypoinToProjectOnEdge);
134   fillAttribute(theEdge, myedgeForPointProjection);
135
136   execute();
137 }
138
139 //==================================================================================================
140 void ConstructionAPI_Point::setByProjectionOnFace(const ModelHighAPI_Selection& theVertex,
141                                                   const ModelHighAPI_Selection& theFace)
142 {
143   fillAttribute(ConstructionPlugin_Point::CREATION_METHOD_BY_PROJECTION_ON_FACE(),
144                 mycreationMethod);
145   fillAttribute(theVertex, mypoinToProjectOnFace);
146   fillAttribute(theFace, myfaceForPointProjection);
147
148   execute();
149 }
150
151 /*
152 //==================================================================================================
153 void ConstructionAPI_Point::setByLinesIntersection(const ModelHighAPI_Selection& theEdge1,
154                                                    const ModelHighAPI_Selection& theEdge2)
155 {
156   fillAttribute(ConstructionPlugin_Point::CREATION_METHOD_BY_LINES_INTERSECTION(), mycreationMethod);
157   fillAttribute(theEdge1, myfirstLine);
158   fillAttribute(theEdge2, mysecondLine);
159
160   execute();
161 }
162 */
163
164 //==================================================================================================
165 void ConstructionAPI_Point::setByLineAndPlaneIntersection(const ModelHighAPI_Selection& theEdge,
166                                                           const ModelHighAPI_Selection& theFace)
167 {
168   fillAttribute(
169     ConstructionPlugin_Point::CREATION_METHOD_BY_LINE_AND_PLANE_INTERSECTION(), mycreationMethod);
170   fillAttribute(theEdge, myintersectionLine);
171   fillAttribute(theFace, myintersectionPlane);
172   fillAttribute("", useOffset()); // not used by default
173   execute();
174 }
175
176 //==================================================================================================
177 void ConstructionAPI_Point::dump(ModelHighAPI_Dumper& theDumper) const
178 {
179   FeaturePtr aBase = feature();
180   const std::string& aDocName = theDumper.name(aBase->document());
181   const std::string& aMeth = creationMethod()->value();
182
183   // common part
184   theDumper << aBase << " = model.addPoint(" << aDocName << ", ";
185
186   if (aMeth == "" || // default is XYZ
187       aMeth == ConstructionPlugin_Point::CREATION_METHOD_BY_XYZ()) {
188     theDumper << x() << ", " << y() << ", " << z();
189   } else if (aMeth == ConstructionPlugin_Point::CREATION_METHOD_BY_LINE_AND_PLANE_INTERSECTION()) {
190     theDumper << intersectionLine() << ", " <<intersectionPlane() ;
191     if (!useOffset()->value().empty()) { // call method with defined offset
192       theDumper << ", " << offset() << ", " << reverseOffset();
193     }
194   } else if (aMeth == ConstructionPlugin_Point::CREATION_METHOD_BY_DISTANCE_ON_EDGE()) {
195     theDumper << edge() << ", ";
196     if (offsetType()->value() == ConstructionPlugin_Point::OFFSET_TYPE_BY_DISTANCE()) {
197       theDumper << distance() << ", " << false;
198     }
199     else {
200       theDumper << ratio() << ", " << true;
201     }
202     theDumper << ", " << reverse()->value();
203   } else if (aMeth == ConstructionPlugin_Point::CREATION_METHOD_BY_PROJECTION_ON_EDGE()) {
204     theDumper << mypoinToProjectOnEdge << ", " << myedgeForPointProjection;
205   } else if (aMeth == ConstructionPlugin_Point::CREATION_METHOD_BY_PROJECTION_ON_FACE()) {
206     theDumper << mypoinToProjectOnFace << ", " << myfaceForPointProjection;
207   }
208
209   theDumper << ")" << std::endl;
210 }
211
212 //==================================================================================================
213 PointPtr addPoint(const std::shared_ptr<ModelAPI_Document>& thePart,
214                   const ModelHighAPI_Double& theX,
215                   const ModelHighAPI_Double& theY,
216                   const ModelHighAPI_Double& theZ)
217 {
218   std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(ConstructionAPI_Point::ID());
219   return PointPtr(new ConstructionAPI_Point(aFeature, theX, theY, theZ));
220 }
221
222 //==================================================================================================
223 PointPtr addPoint(const std::shared_ptr<ModelAPI_Document> & thePart,
224                   const ModelHighAPI_Selection& theEdge,
225                   const ModelHighAPI_Double& theOffset,
226                   const bool theUseRatio,
227                   const bool theReverse)
228 {
229   std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(ConstructionAPI_Point::ID());
230   return PointPtr(new ConstructionAPI_Point(aFeature, theEdge, theOffset, theUseRatio, theReverse));
231 }
232
233 //==================================================================================================
234 PointPtr addPoint(const std::shared_ptr<ModelAPI_Document> & thePart,
235                   const ModelHighAPI_Selection& theObject1,
236                   const ModelHighAPI_Selection& theObject2)
237 {
238   std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(ConstructionAPI_Point::ID());
239   return PointPtr(new ConstructionAPI_Point(aFeature, theObject1, theObject2));
240 }
241
242 //==================================================================================================
243 PointPtr addPoint(const std::shared_ptr<ModelAPI_Document> & thePart,
244                   const ModelHighAPI_Selection& theObject1,
245                   const ModelHighAPI_Selection& theObject2,
246                   const ModelHighAPI_Double& theDistanceValue,
247                   const bool theReverse)
248 {
249   std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(ConstructionAPI_Point::ID());
250   PointPtr anAPI(new ConstructionAPI_Point(aFeature, theObject1, theObject2));
251
252   fillAttribute(ConstructionPlugin_Point::USE_OFFSET(), anAPI->useOffset());
253   fillAttribute(theDistanceValue, anAPI->offset());
254   fillAttribute(theReverse, anAPI->reverseOffset());
255
256   return anAPI;
257 }