Salome HOME
Movement group uses solids, which are defined in extrusion/revolution/boolean. So...
[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()) {
99       aShape = aFaceAttr->context()->shape();
100     }
101
102     if (aShape.get() != NULL) {
103       std::shared_ptr<GeomAPI_Pln> aPln = GeomAlgoAPI_FaceBuilder::plane(aShape);
104       std::shared_ptr<GeomAPI_Pnt> aOrig = aPln->location();
105       std::shared_ptr<GeomAPI_Dir> aDir = aPln->direction();
106
107       aOrig->translate(aDir, aDist);
108       std::shared_ptr<GeomAPI_Pln> aNewPln = std::shared_ptr<GeomAPI_Pln>(
109           new GeomAPI_Pln(aOrig, aDir));
110
111       // Create rectangular face close to the selected
112       double aXmin, aYmin, Zmin, aXmax, aYmax, Zmax;
113       aShape->computeSize(aXmin, aYmin, Zmin, aXmax, aYmax, Zmax);
114
115       std::shared_ptr<GeomAPI_Pnt> aPnt1 = std::shared_ptr<GeomAPI_Pnt>(
116           new GeomAPI_Pnt(aXmin, aYmin, Zmin));
117       std::shared_ptr<GeomAPI_Pnt> aPnt2 = std::shared_ptr<GeomAPI_Pnt>(
118           new GeomAPI_Pnt(aXmax, aYmax, Zmax));
119
120       std::shared_ptr<GeomAPI_Pnt2d> aPnt2d1 = aPnt1->to2D(aNewPln);
121       std::shared_ptr<GeomAPI_Pnt2d> aPnt2d2 = aPnt2->to2D(aNewPln);
122
123       double aWidth = aPnt2d2->x() - aPnt2d1->x();
124       double aHeight = aPnt2d2->y() - aPnt2d1->y();
125       double aWgap = aWidth * 0.1;
126       double aHgap = aHeight * 0.1;
127
128       aPlane = GeomAlgoAPI_FaceBuilder::planarFace(aNewPln,
129                                                    aPnt2d1->x() - aWgap,
130                                                    aPnt2d1->y() - aHgap,
131                                                    aWidth + 2 * aWgap,
132                                                    aHeight + 2 * aHgap);
133     }
134   }
135   return aPlane;
136 }
137
138 std::shared_ptr<GeomAPI_Shape> ConstructionPlugin_Plane::createPlaneByGeneralEquation()
139 {
140   AttributeDoublePtr anAttrA = real(ConstructionPlugin_Plane::A());
141   AttributeDoublePtr anAttrB = real(ConstructionPlugin_Plane::B());
142   AttributeDoublePtr anAttrC = real(ConstructionPlugin_Plane::C());
143   AttributeDoublePtr anAttrD = real(ConstructionPlugin_Plane::D());
144   std::shared_ptr<GeomAPI_Shape> aPlaneFace;
145   if ((anAttrA.get() != NULL) && (anAttrB.get() != NULL) &&
146       (anAttrC.get() != NULL) && (anAttrD.get() != NULL) &&
147       anAttrA->isInitialized() && anAttrB->isInitialized() &&
148       anAttrC->isInitialized() && anAttrD->isInitialized() ) {
149     double aA = anAttrA->value(), aB = anAttrB->value(),
150            aC = anAttrC->value(), aD = anAttrD->value();
151     std::shared_ptr<GeomAPI_Pln> aPlane = 
152       std::shared_ptr<GeomAPI_Pln>(new GeomAPI_Pln(aA, aB, aC, aD));
153     std::string kDefaultPlaneSize = "200";
154     double aSize = Config_PropManager::integer("Sketch planes", "planes_size", kDefaultPlaneSize);
155     aSize *= 4.;
156     aPlaneFace = GeomAlgoAPI_FaceBuilder::square(aPlane, aSize, true);
157   }
158   return aPlaneFace;
159 }
160