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