1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 // File: ConstructionPlugin_Plane.cpp
4 // Created: 12 Dec 2014
5 // Author: Vitaly Smetannikov
7 #include "ConstructionPlugin_Plane.h"
9 #include <Config_PropManager.h>
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>
18 #include <GeomAPI_Pnt2d.h>
20 ConstructionPlugin_Plane::ConstructionPlugin_Plane()
24 void ConstructionPlugin_Plane::initAttributes()
26 data()->addAttribute(ConstructionPlugin_Plane::METHOD(), ModelAPI_AttributeString::type());
28 data()->addAttribute(ConstructionPlugin_Plane::FACE(), ModelAPI_AttributeSelection::type());
29 data()->addAttribute(ConstructionPlugin_Plane::DISTANCE(), ModelAPI_AttributeDouble::type());
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());
37 void ConstructionPlugin_Plane::execute()
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();
47 if (!aPlaneFace.get())
49 ResultConstructionPtr aConstr = document()->createConstruction(data());
50 aConstr->setShape(aPlaneFace);
54 bool ConstructionPlugin_Plane::customisePresentation(ResultPtr theResult, AISObjectPtr thePrs,
55 std::shared_ptr<GeomAPI_ICustomPrs> theDefaultPrs)
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));
68 aColor = Config_PropManager::color("Visualization", "construction_plane_color",
69 ConstructionPlugin_Plane::DEFAULT_COLOR());
71 bool isCustomized = false;
72 if (aColor.size() == 3)
73 isCustomized = thePrs->setColor(aColor[0], aColor[1], aColor[2]);
75 isCustomized = thePrs->setTransparensy(0.6) || isCustomized;
80 std::shared_ptr<GeomAPI_Shape> ConstructionPlugin_Plane::createPlaneByFaceAndDistance()
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()) {
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();
96 aOrig->translate(aDir, aDist);
97 std::shared_ptr<GeomAPI_Pln> aNewPln = std::shared_ptr<GeomAPI_Pln>(
98 new GeomAPI_Pln(aOrig, aDir));
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);
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));
109 std::shared_ptr<GeomAPI_Pnt2d> aPnt2d1 = aPnt1->to2D(aNewPln);
110 std::shared_ptr<GeomAPI_Pnt2d> aPnt2d2 = aPnt2->to2D(aNewPln);
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;
117 aPlane = GeomAlgoAPI_FaceBuilder::planarFace(aNewPln,
118 aPnt2d1->x() - aWgap,
119 aPnt2d1->y() - aHgap,
121 aHeight + 2 * aHgap);
127 std::shared_ptr<GeomAPI_Shape> ConstructionPlugin_Plane::createPlaneByGeneralEquation()
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);
144 aPlaneFace = GeomAlgoAPI_FaceBuilder::square(aPlane, aSize);