Salome HOME
Change color for construction/body/group.
[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 <GeomAlgoAPI_FaceBuilder.h>
16
17 #include <GeomAPI_Pnt2d.h>
18
19
20 #define PLANE_SIZE 300
21
22 ConstructionPlugin_Plane::ConstructionPlugin_Plane()
23 {
24 }
25
26 void ConstructionPlugin_Plane::initAttributes()
27 {
28   data()->addAttribute(ConstructionPlugin_Plane::FACE(),  ModelAPI_AttributeSelection::type());
29   data()->addAttribute(ConstructionPlugin_Plane::DISTANCE(), ModelAPI_AttributeDouble::type());
30 }
31
32 void ConstructionPlugin_Plane::execute()
33 {
34   AttributeSelectionPtr aFaceAttr = data()->selection(ConstructionPlugin_Plane::FACE());
35   AttributeDoublePtr aDistAttr = data()->real(ConstructionPlugin_Plane::DISTANCE());
36   if ((aFaceAttr.get() != NULL) && (aDistAttr.get() != NULL) && 
37     aFaceAttr->isInitialized() && aDistAttr->isInitialized()) {
38
39     double aDist = aDistAttr->value();
40     GeomShapePtr aShape = aFaceAttr->value();
41     if (aShape.get() != NULL) {
42       std::shared_ptr<GeomAPI_Pln> aPln = GeomAlgoAPI_FaceBuilder::plane(aShape);
43       std::shared_ptr<GeomAPI_Pnt> aOrig = aPln->location();
44       std::shared_ptr<GeomAPI_Dir> aDir = aPln->direction();
45
46       aOrig->translate(aDir, aDist);
47       std::shared_ptr<GeomAPI_Pln> aNewPln = 
48         std::shared_ptr<GeomAPI_Pln>(new GeomAPI_Pln(aOrig, aDir));
49
50       // Create rectangular face close to the selected
51       double aXmin, aYmin, Zmin, aXmax, aYmax, Zmax;
52       aShape->computeSize(aXmin, aYmin, Zmin, aXmax, aYmax, Zmax);
53
54       std::shared_ptr<GeomAPI_Pnt> aPnt1 = 
55         std::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(aXmin, aYmin, Zmin));
56       std::shared_ptr<GeomAPI_Pnt> aPnt2 = 
57         std::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(aXmax, aYmax, Zmax));
58
59       std::shared_ptr<GeomAPI_Pnt2d> aPnt2d1 = aPnt1->to2D(aNewPln);
60       std::shared_ptr<GeomAPI_Pnt2d> aPnt2d2 = aPnt2->to2D(aNewPln);
61
62       double aWidth = aPnt2d2->x() - aPnt2d1->x();
63       double aHeight = aPnt2d2->y() - aPnt2d1->y();
64       double aWgap = aWidth * 0.1;
65       double aHgap = aHeight * 0.1;
66
67       std::shared_ptr<GeomAPI_Shape> aPlane = 
68         GeomAlgoAPI_FaceBuilder::planarFace(aNewPln, aPnt2d1->x() - aWgap, aPnt2d1->y() - aHgap, 
69                                             aWidth + 2 * aWgap, aHeight + 2 * aHgap);
70       ResultConstructionPtr aConstr = document()->createConstruction(data());
71       aConstr->setShape(aPlane);
72       setResult(aConstr);
73     }
74   }
75 }
76
77 bool ConstructionPlugin_Plane::customisePresentation(ResultPtr theResult, AISObjectPtr thePrs,
78                                                      std::shared_ptr<GeomAPI_ICustomPrs> theDefaultPrs)
79 {
80   std::vector<int> aColor;
81   // get color from the attribute of the result
82   if (theResult.get() != NULL && theResult->data()->attribute(ModelAPI_Result::COLOR_ID()).get() != NULL) {
83     AttributeIntArrayPtr aColorAttr = theResult->data()->intArray(ModelAPI_Result::COLOR_ID());
84     if (aColorAttr.get() && aColorAttr->size()) {
85       aColor.push_back(aColorAttr->value(0));
86       aColor.push_back(aColorAttr->value(1));
87       aColor.push_back(aColorAttr->value(2));
88     }
89   }
90   if (aColor.empty())
91     aColor = Config_PropManager::color("Visualization", "construction_plane_color",
92                                        ConstructionPlugin_Plane::DEFAULT_COLOR());
93
94   bool isCustomized = false;
95   if (aColor.size() == 3)
96     isCustomized = thePrs->setColor(aColor[0], aColor[1], aColor[2]);
97
98   isCustomized = thePrs->setTransparensy(0.6) || isCustomized;
99
100   return isCustomized;
101 }