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();
109 setError("Error: Could not create a plane.");
113 ResultConstructionPtr aConstr = document()->createConstruction(data());
114 aConstr->setInfinite(true);
115 aConstr->setShape(aShape);
119 //==================================================================================================
120 bool ConstructionPlugin_Plane::customisePresentation(ResultPtr theResult, AISObjectPtr thePrs,
121 std::shared_ptr<GeomAPI_ICustomPrs> theDefaultPrs)
123 std::vector<int> aColor;
124 // get color from the attribute of the result
125 if (theResult.get() != NULL && theResult->data()->attribute(ModelAPI_Result::COLOR_ID()).get() != NULL) {
126 AttributeIntArrayPtr aColorAttr = theResult->data()->intArray(ModelAPI_Result::COLOR_ID());
127 if (aColorAttr.get() && aColorAttr->size()) {
128 aColor.push_back(aColorAttr->value(0));
129 aColor.push_back(aColorAttr->value(1));
130 aColor.push_back(aColorAttr->value(2));
134 aColor = Config_PropManager::color("Visualization", "construction_plane_color",
135 ConstructionPlugin_Plane::DEFAULT_COLOR());
137 bool isCustomized = false;
138 if (aColor.size() == 3)
139 isCustomized = thePrs->setColor(aColor[0], aColor[1], aColor[2]);
141 isCustomized = thePrs->setTransparensy(0.6) || isCustomized;
146 //==================================================================================================
147 std::shared_ptr<GeomAPI_Shape> ConstructionPlugin_Plane::createByGeneralEquation()
149 AttributeDoublePtr anAttrA = real(ConstructionPlugin_Plane::A());
150 AttributeDoublePtr anAttrB = real(ConstructionPlugin_Plane::B());
151 AttributeDoublePtr anAttrC = real(ConstructionPlugin_Plane::C());
152 AttributeDoublePtr anAttrD = real(ConstructionPlugin_Plane::D());
153 std::shared_ptr<GeomAPI_Shape> aPlaneFace;
154 if ((anAttrA.get() != NULL) && (anAttrB.get() != NULL) &&
155 (anAttrC.get() != NULL) && (anAttrD.get() != NULL) &&
156 anAttrA->isInitialized() && anAttrB->isInitialized() &&
157 anAttrC->isInitialized() && anAttrD->isInitialized() ) {
158 double aA = anAttrA->value(), aB = anAttrB->value(),
159 aC = anAttrC->value(), aD = anAttrD->value();
160 std::shared_ptr<GeomAPI_Pln> aPlane =
161 std::shared_ptr<GeomAPI_Pln>(new GeomAPI_Pln(aA, aB, aC, aD));
162 std::string kDefaultPlaneSize = "200";
163 double aSize = Config_PropManager::integer(SKETCH_TAB_NAME, "planes_size", kDefaultPlaneSize);
165 aPlaneFace = GeomAlgoAPI_FaceBuilder::squareFace(aPlane, aSize);
170 //==================================================================================================
171 std::shared_ptr<GeomAPI_Shape> ConstructionPlugin_Plane::createByThreePoints()
174 AttributeSelectionPtr aPointSelection1 = selection(POINT1());
175 GeomShapePtr aPointShape1 = aPointSelection1->value();
176 if(!aPointShape1.get()) {
177 aPointShape1 = aPointSelection1->context()->shape();
179 std::shared_ptr<GeomAPI_Vertex> aVertex1(new GeomAPI_Vertex(aPointShape1));
182 AttributeSelectionPtr aPointSelection2 = selection(POINT2());
183 GeomShapePtr aPointShape2 = aPointSelection2->value();
184 if(!aPointShape2.get()) {
185 aPointShape2 = aPointSelection2->context()->shape();
187 std::shared_ptr<GeomAPI_Vertex> aVertex2(new GeomAPI_Vertex(aPointShape2));
190 AttributeSelectionPtr aPointSelection3 = selection(POINT3());
191 GeomShapePtr aPointShape3 = aPointSelection3->value();
192 if(!aPointShape3.get()) {
193 aPointShape3 = aPointSelection3->context()->shape();
195 std::shared_ptr<GeomAPI_Vertex> aVertex3(new GeomAPI_Vertex(aPointShape3));
197 GeomShapePtr aRes = faceByThreeVertices(aVertex1, aVertex2, aVertex3);
202 //==================================================================================================
203 std::shared_ptr<GeomAPI_Shape> ConstructionPlugin_Plane::createByLineAndPoint()
206 AttributeSelectionPtr anEdgeSelection = selection(LINE());
207 GeomShapePtr aLineShape = anEdgeSelection->value();
208 if(!aLineShape.get()) {
209 aLineShape = anEdgeSelection->context()->shape();
211 std::shared_ptr<GeomAPI_Edge> anEdge(new GeomAPI_Edge(aLineShape));
214 AttributeSelectionPtr aPointSelection = selection(POINT());
215 GeomShapePtr aPointShape = aPointSelection->value();
216 if(!aPointShape.get()) {
217 aPointShape = aPointSelection->context()->shape();
219 std::shared_ptr<GeomAPI_Vertex> aVertex(new GeomAPI_Vertex(aPointShape));
221 // Get perpendicular flag.
222 bool anIsPerpendicular= boolean(PERPENDICULAR())->value();
225 if(anIsPerpendicular) {
226 std::shared_ptr<GeomAPI_Lin> aLin = anEdge->line();
227 std::shared_ptr<GeomAPI_Pnt> aPnt = aVertex->point();
228 std::shared_ptr<GeomAPI_Pln> aNewPln(new GeomAPI_Pln(aPnt, aLin->direction()));
229 double aSize = aLin->distance(aPnt);
231 aRes = GeomAlgoAPI_FaceBuilder::squareFace(aNewPln, aSize);
233 std::shared_ptr<GeomAPI_Vertex> aV1, aV2;
234 GeomAlgoAPI_ShapeTools::findBounds(anEdge, aV1, aV2);
235 aRes = faceByThreeVertices(aV1, aV2, aVertex);
241 //==================================================================================================
242 std::shared_ptr<GeomAPI_Shape> ConstructionPlugin_Plane::createByDistanceFromOther()
244 AttributeSelectionPtr aFaceAttr = data()->selection(ConstructionPlugin_Plane::PLANE());
245 AttributeDoublePtr aDistAttr = data()->real(ConstructionPlugin_Plane::DISTANCE());
246 std::shared_ptr<GeomAPI_Shape> aPlane;
247 if ((aFaceAttr.get() != NULL) &&
248 (aDistAttr.get() != NULL) &&
249 aFaceAttr->isInitialized() && aDistAttr->isInitialized()) {
251 double aDist = aDistAttr->value();
252 bool anIsReverse = boolean(REVERSE())->value();
253 if(anIsReverse) aDist = -aDist;
254 GeomShapePtr aShape = aFaceAttr->value();
256 aShape = aFaceAttr->context()->shape();
263 std::shared_ptr<GeomAPI_Face> aFace(new GeomAPI_Face(aShape));
265 std::shared_ptr<GeomAPI_Pln> aPln = GeomAlgoAPI_FaceBuilder::plane(aFace);
266 std::shared_ptr<GeomAPI_Pnt> aOrig = aPln->location();
267 std::shared_ptr<GeomAPI_Dir> aDir = aPln->direction();
269 aOrig->translate(aDir, aDist);
270 std::shared_ptr<GeomAPI_Pln> aNewPln(new GeomAPI_Pln(aOrig, aDir));
272 aPlane = makeRectangularFace(aFace, aNewPln);
277 //==================================================================================================
278 std::shared_ptr<GeomAPI_Shape> ConstructionPlugin_Plane::createByCoincidentPoint()
281 AttributeSelectionPtr aFaceSelection = selection(PLANE());
282 GeomShapePtr aFaceShape = aFaceSelection->value();
283 if(!aFaceShape.get()) {
284 aFaceShape = aFaceSelection->context()->shape();
286 std::shared_ptr<GeomAPI_Face> aFace(new GeomAPI_Face(aFaceShape));
289 AttributeSelectionPtr aPointSelection = selection(COINCIDENT_POINT());
290 GeomShapePtr aPointShape = aPointSelection->value();
291 if(!aPointShape.get()) {
292 aPointShape = aPointSelection->context()->shape();
294 std::shared_ptr<GeomAPI_Vertex> aVertex(new GeomAPI_Vertex(aPointShape));
296 std::shared_ptr<GeomAPI_Pnt> anOrig = aVertex->point();
297 std::shared_ptr<GeomAPI_Pln> aPln = aFace->getPlane();
298 std::shared_ptr<GeomAPI_Dir> aDir = aPln->direction();
300 std::shared_ptr<GeomAPI_Pln> aNewPln(new GeomAPI_Pln(anOrig, aDir));
302 return makeRectangularFace(aFace, aNewPln);
305 //==================================================================================================
306 std::shared_ptr<GeomAPI_Shape> ConstructionPlugin_Plane::createByRotation()
309 AttributeSelectionPtr aFaceSelection = selection(PLANE());
310 GeomShapePtr aFaceShape = aFaceSelection->value();
311 if(!aFaceShape.get()) {
312 aFaceShape = aFaceSelection->context()->shape();
314 std::shared_ptr<GeomAPI_Face> aFace(new GeomAPI_Face(aFaceShape));
315 aFace = makeRectangularFace(aFace, aFace->getPlane());
318 AttributeSelectionPtr anAxisSelection = selection(AXIS());
319 GeomShapePtr anAxisShape = anAxisSelection->value();
320 if(!anAxisShape.get()) {
321 anAxisShape = anAxisSelection->context()->shape();
323 std::shared_ptr<GeomAPI_Edge> anEdge(new GeomAPI_Edge(anAxisShape));
325 std::shared_ptr<GeomAPI_Ax1> anAxis = std::shared_ptr<GeomAPI_Ax1>(new GeomAPI_Ax1(anEdge->line()->location(),
326 anEdge->line()->direction()));
329 double anAngle = real(ANGLE())->value();
331 GeomAlgoAPI_Rotation aRotationAlgo(aFace, anAxis, anAngle);
332 std::shared_ptr<GeomAPI_Face> aRes(new GeomAPI_Face(aRotationAlgo.shape()));
337 //==================================================================================================
338 std::shared_ptr<GeomAPI_Shape> ConstructionPlugin_Plane::createByTwoParallelPlanes()
341 AttributeSelectionPtr aFaceSelection1 = selection(PLANE1());
342 GeomShapePtr aFaceShape1 = aFaceSelection1->value();
343 if(!aFaceShape1.get()) {
344 aFaceShape1 = aFaceSelection1->context()->shape();
346 std::shared_ptr<GeomAPI_Face> aFace1(new GeomAPI_Face(aFaceShape1));
347 std::shared_ptr<GeomAPI_Pln> aPln1 = aFace1->getPlane();
350 AttributeSelectionPtr aFaceSelection2 = selection(PLANE2());
351 GeomShapePtr aFaceShape2 = aFaceSelection2->value();
352 if(!aFaceShape2.get()) {
353 aFaceShape2 = aFaceSelection2->context()->shape();
355 std::shared_ptr<GeomAPI_Face> aFace2(new GeomAPI_Face(aFaceShape2));
356 std::shared_ptr<GeomAPI_Pln> aPln2 = aFace2->getPlane();
358 double aDist = aPln1->distance(aPln2) / 2.0;
360 std::shared_ptr<GeomAPI_Pnt> aOrig1 = aPln1->location();
361 std::shared_ptr<GeomAPI_Dir> aDir1 = aPln1->direction();
363 aOrig1->translate(aDir1, aDist);
364 std::shared_ptr<GeomAPI_Pln> aNewPln(new GeomAPI_Pln(aOrig1, aDir1));
366 if((aNewPln->distance(aPln2) - aDist) > 1.e-7) {
368 aOrig1->translate(aDir1, 2.0 * aDist);
369 aNewPln.reset(new GeomAPI_Pln(aOrig1, aDir1));
372 std::shared_ptr<GeomAPI_Face> aRes = makeRectangularFace(aFace1, aNewPln);
377 //==================================================================================================
378 GeomShapePtr faceByThreeVertices(const std::shared_ptr<GeomAPI_Vertex> theV1,
379 const std::shared_ptr<GeomAPI_Vertex> theV2,
380 const std::shared_ptr<GeomAPI_Vertex> theV3)
382 std::shared_ptr<GeomAPI_Face> aFace = GeomAlgoAPI_FaceBuilder::planarFaceByThreeVertices(theV1, theV2, theV3);
384 ListOfShape anObjects;
385 anObjects.push_back(theV1);
386 anObjects.push_back(theV2);
387 anObjects.push_back(theV3);
388 std::list<std::shared_ptr<GeomAPI_Pnt> > aBoundingPoints = GeomAlgoAPI_ShapeTools::getBoundingBox(anObjects, 1.0);
389 GeomShapePtr aRes = GeomAlgoAPI_ShapeTools::fitPlaneToBox(aFace, aBoundingPoints);
394 //==================================================================================================
395 std::shared_ptr<GeomAPI_Face> makeRectangularFace(const std::shared_ptr<GeomAPI_Face> theFace,
396 const std::shared_ptr<GeomAPI_Pln> thePln)
398 // Create rectangular face close to the selected
399 double aXmin, aYmin, Zmin, aXmax, aYmax, Zmax;
400 theFace->computeSize(aXmin, aYmin, Zmin, aXmax, aYmax, Zmax);
402 // use all 8 points of the bounding box to find the 2D bounds
404 double aMinX2d, aMaxX2d, aMinY2d, aMaxY2d;
405 for(int aXIsMin = 0; aXIsMin < 2; aXIsMin++) {
406 for(int aYIsMin = 0; aYIsMin < 2; aYIsMin++) {
407 for(int aZIsMin = 0; aZIsMin < 2; aZIsMin++) {
408 std::shared_ptr<GeomAPI_Pnt> aPnt(new GeomAPI_Pnt(
409 aXIsMin ? aXmin : aXmax, aYIsMin ? aYmin : aYmax, aZIsMin ? Zmin : Zmax));
410 std::shared_ptr<GeomAPI_Pnt2d> aPnt2d = aPnt->to2D(thePln);
411 if (isFirst || aPnt2d->x() < aMinX2d)
412 aMinX2d = aPnt2d->x();
413 if (isFirst || aPnt2d->y() < aMinY2d)
414 aMinY2d = aPnt2d->y();
415 if (isFirst || aPnt2d->x() > aMaxX2d)
416 aMaxX2d = aPnt2d->x();
417 if (isFirst || aPnt2d->y() > aMaxY2d)
418 aMaxY2d = aPnt2d->y();
424 double aWgap = (aMaxX2d - aMinX2d) * 0.1;
425 double aHgap = (aMaxY2d - aMinY2d) * 0.1;
426 std::shared_ptr<GeomAPI_Face> aResFace = GeomAlgoAPI_FaceBuilder::planarFace(thePln,
427 aMinX2d - aWgap, aMinY2d - aHgap, aMaxX2d - aMinX2d + 2. * aWgap, aMaxY2d - aMinY2d + 2. * aHgap);