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>
24 #include <ModelAPI_AttributeDouble.h>
25 #include <ModelAPI_AttributeIntArray.h>
26 #include <ModelAPI_AttributeSelection.h>
27 #include <ModelAPI_AttributeString.h>
28 #include <ModelAPI_AttributeBoolean.h>
29 #include <ModelAPI_ResultConstruction.h>
30 #include <ModelAPI_Session.h>
31 #include <ModelAPI_Validator.h>
33 static GeomShapePtr faceByThreeVertices(const std::shared_ptr<GeomAPI_Vertex> theV1,
34 const std::shared_ptr<GeomAPI_Vertex> theV2,
35 const std::shared_ptr<GeomAPI_Vertex> theV3);
36 static std::shared_ptr<GeomAPI_Face> makeRectangularFace(const std::shared_ptr<GeomAPI_Face> theFace,
37 const std::shared_ptr<GeomAPI_Pln> thePln);
39 //==================================================================================================
40 ConstructionPlugin_Plane::ConstructionPlugin_Plane()
44 //==================================================================================================
45 void ConstructionPlugin_Plane::initAttributes()
47 data()->addAttribute(ConstructionPlugin_Plane::CREATION_METHOD(), ModelAPI_AttributeString::typeId());
49 // By general equation.
50 data()->addAttribute(A(), ModelAPI_AttributeDouble::typeId());
51 data()->addAttribute(B(), ModelAPI_AttributeDouble::typeId());
52 data()->addAttribute(C(), ModelAPI_AttributeDouble::typeId());
53 data()->addAttribute(D(), ModelAPI_AttributeDouble::typeId());
54 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), A());
55 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), B());
56 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), C());
57 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), D());
60 data()->addAttribute(POINT1(), ModelAPI_AttributeSelection::typeId());
61 data()->addAttribute(POINT2(), ModelAPI_AttributeSelection::typeId());
62 data()->addAttribute(POINT3(), ModelAPI_AttributeSelection::typeId());
65 data()->addAttribute(LINE(), ModelAPI_AttributeSelection::typeId());
66 data()->addAttribute(POINT(), ModelAPI_AttributeSelection::typeId());
67 data()->addAttribute(PERPENDICULAR(), ModelAPI_AttributeBoolean::typeId());
70 data()->addAttribute(CREATION_METHOD_BY_OTHER_PLANE_OPTION(), ModelAPI_AttributeString::typeId());
71 data()->addAttribute(PLANE(), ModelAPI_AttributeSelection::typeId());
72 data()->addAttribute(DISTANCE(), ModelAPI_AttributeDouble::typeId());
73 data()->addAttribute(REVERSE(), ModelAPI_AttributeBoolean::typeId());
74 data()->addAttribute(COINCIDENT_POINT(), ModelAPI_AttributeSelection::typeId());
75 data()->addAttribute(AXIS(), ModelAPI_AttributeSelection::typeId());
76 data()->addAttribute(ANGLE(), ModelAPI_AttributeDouble::typeId());
78 // By two parallel planes.
79 data()->addAttribute(PLANE1(), ModelAPI_AttributeSelection::typeId());
80 data()->addAttribute(PLANE2(), ModelAPI_AttributeSelection::typeId());
83 //==================================================================================================
84 void ConstructionPlugin_Plane::execute()
88 std::string aCreationMethod = string(CREATION_METHOD())->value();
89 if(aCreationMethod == CREATION_METHOD_BY_GENERAL_EQUATION()) {
90 aShape = createByGeneralEquation();
91 } else if(aCreationMethod == CREATION_METHOD_BY_THREE_POINTS()) {
92 aShape = createByThreePoints();
93 } else if(aCreationMethod == CREATION_METHOD_BY_LINE_AND_POINT()) {
94 aShape = createByLineAndPoint();
95 } else if(aCreationMethod == CREATION_METHOD_BY_OTHER_PLANE()) {
96 std::string aCreationMethodOption = string(CREATION_METHOD_BY_OTHER_PLANE_OPTION())->value();
97 if(aCreationMethodOption == CREATION_METHOD_BY_DISTANCE_FROM_OTHER()) {
98 aShape = createByDistanceFromOther();
99 } else if(aCreationMethodOption == CREATION_METHOD_BY_COINCIDENT_TO_POINT()) {
100 aShape = createByCoincidentPoint();
101 } else if(aCreationMethodOption == CREATION_METHOD_BY_ROTATION()) {
102 aShape = createByRotation();
104 } else if(aCreationMethod == CREATION_METHOD_BY_TWO_PARALLEL_PLANES()) {
105 aShape = createByTwoParallelPlanes();
107 setError("Error: Plane creation method \"" + aCreationMethod + "\" not supported.");
112 setError("Error: Could not create a plane.");
116 ResultConstructionPtr aConstr = document()->createConstruction(data());
117 aConstr->setInfinite(true);
118 aConstr->setShape(aShape);
122 //==================================================================================================
123 bool ConstructionPlugin_Plane::customisePresentation(ResultPtr theResult, AISObjectPtr thePrs,
124 std::shared_ptr<GeomAPI_ICustomPrs> theDefaultPrs)
126 std::vector<int> aColor;
127 // get color from the attribute of the result
128 if (theResult.get() != NULL && theResult->data()->attribute(ModelAPI_Result::COLOR_ID()).get() != NULL) {
129 AttributeIntArrayPtr aColorAttr = theResult->data()->intArray(ModelAPI_Result::COLOR_ID());
130 if (aColorAttr.get() && aColorAttr->size()) {
131 aColor.push_back(aColorAttr->value(0));
132 aColor.push_back(aColorAttr->value(1));
133 aColor.push_back(aColorAttr->value(2));
137 aColor = Config_PropManager::color("Visualization", "construction_plane_color",
138 ConstructionPlugin_Plane::DEFAULT_COLOR());
140 bool isCustomized = false;
141 if (aColor.size() == 3)
142 isCustomized = thePrs->setColor(aColor[0], aColor[1], aColor[2]);
144 isCustomized = thePrs->setTransparensy(0.6) || isCustomized;
149 //==================================================================================================
150 std::shared_ptr<GeomAPI_Shape> ConstructionPlugin_Plane::createByGeneralEquation()
152 AttributeDoublePtr anAttrA = real(ConstructionPlugin_Plane::A());
153 AttributeDoublePtr anAttrB = real(ConstructionPlugin_Plane::B());
154 AttributeDoublePtr anAttrC = real(ConstructionPlugin_Plane::C());
155 AttributeDoublePtr anAttrD = real(ConstructionPlugin_Plane::D());
156 std::shared_ptr<GeomAPI_Shape> aPlaneFace;
157 if ((anAttrA.get() != NULL) && (anAttrB.get() != NULL) &&
158 (anAttrC.get() != NULL) && (anAttrD.get() != NULL) &&
159 anAttrA->isInitialized() && anAttrB->isInitialized() &&
160 anAttrC->isInitialized() && anAttrD->isInitialized() ) {
161 double aA = anAttrA->value(), aB = anAttrB->value(),
162 aC = anAttrC->value(), aD = anAttrD->value();
163 std::shared_ptr<GeomAPI_Pln> aPlane =
164 std::shared_ptr<GeomAPI_Pln>(new GeomAPI_Pln(aA, aB, aC, aD));
165 std::string kDefaultPlaneSize = "200";
166 double aSize = Config_PropManager::integer(SKETCH_TAB_NAME, "planes_size", kDefaultPlaneSize);
168 aPlaneFace = GeomAlgoAPI_FaceBuilder::squareFace(aPlane, aSize);
173 //==================================================================================================
174 std::shared_ptr<GeomAPI_Shape> ConstructionPlugin_Plane::createByThreePoints()
177 AttributeSelectionPtr aPointSelection1 = selection(POINT1());
178 GeomShapePtr aPointShape1 = aPointSelection1->value();
179 if(!aPointShape1.get()) {
180 aPointShape1 = aPointSelection1->context()->shape();
182 std::shared_ptr<GeomAPI_Vertex> aVertex1(new GeomAPI_Vertex(aPointShape1));
185 AttributeSelectionPtr aPointSelection2 = selection(POINT2());
186 GeomShapePtr aPointShape2 = aPointSelection2->value();
187 if(!aPointShape2.get()) {
188 aPointShape2 = aPointSelection2->context()->shape();
190 std::shared_ptr<GeomAPI_Vertex> aVertex2(new GeomAPI_Vertex(aPointShape2));
193 AttributeSelectionPtr aPointSelection3 = selection(POINT3());
194 GeomShapePtr aPointShape3 = aPointSelection3->value();
195 if(!aPointShape3.get()) {
196 aPointShape3 = aPointSelection3->context()->shape();
198 std::shared_ptr<GeomAPI_Vertex> aVertex3(new GeomAPI_Vertex(aPointShape3));
200 GeomShapePtr aRes = faceByThreeVertices(aVertex1, aVertex2, aVertex3);
205 //==================================================================================================
206 std::shared_ptr<GeomAPI_Shape> ConstructionPlugin_Plane::createByLineAndPoint()
209 AttributeSelectionPtr anEdgeSelection = selection(LINE());
210 GeomShapePtr aLineShape = anEdgeSelection->value();
211 if(!aLineShape.get()) {
212 aLineShape = anEdgeSelection->context()->shape();
214 std::shared_ptr<GeomAPI_Edge> anEdge(new GeomAPI_Edge(aLineShape));
217 AttributeSelectionPtr aPointSelection = selection(POINT());
218 GeomShapePtr aPointShape = aPointSelection->value();
219 if(!aPointShape.get()) {
220 aPointShape = aPointSelection->context()->shape();
222 std::shared_ptr<GeomAPI_Vertex> aVertex(new GeomAPI_Vertex(aPointShape));
224 // Get perpendicular flag.
225 bool anIsPerpendicular= boolean(PERPENDICULAR())->value();
228 if(anIsPerpendicular) {
229 std::shared_ptr<GeomAPI_Lin> aLin = anEdge->line();
230 std::shared_ptr<GeomAPI_Pnt> aPnt = aVertex->point();
231 std::shared_ptr<GeomAPI_Pln> aNewPln(new GeomAPI_Pln(aPnt, aLin->direction()));
232 double aSize = aLin->distance(aPnt);
234 aRes = GeomAlgoAPI_FaceBuilder::squareFace(aNewPln, aSize);
236 std::shared_ptr<GeomAPI_Vertex> aV1, aV2;
237 GeomAlgoAPI_ShapeTools::findBounds(anEdge, aV1, aV2);
238 aRes = faceByThreeVertices(aV1, aV2, aVertex);
244 //==================================================================================================
245 std::shared_ptr<GeomAPI_Shape> ConstructionPlugin_Plane::createByDistanceFromOther()
247 AttributeSelectionPtr aFaceAttr = data()->selection(ConstructionPlugin_Plane::PLANE());
248 AttributeDoublePtr aDistAttr = data()->real(ConstructionPlugin_Plane::DISTANCE());
249 std::shared_ptr<GeomAPI_Shape> aPlane;
250 if ((aFaceAttr.get() != NULL) &&
251 (aDistAttr.get() != NULL) &&
252 aFaceAttr->isInitialized() && aDistAttr->isInitialized()) {
254 double aDist = aDistAttr->value();
255 bool anIsReverse = boolean(REVERSE())->value();
256 if(anIsReverse) aDist = -aDist;
257 GeomShapePtr aShape = aFaceAttr->value();
259 aShape = aFaceAttr->context()->shape();
266 std::shared_ptr<GeomAPI_Face> aFace(new GeomAPI_Face(aShape));
268 std::shared_ptr<GeomAPI_Pln> aPln = GeomAlgoAPI_FaceBuilder::plane(aFace);
269 std::shared_ptr<GeomAPI_Pnt> aOrig = aPln->location();
270 std::shared_ptr<GeomAPI_Dir> aDir = aPln->direction();
272 aOrig->translate(aDir, aDist);
273 std::shared_ptr<GeomAPI_Pln> aNewPln(new GeomAPI_Pln(aOrig, aDir));
275 aPlane = makeRectangularFace(aFace, aNewPln);
280 //==================================================================================================
281 std::shared_ptr<GeomAPI_Shape> ConstructionPlugin_Plane::createByCoincidentPoint()
284 AttributeSelectionPtr aFaceSelection = selection(PLANE());
285 GeomShapePtr aFaceShape = aFaceSelection->value();
286 if(!aFaceShape.get()) {
287 aFaceShape = aFaceSelection->context()->shape();
289 std::shared_ptr<GeomAPI_Face> aFace(new GeomAPI_Face(aFaceShape));
292 AttributeSelectionPtr aPointSelection = selection(COINCIDENT_POINT());
293 GeomShapePtr aPointShape = aPointSelection->value();
294 if(!aPointShape.get()) {
295 aPointShape = aPointSelection->context()->shape();
297 std::shared_ptr<GeomAPI_Vertex> aVertex(new GeomAPI_Vertex(aPointShape));
299 std::shared_ptr<GeomAPI_Pnt> anOrig = aVertex->point();
300 std::shared_ptr<GeomAPI_Pln> aPln = aFace->getPlane();
301 std::shared_ptr<GeomAPI_Dir> aDir = aPln->direction();
303 std::shared_ptr<GeomAPI_Pln> aNewPln(new GeomAPI_Pln(anOrig, aDir));
305 return makeRectangularFace(aFace, aNewPln);
308 //==================================================================================================
309 std::shared_ptr<GeomAPI_Shape> ConstructionPlugin_Plane::createByRotation()
312 AttributeSelectionPtr aFaceSelection = selection(PLANE());
313 GeomShapePtr aFaceShape = aFaceSelection->value();
314 if(!aFaceShape.get()) {
315 aFaceShape = aFaceSelection->context()->shape();
317 std::shared_ptr<GeomAPI_Face> aFace(new GeomAPI_Face(aFaceShape));
318 aFace = makeRectangularFace(aFace, aFace->getPlane());
321 AttributeSelectionPtr anAxisSelection = selection(AXIS());
322 GeomShapePtr anAxisShape = anAxisSelection->value();
323 if(!anAxisShape.get()) {
324 anAxisShape = anAxisSelection->context()->shape();
326 std::shared_ptr<GeomAPI_Edge> anEdge(new GeomAPI_Edge(anAxisShape));
328 std::shared_ptr<GeomAPI_Ax1> anAxis = std::shared_ptr<GeomAPI_Ax1>(new GeomAPI_Ax1(anEdge->line()->location(),
329 anEdge->line()->direction()));
332 double anAngle = real(ANGLE())->value();
334 GeomAlgoAPI_Rotation aRotationAlgo(aFace, anAxis, anAngle);
335 std::shared_ptr<GeomAPI_Face> aRes(new GeomAPI_Face(aRotationAlgo.shape()));
340 //==================================================================================================
341 std::shared_ptr<GeomAPI_Shape> ConstructionPlugin_Plane::createByTwoParallelPlanes()
344 AttributeSelectionPtr aFaceSelection1 = selection(PLANE1());
345 GeomShapePtr aFaceShape1 = aFaceSelection1->value();
346 if(!aFaceShape1.get()) {
347 aFaceShape1 = aFaceSelection1->context()->shape();
349 std::shared_ptr<GeomAPI_Face> aFace1(new GeomAPI_Face(aFaceShape1));
350 std::shared_ptr<GeomAPI_Pln> aPln1 = aFace1->getPlane();
353 AttributeSelectionPtr aFaceSelection2 = selection(PLANE2());
354 GeomShapePtr aFaceShape2 = aFaceSelection2->value();
355 if(!aFaceShape2.get()) {
356 aFaceShape2 = aFaceSelection2->context()->shape();
358 std::shared_ptr<GeomAPI_Face> aFace2(new GeomAPI_Face(aFaceShape2));
359 std::shared_ptr<GeomAPI_Pln> aPln2 = aFace2->getPlane();
361 double aDist = aPln1->distance(aPln2) / 2.0;
363 std::shared_ptr<GeomAPI_Pnt> aOrig1 = aPln1->location();
364 std::shared_ptr<GeomAPI_Dir> aDir1 = aPln1->direction();
366 aOrig1->translate(aDir1, aDist);
367 std::shared_ptr<GeomAPI_Pln> aNewPln(new GeomAPI_Pln(aOrig1, aDir1));
369 if((aNewPln->distance(aPln2) - aDist) > 1.e-7) {
371 aOrig1->translate(aDir1, 2.0 * aDist);
372 aNewPln.reset(new GeomAPI_Pln(aOrig1, aDir1));
375 std::shared_ptr<GeomAPI_Face> aRes = makeRectangularFace(aFace1, aNewPln);
380 //==================================================================================================
381 GeomShapePtr faceByThreeVertices(const std::shared_ptr<GeomAPI_Vertex> theV1,
382 const std::shared_ptr<GeomAPI_Vertex> theV2,
383 const std::shared_ptr<GeomAPI_Vertex> theV3)
385 std::shared_ptr<GeomAPI_Face> aFace = GeomAlgoAPI_FaceBuilder::planarFaceByThreeVertices(theV1, theV2, theV3);
387 ListOfShape anObjects;
388 anObjects.push_back(theV1);
389 anObjects.push_back(theV2);
390 anObjects.push_back(theV3);
391 std::list<std::shared_ptr<GeomAPI_Pnt> > aBoundingPoints = GeomAlgoAPI_ShapeTools::getBoundingBox(anObjects, 1.0);
392 GeomShapePtr aRes = GeomAlgoAPI_ShapeTools::fitPlaneToBox(aFace, aBoundingPoints);
397 //==================================================================================================
398 std::shared_ptr<GeomAPI_Face> makeRectangularFace(const std::shared_ptr<GeomAPI_Face> theFace,
399 const std::shared_ptr<GeomAPI_Pln> thePln)
401 // Create rectangular face close to the selected
402 double aXmin, aYmin, Zmin, aXmax, aYmax, Zmax;
403 theFace->computeSize(aXmin, aYmin, Zmin, aXmax, aYmax, Zmax);
405 // use all 8 points of the bounding box to find the 2D bounds
407 double aMinX2d, aMaxX2d, aMinY2d, aMaxY2d;
408 for(int aXIsMin = 0; aXIsMin < 2; aXIsMin++) {
409 for(int aYIsMin = 0; aYIsMin < 2; aYIsMin++) {
410 for(int aZIsMin = 0; aZIsMin < 2; aZIsMin++) {
411 std::shared_ptr<GeomAPI_Pnt> aPnt(new GeomAPI_Pnt(
412 aXIsMin ? aXmin : aXmax, aYIsMin ? aYmin : aYmax, aZIsMin ? Zmin : Zmax));
413 std::shared_ptr<GeomAPI_Pnt2d> aPnt2d = aPnt->to2D(thePln);
414 if (isFirst || aPnt2d->x() < aMinX2d)
415 aMinX2d = aPnt2d->x();
416 if (isFirst || aPnt2d->y() < aMinY2d)
417 aMinY2d = aPnt2d->y();
418 if (isFirst || aPnt2d->x() > aMaxX2d)
419 aMaxX2d = aPnt2d->x();
420 if (isFirst || aPnt2d->y() > aMaxY2d)
421 aMaxY2d = aPnt2d->y();
427 double aWgap = (aMaxX2d - aMinX2d) * 0.1;
428 double aHgap = (aMaxY2d - aMinY2d) * 0.1;
429 std::shared_ptr<GeomAPI_Face> aResFace = GeomAlgoAPI_FaceBuilder::planarFace(thePln,
430 aMinX2d - aWgap, aMinY2d - aHgap, aMaxX2d - aMinX2d + 2. * aWgap, aMaxY2d - aMinY2d + 2. * aHgap);