Salome HOME
Merge branch 'Dev_1.1.0' of newgeom:newgeom.git into Dev_1.1.0
[modules/shaper.git] / src / ConstructionPlugin / ConstructionPlugin_Plane.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        ConstructionPlugin_Plane.cpp
4 // Created:     12 Dec 2014
5 // Author:      Vitaly Smetannikov
6
7 #include "ConstructionPlugin_Plane.h"
8
9 #include <Config_PropManager.h>
10
11 #include <ModelAPI_AttributeSelection.h>
12 #include <ModelAPI_AttributeDouble.h>
13 #include <ModelAPI_ResultConstruction.h>
14 #include <ModelAPI_AttributeIntArray.h>
15 #include <ModelAPI_AttributeString.h>
16 #include <GeomAlgoAPI_FaceBuilder.h>
17
18 #include <GeomAPI_Pnt2d.h>
19
20 ConstructionPlugin_Plane::ConstructionPlugin_Plane()
21 {
22 }
23
24 void ConstructionPlugin_Plane::initAttributes()
25 {
26   data()->addAttribute(ConstructionPlugin_Plane::METHOD(), ModelAPI_AttributeString::type());
27   // Face & Distance
28   data()->addAttribute(ConstructionPlugin_Plane::FACE(),  ModelAPI_AttributeSelection::type());
29   data()->addAttribute(ConstructionPlugin_Plane::DISTANCE(), ModelAPI_AttributeDouble::type());
30   // General equation
31   data()->addAttribute(ConstructionPlugin_Plane::A(),  ModelAPI_AttributeDouble::type());
32   data()->addAttribute(ConstructionPlugin_Plane::B(),  ModelAPI_AttributeDouble::type());
33   data()->addAttribute(ConstructionPlugin_Plane::C(),  ModelAPI_AttributeDouble::type());
34   data()->addAttribute(ConstructionPlugin_Plane::D(),  ModelAPI_AttributeDouble::type());
35 }
36
37 void ConstructionPlugin_Plane::execute()
38 {
39   AttributeStringPtr aMethodTypeAttr = string(ConstructionPlugin_Plane::METHOD());
40   std::string aMethodType = aMethodTypeAttr->value();
41   std::shared_ptr<GeomAPI_Shape> aPlaneFace;
42   if (aMethodType == "PlaneByFaceAndDistance") {
43     aPlaneFace = createPlaneByFaceAndDistance();
44   } else if (aMethodType == "PlaneByGeneralEquation") {
45     aPlaneFace = createPlaneByGeneralEquation();
46   }
47   if (!aPlaneFace.get())
48     return;
49   ResultConstructionPtr aConstr = document()->createConstruction(data());
50   aConstr->setShape(aPlaneFace);
51   setResult(aConstr);
52 }
53
54 bool ConstructionPlugin_Plane::customisePresentation(ResultPtr theResult, AISObjectPtr thePrs,
55                                                      std::shared_ptr<GeomAPI_ICustomPrs> theDefaultPrs)
56 {
57   std::vector<int> aColor;
58   // get color from the attribute of the result
59   if (theResult.get() != NULL && theResult->data()->attribute(ModelAPI_Result::COLOR_ID()).get() != NULL) {
60     AttributeIntArrayPtr aColorAttr = theResult->data()->intArray(ModelAPI_Result::COLOR_ID());
61     if (aColorAttr.get() && aColorAttr->size()) {
62       aColor.push_back(aColorAttr->value(0));
63       aColor.push_back(aColorAttr->value(1));
64       aColor.push_back(aColorAttr->value(2));
65     }
66   }
67   if (aColor.empty())
68     aColor = Config_PropManager::color("Visualization", "construction_plane_color",
69                                        ConstructionPlugin_Plane::DEFAULT_COLOR());
70
71   bool isCustomized = false;
72   if (aColor.size() == 3)
73     isCustomized = thePrs->setColor(aColor[0], aColor[1], aColor[2]);
74
75   isCustomized = thePrs->setTransparensy(0.6) || isCustomized;
76
77   return isCustomized;
78 }
79
80 std::shared_ptr<GeomAPI_Shape>  ConstructionPlugin_Plane::createPlaneByFaceAndDistance()
81 {
82   AttributeSelectionPtr aFaceAttr = data()->selection(ConstructionPlugin_Plane::FACE());
83   AttributeDoublePtr aDistAttr = data()->real(ConstructionPlugin_Plane::DISTANCE());
84   std::shared_ptr<GeomAPI_Shape> aPlane;
85   if ((aFaceAttr.get() != NULL) &&
86       (aDistAttr.get() != NULL) &&
87       aFaceAttr->isInitialized() && aDistAttr->isInitialized()) {
88
89     double aDist = aDistAttr->value();
90     GeomShapePtr aShape = aFaceAttr->value();
91     if (aShape.get() != NULL) {
92       std::shared_ptr<GeomAPI_Pln> aPln = GeomAlgoAPI_FaceBuilder::plane(aShape);
93       std::shared_ptr<GeomAPI_Pnt> aOrig = aPln->location();
94       std::shared_ptr<GeomAPI_Dir> aDir = aPln->direction();
95
96       aOrig->translate(aDir, aDist);
97       std::shared_ptr<GeomAPI_Pln> aNewPln = std::shared_ptr<GeomAPI_Pln>(
98           new GeomAPI_Pln(aOrig, aDir));
99
100       // Create rectangular face close to the selected
101       double aXmin, aYmin, Zmin, aXmax, aYmax, Zmax;
102       aShape->computeSize(aXmin, aYmin, Zmin, aXmax, aYmax, Zmax);
103
104       std::shared_ptr<GeomAPI_Pnt> aPnt1 = std::shared_ptr<GeomAPI_Pnt>(
105           new GeomAPI_Pnt(aXmin, aYmin, Zmin));
106       std::shared_ptr<GeomAPI_Pnt> aPnt2 = std::shared_ptr<GeomAPI_Pnt>(
107           new GeomAPI_Pnt(aXmax, aYmax, Zmax));
108
109       std::shared_ptr<GeomAPI_Pnt2d> aPnt2d1 = aPnt1->to2D(aNewPln);
110       std::shared_ptr<GeomAPI_Pnt2d> aPnt2d2 = aPnt2->to2D(aNewPln);
111
112       double aWidth = aPnt2d2->x() - aPnt2d1->x();
113       double aHeight = aPnt2d2->y() - aPnt2d1->y();
114       double aWgap = aWidth * 0.1;
115       double aHgap = aHeight * 0.1;
116
117       aPlane = GeomAlgoAPI_FaceBuilder::planarFace(aNewPln,
118                                                    aPnt2d1->x() - aWgap,
119                                                    aPnt2d1->y() - aHgap,
120                                                    aWidth + 2 * aWgap,
121                                                    aHeight + 2 * aHgap);
122     }
123   }
124   return aPlane;
125 }
126
127 std::shared_ptr<GeomAPI_Shape> ConstructionPlugin_Plane::createPlaneByGeneralEquation()
128 {
129   AttributeDoublePtr anAttrA = real(ConstructionPlugin_Plane::A());
130   AttributeDoublePtr anAttrB = real(ConstructionPlugin_Plane::B());
131   AttributeDoublePtr anAttrC = real(ConstructionPlugin_Plane::C());
132   AttributeDoublePtr anAttrD = real(ConstructionPlugin_Plane::D());
133   std::shared_ptr<GeomAPI_Shape> aPlaneFace;
134   if ((anAttrA.get() != NULL) && (anAttrB.get() != NULL) &&
135       (anAttrC.get() != NULL) && (anAttrD.get() != NULL) &&
136       anAttrA->isInitialized() && anAttrB->isInitialized() &&
137       anAttrC->isInitialized() && anAttrD->isInitialized() ) {
138     double aA = anAttrA->value(), aB = anAttrB->value(),
139            aC = anAttrC->value(), aD = anAttrD->value();
140     std::shared_ptr<GeomAPI_Pln> aPlane = std::shared_ptr<GeomAPI_Pln>(new GeomAPI_Pln(aA, aB, aC, aD));
141     std::string kDefaultPlaneSize = "200";
142     double aSize = Config_PropManager::integer("Sketch planes", "planes_size", kDefaultPlaneSize);
143     aSize *= 4.;
144     aPlaneFace = GeomAlgoAPI_FaceBuilder::square(aPlane, aSize);
145   }
146   return aPlaneFace;
147 }
148