1 // Copyright (C) 2014-2017 CEA/DEN, EDF R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
21 #include "ConstructionPlugin_Point.h"
23 #include <ModelAPI_AttributeBoolean.h>
24 #include <ModelAPI_AttributeDouble.h>
25 #include <ModelAPI_AttributeSelection.h>
26 #include <ModelAPI_AttributeString.h>
27 #include <ModelAPI_ResultConstruction.h>
29 #include <GeomAlgoAPI_PointBuilder.h>
30 #include <GeomAlgoAPI_ShapeTools.h>
32 #include <GeomAPI_Edge.h>
33 #include <GeomAPI_Pnt.h>
34 #include <GeomAPI_Vertex.h>
35 #include <GeomAPI_Pln.h>
37 //==================================================================================================
38 ConstructionPlugin_Point::ConstructionPlugin_Point()
42 //==================================================================================================
43 const std::string& ConstructionPlugin_Point::getKind()
45 static std::string MY_KIND = ConstructionPlugin_Point::ID();
49 //==================================================================================================
50 void ConstructionPlugin_Point::initAttributes()
52 data()->addAttribute(X(), ModelAPI_AttributeDouble::typeId());
53 data()->addAttribute(Y(), ModelAPI_AttributeDouble::typeId());
54 data()->addAttribute(Z(), ModelAPI_AttributeDouble::typeId());
56 data()->addAttribute(CREATION_METHOD(), ModelAPI_AttributeString::typeId());
58 /*data()->addAttribute(EDGE(), ModelAPI_AttributeSelection::typeId());
59 data()->addAttribute(DISTANCE_VALUE(), ModelAPI_AttributeDouble::typeId());
60 data()->addAttribute(DISTANCE_PERCENT(), ModelAPI_AttributeBoolean::typeId());
61 data()->addAttribute(REVERSE(), ModelAPI_AttributeBoolean::typeId());
63 data()->addAttribute(POINT(), ModelAPI_AttributeSelection::typeId());
64 data()->addAttribute(PLANE(), ModelAPI_AttributeSelection::typeId());
66 data()->addAttribute(FIRST_LINE(), ModelAPI_AttributeSelection::typeId());
67 data()->addAttribute(SECOND_LINE(), ModelAPI_AttributeSelection::typeId());
69 data()->addAttribute(INTERSECTION_LINE(), ModelAPI_AttributeSelection::typeId());
70 data()->addAttribute(INTERSECTION_PLANE(), ModelAPI_AttributeSelection::typeId());
72 data()->addAttribute(USE_OFFSET(), ModelAPI_AttributeString::typeId());
73 data()->addAttribute(OFFSET(), ModelAPI_AttributeDouble::typeId());
74 data()->addAttribute(REVERSE_OFFSET(), ModelAPI_AttributeBoolean::typeId());
77 //==================================================================================================
78 void ConstructionPlugin_Point::execute()
82 // to support compatibility with old documents where aCreationMethod did not exist
83 std::string aCreationMethod =
84 string(CREATION_METHOD()).get() && !string(CREATION_METHOD())->value().empty() ?
85 string(CREATION_METHOD())->value() : CREATION_METHOD_BY_XYZ();
86 if(aCreationMethod == CREATION_METHOD_BY_XYZ()) {
87 aShape = createByXYZ();
88 }/* else if(aCreationMethod == CREATION_METHOD_BY_DISTANCE_ON_EDGE()) {
89 aShape = createByDistanceOnEdge();
90 } else if(aCreationMethod == CREATION_METHOD_BY_PROJECTION()) {
91 aShape = createByProjection();
92 } else if(aCreationMethod == CREATION_METHOD_BY_LINES_INTERSECTION()) {
93 aShape = createByLinesIntersection();
94 }*/ else if(aCreationMethod == CREATION_METHOD_BY_LINE_AND_PLANE_INTERSECTION()) {
95 // this may produce several points
96 std::list<std::shared_ptr<GeomAPI_Vertex> > aPoints = createByLineAndPlaneIntersection();
97 if (!aPoints.empty()) { // if no points found produce the standard error later
99 std::list<std::shared_ptr<GeomAPI_Vertex> >::iterator aPIter = aPoints.begin();
100 for(; aPIter != aPoints.end(); aPIter++, anIndex++) {
101 std::shared_ptr<ModelAPI_ResultConstruction> aConstr =
102 document()->createConstruction(data(), anIndex);
103 aConstr->setShape(*aPIter);
104 setResult(aConstr, anIndex);
106 removeResults(anIndex);
112 setError("Error: intersection not found.");
116 removeResults(1); // for case the point type was switched from multi-results type
117 std::shared_ptr<ModelAPI_ResultConstruction> aConstr = document()->createConstruction(data());
118 aConstr->setShape(aShape);
122 //==================================================================================================
123 bool ConstructionPlugin_Point::customisePresentation(ResultPtr theResult,
125 std::shared_ptr<GeomAPI_ICustomPrs> theDefaultPrs)
127 bool isCustomized = theDefaultPrs.get() != NULL &&
128 theDefaultPrs->customisePresentation(theResult, thePrs, theDefaultPrs);
129 //thePrs->setPointMarker(1, 1.); // Set point as a '+' symbol
133 //==================================================================================================
134 std::shared_ptr<GeomAPI_Vertex> ConstructionPlugin_Point::createByXYZ()
136 return GeomAlgoAPI_PointBuilder::vertex(real(X())->value(),
141 /*//==================================================================================================
142 std::shared_ptr<GeomAPI_Vertex> ConstructionPlugin_Point::createByDistanceOnEdge()
145 AttributeSelectionPtr anEdgeSelection = selection(EDGE());
146 GeomShapePtr aShape = anEdgeSelection->value();
148 aShape = anEdgeSelection->context()->shape();
150 std::shared_ptr<GeomAPI_Edge> anEdge(new GeomAPI_Edge(aShape));
152 // Get distance value and percent flag.
153 double aValue = real(DISTANCE_VALUE())->value();
154 bool anIsPercent = boolean(DISTANCE_PERCENT())->value();
157 bool anIsReverse = boolean(REVERSE())->value();
159 return GeomAlgoAPI_PointBuilder::vertexOnEdge(anEdge, aValue, anIsPercent, anIsReverse);
162 //==================================================================================================
163 std::shared_ptr<GeomAPI_Vertex> ConstructionPlugin_Point::createByProjection()
166 AttributeSelectionPtr aPointSelection = selection(POINT());
167 GeomShapePtr aPointShape = aPointSelection->value();
168 if(!aPointShape.get()) {
169 aPointShape = aPointSelection->context()->shape();
171 std::shared_ptr<GeomAPI_Vertex> aVertex(new GeomAPI_Vertex(aPointShape));
174 AttributeSelectionPtr aPlaneSelection = selection(PLANE());
175 GeomShapePtr aPlaneShape = aPlaneSelection->value();
176 if(!aPlaneShape.get()) {
177 aPlaneShape = aPlaneSelection->context()->shape();
179 std::shared_ptr<GeomAPI_Face> aFace(new GeomAPI_Face(aPlaneShape));
181 return GeomAlgoAPI_PointBuilder::vertexByProjection(aVertex, aFace);
184 //==================================================================================================
185 std::shared_ptr<GeomAPI_Vertex> ConstructionPlugin_Point::createByLinesIntersection()
188 AttributeSelectionPtr aFirstLineSelection= selection(FIRST_LINE());
189 GeomShapePtr aFirstLineShape = aFirstLineSelection->value();
190 if(!aFirstLineShape.get()) {
191 aFirstLineShape = aFirstLineSelection->context()->shape();
193 std::shared_ptr<GeomAPI_Edge> aFirstEdge(new GeomAPI_Edge(aFirstLineShape));
196 AttributeSelectionPtr aSecondLineSelection= selection(SECOND_LINE());
197 GeomShapePtr aSecondLineShape = aSecondLineSelection->value();
198 if(!aSecondLineShape.get()) {
199 aSecondLineShape = aSecondLineSelection->context()->shape();
201 std::shared_ptr<GeomAPI_Edge> aSecondEdge(new GeomAPI_Edge(aSecondLineShape));
203 return GeomAlgoAPI_PointBuilder::vertexByIntersection(aFirstEdge, aSecondEdge);
207 //==================================================================================================
208 std::list<std::shared_ptr<GeomAPI_Vertex> >
209 ConstructionPlugin_Point::createByLineAndPlaneIntersection()
212 AttributeSelectionPtr aLineSelection = selection(INTERSECTION_LINE());
213 GeomShapePtr aLineShape = aLineSelection->value();
214 if(!aLineShape.get()) {
215 aLineShape = aLineSelection->context()->shape();
217 std::shared_ptr<GeomAPI_Edge> anEdge(new GeomAPI_Edge(aLineShape));
220 AttributeSelectionPtr aPlaneSelection= selection(INTERSECTION_PLANE());
221 GeomShapePtr aPlaneShape = aPlaneSelection->value();
222 if(!aPlaneShape.get()) {
223 aPlaneShape = aPlaneSelection->context()->shape();
225 std::shared_ptr<GeomAPI_Face> aFace(new GeomAPI_Face(aPlaneShape));
227 if (!string(USE_OFFSET())->value().empty()) {
228 double anOffset = real(OFFSET())->value();
229 if (boolean(REVERSE_OFFSET())->value())
230 anOffset = -anOffset;
231 if (fabs(anOffset) > 1.e-9) { // move face
232 aFace->translate(aFace->getPlane()->direction(), anOffset);
236 return GeomAlgoAPI_ShapeTools::intersect(anEdge, aFace,
237 aPlaneSelection->context()->groupName() == ModelAPI_ResultConstruction::group());