1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 // File: ConstructionPlugin_Plane.cpp
4 // Created: 12 Dec 2014
5 // Author: Vitaly Smetannikov
7 #include "ConstructionPlugin_Plane.h"
9 #include <Config_PropManager.h>
11 #include <GeomAlgoAPI_FaceBuilder.h>
12 #include <GeomAlgoAPI_Rotation.h>
13 #include <GeomAlgoAPI_ShapeTools.h>
15 #include <GeomAPI_Ax1.h>
16 #include <GeomAPI_Edge.h>
17 #include <GeomAPI_Face.h>
18 #include <GeomAPI_Lin.h>
19 #include <GeomAPI_Pln.h>
20 #include <GeomAPI_Pnt.h>
21 #include <GeomAPI_Pnt2d.h>
22 #include <GeomAPI_Vertex.h>
23 #include <GeomAPI_XYZ.h>
25 #include <ModelAPI_AttributeDouble.h>
26 #include <ModelAPI_AttributeIntArray.h>
27 #include <ModelAPI_AttributeSelection.h>
28 #include <ModelAPI_AttributeString.h>
29 #include <ModelAPI_AttributeBoolean.h>
30 #include <ModelAPI_ResultConstruction.h>
31 #include <ModelAPI_Session.h>
32 #include <ModelAPI_Validator.h>
34 static GeomShapePtr faceByThreeVertices(const std::shared_ptr<GeomAPI_Vertex> theV1,
35 const std::shared_ptr<GeomAPI_Vertex> theV2,
36 const std::shared_ptr<GeomAPI_Vertex> theV3);
37 static std::shared_ptr<GeomAPI_Face> makeRectangularFace(
38 const std::shared_ptr<GeomAPI_Face> theFace,
39 const std::shared_ptr<GeomAPI_Pln> thePln);
41 //==================================================================================================
42 ConstructionPlugin_Plane::ConstructionPlugin_Plane()
46 //==================================================================================================
47 void ConstructionPlugin_Plane::initAttributes()
49 data()->addAttribute(ConstructionPlugin_Plane::CREATION_METHOD(),
50 ModelAPI_AttributeString::typeId());
52 data()->addAttribute(PLANE(), ModelAPI_AttributeSelection::typeId());
53 data()->addAttribute(DISTANCE(), ModelAPI_AttributeDouble::typeId());
54 // By general equation.
55 data()->addAttribute(A(), ModelAPI_AttributeDouble::typeId());
56 data()->addAttribute(B(), ModelAPI_AttributeDouble::typeId());
57 data()->addAttribute(C(), ModelAPI_AttributeDouble::typeId());
58 data()->addAttribute(D(), ModelAPI_AttributeDouble::typeId());
59 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), A());
60 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), B());
61 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), C());
62 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), D());
65 data()->addAttribute(POINT1(), ModelAPI_AttributeSelection::typeId());
66 data()->addAttribute(POINT2(), ModelAPI_AttributeSelection::typeId());
67 data()->addAttribute(POINT3(), ModelAPI_AttributeSelection::typeId());
70 data()->addAttribute(LINE(), ModelAPI_AttributeSelection::typeId());
71 data()->addAttribute(POINT(), ModelAPI_AttributeSelection::typeId());
72 data()->addAttribute(PERPENDICULAR(), ModelAPI_AttributeBoolean::typeId());
75 data()->addAttribute(CREATION_METHOD_BY_OTHER_PLANE_OPTION(), ModelAPI_AttributeString::typeId());
76 data()->addAttribute(REVERSE(), ModelAPI_AttributeBoolean::typeId());
77 data()->addAttribute(COINCIDENT_POINT(), ModelAPI_AttributeSelection::typeId());
78 data()->addAttribute(AXIS(), ModelAPI_AttributeSelection::typeId());
79 data()->addAttribute(ANGLE(), ModelAPI_AttributeDouble::typeId());
81 // By two parallel planes.
82 data()->addAttribute(PLANE1(), ModelAPI_AttributeSelection::typeId());
83 data()->addAttribute(PLANE2(), ModelAPI_AttributeSelection::typeId());
86 //==================================================================================================
87 void ConstructionPlugin_Plane::execute()
91 std::string aCreationMethod = string(CREATION_METHOD())->value();
92 if(aCreationMethod == CREATION_METHOD_BY_GENERAL_EQUATION() ||
93 aCreationMethod == "PlaneByGeneralEquation") {
94 aShape = createByGeneralEquation();
95 } else if(aCreationMethod == CREATION_METHOD_BY_THREE_POINTS()) {
96 aShape = createByThreePoints();
97 } else if(aCreationMethod == CREATION_METHOD_BY_LINE_AND_POINT()) {
98 aShape = createByLineAndPoint();
99 } else if(aCreationMethod == CREATION_METHOD_BY_OTHER_PLANE()) {
100 std::string aCreationMethodOption = string(CREATION_METHOD_BY_OTHER_PLANE_OPTION())->value();
101 if(aCreationMethodOption == CREATION_METHOD_BY_DISTANCE_FROM_OTHER()) {
102 aShape = createByDistanceFromOther();
103 } else if(aCreationMethodOption == CREATION_METHOD_BY_COINCIDENT_TO_POINT()) {
104 aShape = createByCoincidentPoint();
105 } else if(aCreationMethodOption == CREATION_METHOD_BY_ROTATION()) {
106 aShape = createByRotation();
108 } else if(aCreationMethod == CREATION_METHOD_BY_TWO_PARALLEL_PLANES()) {
109 aShape = createByTwoParallelPlanes();
111 setError("Error: Plane creation method \"" + aCreationMethod + "\" not supported.");
116 setError("Error: Could not create a plane.");
120 ResultConstructionPtr aConstr = document()->createConstruction(data());
121 aConstr->setInfinite(true);
122 aConstr->setShape(aShape);
126 //==================================================================================================
127 bool ConstructionPlugin_Plane::customisePresentation(ResultPtr theResult, AISObjectPtr thePrs,
128 std::shared_ptr<GeomAPI_ICustomPrs> theDefaultPrs)
130 std::vector<int> aColor;
131 // get color from the attribute of the result
132 if (theResult.get() != NULL &&
133 theResult->data()->attribute(ModelAPI_Result::COLOR_ID()).get() != NULL) {
134 AttributeIntArrayPtr aColorAttr = theResult->data()->intArray(ModelAPI_Result::COLOR_ID());
135 if (aColorAttr.get() && aColorAttr->size()) {
136 aColor.push_back(aColorAttr->value(0));
137 aColor.push_back(aColorAttr->value(1));
138 aColor.push_back(aColorAttr->value(2));
142 aColor = Config_PropManager::color("Visualization", "construction_plane_color",
143 ConstructionPlugin_Plane::DEFAULT_COLOR());
145 bool isCustomized = false;
146 if (aColor.size() == 3)
147 isCustomized = thePrs->setColor(aColor[0], aColor[1], aColor[2]);
149 isCustomized = thePrs->setTransparensy(0.6) || isCustomized;
154 //==================================================================================================
155 std::shared_ptr<GeomAPI_Shape> ConstructionPlugin_Plane::createByGeneralEquation()
157 AttributeDoublePtr anAttrA = real(ConstructionPlugin_Plane::A());
158 AttributeDoublePtr anAttrB = real(ConstructionPlugin_Plane::B());
159 AttributeDoublePtr anAttrC = real(ConstructionPlugin_Plane::C());
160 AttributeDoublePtr anAttrD = real(ConstructionPlugin_Plane::D());
161 std::shared_ptr<GeomAPI_Shape> aPlaneFace;
162 if ((anAttrA.get() != NULL) && (anAttrB.get() != NULL) &&
163 (anAttrC.get() != NULL) && (anAttrD.get() != NULL) &&
164 anAttrA->isInitialized() && anAttrB->isInitialized() &&
165 anAttrC->isInitialized() && anAttrD->isInitialized() ) {
166 double aA = anAttrA->value(), aB = anAttrB->value(),
167 aC = anAttrC->value(), aD = anAttrD->value();
168 std::shared_ptr<GeomAPI_Pln> aPlane =
169 std::shared_ptr<GeomAPI_Pln>(new GeomAPI_Pln(aA, aB, aC, aD));
170 std::string kDefaultPlaneSize = "200";
171 double aSize = Config_PropManager::integer(SKETCH_TAB_NAME, "planes_size", kDefaultPlaneSize);
173 aPlaneFace = GeomAlgoAPI_FaceBuilder::squareFace(aPlane, aSize);
178 //==================================================================================================
179 std::shared_ptr<GeomAPI_Shape> ConstructionPlugin_Plane::createByThreePoints()
182 AttributeSelectionPtr aPointSelection1 = selection(POINT1());
183 GeomShapePtr aPointShape1 = aPointSelection1->value();
184 if(!aPointShape1.get()) {
185 aPointShape1 = aPointSelection1->context()->shape();
187 std::shared_ptr<GeomAPI_Vertex> aVertex1(new GeomAPI_Vertex(aPointShape1));
190 AttributeSelectionPtr aPointSelection2 = selection(POINT2());
191 GeomShapePtr aPointShape2 = aPointSelection2->value();
192 if(!aPointShape2.get()) {
193 aPointShape2 = aPointSelection2->context()->shape();
195 std::shared_ptr<GeomAPI_Vertex> aVertex2(new GeomAPI_Vertex(aPointShape2));
198 AttributeSelectionPtr aPointSelection3 = selection(POINT3());
199 GeomShapePtr aPointShape3 = aPointSelection3->value();
200 if(!aPointShape3.get()) {
201 aPointShape3 = aPointSelection3->context()->shape();
203 std::shared_ptr<GeomAPI_Vertex> aVertex3(new GeomAPI_Vertex(aPointShape3));
205 GeomShapePtr aRes = faceByThreeVertices(aVertex1, aVertex2, aVertex3);
210 //==================================================================================================
211 std::shared_ptr<GeomAPI_Shape> ConstructionPlugin_Plane::createByLineAndPoint()
214 AttributeSelectionPtr anEdgeSelection = selection(LINE());
215 GeomShapePtr aLineShape = anEdgeSelection->value();
216 if(!aLineShape.get()) {
217 aLineShape = anEdgeSelection->context()->shape();
219 std::shared_ptr<GeomAPI_Edge> anEdge(new GeomAPI_Edge(aLineShape));
222 AttributeSelectionPtr aPointSelection = selection(POINT());
223 GeomShapePtr aPointShape = aPointSelection->value();
224 if(!aPointShape.get()) {
225 aPointShape = aPointSelection->context()->shape();
227 std::shared_ptr<GeomAPI_Vertex> aVertex(new GeomAPI_Vertex(aPointShape));
229 // Get perpendicular flag.
230 bool anIsPerpendicular= boolean(PERPENDICULAR())->value();
233 if(anIsPerpendicular) {
234 std::shared_ptr<GeomAPI_Lin> aLin = anEdge->line();
235 std::shared_ptr<GeomAPI_Pnt> aPnt = aVertex->point();
236 std::shared_ptr<GeomAPI_Pln> aNewPln(new GeomAPI_Pln(aPnt, aLin->direction()));
237 double aSize = aLin->distance(aPnt) * 2;
238 // point may belong to line, so for the face size use maximum distance between point and line
239 // and the line size (distance between the start and end point)
240 double aDistance = anEdge->firstPoint()->distance(anEdge->lastPoint());
241 aRes = GeomAlgoAPI_FaceBuilder::squareFace(aNewPln, aSize > aDistance ? aSize : aDistance);
243 std::shared_ptr<GeomAPI_Vertex> aV1, aV2;
244 GeomAlgoAPI_ShapeTools::findBounds(anEdge, aV1, aV2);
245 aRes = faceByThreeVertices(aV1, aV2, aVertex);
251 //==================================================================================================
252 std::shared_ptr<GeomAPI_Shape> ConstructionPlugin_Plane::createByDistanceFromOther()
254 AttributeSelectionPtr aFaceAttr = data()->selection(ConstructionPlugin_Plane::PLANE());
255 AttributeDoublePtr aDistAttr = data()->real(ConstructionPlugin_Plane::DISTANCE());
256 std::shared_ptr<GeomAPI_Shape> aPlane;
257 if ((aFaceAttr.get() != NULL) &&
258 (aDistAttr.get() != NULL) &&
259 aFaceAttr->isInitialized() && aDistAttr->isInitialized()) {
261 double aDist = aDistAttr->value();
262 bool anIsReverse = boolean(REVERSE())->value();
263 if(anIsReverse) aDist = -aDist;
264 GeomShapePtr aShape = aFaceAttr->value();
266 aShape = aFaceAttr->context()->shape();
273 std::shared_ptr<GeomAPI_Face> aFace(new GeomAPI_Face(aShape));
275 std::shared_ptr<GeomAPI_Pln> aPln = aFace->getPlane();
276 std::shared_ptr<GeomAPI_Pnt> aOrig = aPln->location();
277 std::shared_ptr<GeomAPI_Dir> aDir = aPln->direction();
279 aOrig->translate(aDir, aDist);
280 std::shared_ptr<GeomAPI_Pln> aNewPln(new GeomAPI_Pln(aOrig, aDir));
282 aPlane = makeRectangularFace(aFace, aNewPln);
287 //==================================================================================================
288 std::shared_ptr<GeomAPI_Shape> ConstructionPlugin_Plane::createByCoincidentPoint()
291 AttributeSelectionPtr aFaceSelection = selection(PLANE());
292 GeomShapePtr aFaceShape = aFaceSelection->value();
293 if(!aFaceShape.get()) {
294 aFaceShape = aFaceSelection->context()->shape();
296 std::shared_ptr<GeomAPI_Face> aFace(new GeomAPI_Face(aFaceShape));
299 AttributeSelectionPtr aPointSelection = selection(COINCIDENT_POINT());
300 GeomShapePtr aPointShape = aPointSelection->value();
301 if(!aPointShape.get()) {
302 aPointShape = aPointSelection->context()->shape();
304 std::shared_ptr<GeomAPI_Vertex> aVertex(new GeomAPI_Vertex(aPointShape));
306 std::shared_ptr<GeomAPI_Pnt> anOrig = aVertex->point();
307 std::shared_ptr<GeomAPI_Pln> aPln = aFace->getPlane();
308 std::shared_ptr<GeomAPI_Dir> aDir = aPln->direction();
310 std::shared_ptr<GeomAPI_Pln> aNewPln(new GeomAPI_Pln(anOrig, aDir));
312 return makeRectangularFace(aFace, aNewPln);
315 //==================================================================================================
316 std::shared_ptr<GeomAPI_Shape> ConstructionPlugin_Plane::createByRotation()
319 AttributeSelectionPtr aFaceSelection = selection(PLANE());
320 GeomShapePtr aFaceShape = aFaceSelection->value();
321 if(!aFaceShape.get()) {
322 aFaceShape = aFaceSelection->context()->shape();
324 std::shared_ptr<GeomAPI_Face> aFace(new GeomAPI_Face(aFaceShape));
325 aFace = makeRectangularFace(aFace, aFace->getPlane());
328 AttributeSelectionPtr anAxisSelection = selection(AXIS());
329 GeomShapePtr anAxisShape = anAxisSelection->value();
330 if(!anAxisShape.get()) {
331 anAxisShape = anAxisSelection->context()->shape();
333 std::shared_ptr<GeomAPI_Edge> anEdge(new GeomAPI_Edge(anAxisShape));
335 std::shared_ptr<GeomAPI_Ax1> anAxis =
336 std::shared_ptr<GeomAPI_Ax1>(new GeomAPI_Ax1(anEdge->line()->location(),
337 anEdge->line()->direction()));
340 double anAngle = real(ANGLE())->value();
342 GeomAlgoAPI_Rotation aRotationAlgo(aFace, anAxis, anAngle);
343 std::shared_ptr<GeomAPI_Face> aRes(new GeomAPI_Face(aRotationAlgo.shape()));
348 //==================================================================================================
349 std::shared_ptr<GeomAPI_Shape> ConstructionPlugin_Plane::createByTwoParallelPlanes()
352 AttributeSelectionPtr aFaceSelection1 = selection(PLANE1());
353 GeomShapePtr aFaceShape1 = aFaceSelection1->value();
354 if(!aFaceShape1.get()) {
355 aFaceShape1 = aFaceSelection1->context()->shape();
357 std::shared_ptr<GeomAPI_Face> aFace1(new GeomAPI_Face(aFaceShape1));
358 std::shared_ptr<GeomAPI_Pln> aPln1 = aFace1->getPlane();
361 AttributeSelectionPtr aFaceSelection2 = selection(PLANE2());
362 GeomShapePtr aFaceShape2 = aFaceSelection2->value();
363 if(!aFaceShape2.get()) {
364 aFaceShape2 = aFaceSelection2->context()->shape();
366 std::shared_ptr<GeomAPI_Face> aFace2(new GeomAPI_Face(aFaceShape2));
367 std::shared_ptr<GeomAPI_Pln> aPln2 = aFace2->getPlane();
369 std::shared_ptr<GeomAPI_Pnt> anOrig1 = aPln1->location();
370 std::shared_ptr<GeomAPI_Pnt> aPntOnPln2 = aPln2->project(anOrig1);
372 std::shared_ptr<GeomAPI_Pnt> aNewOrig(new GeomAPI_Pnt(anOrig1->xyz()->added(
373 aPntOnPln2->xyz())->multiplied(0.5)));
375 std::shared_ptr<GeomAPI_Pln> aNewPln(new GeomAPI_Pln(aNewOrig, aPln1->direction()));
377 std::shared_ptr<GeomAPI_Face> aRes = makeRectangularFace(aFace1, aNewPln);
382 //==================================================================================================
383 GeomShapePtr faceByThreeVertices(const std::shared_ptr<GeomAPI_Vertex> theV1,
384 const std::shared_ptr<GeomAPI_Vertex> theV2,
385 const std::shared_ptr<GeomAPI_Vertex> theV3)
387 std::shared_ptr<GeomAPI_Face> aFace =
388 GeomAlgoAPI_FaceBuilder::planarFaceByThreeVertices(theV1, theV2, theV3);
390 ListOfShape anObjects;
391 anObjects.push_back(theV1);
392 anObjects.push_back(theV2);
393 anObjects.push_back(theV3);
394 std::list<std::shared_ptr<GeomAPI_Pnt> > aBoundingPoints =
395 GeomAlgoAPI_ShapeTools::getBoundingBox(anObjects, 1.0);
396 GeomShapePtr aRes = GeomAlgoAPI_ShapeTools::fitPlaneToBox(aFace, aBoundingPoints);
401 //==================================================================================================
402 std::shared_ptr<GeomAPI_Face> makeRectangularFace(const std::shared_ptr<GeomAPI_Face> theFace,
403 const std::shared_ptr<GeomAPI_Pln> thePln)
405 // Create rectangular face close to the selected
406 double aXmin, aYmin, Zmin, aXmax, aYmax, Zmax;
407 theFace->computeSize(aXmin, aYmin, Zmin, aXmax, aYmax, Zmax);
409 // use all 8 points of the bounding box to find the 2D bounds
411 double aMinX2d, aMaxX2d, aMinY2d, aMaxY2d;
412 for(int aXIsMin = 0; aXIsMin < 2; aXIsMin++) {
413 for(int aYIsMin = 0; aYIsMin < 2; aYIsMin++) {
414 for(int aZIsMin = 0; aZIsMin < 2; aZIsMin++) {
415 std::shared_ptr<GeomAPI_Pnt> aPnt(new GeomAPI_Pnt(
416 aXIsMin ? aXmin : aXmax, aYIsMin ? aYmin : aYmax, aZIsMin ? Zmin : Zmax));
417 std::shared_ptr<GeomAPI_Pnt2d> aPnt2d = aPnt->to2D(thePln);
418 if (isFirst || aPnt2d->x() < aMinX2d)
419 aMinX2d = aPnt2d->x();
420 if (isFirst || aPnt2d->y() < aMinY2d)
421 aMinY2d = aPnt2d->y();
422 if (isFirst || aPnt2d->x() > aMaxX2d)
423 aMaxX2d = aPnt2d->x();
424 if (isFirst || aPnt2d->y() > aMaxY2d)
425 aMaxY2d = aPnt2d->y();
431 double aWgap = (aMaxX2d - aMinX2d) * 0.1;
432 double aHgap = (aMaxY2d - aMinY2d) * 0.1;
433 std::shared_ptr<GeomAPI_Face> aResFace = GeomAlgoAPI_FaceBuilder::planarFace(thePln,
434 aMinX2d - aWgap, aMinY2d - aHgap, aMaxX2d - aMinX2d + 2. * aWgap,
435 aMaxY2d - aMinY2d + 2. * aHgap);