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_Plane.h"
23 #include <Config_PropManager.h>
25 #include <GeomAlgoAPI_FaceBuilder.h>
26 #include <GeomAlgoAPI_Rotation.h>
27 #include <GeomAlgoAPI_ShapeTools.h>
29 #include <GeomAPI_Ax1.h>
30 #include <GeomAPI_Edge.h>
31 #include <GeomAPI_Face.h>
32 #include <GeomAPI_Lin.h>
33 #include <GeomAPI_Pln.h>
34 #include <GeomAPI_Pnt.h>
35 #include <GeomAPI_Pnt2d.h>
36 #include <GeomAPI_Vertex.h>
37 #include <GeomAPI_XYZ.h>
39 #include <ModelAPI_AttributeDouble.h>
40 #include <ModelAPI_AttributeIntArray.h>
41 #include <ModelAPI_AttributeSelection.h>
42 #include <ModelAPI_AttributeString.h>
43 #include <ModelAPI_AttributeBoolean.h>
44 #include <ModelAPI_ResultConstruction.h>
45 #include <ModelAPI_Session.h>
46 #include <ModelAPI_Validator.h>
48 static GeomShapePtr faceByThreeVertices(const std::shared_ptr<GeomAPI_Vertex> theV1,
49 const std::shared_ptr<GeomAPI_Vertex> theV2,
50 const std::shared_ptr<GeomAPI_Vertex> theV3);
51 static std::shared_ptr<GeomAPI_Face> makeRectangularFace(
52 const std::shared_ptr<GeomAPI_Face> theFace,
53 const std::shared_ptr<GeomAPI_Pln> thePln);
55 //==================================================================================================
56 ConstructionPlugin_Plane::ConstructionPlugin_Plane()
60 //==================================================================================================
61 void ConstructionPlugin_Plane::initAttributes()
63 data()->addAttribute(ConstructionPlugin_Plane::CREATION_METHOD(),
64 ModelAPI_AttributeString::typeId());
66 data()->addAttribute(PLANE(), ModelAPI_AttributeSelection::typeId());
67 data()->addAttribute(DISTANCE(), ModelAPI_AttributeDouble::typeId());
68 // By general equation.
69 data()->addAttribute(A(), ModelAPI_AttributeDouble::typeId());
70 data()->addAttribute(B(), ModelAPI_AttributeDouble::typeId());
71 data()->addAttribute(C(), ModelAPI_AttributeDouble::typeId());
72 data()->addAttribute(D(), ModelAPI_AttributeDouble::typeId());
73 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), A());
74 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), B());
75 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), C());
76 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), D());
79 data()->addAttribute(POINT1(), ModelAPI_AttributeSelection::typeId());
80 data()->addAttribute(POINT2(), ModelAPI_AttributeSelection::typeId());
81 data()->addAttribute(POINT3(), ModelAPI_AttributeSelection::typeId());
84 data()->addAttribute(LINE(), ModelAPI_AttributeSelection::typeId());
85 data()->addAttribute(POINT(), ModelAPI_AttributeSelection::typeId());
86 data()->addAttribute(PERPENDICULAR(), ModelAPI_AttributeBoolean::typeId());
89 data()->addAttribute(CREATION_METHOD_BY_OTHER_PLANE_OPTION(), ModelAPI_AttributeString::typeId());
90 data()->addAttribute(REVERSE(), ModelAPI_AttributeBoolean::typeId());
91 data()->addAttribute(COINCIDENT_POINT(), ModelAPI_AttributeSelection::typeId());
92 data()->addAttribute(AXIS(), ModelAPI_AttributeSelection::typeId());
93 data()->addAttribute(ANGLE(), ModelAPI_AttributeDouble::typeId());
95 // By two parallel planes.
96 data()->addAttribute(PLANE1(), ModelAPI_AttributeSelection::typeId());
97 data()->addAttribute(PLANE2(), ModelAPI_AttributeSelection::typeId());
100 //==================================================================================================
101 void ConstructionPlugin_Plane::execute()
105 std::string aCreationMethod = string(CREATION_METHOD())->value();
106 if(aCreationMethod == CREATION_METHOD_BY_GENERAL_EQUATION() ||
107 aCreationMethod == "PlaneByGeneralEquation") {
108 aShape = createByGeneralEquation();
109 } else if(aCreationMethod == CREATION_METHOD_BY_THREE_POINTS()) {
110 aShape = createByThreePoints();
111 } else if(aCreationMethod == CREATION_METHOD_BY_LINE_AND_POINT()) {
112 aShape = createByLineAndPoint();
113 } else if(aCreationMethod == CREATION_METHOD_BY_OTHER_PLANE()) {
114 std::string aCreationMethodOption = string(CREATION_METHOD_BY_OTHER_PLANE_OPTION())->value();
115 if(aCreationMethodOption == CREATION_METHOD_BY_DISTANCE_FROM_OTHER()) {
116 aShape = createByDistanceFromOther();
117 } else if(aCreationMethodOption == CREATION_METHOD_BY_COINCIDENT_TO_POINT()) {
118 aShape = createByCoincidentPoint();
119 } else if(aCreationMethodOption == CREATION_METHOD_BY_ROTATION()) {
120 aShape = createByRotation();
122 } else if(aCreationMethod == CREATION_METHOD_BY_TWO_PARALLEL_PLANES()) {
123 aShape = createByTwoParallelPlanes();
125 setError("Error: Plane creation method \"" + aCreationMethod + "\" not supported.");
130 setError("Error: Could not create a plane.");
134 ResultConstructionPtr aConstr = document()->createConstruction(data());
135 aConstr->setInfinite(true);
136 aConstr->setShape(aShape);
140 //==================================================================================================
141 bool ConstructionPlugin_Plane::customisePresentation(ResultPtr theResult, AISObjectPtr thePrs,
142 std::shared_ptr<GeomAPI_ICustomPrs> theDefaultPrs)
144 std::vector<int> aColor;
145 // get color from the attribute of the result
146 if (theResult.get() != NULL &&
147 theResult->data()->attribute(ModelAPI_Result::COLOR_ID()).get() != NULL) {
148 AttributeIntArrayPtr aColorAttr = theResult->data()->intArray(ModelAPI_Result::COLOR_ID());
149 if (aColorAttr.get() && aColorAttr->size()) {
150 aColor.push_back(aColorAttr->value(0));
151 aColor.push_back(aColorAttr->value(1));
152 aColor.push_back(aColorAttr->value(2));
156 aColor = Config_PropManager::color("Visualization", "construction_plane_color");
158 bool isCustomized = false;
159 if (aColor.size() == 3)
160 isCustomized = thePrs->setColor(aColor[0], aColor[1], aColor[2]);
162 isCustomized = thePrs->setTransparensy(0.6) || isCustomized;
167 //==================================================================================================
168 std::shared_ptr<GeomAPI_Shape> ConstructionPlugin_Plane::createByGeneralEquation()
170 AttributeDoublePtr anAttrA = real(ConstructionPlugin_Plane::A());
171 AttributeDoublePtr anAttrB = real(ConstructionPlugin_Plane::B());
172 AttributeDoublePtr anAttrC = real(ConstructionPlugin_Plane::C());
173 AttributeDoublePtr anAttrD = real(ConstructionPlugin_Plane::D());
174 std::shared_ptr<GeomAPI_Shape> aPlaneFace;
175 if ((anAttrA.get() != NULL) && (anAttrB.get() != NULL) &&
176 (anAttrC.get() != NULL) && (anAttrD.get() != NULL) &&
177 anAttrA->isInitialized() && anAttrB->isInitialized() &&
178 anAttrC->isInitialized() && anAttrD->isInitialized() ) {
179 double aA = anAttrA->value(), aB = anAttrB->value(),
180 aC = anAttrC->value(), aD = anAttrD->value();
181 std::shared_ptr<GeomAPI_Pln> aPlane =
182 std::shared_ptr<GeomAPI_Pln>(new GeomAPI_Pln(aA, aB, aC, aD));
183 double aSize = Config_PropManager::integer(SKETCH_TAB_NAME, "planes_size");
185 aPlaneFace = GeomAlgoAPI_FaceBuilder::squareFace(aPlane, aSize);
190 //==================================================================================================
191 std::shared_ptr<GeomAPI_Shape> ConstructionPlugin_Plane::createByThreePoints()
194 AttributeSelectionPtr aPointSelection1 = selection(POINT1());
195 GeomShapePtr aPointShape1 = aPointSelection1->value();
196 if(!aPointShape1.get()) {
197 aPointShape1 = aPointSelection1->context()->shape();
199 std::shared_ptr<GeomAPI_Vertex> aVertex1(new GeomAPI_Vertex(aPointShape1));
202 AttributeSelectionPtr aPointSelection2 = selection(POINT2());
203 GeomShapePtr aPointShape2 = aPointSelection2->value();
204 if(!aPointShape2.get()) {
205 aPointShape2 = aPointSelection2->context()->shape();
207 std::shared_ptr<GeomAPI_Vertex> aVertex2(new GeomAPI_Vertex(aPointShape2));
210 AttributeSelectionPtr aPointSelection3 = selection(POINT3());
211 GeomShapePtr aPointShape3 = aPointSelection3->value();
212 if(!aPointShape3.get()) {
213 aPointShape3 = aPointSelection3->context()->shape();
215 std::shared_ptr<GeomAPI_Vertex> aVertex3(new GeomAPI_Vertex(aPointShape3));
217 GeomShapePtr aRes = faceByThreeVertices(aVertex1, aVertex2, aVertex3);
222 //==================================================================================================
223 std::shared_ptr<GeomAPI_Shape> ConstructionPlugin_Plane::createByLineAndPoint()
226 AttributeSelectionPtr anEdgeSelection = selection(LINE());
227 GeomShapePtr aLineShape = anEdgeSelection->value();
228 if(!aLineShape.get()) {
229 aLineShape = anEdgeSelection->context()->shape();
231 std::shared_ptr<GeomAPI_Edge> anEdge(new GeomAPI_Edge(aLineShape));
234 AttributeSelectionPtr aPointSelection = selection(POINT());
235 GeomShapePtr aPointShape = aPointSelection->value();
236 if(!aPointShape.get()) {
237 aPointShape = aPointSelection->context()->shape();
239 std::shared_ptr<GeomAPI_Vertex> aVertex(new GeomAPI_Vertex(aPointShape));
241 // Get perpendicular flag.
242 bool anIsPerpendicular= boolean(PERPENDICULAR())->value();
245 if(anIsPerpendicular) {
246 std::shared_ptr<GeomAPI_Lin> aLin = anEdge->line();
247 std::shared_ptr<GeomAPI_Pnt> aPnt = aVertex->point();
248 std::shared_ptr<GeomAPI_Pln> aNewPln(new GeomAPI_Pln(aPnt, aLin->direction()));
249 double aSize = aLin->distance(aPnt) * 2;
250 // point may belong to line, so for the face size use maximum distance between point and line
251 // and the line size (distance between the start and end point)
252 double aDistance = anEdge->firstPoint()->distance(anEdge->lastPoint());
253 aRes = GeomAlgoAPI_FaceBuilder::squareFace(aNewPln, aSize > aDistance ? aSize : aDistance);
255 std::shared_ptr<GeomAPI_Vertex> aV1, aV2;
256 GeomAlgoAPI_ShapeTools::findBounds(anEdge, aV1, aV2);
257 aRes = faceByThreeVertices(aV1, aV2, aVertex);
263 //==================================================================================================
264 std::shared_ptr<GeomAPI_Shape> ConstructionPlugin_Plane::createByDistanceFromOther()
266 AttributeSelectionPtr aFaceAttr = data()->selection(ConstructionPlugin_Plane::PLANE());
267 AttributeDoublePtr aDistAttr = data()->real(ConstructionPlugin_Plane::DISTANCE());
268 std::shared_ptr<GeomAPI_Shape> aPlane;
269 if ((aFaceAttr.get() != NULL) &&
270 (aDistAttr.get() != NULL) &&
271 aFaceAttr->isInitialized() && aDistAttr->isInitialized()) {
273 double aDist = aDistAttr->value();
274 bool anIsReverse = boolean(REVERSE())->value();
275 if(anIsReverse) aDist = -aDist;
276 GeomShapePtr aShape = aFaceAttr->value();
278 aShape = aFaceAttr->context()->shape();
285 std::shared_ptr<GeomAPI_Face> aFace(new GeomAPI_Face(aShape));
287 std::shared_ptr<GeomAPI_Pln> aPln = aFace->getPlane();
288 std::shared_ptr<GeomAPI_Pnt> aOrig = aPln->location();
289 std::shared_ptr<GeomAPI_Dir> aDir = aPln->direction();
291 aOrig->translate(aDir, aDist);
292 std::shared_ptr<GeomAPI_Pln> aNewPln(new GeomAPI_Pln(aOrig, aDir));
294 aPlane = makeRectangularFace(aFace, aNewPln);
299 //==================================================================================================
300 std::shared_ptr<GeomAPI_Shape> ConstructionPlugin_Plane::createByCoincidentPoint()
303 AttributeSelectionPtr aFaceSelection = selection(PLANE());
304 GeomShapePtr aFaceShape = aFaceSelection->value();
305 if(!aFaceShape.get()) {
306 aFaceShape = aFaceSelection->context()->shape();
308 std::shared_ptr<GeomAPI_Face> aFace(new GeomAPI_Face(aFaceShape));
311 AttributeSelectionPtr aPointSelection = selection(COINCIDENT_POINT());
312 GeomShapePtr aPointShape = aPointSelection->value();
313 if(!aPointShape.get()) {
314 aPointShape = aPointSelection->context()->shape();
316 std::shared_ptr<GeomAPI_Vertex> aVertex(new GeomAPI_Vertex(aPointShape));
318 std::shared_ptr<GeomAPI_Pnt> anOrig = aVertex->point();
319 std::shared_ptr<GeomAPI_Pln> aPln = aFace->getPlane();
320 std::shared_ptr<GeomAPI_Dir> aDir = aPln->direction();
322 std::shared_ptr<GeomAPI_Pln> aNewPln(new GeomAPI_Pln(anOrig, aDir));
324 return makeRectangularFace(aFace, aNewPln);
327 //==================================================================================================
328 std::shared_ptr<GeomAPI_Shape> ConstructionPlugin_Plane::createByRotation()
331 AttributeSelectionPtr aFaceSelection = selection(PLANE());
332 GeomShapePtr aFaceShape = aFaceSelection->value();
333 if(!aFaceShape.get()) {
334 aFaceShape = aFaceSelection->context()->shape();
336 std::shared_ptr<GeomAPI_Face> aFace(new GeomAPI_Face(aFaceShape));
337 aFace = makeRectangularFace(aFace, aFace->getPlane());
340 AttributeSelectionPtr anAxisSelection = selection(AXIS());
341 GeomShapePtr anAxisShape = anAxisSelection->value();
342 if(!anAxisShape.get()) {
343 anAxisShape = anAxisSelection->context()->shape();
345 std::shared_ptr<GeomAPI_Edge> anEdge(new GeomAPI_Edge(anAxisShape));
347 std::shared_ptr<GeomAPI_Ax1> anAxis =
348 std::shared_ptr<GeomAPI_Ax1>(new GeomAPI_Ax1(anEdge->line()->location(),
349 anEdge->line()->direction()));
352 double anAngle = real(ANGLE())->value();
354 GeomAlgoAPI_Rotation aRotationAlgo(aFace, anAxis, anAngle);
355 if (!aRotationAlgo.check()) {
356 setError(aRotationAlgo.getError());
357 return GeomShapePtr();
359 aRotationAlgo.build();
360 if (!aRotationAlgo.isDone()) {
361 setError("Error: Failed to rotate plane");
362 return GeomShapePtr();
365 std::shared_ptr<GeomAPI_Face> aRes(new GeomAPI_Face(aRotationAlgo.shape()));
369 //==================================================================================================
370 std::shared_ptr<GeomAPI_Shape> ConstructionPlugin_Plane::createByTwoParallelPlanes()
373 AttributeSelectionPtr aFaceSelection1 = selection(PLANE1());
374 GeomShapePtr aFaceShape1 = aFaceSelection1->value();
375 if(!aFaceShape1.get()) {
376 aFaceShape1 = aFaceSelection1->context()->shape();
378 std::shared_ptr<GeomAPI_Face> aFace1(new GeomAPI_Face(aFaceShape1));
379 std::shared_ptr<GeomAPI_Pln> aPln1 = aFace1->getPlane();
382 AttributeSelectionPtr aFaceSelection2 = selection(PLANE2());
383 GeomShapePtr aFaceShape2 = aFaceSelection2->value();
384 if(!aFaceShape2.get()) {
385 aFaceShape2 = aFaceSelection2->context()->shape();
387 std::shared_ptr<GeomAPI_Face> aFace2(new GeomAPI_Face(aFaceShape2));
388 std::shared_ptr<GeomAPI_Pln> aPln2 = aFace2->getPlane();
390 std::shared_ptr<GeomAPI_Pnt> anOrig1 = aPln1->location();
391 std::shared_ptr<GeomAPI_Pnt> aPntOnPln2 = aPln2->project(anOrig1);
393 std::shared_ptr<GeomAPI_Pnt> aNewOrig(new GeomAPI_Pnt(anOrig1->xyz()->added(
394 aPntOnPln2->xyz())->multiplied(0.5)));
396 std::shared_ptr<GeomAPI_Pln> aNewPln(new GeomAPI_Pln(aNewOrig, aPln1->direction()));
398 std::shared_ptr<GeomAPI_Face> aRes = makeRectangularFace(aFace1, aNewPln);
403 //==================================================================================================
404 GeomShapePtr faceByThreeVertices(const std::shared_ptr<GeomAPI_Vertex> theV1,
405 const std::shared_ptr<GeomAPI_Vertex> theV2,
406 const std::shared_ptr<GeomAPI_Vertex> theV3)
408 std::shared_ptr<GeomAPI_Face> aFace =
409 GeomAlgoAPI_FaceBuilder::planarFaceByThreeVertices(theV1, theV2, theV3);
411 ListOfShape anObjects;
412 anObjects.push_back(theV1);
413 anObjects.push_back(theV2);
414 anObjects.push_back(theV3);
415 std::list<std::shared_ptr<GeomAPI_Pnt> > aBoundingPoints =
416 GeomAlgoAPI_ShapeTools::getBoundingBox(anObjects, 1.0);
417 GeomShapePtr aRes = GeomAlgoAPI_ShapeTools::fitPlaneToBox(aFace, aBoundingPoints);
422 //==================================================================================================
423 std::shared_ptr<GeomAPI_Face> makeRectangularFace(const std::shared_ptr<GeomAPI_Face> theFace,
424 const std::shared_ptr<GeomAPI_Pln> thePln)
426 // Create rectangular face close to the selected
427 double aXmin, aYmin, Zmin, aXmax, aYmax, Zmax;
428 theFace->computeSize(aXmin, aYmin, Zmin, aXmax, aYmax, Zmax);
430 // use all 8 points of the bounding box to find the 2D bounds
432 double aMinX2d, aMaxX2d, aMinY2d, aMaxY2d;
433 for(int aXIsMin = 0; aXIsMin < 2; aXIsMin++) {
434 for(int aYIsMin = 0; aYIsMin < 2; aYIsMin++) {
435 for(int aZIsMin = 0; aZIsMin < 2; aZIsMin++) {
436 std::shared_ptr<GeomAPI_Pnt> aPnt(new GeomAPI_Pnt(
437 aXIsMin ? aXmin : aXmax, aYIsMin ? aYmin : aYmax, aZIsMin ? Zmin : Zmax));
438 std::shared_ptr<GeomAPI_Pnt2d> aPnt2d = aPnt->to2D(thePln);
439 if (isFirst || aPnt2d->x() < aMinX2d)
440 aMinX2d = aPnt2d->x();
441 if (isFirst || aPnt2d->y() < aMinY2d)
442 aMinY2d = aPnt2d->y();
443 if (isFirst || aPnt2d->x() > aMaxX2d)
444 aMaxX2d = aPnt2d->x();
445 if (isFirst || aPnt2d->y() > aMaxY2d)
446 aMaxY2d = aPnt2d->y();
452 double aWgap = (aMaxX2d - aMinX2d) * 0.1;
453 double aHgap = (aMaxY2d - aMinY2d) * 0.1;
454 std::shared_ptr<GeomAPI_Face> aResFace = GeomAlgoAPI_FaceBuilder::planarFace(thePln,
455 aMinX2d - aWgap, aMinY2d - aHgap, aMaxX2d - aMinX2d + 2. * aWgap,
456 aMaxY2d - aMinY2d + 2. * aHgap);