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_ResultConstruction.h>
28 #include <ModelAPI_Session.h>
29 #include <ModelAPI_Validator.h>
31 static GeomShapePtr faceByThreeVertices(const std::shared_ptr<GeomAPI_Vertex> theV1,
32 const std::shared_ptr<GeomAPI_Vertex> theV2,
33 const std::shared_ptr<GeomAPI_Vertex> theV3);
34 static std::shared_ptr<GeomAPI_Face> makeRectangularFace(const std::shared_ptr<GeomAPI_Face> theFace,
35 const std::shared_ptr<GeomAPI_Pln> thePln);
37 //==================================================================================================
38 ConstructionPlugin_Plane::ConstructionPlugin_Plane()
42 //==================================================================================================
43 void ConstructionPlugin_Plane::initAttributes()
45 data()->addAttribute(ConstructionPlugin_Plane::CREATION_METHOD(), ModelAPI_AttributeString::typeId());
47 // By general equation.
48 data()->addAttribute(A(), ModelAPI_AttributeDouble::typeId());
49 data()->addAttribute(B(), ModelAPI_AttributeDouble::typeId());
50 data()->addAttribute(C(), ModelAPI_AttributeDouble::typeId());
51 data()->addAttribute(D(), ModelAPI_AttributeDouble::typeId());
52 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), A());
53 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), B());
54 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), C());
55 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), D());
58 data()->addAttribute(POINT1(), ModelAPI_AttributeSelection::typeId());
59 data()->addAttribute(POINT2(), ModelAPI_AttributeSelection::typeId());
60 data()->addAttribute(POINT3(), ModelAPI_AttributeSelection::typeId());
63 data()->addAttribute(LINE(), ModelAPI_AttributeSelection::typeId());
64 data()->addAttribute(POINT(), ModelAPI_AttributeSelection::typeId());
67 data()->addAttribute(CREATION_METHOD_BY_OTHER_PLANE_OPTION(), ModelAPI_AttributeString::typeId());
68 data()->addAttribute(PLANE(), ModelAPI_AttributeSelection::typeId());
69 data()->addAttribute(DISTANCE(), ModelAPI_AttributeDouble::typeId());
70 data()->addAttribute(COINCIDENT_POINT(), ModelAPI_AttributeSelection::typeId());
71 data()->addAttribute(AXIS(), ModelAPI_AttributeSelection::typeId());
72 data()->addAttribute(ANGLE(), ModelAPI_AttributeDouble::typeId());
74 // By two parallel planes.
75 data()->addAttribute(PLANE1(), ModelAPI_AttributeSelection::typeId());
76 data()->addAttribute(PLANE2(), ModelAPI_AttributeSelection::typeId());
79 //==================================================================================================
80 void ConstructionPlugin_Plane::execute()
84 std::string aCreationMethod = string(CREATION_METHOD())->value();
85 if(aCreationMethod == CREATION_METHOD_BY_GENERAL_EQUATION()) {
86 aShape = createByGeneralEquation();
87 } else if(aCreationMethod == CREATION_METHOD_BY_THREE_POINTS()) {
88 aShape = createByThreePoints();
89 } else if(aCreationMethod == CREATION_METHOD_BY_LINE_AND_POINT()) {
90 aShape = createByLineAndPoint();
91 } else if(aCreationMethod == CREATION_METHOD_BY_OTHER_PLANE()) {
92 std::string aCreationMethodOption = string(CREATION_METHOD_BY_OTHER_PLANE_OPTION())->value();
93 if(aCreationMethodOption == CREATION_METHOD_BY_DISTANCE_FROM_OTHER()) {
94 aShape = createByDistanceFromOther();
95 } else if(aCreationMethodOption == CREATION_METHOD_BY_COINCIDENT_TO_POINT()) {
96 aShape = createByCoincidentPoint();
97 } else if(aCreationMethodOption == CREATION_METHOD_BY_ROTATION()) {
98 aShape = createByRotation();
100 } else if(aCreationMethod == CREATION_METHOD_BY_TWO_PARALLEL_PLANES()) {
101 aShape = createByTwoParallelPlanes();
105 setError("Error: Could not create a plane.");
109 ResultConstructionPtr aConstr = document()->createConstruction(data());
110 aConstr->setInfinite(true);
111 aConstr->setShape(aShape);
115 //==================================================================================================
116 bool ConstructionPlugin_Plane::customisePresentation(ResultPtr theResult, AISObjectPtr thePrs,
117 std::shared_ptr<GeomAPI_ICustomPrs> theDefaultPrs)
119 std::vector<int> aColor;
120 // get color from the attribute of the result
121 if (theResult.get() != NULL && theResult->data()->attribute(ModelAPI_Result::COLOR_ID()).get() != NULL) {
122 AttributeIntArrayPtr aColorAttr = theResult->data()->intArray(ModelAPI_Result::COLOR_ID());
123 if (aColorAttr.get() && aColorAttr->size()) {
124 aColor.push_back(aColorAttr->value(0));
125 aColor.push_back(aColorAttr->value(1));
126 aColor.push_back(aColorAttr->value(2));
130 aColor = Config_PropManager::color("Visualization", "construction_plane_color",
131 ConstructionPlugin_Plane::DEFAULT_COLOR());
133 bool isCustomized = false;
134 if (aColor.size() == 3)
135 isCustomized = thePrs->setColor(aColor[0], aColor[1], aColor[2]);
137 isCustomized = thePrs->setTransparensy(0.6) || isCustomized;
142 //==================================================================================================
143 std::shared_ptr<GeomAPI_Shape> ConstructionPlugin_Plane::createByGeneralEquation()
145 AttributeDoublePtr anAttrA = real(ConstructionPlugin_Plane::A());
146 AttributeDoublePtr anAttrB = real(ConstructionPlugin_Plane::B());
147 AttributeDoublePtr anAttrC = real(ConstructionPlugin_Plane::C());
148 AttributeDoublePtr anAttrD = real(ConstructionPlugin_Plane::D());
149 std::shared_ptr<GeomAPI_Shape> aPlaneFace;
150 if ((anAttrA.get() != NULL) && (anAttrB.get() != NULL) &&
151 (anAttrC.get() != NULL) && (anAttrD.get() != NULL) &&
152 anAttrA->isInitialized() && anAttrB->isInitialized() &&
153 anAttrC->isInitialized() && anAttrD->isInitialized() ) {
154 double aA = anAttrA->value(), aB = anAttrB->value(),
155 aC = anAttrC->value(), aD = anAttrD->value();
156 std::shared_ptr<GeomAPI_Pln> aPlane =
157 std::shared_ptr<GeomAPI_Pln>(new GeomAPI_Pln(aA, aB, aC, aD));
158 std::string kDefaultPlaneSize = "200";
159 double aSize = Config_PropManager::integer("Sketch planes", "planes_size", kDefaultPlaneSize);
161 aPlaneFace = GeomAlgoAPI_FaceBuilder::squareFace(aPlane, aSize);
166 //==================================================================================================
167 std::shared_ptr<GeomAPI_Shape> ConstructionPlugin_Plane::createByThreePoints()
170 AttributeSelectionPtr aPointSelection1 = selection(POINT1());
171 GeomShapePtr aPointShape1 = aPointSelection1->value();
172 if(!aPointShape1.get()) {
173 aPointShape1 = aPointSelection1->context()->shape();
175 std::shared_ptr<GeomAPI_Vertex> aVertex1(new GeomAPI_Vertex(aPointShape1));
178 AttributeSelectionPtr aPointSelection2 = selection(POINT2());
179 GeomShapePtr aPointShape2 = aPointSelection2->value();
180 if(!aPointShape2.get()) {
181 aPointShape2 = aPointSelection2->context()->shape();
183 std::shared_ptr<GeomAPI_Vertex> aVertex2(new GeomAPI_Vertex(aPointShape2));
186 AttributeSelectionPtr aPointSelection3 = selection(POINT3());
187 GeomShapePtr aPointShape3 = aPointSelection3->value();
188 if(!aPointShape3.get()) {
189 aPointShape3 = aPointSelection3->context()->shape();
191 std::shared_ptr<GeomAPI_Vertex> aVertex3(new GeomAPI_Vertex(aPointShape3));
193 GeomShapePtr aRes = faceByThreeVertices(aVertex1, aVertex2, aVertex3);
198 //==================================================================================================
199 std::shared_ptr<GeomAPI_Shape> ConstructionPlugin_Plane::createByLineAndPoint()
202 AttributeSelectionPtr anEdgeSelection = selection(LINE());
203 GeomShapePtr aLineShape = anEdgeSelection->value();
204 if(!aLineShape.get()) {
205 aLineShape = anEdgeSelection->context()->shape();
207 std::shared_ptr<GeomAPI_Edge> anEdge(new GeomAPI_Edge(aLineShape));
208 std::shared_ptr<GeomAPI_Vertex> aV1, aV2;
209 GeomAlgoAPI_ShapeTools::findBounds(anEdge, aV1, aV2);
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 GeomShapePtr aRes = faceByThreeVertices(aV1, aV2, aVertex);
225 //==================================================================================================
226 std::shared_ptr<GeomAPI_Shape> ConstructionPlugin_Plane::createByDistanceFromOther()
228 AttributeSelectionPtr aFaceAttr = data()->selection(ConstructionPlugin_Plane::PLANE());
229 AttributeDoublePtr aDistAttr = data()->real(ConstructionPlugin_Plane::DISTANCE());
230 std::shared_ptr<GeomAPI_Shape> aPlane;
231 if ((aFaceAttr.get() != NULL) &&
232 (aDistAttr.get() != NULL) &&
233 aFaceAttr->isInitialized() && aDistAttr->isInitialized()) {
235 double aDist = aDistAttr->value();
236 GeomShapePtr aShape = aFaceAttr->value();
238 aShape = aFaceAttr->context()->shape();
245 std::shared_ptr<GeomAPI_Face> aFace(new GeomAPI_Face(aShape));
247 std::shared_ptr<GeomAPI_Pln> aPln = GeomAlgoAPI_FaceBuilder::plane(aFace);
248 std::shared_ptr<GeomAPI_Pnt> aOrig = aPln->location();
249 std::shared_ptr<GeomAPI_Dir> aDir = aPln->direction();
251 aOrig->translate(aDir, aDist);
252 std::shared_ptr<GeomAPI_Pln> aNewPln(new GeomAPI_Pln(aOrig, aDir));
254 aPlane = makeRectangularFace(aFace, aNewPln);
259 //==================================================================================================
260 std::shared_ptr<GeomAPI_Shape> ConstructionPlugin_Plane::createByCoincidentPoint()
263 AttributeSelectionPtr aFaceSelection = selection(PLANE());
264 GeomShapePtr aFaceShape = aFaceSelection->value();
265 if(!aFaceShape.get()) {
266 aFaceShape = aFaceSelection->context()->shape();
268 std::shared_ptr<GeomAPI_Face> aFace(new GeomAPI_Face(aFaceShape));
271 AttributeSelectionPtr aPointSelection = selection(COINCIDENT_POINT());
272 GeomShapePtr aPointShape = aPointSelection->value();
273 if(!aPointShape.get()) {
274 aPointShape = aPointSelection->context()->shape();
276 std::shared_ptr<GeomAPI_Vertex> aVertex(new GeomAPI_Vertex(aPointShape));
278 std::shared_ptr<GeomAPI_Pnt> anOrig = aVertex->point();
279 std::shared_ptr<GeomAPI_Pln> aPln = aFace->getPlane();
280 std::shared_ptr<GeomAPI_Dir> aDir = aPln->direction();
282 std::shared_ptr<GeomAPI_Pln> aNewPln(new GeomAPI_Pln(anOrig, aDir));
284 return makeRectangularFace(aFace, aNewPln);
287 //==================================================================================================
288 std::shared_ptr<GeomAPI_Shape> ConstructionPlugin_Plane::createByRotation()
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 anAxisSelection = selection(AXIS());
300 GeomShapePtr anAxisShape = anAxisSelection->value();
301 if(!anAxisShape.get()) {
302 anAxisShape = anAxisSelection->context()->shape();
304 std::shared_ptr<GeomAPI_Edge> anEdge(new GeomAPI_Edge(anAxisShape));
306 std::shared_ptr<GeomAPI_Ax1> anAxis = std::shared_ptr<GeomAPI_Ax1>(new GeomAPI_Ax1(anEdge->line()->location(),
307 anEdge->line()->direction()));
310 double anAngle = real(ANGLE())->value();
312 GeomAlgoAPI_Rotation aRotationAlgo(aFace, anAxis, anAngle);
314 std::shared_ptr<GeomAPI_Face> aRes(new GeomAPI_Face(aRotationAlgo.shape()));
315 std::shared_ptr<GeomAPI_Pln> aNewPln = aRes->getPlane();
317 aRes = makeRectangularFace(aRes, aNewPln);
322 //==================================================================================================
323 std::shared_ptr<GeomAPI_Shape> ConstructionPlugin_Plane::createByTwoParallelPlanes()
326 AttributeSelectionPtr aFaceSelection1 = selection(PLANE1());
327 GeomShapePtr aFaceShape1 = aFaceSelection1->value();
328 if(!aFaceShape1.get()) {
329 aFaceShape1 = aFaceSelection1->context()->shape();
331 std::shared_ptr<GeomAPI_Face> aFace1(new GeomAPI_Face(aFaceShape1));
332 std::shared_ptr<GeomAPI_Pln> aPln1 = aFace1->getPlane();
335 AttributeSelectionPtr aFaceSelection2 = selection(PLANE2());
336 GeomShapePtr aFaceShape2 = aFaceSelection2->value();
337 if(!aFaceShape2.get()) {
338 aFaceShape2 = aFaceSelection2->context()->shape();
340 std::shared_ptr<GeomAPI_Face> aFace2(new GeomAPI_Face(aFaceShape2));
341 std::shared_ptr<GeomAPI_Pln> aPln2 = aFace2->getPlane();
343 double aDist = aPln1->distance(aPln2) / 2.0;
345 std::shared_ptr<GeomAPI_Pnt> aOrig1 = aPln1->location();
346 std::shared_ptr<GeomAPI_Dir> aDir1 = aPln1->direction();
348 aOrig1->translate(aDir1, aDist);
349 std::shared_ptr<GeomAPI_Pln> aNewPln(new GeomAPI_Pln(aOrig1, aDir1));
351 if((aNewPln->distance(aPln2) - aDist) > 1.e-7) {
353 aOrig1->translate(aDir1, 2.0 * aDist);
354 aNewPln.reset(new GeomAPI_Pln(aOrig1, aDir1));
357 std::shared_ptr<GeomAPI_Face> aRes = makeRectangularFace(aFace1, aNewPln);
362 //==================================================================================================
363 GeomShapePtr faceByThreeVertices(const std::shared_ptr<GeomAPI_Vertex> theV1,
364 const std::shared_ptr<GeomAPI_Vertex> theV2,
365 const std::shared_ptr<GeomAPI_Vertex> theV3)
367 std::shared_ptr<GeomAPI_Face> aFace = GeomAlgoAPI_FaceBuilder::planarFaceByThreeVertices(theV1, theV2, theV3);
369 ListOfShape anObjects;
370 anObjects.push_back(theV1);
371 anObjects.push_back(theV2);
372 anObjects.push_back(theV3);
373 std::list<std::shared_ptr<GeomAPI_Pnt> > aBoundingPoints = GeomAlgoAPI_ShapeTools::getBoundingBox(anObjects, 1.0);
374 GeomShapePtr aRes = GeomAlgoAPI_ShapeTools::fitPlaneToBox(aFace, aBoundingPoints);
379 //==================================================================================================
380 std::shared_ptr<GeomAPI_Face> makeRectangularFace(const std::shared_ptr<GeomAPI_Face> theFace,
381 const std::shared_ptr<GeomAPI_Pln> thePln)
383 // Create rectangular face close to the selected
384 double aXmin, aYmin, Zmin, aXmax, aYmax, Zmax;
385 theFace->computeSize(aXmin, aYmin, Zmin, aXmax, aYmax, Zmax);
387 // use all 8 points of the bounding box to find the 2D bounds
389 double aMinX2d, aMaxX2d, aMinY2d, aMaxY2d;
390 for(int aXIsMin = 0; aXIsMin < 2; aXIsMin++) {
391 for(int aYIsMin = 0; aYIsMin < 2; aYIsMin++) {
392 for(int aZIsMin = 0; aZIsMin < 2; aZIsMin++) {
393 std::shared_ptr<GeomAPI_Pnt> aPnt(new GeomAPI_Pnt(
394 aXIsMin ? aXmin : aXmax, aYIsMin ? aYmin : aYmax, aZIsMin ? Zmin : Zmax));
395 std::shared_ptr<GeomAPI_Pnt2d> aPnt2d = aPnt->to2D(thePln);
396 if (isFirst || aPnt2d->x() < aMinX2d)
397 aMinX2d = aPnt2d->x();
398 if (isFirst || aPnt2d->y() < aMinY2d)
399 aMinY2d = aPnt2d->y();
400 if (isFirst || aPnt2d->x() > aMaxX2d)
401 aMaxX2d = aPnt2d->x();
402 if (isFirst || aPnt2d->y() > aMaxY2d)
403 aMaxY2d = aPnt2d->y();
409 double aWgap = (aMaxX2d - aMinX2d) * 0.1;
410 double aHgap = (aMaxY2d - aMinY2d) * 0.1;
411 std::shared_ptr<GeomAPI_Face> aResFace = GeomAlgoAPI_FaceBuilder::planarFace(thePln,
412 aMinX2d - aWgap, aMinY2d - aHgap, aMaxX2d - aMinX2d + 2. * aWgap, aMaxY2d - aMinY2d + 2. * aHgap);