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());
68 data()->addAttribute(CREATION_METHOD_BY_OTHER_PLANE_OPTION(), ModelAPI_AttributeString::typeId());
69 data()->addAttribute(PLANE(), ModelAPI_AttributeSelection::typeId());
70 data()->addAttribute(DISTANCE(), ModelAPI_AttributeDouble::typeId());
71 data()->addAttribute(DISTANCE_REVERSE(), ModelAPI_AttributeBoolean::typeId());
72 data()->addAttribute(COINCIDENT_POINT(), ModelAPI_AttributeSelection::typeId());
73 data()->addAttribute(AXIS(), ModelAPI_AttributeSelection::typeId());
74 data()->addAttribute(ANGLE(), ModelAPI_AttributeDouble::typeId());
76 // By two parallel planes.
77 data()->addAttribute(PLANE1(), ModelAPI_AttributeSelection::typeId());
78 data()->addAttribute(PLANE2(), ModelAPI_AttributeSelection::typeId());
81 //==================================================================================================
82 void ConstructionPlugin_Plane::execute()
86 std::string aCreationMethod = string(CREATION_METHOD())->value();
87 if(aCreationMethod == CREATION_METHOD_BY_GENERAL_EQUATION()) {
88 aShape = createByGeneralEquation();
89 } else if(aCreationMethod == CREATION_METHOD_BY_THREE_POINTS()) {
90 aShape = createByThreePoints();
91 } else if(aCreationMethod == CREATION_METHOD_BY_LINE_AND_POINT()) {
92 aShape = createByLineAndPoint();
93 } else if(aCreationMethod == CREATION_METHOD_BY_OTHER_PLANE()) {
94 std::string aCreationMethodOption = string(CREATION_METHOD_BY_OTHER_PLANE_OPTION())->value();
95 if(aCreationMethodOption == CREATION_METHOD_BY_DISTANCE_FROM_OTHER()) {
96 aShape = createByDistanceFromOther();
97 } else if(aCreationMethodOption == CREATION_METHOD_BY_COINCIDENT_TO_POINT()) {
98 aShape = createByCoincidentPoint();
99 } else if(aCreationMethodOption == CREATION_METHOD_BY_ROTATION()) {
100 aShape = createByRotation();
102 } else if(aCreationMethod == CREATION_METHOD_BY_TWO_PARALLEL_PLANES()) {
103 aShape = createByTwoParallelPlanes();
107 setError("Error: Could not create a plane.");
111 ResultConstructionPtr aConstr = document()->createConstruction(data());
112 aConstr->setInfinite(true);
113 aConstr->setShape(aShape);
117 //==================================================================================================
118 bool ConstructionPlugin_Plane::customisePresentation(ResultPtr theResult, AISObjectPtr thePrs,
119 std::shared_ptr<GeomAPI_ICustomPrs> theDefaultPrs)
121 std::vector<int> aColor;
122 // get color from the attribute of the result
123 if (theResult.get() != NULL && theResult->data()->attribute(ModelAPI_Result::COLOR_ID()).get() != NULL) {
124 AttributeIntArrayPtr aColorAttr = theResult->data()->intArray(ModelAPI_Result::COLOR_ID());
125 if (aColorAttr.get() && aColorAttr->size()) {
126 aColor.push_back(aColorAttr->value(0));
127 aColor.push_back(aColorAttr->value(1));
128 aColor.push_back(aColorAttr->value(2));
132 aColor = Config_PropManager::color("Visualization", "construction_plane_color",
133 ConstructionPlugin_Plane::DEFAULT_COLOR());
135 bool isCustomized = false;
136 if (aColor.size() == 3)
137 isCustomized = thePrs->setColor(aColor[0], aColor[1], aColor[2]);
139 isCustomized = thePrs->setTransparensy(0.6) || isCustomized;
144 //==================================================================================================
145 std::shared_ptr<GeomAPI_Shape> ConstructionPlugin_Plane::createByGeneralEquation()
147 AttributeDoublePtr anAttrA = real(ConstructionPlugin_Plane::A());
148 AttributeDoublePtr anAttrB = real(ConstructionPlugin_Plane::B());
149 AttributeDoublePtr anAttrC = real(ConstructionPlugin_Plane::C());
150 AttributeDoublePtr anAttrD = real(ConstructionPlugin_Plane::D());
151 std::shared_ptr<GeomAPI_Shape> aPlaneFace;
152 if ((anAttrA.get() != NULL) && (anAttrB.get() != NULL) &&
153 (anAttrC.get() != NULL) && (anAttrD.get() != NULL) &&
154 anAttrA->isInitialized() && anAttrB->isInitialized() &&
155 anAttrC->isInitialized() && anAttrD->isInitialized() ) {
156 double aA = anAttrA->value(), aB = anAttrB->value(),
157 aC = anAttrC->value(), aD = anAttrD->value();
158 std::shared_ptr<GeomAPI_Pln> aPlane =
159 std::shared_ptr<GeomAPI_Pln>(new GeomAPI_Pln(aA, aB, aC, aD));
160 std::string kDefaultPlaneSize = "200";
161 double aSize = Config_PropManager::integer("Sketch planes", "planes_size", kDefaultPlaneSize);
163 aPlaneFace = GeomAlgoAPI_FaceBuilder::squareFace(aPlane, aSize);
168 //==================================================================================================
169 std::shared_ptr<GeomAPI_Shape> ConstructionPlugin_Plane::createByThreePoints()
172 AttributeSelectionPtr aPointSelection1 = selection(POINT1());
173 GeomShapePtr aPointShape1 = aPointSelection1->value();
174 if(!aPointShape1.get()) {
175 aPointShape1 = aPointSelection1->context()->shape();
177 std::shared_ptr<GeomAPI_Vertex> aVertex1(new GeomAPI_Vertex(aPointShape1));
180 AttributeSelectionPtr aPointSelection2 = selection(POINT2());
181 GeomShapePtr aPointShape2 = aPointSelection2->value();
182 if(!aPointShape2.get()) {
183 aPointShape2 = aPointSelection2->context()->shape();
185 std::shared_ptr<GeomAPI_Vertex> aVertex2(new GeomAPI_Vertex(aPointShape2));
188 AttributeSelectionPtr aPointSelection3 = selection(POINT3());
189 GeomShapePtr aPointShape3 = aPointSelection3->value();
190 if(!aPointShape3.get()) {
191 aPointShape3 = aPointSelection3->context()->shape();
193 std::shared_ptr<GeomAPI_Vertex> aVertex3(new GeomAPI_Vertex(aPointShape3));
195 GeomShapePtr aRes = faceByThreeVertices(aVertex1, aVertex2, aVertex3);
200 //==================================================================================================
201 std::shared_ptr<GeomAPI_Shape> ConstructionPlugin_Plane::createByLineAndPoint()
204 AttributeSelectionPtr anEdgeSelection = selection(LINE());
205 GeomShapePtr aLineShape = anEdgeSelection->value();
206 if(!aLineShape.get()) {
207 aLineShape = anEdgeSelection->context()->shape();
209 std::shared_ptr<GeomAPI_Edge> anEdge(new GeomAPI_Edge(aLineShape));
210 std::shared_ptr<GeomAPI_Vertex> aV1, aV2;
211 GeomAlgoAPI_ShapeTools::findBounds(anEdge, aV1, aV2);
215 AttributeSelectionPtr aPointSelection = selection(POINT());
216 GeomShapePtr aPointShape = aPointSelection->value();
217 if(!aPointShape.get()) {
218 aPointShape = aPointSelection->context()->shape();
220 std::shared_ptr<GeomAPI_Vertex> aVertex(new GeomAPI_Vertex(aPointShape));
222 GeomShapePtr aRes = faceByThreeVertices(aV1, aV2, aVertex);
227 //==================================================================================================
228 std::shared_ptr<GeomAPI_Shape> ConstructionPlugin_Plane::createByDistanceFromOther()
230 AttributeSelectionPtr aFaceAttr = data()->selection(ConstructionPlugin_Plane::PLANE());
231 AttributeDoublePtr aDistAttr = data()->real(ConstructionPlugin_Plane::DISTANCE());
232 std::shared_ptr<GeomAPI_Shape> aPlane;
233 if ((aFaceAttr.get() != NULL) &&
234 (aDistAttr.get() != NULL) &&
235 aFaceAttr->isInitialized() && aDistAttr->isInitialized()) {
237 double aDist = aDistAttr->value();
238 GeomShapePtr aShape = aFaceAttr->value();
240 aShape = aFaceAttr->context()->shape();
247 std::shared_ptr<GeomAPI_Face> aFace(new GeomAPI_Face(aShape));
249 std::shared_ptr<GeomAPI_Pln> aPln = GeomAlgoAPI_FaceBuilder::plane(aFace);
250 std::shared_ptr<GeomAPI_Pnt> aOrig = aPln->location();
251 std::shared_ptr<GeomAPI_Dir> aDir = aPln->direction();
253 aOrig->translate(aDir, aDist);
254 std::shared_ptr<GeomAPI_Pln> aNewPln(new GeomAPI_Pln(aOrig, aDir));
256 aPlane = makeRectangularFace(aFace, aNewPln);
261 //==================================================================================================
262 std::shared_ptr<GeomAPI_Shape> ConstructionPlugin_Plane::createByCoincidentPoint()
265 AttributeSelectionPtr aFaceSelection = selection(PLANE());
266 GeomShapePtr aFaceShape = aFaceSelection->value();
267 if(!aFaceShape.get()) {
268 aFaceShape = aFaceSelection->context()->shape();
270 std::shared_ptr<GeomAPI_Face> aFace(new GeomAPI_Face(aFaceShape));
273 AttributeSelectionPtr aPointSelection = selection(COINCIDENT_POINT());
274 GeomShapePtr aPointShape = aPointSelection->value();
275 if(!aPointShape.get()) {
276 aPointShape = aPointSelection->context()->shape();
278 std::shared_ptr<GeomAPI_Vertex> aVertex(new GeomAPI_Vertex(aPointShape));
280 std::shared_ptr<GeomAPI_Pnt> anOrig = aVertex->point();
281 std::shared_ptr<GeomAPI_Pln> aPln = aFace->getPlane();
282 std::shared_ptr<GeomAPI_Dir> aDir = aPln->direction();
284 std::shared_ptr<GeomAPI_Pln> aNewPln(new GeomAPI_Pln(anOrig, aDir));
286 return makeRectangularFace(aFace, aNewPln);
289 //==================================================================================================
290 std::shared_ptr<GeomAPI_Shape> ConstructionPlugin_Plane::createByRotation()
293 AttributeSelectionPtr aFaceSelection = selection(PLANE());
294 GeomShapePtr aFaceShape = aFaceSelection->value();
295 if(!aFaceShape.get()) {
296 aFaceShape = aFaceSelection->context()->shape();
298 std::shared_ptr<GeomAPI_Face> aFace(new GeomAPI_Face(aFaceShape));
301 AttributeSelectionPtr anAxisSelection = selection(AXIS());
302 GeomShapePtr anAxisShape = anAxisSelection->value();
303 if(!anAxisShape.get()) {
304 anAxisShape = anAxisSelection->context()->shape();
306 std::shared_ptr<GeomAPI_Edge> anEdge(new GeomAPI_Edge(anAxisShape));
308 std::shared_ptr<GeomAPI_Ax1> anAxis = std::shared_ptr<GeomAPI_Ax1>(new GeomAPI_Ax1(anEdge->line()->location(),
309 anEdge->line()->direction()));
312 double anAngle = real(ANGLE())->value();
314 GeomAlgoAPI_Rotation aRotationAlgo(aFace, anAxis, anAngle);
316 std::shared_ptr<GeomAPI_Face> aRes(new GeomAPI_Face(aRotationAlgo.shape()));
317 std::shared_ptr<GeomAPI_Pln> aNewPln = aRes->getPlane();
319 aRes = makeRectangularFace(aRes, aNewPln);
324 //==================================================================================================
325 std::shared_ptr<GeomAPI_Shape> ConstructionPlugin_Plane::createByTwoParallelPlanes()
328 AttributeSelectionPtr aFaceSelection1 = selection(PLANE1());
329 GeomShapePtr aFaceShape1 = aFaceSelection1->value();
330 if(!aFaceShape1.get()) {
331 aFaceShape1 = aFaceSelection1->context()->shape();
333 std::shared_ptr<GeomAPI_Face> aFace1(new GeomAPI_Face(aFaceShape1));
334 std::shared_ptr<GeomAPI_Pln> aPln1 = aFace1->getPlane();
337 AttributeSelectionPtr aFaceSelection2 = selection(PLANE2());
338 GeomShapePtr aFaceShape2 = aFaceSelection2->value();
339 if(!aFaceShape2.get()) {
340 aFaceShape2 = aFaceSelection2->context()->shape();
342 std::shared_ptr<GeomAPI_Face> aFace2(new GeomAPI_Face(aFaceShape2));
343 std::shared_ptr<GeomAPI_Pln> aPln2 = aFace2->getPlane();
345 double aDist = aPln1->distance(aPln2) / 2.0;
347 std::shared_ptr<GeomAPI_Pnt> aOrig1 = aPln1->location();
348 std::shared_ptr<GeomAPI_Dir> aDir1 = aPln1->direction();
350 aOrig1->translate(aDir1, aDist);
351 std::shared_ptr<GeomAPI_Pln> aNewPln(new GeomAPI_Pln(aOrig1, aDir1));
353 if((aNewPln->distance(aPln2) - aDist) > 1.e-7) {
355 aOrig1->translate(aDir1, 2.0 * aDist);
356 aNewPln.reset(new GeomAPI_Pln(aOrig1, aDir1));
359 std::shared_ptr<GeomAPI_Face> aRes = makeRectangularFace(aFace1, aNewPln);
364 //==================================================================================================
365 GeomShapePtr faceByThreeVertices(const std::shared_ptr<GeomAPI_Vertex> theV1,
366 const std::shared_ptr<GeomAPI_Vertex> theV2,
367 const std::shared_ptr<GeomAPI_Vertex> theV3)
369 std::shared_ptr<GeomAPI_Face> aFace = GeomAlgoAPI_FaceBuilder::planarFaceByThreeVertices(theV1, theV2, theV3);
371 ListOfShape anObjects;
372 anObjects.push_back(theV1);
373 anObjects.push_back(theV2);
374 anObjects.push_back(theV3);
375 std::list<std::shared_ptr<GeomAPI_Pnt> > aBoundingPoints = GeomAlgoAPI_ShapeTools::getBoundingBox(anObjects, 1.0);
376 GeomShapePtr aRes = GeomAlgoAPI_ShapeTools::fitPlaneToBox(aFace, aBoundingPoints);
381 //==================================================================================================
382 std::shared_ptr<GeomAPI_Face> makeRectangularFace(const std::shared_ptr<GeomAPI_Face> theFace,
383 const std::shared_ptr<GeomAPI_Pln> thePln)
385 // Create rectangular face close to the selected
386 double aXmin, aYmin, Zmin, aXmax, aYmax, Zmax;
387 theFace->computeSize(aXmin, aYmin, Zmin, aXmax, aYmax, Zmax);
389 // use all 8 points of the bounding box to find the 2D bounds
391 double aMinX2d, aMaxX2d, aMinY2d, aMaxY2d;
392 for(int aXIsMin = 0; aXIsMin < 2; aXIsMin++) {
393 for(int aYIsMin = 0; aYIsMin < 2; aYIsMin++) {
394 for(int aZIsMin = 0; aZIsMin < 2; aZIsMin++) {
395 std::shared_ptr<GeomAPI_Pnt> aPnt(new GeomAPI_Pnt(
396 aXIsMin ? aXmin : aXmax, aYIsMin ? aYmin : aYmax, aZIsMin ? Zmin : Zmax));
397 std::shared_ptr<GeomAPI_Pnt2d> aPnt2d = aPnt->to2D(thePln);
398 if (isFirst || aPnt2d->x() < aMinX2d)
399 aMinX2d = aPnt2d->x();
400 if (isFirst || aPnt2d->y() < aMinY2d)
401 aMinY2d = aPnt2d->y();
402 if (isFirst || aPnt2d->x() > aMaxX2d)
403 aMaxX2d = aPnt2d->x();
404 if (isFirst || aPnt2d->y() > aMaxY2d)
405 aMaxY2d = aPnt2d->y();
411 double aWgap = (aMaxX2d - aMinX2d) * 0.1;
412 double aHgap = (aMaxY2d - aMinY2d) * 0.1;
413 std::shared_ptr<GeomAPI_Face> aResFace = GeomAlgoAPI_FaceBuilder::planarFace(thePln,
414 aMinX2d - aWgap, aMinY2d - aHgap, aMaxX2d - aMinX2d + 2. * aWgap, aMaxY2d - aMinY2d + 2. * aHgap);