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->setInfinite(true);
58 aConstr->setShape(aPlaneFace);
62 bool ConstructionPlugin_Plane::customisePresentation(ResultPtr theResult, AISObjectPtr thePrs,
63 std::shared_ptr<GeomAPI_ICustomPrs> theDefaultPrs)
65 std::vector<int> aColor;
66 // get color from the attribute of the result
67 if (theResult.get() != NULL && theResult->data()->attribute(ModelAPI_Result::COLOR_ID()).get() != NULL) {
68 AttributeIntArrayPtr aColorAttr = theResult->data()->intArray(ModelAPI_Result::COLOR_ID());
69 if (aColorAttr.get() && aColorAttr->size()) {
70 aColor.push_back(aColorAttr->value(0));
71 aColor.push_back(aColorAttr->value(1));
72 aColor.push_back(aColorAttr->value(2));
76 aColor = Config_PropManager::color("Visualization", "construction_plane_color",
77 ConstructionPlugin_Plane::DEFAULT_COLOR());
79 bool isCustomized = false;
80 if (aColor.size() == 3)
81 isCustomized = thePrs->setColor(aColor[0], aColor[1], aColor[2]);
83 isCustomized = thePrs->setTransparensy(0.6) || isCustomized;
88 std::shared_ptr<GeomAPI_Shape> ConstructionPlugin_Plane::createPlaneByFaceAndDistance()
90 AttributeSelectionPtr aFaceAttr = data()->selection(ConstructionPlugin_Plane::FACE());
91 AttributeDoublePtr aDistAttr = data()->real(ConstructionPlugin_Plane::DISTANCE());
92 std::shared_ptr<GeomAPI_Shape> aPlane;
93 if ((aFaceAttr.get() != NULL) &&
94 (aDistAttr.get() != NULL) &&
95 aFaceAttr->isInitialized() && aDistAttr->isInitialized()) {
97 double aDist = aDistAttr->value();
98 GeomShapePtr aShape = aFaceAttr->value();
100 aShape = aFaceAttr->context()->shape();
103 if (aShape.get() != NULL) {
104 std::shared_ptr<GeomAPI_Pln> aPln = GeomAlgoAPI_FaceBuilder::plane(aShape);
105 std::shared_ptr<GeomAPI_Pnt> aOrig = aPln->location();
106 std::shared_ptr<GeomAPI_Dir> aDir = aPln->direction();
108 aOrig->translate(aDir, aDist);
109 std::shared_ptr<GeomAPI_Pln> aNewPln = std::shared_ptr<GeomAPI_Pln>(
110 new GeomAPI_Pln(aOrig, aDir));
112 // Create rectangular face close to the selected
113 double aXmin, aYmin, Zmin, aXmax, aYmax, Zmax;
114 aShape->computeSize(aXmin, aYmin, Zmin, aXmax, aYmax, Zmax);
116 // use all 8 points of the bounding box to find the 2D bounds
118 double aMinX2d, aMaxX2d, aMinY2d, aMaxY2d;
119 for(int aXIsMin = 0; aXIsMin < 2; aXIsMin++) {
120 for(int aYIsMin = 0; aYIsMin < 2; aYIsMin++) {
121 for(int aZIsMin = 0; aZIsMin < 2; aZIsMin++) {
122 std::shared_ptr<GeomAPI_Pnt> aPnt = std::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(
123 aXIsMin ? aXmin : aXmax, aYIsMin ? aYmin : aYmax, aZIsMin ? Zmin : Zmax));
124 std::shared_ptr<GeomAPI_Pnt2d> aPnt2d = aPnt->to2D(aNewPln);
125 if (isFirst || aPnt2d->x() < aMinX2d)
126 aMinX2d = aPnt2d->x();
127 if (isFirst || aPnt2d->y() < aMinY2d)
128 aMinY2d = aPnt2d->y();
129 if (isFirst || aPnt2d->x() > aMaxX2d)
130 aMaxX2d = aPnt2d->x();
131 if (isFirst || aPnt2d->y() > aMaxY2d)
132 aMaxY2d = aPnt2d->y();
138 double aWgap = (aMaxX2d - aMinX2d) * 0.1;
139 double aHgap = (aMaxY2d - aMinY2d) * 0.1;
140 aPlane = GeomAlgoAPI_FaceBuilder::planarFace(aNewPln,
141 aMinX2d - aWgap, aMinY2d - aHgap, aMaxX2d - aMinX2d + 2. * aWgap, aMaxY2d - aMinY2d + 2. * aHgap);
147 std::shared_ptr<GeomAPI_Shape> ConstructionPlugin_Plane::createPlaneByGeneralEquation()
149 AttributeDoublePtr anAttrA = real(ConstructionPlugin_Plane::A());
150 AttributeDoublePtr anAttrB = real(ConstructionPlugin_Plane::B());
151 AttributeDoublePtr anAttrC = real(ConstructionPlugin_Plane::C());
152 AttributeDoublePtr anAttrD = real(ConstructionPlugin_Plane::D());
153 std::shared_ptr<GeomAPI_Shape> aPlaneFace;
154 if ((anAttrA.get() != NULL) && (anAttrB.get() != NULL) &&
155 (anAttrC.get() != NULL) && (anAttrD.get() != NULL) &&
156 anAttrA->isInitialized() && anAttrB->isInitialized() &&
157 anAttrC->isInitialized() && anAttrD->isInitialized() ) {
158 double aA = anAttrA->value(), aB = anAttrB->value(),
159 aC = anAttrC->value(), aD = anAttrD->value();
160 std::shared_ptr<GeomAPI_Pln> aPlane =
161 std::shared_ptr<GeomAPI_Pln>(new GeomAPI_Pln(aA, aB, aC, aD));
162 std::string kDefaultPlaneSize = "200";
163 double aSize = Config_PropManager::integer("Sketch planes", "planes_size", kDefaultPlaneSize);
165 aPlaneFace = GeomAlgoAPI_FaceBuilder::square(aPlane, aSize);