Salome HOME
ca427fe7de5930e12a5b2c886d5afd58384d0180
[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::EDGE && aType2 == GeomAPI_Shape::EDGE) {
74       // If both objects are edges then set by lines intersection.
75       setByLinesIntersection(theObject1, theObject2);
76     } */ else if(aType1 == GeomAPI_Shape::EDGE && aType2 == GeomAPI_Shape::FACE) {
77       // If first object is edge and second object is face then set by line and plane intersection.
78       setByLineAndPlaneIntersection(theObject1, theObject2);
79     }
80   }
81 }
82
83 //==================================================================================================
84 ConstructionAPI_Point::~ConstructionAPI_Point()
85 {
86
87 }
88
89 //==================================================================================================
90 void ConstructionAPI_Point::setByXYZ(const ModelHighAPI_Double& theX,
91                                      const ModelHighAPI_Double& theY,
92                                      const ModelHighAPI_Double& theZ)
93 {
94   //fillAttribute(ConstructionPlugin_Point::CREATION_METHOD_BY_XYZ(), mycreationMethod);
95   fillAttribute(theX, myx);
96   fillAttribute(theY, myy);
97   fillAttribute(theZ, myz);
98   fillAttribute(ConstructionPlugin_Point::CREATION_METHOD_BY_XYZ(), mycreationMethod);
99
100   execute(false);
101 }
102
103 //==================================================================================================
104 void ConstructionAPI_Point::setByOffsetOnEdge(const ModelHighAPI_Selection& theEdge,
105                                               const ModelHighAPI_Double& theOffset,
106                                               const bool theUseRatio,
107                                               const bool theReverse)
108 {
109   fillAttribute(ConstructionPlugin_Point::CREATION_METHOD_BY_DISTANCE_ON_EDGE(), mycreationMethod);
110   fillAttribute(theEdge, myedge);
111   if (theUseRatio) {
112     fillAttribute(ConstructionPlugin_Point::OFFSET_TYPE_BY_RATIO(), myoffsetType);
113     fillAttribute(theOffset, myratio);
114   }
115   else {
116     fillAttribute(ConstructionPlugin_Point::OFFSET_TYPE_BY_DISTANCE(), myoffsetType);
117     fillAttribute(theOffset, mydistance);
118   }
119   fillAttribute(theReverse, myreverse);
120
121   execute();
122 }
123
124 //==================================================================================================
125 void ConstructionAPI_Point::setByProjectionOnFace(const ModelHighAPI_Selection& theVertex,
126                                                   const ModelHighAPI_Selection& theFace)
127 {
128   fillAttribute(ConstructionPlugin_Point::CREATION_METHOD_BY_PROJECTION_ON_FACE(),
129                 mycreationMethod);
130   fillAttribute(theVertex, mypoinToProjectOnFace);
131   fillAttribute(theFace, myfaceForPointProjection);
132
133   execute();
134 }
135
136 /*
137 //==================================================================================================
138 void ConstructionAPI_Point::setByLinesIntersection(const ModelHighAPI_Selection& theEdge1,
139                                                    const ModelHighAPI_Selection& theEdge2)
140 {
141   fillAttribute(ConstructionPlugin_Point::CREATION_METHOD_BY_LINES_INTERSECTION(), mycreationMethod);
142   fillAttribute(theEdge1, myfirstLine);
143   fillAttribute(theEdge2, mysecondLine);
144
145   execute();
146 }
147 */
148
149 //==================================================================================================
150 void ConstructionAPI_Point::setByLineAndPlaneIntersection(const ModelHighAPI_Selection& theEdge,
151                                                           const ModelHighAPI_Selection& theFace)
152 {
153   fillAttribute(
154     ConstructionPlugin_Point::CREATION_METHOD_BY_LINE_AND_PLANE_INTERSECTION(), mycreationMethod);
155   fillAttribute(theEdge, myintersectionLine);
156   fillAttribute(theFace, myintersectionPlane);
157   fillAttribute("", useOffset()); // not used by default
158   execute();
159 }
160
161 //==================================================================================================
162 void ConstructionAPI_Point::dump(ModelHighAPI_Dumper& theDumper) const
163 {
164   FeaturePtr aBase = feature();
165   const std::string& aDocName = theDumper.name(aBase->document());
166   const std::string& aMeth = creationMethod()->value();
167
168   // common part
169   theDumper << aBase << " = model.addPoint(" << aDocName << ", ";
170
171   if (aMeth == "" || // default is XYZ
172       aMeth == ConstructionPlugin_Point::CREATION_METHOD_BY_XYZ()) {
173     theDumper << x() << ", " << y() << ", " << z();
174   } else if (aMeth == ConstructionPlugin_Point::CREATION_METHOD_BY_LINE_AND_PLANE_INTERSECTION()) {
175     theDumper << intersectionLine() << ", " <<intersectionPlane() ;
176     if (!useOffset()->value().empty()) { // call method with defined offset
177       theDumper << ", " << offset() << ", " << reverseOffset();
178     }
179   } else if (aMeth == ConstructionPlugin_Point::CREATION_METHOD_BY_DISTANCE_ON_EDGE()) {
180     theDumper << edge() << ", ";
181     if (offsetType()->value() == ConstructionPlugin_Point::OFFSET_TYPE_BY_DISTANCE()) {
182       theDumper << distance() << ", " << false;
183     }
184     else {
185       theDumper << ratio() << ", " << true;
186     }
187     theDumper << ", " << reverse()->value();
188   } else if (aMeth == ConstructionPlugin_Point::CREATION_METHOD_BY_PROJECTION_ON_FACE()) {
189     theDumper << mypoinToProjectOnFace << ", " << myfaceForPointProjection;
190   }
191
192   theDumper << ")" << std::endl;
193 }
194
195 //==================================================================================================
196 PointPtr addPoint(const std::shared_ptr<ModelAPI_Document>& thePart,
197                   const ModelHighAPI_Double& theX,
198                   const ModelHighAPI_Double& theY,
199                   const ModelHighAPI_Double& theZ)
200 {
201   std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(ConstructionAPI_Point::ID());
202   return PointPtr(new ConstructionAPI_Point(aFeature, theX, theY, theZ));
203 }
204
205 //==================================================================================================
206 PointPtr addPoint(const std::shared_ptr<ModelAPI_Document> & thePart,
207                   const ModelHighAPI_Selection& theEdge,
208                   const ModelHighAPI_Double& theOffset,
209                   const bool theUseRatio,
210                   const bool theReverse)
211 {
212   std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(ConstructionAPI_Point::ID());
213   return PointPtr(new ConstructionAPI_Point(aFeature, theEdge, theOffset, theUseRatio, theReverse));
214 }
215
216 //==================================================================================================
217 PointPtr addPoint(const std::shared_ptr<ModelAPI_Document> & thePart,
218                   const ModelHighAPI_Selection& theObject1,
219                   const ModelHighAPI_Selection& theObject2)
220 {
221   std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(ConstructionAPI_Point::ID());
222   return PointPtr(new ConstructionAPI_Point(aFeature, theObject1, theObject2));
223 }
224
225 //==================================================================================================
226 PointPtr addPoint(const std::shared_ptr<ModelAPI_Document> & thePart,
227                   const ModelHighAPI_Selection& theObject1,
228                   const ModelHighAPI_Selection& theObject2,
229                   const ModelHighAPI_Double& theDistanceValue,
230                   const bool theReverse)
231 {
232   std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(ConstructionAPI_Point::ID());
233   PointPtr anAPI(new ConstructionAPI_Point(aFeature, theObject1, theObject2));
234
235   fillAttribute(ConstructionPlugin_Point::USE_OFFSET(), anAPI->useOffset());
236   fillAttribute(theDistanceValue, anAPI->offset());
237   fillAttribute(theReverse, anAPI->reverseOffset());
238
239   return anAPI;
240 }