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 <ModelAPI_Session.h>
17 #include <ModelAPI_Validator.h>
18 #include <GeomAlgoAPI_FaceBuilder.h>
20 #include <GeomAPI_Pnt2d.h>
22 ConstructionPlugin_Plane::ConstructionPlugin_Plane()
26 void ConstructionPlugin_Plane::initAttributes()
28 data()->addAttribute(ConstructionPlugin_Plane::METHOD(), ModelAPI_AttributeString::typeId());
30 data()->addAttribute(ConstructionPlugin_Plane::FACE(), ModelAPI_AttributeSelection::typeId());
31 data()->addAttribute(ConstructionPlugin_Plane::DISTANCE(), ModelAPI_AttributeDouble::typeId());
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());
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());
44 void ConstructionPlugin_Plane::execute()
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();
54 if (!aPlaneFace.get())
56 ResultConstructionPtr aConstr = document()->createConstruction(data());
57 aConstr->setShape(aPlaneFace);
61 bool ConstructionPlugin_Plane::customisePresentation(ResultPtr theResult, AISObjectPtr thePrs,
62 std::shared_ptr<GeomAPI_ICustomPrs> theDefaultPrs)
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));
75 aColor = Config_PropManager::color("Visualization", "construction_plane_color",
76 ConstructionPlugin_Plane::DEFAULT_COLOR());
78 bool isCustomized = false;
79 if (aColor.size() == 3)
80 isCustomized = thePrs->setColor(aColor[0], aColor[1], aColor[2]);
82 isCustomized = thePrs->setTransparensy(0.6) || isCustomized;
87 std::shared_ptr<GeomAPI_Shape> ConstructionPlugin_Plane::createPlaneByFaceAndDistance()
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()) {
96 double aDist = aDistAttr->value();
97 GeomShapePtr aShape = aFaceAttr->value();
99 aShape = aFaceAttr->context()->shape();
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();
107 aOrig->translate(aDir, aDist);
108 std::shared_ptr<GeomAPI_Pln> aNewPln = std::shared_ptr<GeomAPI_Pln>(
109 new GeomAPI_Pln(aOrig, aDir));
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);
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));
120 std::shared_ptr<GeomAPI_Pnt2d> aPnt2d1 = aPnt1->to2D(aNewPln);
121 std::shared_ptr<GeomAPI_Pnt2d> aPnt2d2 = aPnt2->to2D(aNewPln);
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;
128 aPlane = GeomAlgoAPI_FaceBuilder::planarFace(aNewPln,
129 aPnt2d1->x() - aWgap,
130 aPnt2d1->y() - aHgap,
132 aHeight + 2 * aHgap);
138 std::shared_ptr<GeomAPI_Shape> ConstructionPlugin_Plane::createPlaneByGeneralEquation()
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);
156 aPlaneFace = GeomAlgoAPI_FaceBuilder::square(aPlane, aSize, true);