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_Lin.h>
18 #include <GeomAPI_Pln.h>
19 #include <GeomAPI_Pnt.h>
20 #include <GeomAPI_Pnt2d.h>
21 #include <GeomAPI_Vertex.h>
23 #include <ModelAPI_AttributeDouble.h>
24 #include <ModelAPI_AttributeIntArray.h>
25 #include <ModelAPI_AttributeSelection.h>
26 #include <ModelAPI_AttributeString.h>
27 #include <ModelAPI_AttributeBoolean.h>
28 #include <ModelAPI_ResultConstruction.h>
29 #include <ModelAPI_Session.h>
30 #include <ModelAPI_Validator.h>
32 static GeomShapePtr faceByThreeVertices(const std::shared_ptr<GeomAPI_Vertex> theV1,
33 const std::shared_ptr<GeomAPI_Vertex> theV2,
34 const std::shared_ptr<GeomAPI_Vertex> theV3);
35 static std::shared_ptr<GeomAPI_Face> makeRectangularFace(const std::shared_ptr<GeomAPI_Face> theFace,
36 const std::shared_ptr<GeomAPI_Pln> thePln);
38 //==================================================================================================
39 ConstructionPlugin_Plane::ConstructionPlugin_Plane()
43 //==================================================================================================
44 void ConstructionPlugin_Plane::initAttributes()
46 data()->addAttribute(ConstructionPlugin_Plane::CREATION_METHOD(), ModelAPI_AttributeString::typeId());
48 // By general equation.
49 data()->addAttribute(A(), ModelAPI_AttributeDouble::typeId());
50 data()->addAttribute(B(), ModelAPI_AttributeDouble::typeId());
51 data()->addAttribute(C(), ModelAPI_AttributeDouble::typeId());
52 data()->addAttribute(D(), ModelAPI_AttributeDouble::typeId());
53 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), A());
54 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), B());
55 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), C());
56 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), D());
59 data()->addAttribute(POINT1(), ModelAPI_AttributeSelection::typeId());
60 data()->addAttribute(POINT2(), ModelAPI_AttributeSelection::typeId());
61 data()->addAttribute(POINT3(), ModelAPI_AttributeSelection::typeId());
64 data()->addAttribute(LINE(), ModelAPI_AttributeSelection::typeId());
65 data()->addAttribute(POINT(), ModelAPI_AttributeSelection::typeId());
66 data()->addAttribute(PERPENDICULAR(), ModelAPI_AttributeBoolean::typeId());
69 data()->addAttribute(CREATION_METHOD_BY_OTHER_PLANE_OPTION(), ModelAPI_AttributeString::typeId());
70 data()->addAttribute(PLANE(), ModelAPI_AttributeSelection::typeId());
71 data()->addAttribute(DISTANCE(), ModelAPI_AttributeDouble::typeId());
72 data()->addAttribute(REVERSE(), ModelAPI_AttributeBoolean::typeId());
73 data()->addAttribute(COINCIDENT_POINT(), ModelAPI_AttributeSelection::typeId());
74 data()->addAttribute(AXIS(), ModelAPI_AttributeSelection::typeId());
75 data()->addAttribute(ANGLE(), ModelAPI_AttributeDouble::typeId());
77 // By two parallel planes.
78 data()->addAttribute(PLANE1(), ModelAPI_AttributeSelection::typeId());
79 data()->addAttribute(PLANE2(), ModelAPI_AttributeSelection::typeId());
82 //==================================================================================================
83 void ConstructionPlugin_Plane::execute()
87 std::string aCreationMethod = string(CREATION_METHOD())->value();
88 if(aCreationMethod == CREATION_METHOD_BY_GENERAL_EQUATION()) {
89 aShape = createByGeneralEquation();
90 } else if(aCreationMethod == CREATION_METHOD_BY_THREE_POINTS()) {
91 aShape = createByThreePoints();
92 } else if(aCreationMethod == CREATION_METHOD_BY_LINE_AND_POINT()) {
93 aShape = createByLineAndPoint();
94 } else if(aCreationMethod == CREATION_METHOD_BY_OTHER_PLANE()) {
95 std::string aCreationMethodOption = string(CREATION_METHOD_BY_OTHER_PLANE_OPTION())->value();
96 if(aCreationMethodOption == CREATION_METHOD_BY_DISTANCE_FROM_OTHER()) {
97 aShape = createByDistanceFromOther();
98 } else if(aCreationMethodOption == CREATION_METHOD_BY_COINCIDENT_TO_POINT()) {
99 aShape = createByCoincidentPoint();
100 } else if(aCreationMethodOption == CREATION_METHOD_BY_ROTATION()) {
101 aShape = createByRotation();
103 } else if(aCreationMethod == CREATION_METHOD_BY_TWO_PARALLEL_PLANES()) {
104 aShape = createByTwoParallelPlanes();
108 setError("Error: Could not create a plane.");
112 ResultConstructionPtr aConstr = document()->createConstruction(data());
113 aConstr->setInfinite(true);
114 aConstr->setShape(aShape);
118 //==================================================================================================
119 bool ConstructionPlugin_Plane::customisePresentation(ResultPtr theResult, AISObjectPtr thePrs,
120 std::shared_ptr<GeomAPI_ICustomPrs> theDefaultPrs)
122 std::vector<int> aColor;
123 // get color from the attribute of the result
124 if (theResult.get() != NULL && theResult->data()->attribute(ModelAPI_Result::COLOR_ID()).get() != NULL) {
125 AttributeIntArrayPtr aColorAttr = theResult->data()->intArray(ModelAPI_Result::COLOR_ID());
126 if (aColorAttr.get() && aColorAttr->size()) {
127 aColor.push_back(aColorAttr->value(0));
128 aColor.push_back(aColorAttr->value(1));
129 aColor.push_back(aColorAttr->value(2));
133 aColor = Config_PropManager::color("Visualization", "construction_plane_color",
134 ConstructionPlugin_Plane::DEFAULT_COLOR());
136 bool isCustomized = false;
137 if (aColor.size() == 3)
138 isCustomized = thePrs->setColor(aColor[0], aColor[1], aColor[2]);
140 isCustomized = thePrs->setTransparensy(0.6) || isCustomized;
145 //==================================================================================================
146 std::shared_ptr<GeomAPI_Shape> ConstructionPlugin_Plane::createByGeneralEquation()
148 AttributeDoublePtr anAttrA = real(ConstructionPlugin_Plane::A());
149 AttributeDoublePtr anAttrB = real(ConstructionPlugin_Plane::B());
150 AttributeDoublePtr anAttrC = real(ConstructionPlugin_Plane::C());
151 AttributeDoublePtr anAttrD = real(ConstructionPlugin_Plane::D());
152 std::shared_ptr<GeomAPI_Shape> aPlaneFace;
153 if ((anAttrA.get() != NULL) && (anAttrB.get() != NULL) &&
154 (anAttrC.get() != NULL) && (anAttrD.get() != NULL) &&
155 anAttrA->isInitialized() && anAttrB->isInitialized() &&
156 anAttrC->isInitialized() && anAttrD->isInitialized() ) {
157 double aA = anAttrA->value(), aB = anAttrB->value(),
158 aC = anAttrC->value(), aD = anAttrD->value();
159 std::shared_ptr<GeomAPI_Pln> aPlane =
160 std::shared_ptr<GeomAPI_Pln>(new GeomAPI_Pln(aA, aB, aC, aD));
161 std::string kDefaultPlaneSize = "200";
162 double aSize = Config_PropManager::integer(SKETCH_TAB_NAME, "planes_size", kDefaultPlaneSize);
164 aPlaneFace = GeomAlgoAPI_FaceBuilder::squareFace(aPlane, aSize);
169 //==================================================================================================
170 std::shared_ptr<GeomAPI_Shape> ConstructionPlugin_Plane::createByThreePoints()
173 AttributeSelectionPtr aPointSelection1 = selection(POINT1());
174 GeomShapePtr aPointShape1 = aPointSelection1->value();
175 if(!aPointShape1.get()) {
176 aPointShape1 = aPointSelection1->context()->shape();
178 std::shared_ptr<GeomAPI_Vertex> aVertex1(new GeomAPI_Vertex(aPointShape1));
181 AttributeSelectionPtr aPointSelection2 = selection(POINT2());
182 GeomShapePtr aPointShape2 = aPointSelection2->value();
183 if(!aPointShape2.get()) {
184 aPointShape2 = aPointSelection2->context()->shape();
186 std::shared_ptr<GeomAPI_Vertex> aVertex2(new GeomAPI_Vertex(aPointShape2));
189 AttributeSelectionPtr aPointSelection3 = selection(POINT3());
190 GeomShapePtr aPointShape3 = aPointSelection3->value();
191 if(!aPointShape3.get()) {
192 aPointShape3 = aPointSelection3->context()->shape();
194 std::shared_ptr<GeomAPI_Vertex> aVertex3(new GeomAPI_Vertex(aPointShape3));
196 GeomShapePtr aRes = faceByThreeVertices(aVertex1, aVertex2, aVertex3);
201 //==================================================================================================
202 std::shared_ptr<GeomAPI_Shape> ConstructionPlugin_Plane::createByLineAndPoint()
205 AttributeSelectionPtr anEdgeSelection = selection(LINE());
206 GeomShapePtr aLineShape = anEdgeSelection->value();
207 if(!aLineShape.get()) {
208 aLineShape = anEdgeSelection->context()->shape();
210 std::shared_ptr<GeomAPI_Edge> anEdge(new GeomAPI_Edge(aLineShape));
213 AttributeSelectionPtr aPointSelection = selection(POINT());
214 GeomShapePtr aPointShape = aPointSelection->value();
215 if(!aPointShape.get()) {
216 aPointShape = aPointSelection->context()->shape();
218 std::shared_ptr<GeomAPI_Vertex> aVertex(new GeomAPI_Vertex(aPointShape));
220 // Get perpendicular flag.
221 bool anIsPerpendicular= boolean(PERPENDICULAR())->value();
224 if(anIsPerpendicular) {
225 std::shared_ptr<GeomAPI_Lin> aLin = anEdge->line();
226 std::shared_ptr<GeomAPI_Pnt> aPnt = aVertex->point();
227 std::shared_ptr<GeomAPI_Pln> aNewPln(new GeomAPI_Pln(aPnt, aLin->direction()));
228 int aSize = aLin->distance(aPnt);
230 aRes = GeomAlgoAPI_FaceBuilder::squareFace(aNewPln, aSize);
232 std::shared_ptr<GeomAPI_Vertex> aV1, aV2;
233 GeomAlgoAPI_ShapeTools::findBounds(anEdge, aV1, aV2);
234 aRes = faceByThreeVertices(aV1, aV2, aVertex);
240 //==================================================================================================
241 std::shared_ptr<GeomAPI_Shape> ConstructionPlugin_Plane::createByDistanceFromOther()
243 AttributeSelectionPtr aFaceAttr = data()->selection(ConstructionPlugin_Plane::PLANE());
244 AttributeDoublePtr aDistAttr = data()->real(ConstructionPlugin_Plane::DISTANCE());
245 std::shared_ptr<GeomAPI_Shape> aPlane;
246 if ((aFaceAttr.get() != NULL) &&
247 (aDistAttr.get() != NULL) &&
248 aFaceAttr->isInitialized() && aDistAttr->isInitialized()) {
250 double aDist = aDistAttr->value();
251 GeomShapePtr aShape = aFaceAttr->value();
253 aShape = aFaceAttr->context()->shape();
260 std::shared_ptr<GeomAPI_Face> aFace(new GeomAPI_Face(aShape));
262 std::shared_ptr<GeomAPI_Pln> aPln = GeomAlgoAPI_FaceBuilder::plane(aFace);
263 std::shared_ptr<GeomAPI_Pnt> aOrig = aPln->location();
264 std::shared_ptr<GeomAPI_Dir> aDir = aPln->direction();
266 aOrig->translate(aDir, aDist);
267 std::shared_ptr<GeomAPI_Pln> aNewPln(new GeomAPI_Pln(aOrig, aDir));
269 aPlane = makeRectangularFace(aFace, aNewPln);
274 //==================================================================================================
275 std::shared_ptr<GeomAPI_Shape> ConstructionPlugin_Plane::createByCoincidentPoint()
278 AttributeSelectionPtr aFaceSelection = selection(PLANE());
279 GeomShapePtr aFaceShape = aFaceSelection->value();
280 if(!aFaceShape.get()) {
281 aFaceShape = aFaceSelection->context()->shape();
283 std::shared_ptr<GeomAPI_Face> aFace(new GeomAPI_Face(aFaceShape));
286 AttributeSelectionPtr aPointSelection = selection(COINCIDENT_POINT());
287 GeomShapePtr aPointShape = aPointSelection->value();
288 if(!aPointShape.get()) {
289 aPointShape = aPointSelection->context()->shape();
291 std::shared_ptr<GeomAPI_Vertex> aVertex(new GeomAPI_Vertex(aPointShape));
293 std::shared_ptr<GeomAPI_Pnt> anOrig = aVertex->point();
294 std::shared_ptr<GeomAPI_Pln> aPln = aFace->getPlane();
295 std::shared_ptr<GeomAPI_Dir> aDir = aPln->direction();
297 std::shared_ptr<GeomAPI_Pln> aNewPln(new GeomAPI_Pln(anOrig, aDir));
299 return makeRectangularFace(aFace, aNewPln);
302 //==================================================================================================
303 std::shared_ptr<GeomAPI_Shape> ConstructionPlugin_Plane::createByRotation()
306 AttributeSelectionPtr aFaceSelection = selection(PLANE());
307 GeomShapePtr aFaceShape = aFaceSelection->value();
308 if(!aFaceShape.get()) {
309 aFaceShape = aFaceSelection->context()->shape();
311 std::shared_ptr<GeomAPI_Face> aFace(new GeomAPI_Face(aFaceShape));
314 AttributeSelectionPtr anAxisSelection = selection(AXIS());
315 GeomShapePtr anAxisShape = anAxisSelection->value();
316 if(!anAxisShape.get()) {
317 anAxisShape = anAxisSelection->context()->shape();
319 std::shared_ptr<GeomAPI_Edge> anEdge(new GeomAPI_Edge(anAxisShape));
321 std::shared_ptr<GeomAPI_Ax1> anAxis = std::shared_ptr<GeomAPI_Ax1>(new GeomAPI_Ax1(anEdge->line()->location(),
322 anEdge->line()->direction()));
325 double anAngle = real(ANGLE())->value();
327 GeomAlgoAPI_Rotation aRotationAlgo(aFace, anAxis, anAngle);
329 std::shared_ptr<GeomAPI_Face> aRes(new GeomAPI_Face(aRotationAlgo.shape()));
330 std::shared_ptr<GeomAPI_Pln> aNewPln = aRes->getPlane();
332 aRes = makeRectangularFace(aRes, aNewPln);
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);