+//==================================================================================================
+std::shared_ptr<GeomAPI_Shape> ConstructionPlugin_Plane::createByThreePoints()
+{
+ // Get first point.
+ AttributeSelectionPtr aPointSelection1 = selection(POINT1());
+ GeomShapePtr aPointShape1 = aPointSelection1->value();
+ if(!aPointShape1.get()) {
+ aPointShape1 = aPointSelection1->context()->shape();
+ }
+ std::shared_ptr<GeomAPI_Vertex> aVertex1(new GeomAPI_Vertex(aPointShape1));
+
+ // Get second point.
+ AttributeSelectionPtr aPointSelection2 = selection(POINT2());
+ GeomShapePtr aPointShape2 = aPointSelection2->value();
+ if(!aPointShape2.get()) {
+ aPointShape2 = aPointSelection2->context()->shape();
+ }
+ std::shared_ptr<GeomAPI_Vertex> aVertex2(new GeomAPI_Vertex(aPointShape2));
+
+ // Get third point.
+ AttributeSelectionPtr aPointSelection3 = selection(POINT3());
+ GeomShapePtr aPointShape3 = aPointSelection3->value();
+ if(!aPointShape3.get()) {
+ aPointShape3 = aPointSelection3->context()->shape();
+ }
+ std::shared_ptr<GeomAPI_Vertex> aVertex3(new GeomAPI_Vertex(aPointShape3));
+
+ GeomShapePtr aRes = faceByThreeVertices(aVertex1, aVertex2, aVertex3);
+
+ return aRes;
+}
+
+//==================================================================================================
+std::shared_ptr<GeomAPI_Shape> ConstructionPlugin_Plane::createByLineAndPoint()
+{
+ // Get edge.
+ AttributeSelectionPtr anEdgeSelection = selection(LINE());
+ GeomShapePtr aLineShape = anEdgeSelection->value();
+ if(!aLineShape.get()) {
+ aLineShape = anEdgeSelection->context()->shape();
+ }
+ std::shared_ptr<GeomAPI_Edge> anEdge(new GeomAPI_Edge(aLineShape));
+
+ // Get point.
+ AttributeSelectionPtr aPointSelection = selection(POINT());
+ GeomShapePtr aPointShape = aPointSelection->value();
+ if(!aPointShape.get()) {
+ aPointShape = aPointSelection->context()->shape();
+ }
+ std::shared_ptr<GeomAPI_Vertex> aVertex(new GeomAPI_Vertex(aPointShape));
+
+ // Get perpendicular flag.
+ bool anIsPerpendicular= boolean(PERPENDICULAR())->value();
+
+ GeomShapePtr aRes;
+ if(anIsPerpendicular) {
+ std::shared_ptr<GeomAPI_Lin> aLin = anEdge->line();
+ std::shared_ptr<GeomAPI_Pnt> aPnt = aVertex->point();
+ std::shared_ptr<GeomAPI_Pln> aNewPln(new GeomAPI_Pln(aPnt, aLin->direction()));
+ double aSize = aLin->distance(aPnt) * 2;
+ // point may belong to line, so for the face size use maximum distance between point and line
+ // and the line size (distance between the start and end point)
+ double aDistance = anEdge->firstPoint()->distance(anEdge->lastPoint());
+ aRes = GeomAlgoAPI_FaceBuilder::squareFace(aNewPln, aSize > aDistance ? aSize : aDistance);
+ } else {
+ std::shared_ptr<GeomAPI_Vertex> aV1, aV2;
+ GeomAlgoAPI_ShapeTools::findBounds(anEdge, aV1, aV2);
+ aRes = faceByThreeVertices(aV1, aV2, aVertex);
+ }
+
+ return aRes;
+}
+
+//==================================================================================================
+std::shared_ptr<GeomAPI_Shape> ConstructionPlugin_Plane::createByDistanceFromOther()
+{
+ AttributeSelectionPtr aFaceAttr = data()->selection(ConstructionPlugin_Plane::PLANE());
+ AttributeDoublePtr aDistAttr = data()->real(ConstructionPlugin_Plane::DISTANCE());
+ std::shared_ptr<GeomAPI_Shape> aPlane;
+ if ((aFaceAttr.get() != NULL) &&
+ (aDistAttr.get() != NULL) &&
+ aFaceAttr->isInitialized() && aDistAttr->isInitialized()) {
+
+ double aDist = aDistAttr->value();
+ bool anIsReverse = boolean(REVERSE())->value();
+ if(anIsReverse) aDist = -aDist;
+ GeomShapePtr aShape = aFaceAttr->value();
+ if (!aShape.get()) {
+ aShape = aFaceAttr->context()->shape();
+ }
+
+ if(!aShape.get()) {
+ return aPlane;
+ }
+
+ std::shared_ptr<GeomAPI_Face> aFace(new GeomAPI_Face(aShape));
+
+ std::shared_ptr<GeomAPI_Pln> aPln = aFace->getPlane();
+ std::shared_ptr<GeomAPI_Pnt> aOrig = aPln->location();
+ std::shared_ptr<GeomAPI_Dir> aDir = aPln->direction();
+
+ aOrig->translate(aDir, aDist);
+ std::shared_ptr<GeomAPI_Pln> aNewPln(new GeomAPI_Pln(aOrig, aDir));
+
+ aPlane = makeRectangularFace(aFace, aNewPln);
+ }
+ return aPlane;
+}
+
+//==================================================================================================
+std::shared_ptr<GeomAPI_Shape> ConstructionPlugin_Plane::createByCoincidentPoint()
+{
+ // Get face.
+ AttributeSelectionPtr aFaceSelection = selection(PLANE());
+ GeomShapePtr aFaceShape = aFaceSelection->value();
+ if(!aFaceShape.get()) {
+ aFaceShape = aFaceSelection->context()->shape();
+ }
+ std::shared_ptr<GeomAPI_Face> aFace(new GeomAPI_Face(aFaceShape));
+
+ // Get point.
+ AttributeSelectionPtr aPointSelection = selection(COINCIDENT_POINT());
+ GeomShapePtr aPointShape = aPointSelection->value();
+ if(!aPointShape.get()) {
+ aPointShape = aPointSelection->context()->shape();
+ }
+ std::shared_ptr<GeomAPI_Vertex> aVertex(new GeomAPI_Vertex(aPointShape));
+
+ std::shared_ptr<GeomAPI_Pnt> anOrig = aVertex->point();
+ std::shared_ptr<GeomAPI_Pln> aPln = aFace->getPlane();
+ std::shared_ptr<GeomAPI_Dir> aDir = aPln->direction();
+
+ std::shared_ptr<GeomAPI_Pln> aNewPln(new GeomAPI_Pln(anOrig, aDir));
+
+ return makeRectangularFace(aFace, aNewPln);
+}
+
+//==================================================================================================
+std::shared_ptr<GeomAPI_Shape> ConstructionPlugin_Plane::createByRotation()
+{
+ // Get face.
+ AttributeSelectionPtr aFaceSelection = selection(PLANE());
+ GeomShapePtr aFaceShape = aFaceSelection->value();
+ if(!aFaceShape.get()) {
+ aFaceShape = aFaceSelection->context()->shape();
+ }
+ std::shared_ptr<GeomAPI_Face> aFace(new GeomAPI_Face(aFaceShape));
+ aFace = makeRectangularFace(aFace, aFace->getPlane());
+
+ // Get axis.
+ AttributeSelectionPtr anAxisSelection = selection(AXIS());
+ GeomShapePtr anAxisShape = anAxisSelection->value();
+ if(!anAxisShape.get()) {
+ anAxisShape = anAxisSelection->context()->shape();
+ }
+ std::shared_ptr<GeomAPI_Edge> anEdge(new GeomAPI_Edge(anAxisShape));
+
+ std::shared_ptr<GeomAPI_Ax1> anAxis =
+ std::shared_ptr<GeomAPI_Ax1>(new GeomAPI_Ax1(anEdge->line()->location(),
+ anEdge->line()->direction()));
+
+ // Getting angle.
+ double anAngle = real(ANGLE())->value();
+
+ GeomAlgoAPI_Rotation aRotationAlgo(aFace, anAxis, anAngle);
+ if (!aRotationAlgo.check()) {
+ setError(aRotationAlgo.getError());
+ return GeomShapePtr();
+ }
+ aRotationAlgo.build();
+ if (!aRotationAlgo.isDone()) {
+ setError("Error: Failed to rotate plane");
+ return GeomShapePtr();
+ }
+
+ std::shared_ptr<GeomAPI_Face> aRes(new GeomAPI_Face(aRotationAlgo.shape()));
+ return aRes;
+}
+
+//==================================================================================================
+std::shared_ptr<GeomAPI_Shape> ConstructionPlugin_Plane::createByTwoParallelPlanes()
+{
+ // Get plane 1.
+ AttributeSelectionPtr aFaceSelection1 = selection(PLANE1());
+ GeomShapePtr aFaceShape1 = aFaceSelection1->value();
+ if(!aFaceShape1.get()) {
+ aFaceShape1 = aFaceSelection1->context()->shape();
+ }
+ std::shared_ptr<GeomAPI_Face> aFace1(new GeomAPI_Face(aFaceShape1));
+ std::shared_ptr<GeomAPI_Pln> aPln1 = aFace1->getPlane();
+
+ // Get plane 2.
+ AttributeSelectionPtr aFaceSelection2 = selection(PLANE2());
+ GeomShapePtr aFaceShape2 = aFaceSelection2->value();
+ if(!aFaceShape2.get()) {
+ aFaceShape2 = aFaceSelection2->context()->shape();
+ }
+ std::shared_ptr<GeomAPI_Face> aFace2(new GeomAPI_Face(aFaceShape2));
+ std::shared_ptr<GeomAPI_Pln> aPln2 = aFace2->getPlane();
+
+ std::shared_ptr<GeomAPI_Pnt> anOrig1 = aPln1->location();
+ std::shared_ptr<GeomAPI_Pnt> aPntOnPln2 = aPln2->project(anOrig1);
+
+ std::shared_ptr<GeomAPI_Pnt> aNewOrig(new GeomAPI_Pnt(anOrig1->xyz()->added(
+ aPntOnPln2->xyz())->multiplied(0.5)));
+
+ std::shared_ptr<GeomAPI_Pln> aNewPln(new GeomAPI_Pln(aNewOrig, aPln1->direction()));
+
+ std::shared_ptr<GeomAPI_Face> aRes = makeRectangularFace(aFace1, aNewPln);
+
+ return aRes;
+}
+
+//==================================================================================================
+GeomShapePtr faceByThreeVertices(const std::shared_ptr<GeomAPI_Vertex> theV1,
+ const std::shared_ptr<GeomAPI_Vertex> theV2,
+ const std::shared_ptr<GeomAPI_Vertex> theV3)
+{
+ std::shared_ptr<GeomAPI_Face> aFace =
+ GeomAlgoAPI_FaceBuilder::planarFaceByThreeVertices(theV1, theV2, theV3);
+
+ ListOfShape anObjects;
+ anObjects.push_back(theV1);
+ anObjects.push_back(theV2);
+ anObjects.push_back(theV3);
+ std::list<std::shared_ptr<GeomAPI_Pnt> > aBoundingPoints =
+ GeomAlgoAPI_ShapeTools::getBoundingBox(anObjects, 1.0);
+ GeomShapePtr aRes = GeomAlgoAPI_ShapeTools::fitPlaneToBox(aFace, aBoundingPoints);
+
+ return aRes;
+}
+
+//==================================================================================================
+std::shared_ptr<GeomAPI_Face> makeRectangularFace(const std::shared_ptr<GeomAPI_Face> theFace,
+ const std::shared_ptr<GeomAPI_Pln> thePln)
+{
+ // Create rectangular face close to the selected
+ double aXmin, aYmin, Zmin, aXmax, aYmax, Zmax;
+ theFace->computeSize(aXmin, aYmin, Zmin, aXmax, aYmax, Zmax);
+
+ // use all 8 points of the bounding box to find the 2D bounds
+ bool isFirst = true;
+ double aMinX2d, aMaxX2d, aMinY2d, aMaxY2d;
+ for(int aXIsMin = 0; aXIsMin < 2; aXIsMin++) {
+ for(int aYIsMin = 0; aYIsMin < 2; aYIsMin++) {
+ for(int aZIsMin = 0; aZIsMin < 2; aZIsMin++) {
+ std::shared_ptr<GeomAPI_Pnt> aPnt(new GeomAPI_Pnt(
+ aXIsMin ? aXmin : aXmax, aYIsMin ? aYmin : aYmax, aZIsMin ? Zmin : Zmax));
+ std::shared_ptr<GeomAPI_Pnt2d> aPnt2d = aPnt->to2D(thePln);
+ if (isFirst || aPnt2d->x() < aMinX2d)
+ aMinX2d = aPnt2d->x();
+ if (isFirst || aPnt2d->y() < aMinY2d)
+ aMinY2d = aPnt2d->y();
+ if (isFirst || aPnt2d->x() > aMaxX2d)
+ aMaxX2d = aPnt2d->x();
+ if (isFirst || aPnt2d->y() > aMaxY2d)
+ aMaxY2d = aPnt2d->y();
+ if (isFirst)
+ isFirst = !isFirst;
+ }
+ }
+ }
+ double aWgap = (aMaxX2d - aMinX2d) * 0.1;
+ double aHgap = (aMaxY2d - aMinY2d) * 0.1;
+ std::shared_ptr<GeomAPI_Face> aResFace = GeomAlgoAPI_FaceBuilder::planarFace(thePln,
+ aMinX2d - aWgap, aMinY2d - aHgap, aMaxX2d - aMinX2d + 2. * aWgap,
+ aMaxY2d - aMinY2d + 2. * aHgap);
+
+ return aResFace;
+}