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 <GeomAlgoAPI_FaceBuilder.h>
12 #include <GeomAlgoAPI_Rotation.h>
13 #include <GeomAlgoAPI_ShapeTools.h>
15 #include <GeomAPI_Ax1.h>
16 #include <GeomAPI_Edge.h>
17 #include <GeomAPI_Lin.h>
18 #include <GeomAPI_Pln.h>
19 #include <GeomAPI_Pnt.h>
20 #include <GeomAPI_Pnt2d.h>
21 #include <GeomAPI_Vertex.h>
23 #include <ModelAPI_AttributeDouble.h>
24 #include <ModelAPI_AttributeIntArray.h>
25 #include <ModelAPI_AttributeSelection.h>
26 #include <ModelAPI_AttributeString.h>
27 #include <ModelAPI_ResultConstruction.h>
28 #include <ModelAPI_Session.h>
29 #include <ModelAPI_Validator.h>
31 static GeomShapePtr faceByThreeVertices(const std::shared_ptr<GeomAPI_Vertex> theV1,
32 const std::shared_ptr<GeomAPI_Vertex> theV2,
33 const std::shared_ptr<GeomAPI_Vertex> theV3);
34 static std::shared_ptr<GeomAPI_Face> makeRectangularFace(const std::shared_ptr<GeomAPI_Face> theFace,
35 const std::shared_ptr<GeomAPI_Pln> thePln);
37 //==================================================================================================
38 ConstructionPlugin_Plane::ConstructionPlugin_Plane()
42 //==================================================================================================
43 void ConstructionPlugin_Plane::initAttributes()
45 data()->addAttribute(ConstructionPlugin_Plane::CREATION_METHOD(), ModelAPI_AttributeString::typeId());
47 // By general equation.
48 data()->addAttribute(A(), ModelAPI_AttributeDouble::typeId());
49 data()->addAttribute(B(), ModelAPI_AttributeDouble::typeId());
50 data()->addAttribute(C(), ModelAPI_AttributeDouble::typeId());
51 data()->addAttribute(D(), ModelAPI_AttributeDouble::typeId());
52 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), A());
53 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), B());
54 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), C());
55 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), D());
58 data()->addAttribute(POINT1(), ModelAPI_AttributeSelection::typeId());
59 data()->addAttribute(POINT2(), ModelAPI_AttributeSelection::typeId());
60 data()->addAttribute(POINT3(), ModelAPI_AttributeSelection::typeId());
63 data()->addAttribute(LINE(), ModelAPI_AttributeSelection::typeId());
64 data()->addAttribute(POINT(), ModelAPI_AttributeSelection::typeId());
67 data()->addAttribute(CREATION_METHOD_BY_OTHER_PLANE_OPTION(), ModelAPI_AttributeString::typeId());
68 data()->addAttribute(PLANE(), ModelAPI_AttributeSelection::typeId());
69 data()->addAttribute(DISTANCE(), ModelAPI_AttributeDouble::typeId());
70 data()->addAttribute(COINCIDENT_POINT(), ModelAPI_AttributeSelection::typeId());
71 data()->addAttribute(AXIS(), ModelAPI_AttributeSelection::typeId());
72 data()->addAttribute(ANGLE(), ModelAPI_AttributeDouble::typeId());
75 //==================================================================================================
76 void ConstructionPlugin_Plane::execute()
80 std::string aCreationMethod = string(CREATION_METHOD())->value();
81 if(aCreationMethod == CREATION_METHOD_BY_GENERAL_EQUATION()) {
82 aShape = createByGeneralEquation();
83 } else if(aCreationMethod == CREATION_METHOD_BY_THREE_POINTS()) {
84 aShape = createByThreePoints();
85 } else if(aCreationMethod == CREATION_METHOD_BY_LINE_AND_POINT()) {
86 aShape = createByLineAndPoint();
87 } else if(aCreationMethod == CREATION_METHOD_BY_OTHER_PLANE()) {
88 std::string aCreationMethodOption = string(CREATION_METHOD_BY_OTHER_PLANE_OPTION())->value();
89 if(aCreationMethodOption == CREATION_METHOD_BY_DISTANCE_FROM_OTHER()) {
90 aShape = createByDistanceFromOther();
91 } else if(aCreationMethodOption == CREATION_METHOD_BY_COINCIDENT_TO_POINT()) {
92 aShape = createByCoincidentPoint();
93 } else if(aCreationMethodOption == CREATION_METHOD_BY_ROTATION()) {
94 aShape = createByRotation();
99 setError("Error: Could not create a plane.");
104 ResultConstructionPtr aConstr = document()->createConstruction(data());
105 aConstr->setInfinite(true);
106 aConstr->setShape(aShape);
110 //==================================================================================================
111 bool ConstructionPlugin_Plane::customisePresentation(ResultPtr theResult, AISObjectPtr thePrs,
112 std::shared_ptr<GeomAPI_ICustomPrs> theDefaultPrs)
114 std::vector<int> aColor;
115 // get color from the attribute of the result
116 if (theResult.get() != NULL && theResult->data()->attribute(ModelAPI_Result::COLOR_ID()).get() != NULL) {
117 AttributeIntArrayPtr aColorAttr = theResult->data()->intArray(ModelAPI_Result::COLOR_ID());
118 if (aColorAttr.get() && aColorAttr->size()) {
119 aColor.push_back(aColorAttr->value(0));
120 aColor.push_back(aColorAttr->value(1));
121 aColor.push_back(aColorAttr->value(2));
125 aColor = Config_PropManager::color("Visualization", "construction_plane_color",
126 ConstructionPlugin_Plane::DEFAULT_COLOR());
128 bool isCustomized = false;
129 if (aColor.size() == 3)
130 isCustomized = thePrs->setColor(aColor[0], aColor[1], aColor[2]);
132 isCustomized = thePrs->setTransparensy(0.6) || isCustomized;
137 //==================================================================================================
138 std::shared_ptr<GeomAPI_Shape> ConstructionPlugin_Plane::createByGeneralEquation()
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::squareFace(aPlane, aSize);
161 //==================================================================================================
162 std::shared_ptr<GeomAPI_Shape> ConstructionPlugin_Plane::createByThreePoints()
165 AttributeSelectionPtr aPointSelection1 = selection(POINT1());
166 GeomShapePtr aPointShape1 = aPointSelection1->value();
167 if(!aPointShape1.get()) {
168 aPointShape1 = aPointSelection1->context()->shape();
170 std::shared_ptr<GeomAPI_Vertex> aVertex1(new GeomAPI_Vertex(aPointShape1));
173 AttributeSelectionPtr aPointSelection2 = selection(POINT2());
174 GeomShapePtr aPointShape2 = aPointSelection2->value();
175 if(!aPointShape2.get()) {
176 aPointShape2 = aPointSelection2->context()->shape();
178 std::shared_ptr<GeomAPI_Vertex> aVertex2(new GeomAPI_Vertex(aPointShape2));
181 AttributeSelectionPtr aPointSelection3 = selection(POINT3());
182 GeomShapePtr aPointShape3 = aPointSelection3->value();
183 if(!aPointShape3.get()) {
184 aPointShape3 = aPointSelection3->context()->shape();
186 std::shared_ptr<GeomAPI_Vertex> aVertex3(new GeomAPI_Vertex(aPointShape3));
188 GeomShapePtr aRes = faceByThreeVertices(aVertex1, aVertex2, aVertex3);
193 //==================================================================================================
194 std::shared_ptr<GeomAPI_Shape> ConstructionPlugin_Plane::createByLineAndPoint()
197 AttributeSelectionPtr anEdgeSelection = selection(LINE());
198 GeomShapePtr aLineShape = anEdgeSelection->value();
199 if(!aLineShape.get()) {
200 aLineShape = anEdgeSelection->context()->shape();
202 std::shared_ptr<GeomAPI_Edge> anEdge(new GeomAPI_Edge(aLineShape));
203 std::shared_ptr<GeomAPI_Vertex> aV1, aV2;
204 GeomAlgoAPI_ShapeTools::findBounds(anEdge, aV1, aV2);
208 AttributeSelectionPtr aPointSelection = selection(POINT());
209 GeomShapePtr aPointShape = aPointSelection->value();
210 if(!aPointShape.get()) {
211 aPointShape = aPointSelection->context()->shape();
213 std::shared_ptr<GeomAPI_Vertex> aVertex(new GeomAPI_Vertex(aPointShape));
215 GeomShapePtr aRes = faceByThreeVertices(aV1, aV2, aVertex);
220 //==================================================================================================
221 std::shared_ptr<GeomAPI_Shape> ConstructionPlugin_Plane::createByDistanceFromOther()
223 AttributeSelectionPtr aFaceAttr = data()->selection(ConstructionPlugin_Plane::PLANE());
224 AttributeDoublePtr aDistAttr = data()->real(ConstructionPlugin_Plane::DISTANCE());
225 std::shared_ptr<GeomAPI_Shape> aPlane;
226 if ((aFaceAttr.get() != NULL) &&
227 (aDistAttr.get() != NULL) &&
228 aFaceAttr->isInitialized() && aDistAttr->isInitialized()) {
230 double aDist = aDistAttr->value();
231 GeomShapePtr aShape = aFaceAttr->value();
233 aShape = aFaceAttr->context()->shape();
240 std::shared_ptr<GeomAPI_Face> aFace(new GeomAPI_Face(aShape));
242 std::shared_ptr<GeomAPI_Pln> aPln = GeomAlgoAPI_FaceBuilder::plane(aFace);
243 std::shared_ptr<GeomAPI_Pnt> aOrig = aPln->location();
244 std::shared_ptr<GeomAPI_Dir> aDir = aPln->direction();
246 aOrig->translate(aDir, aDist);
247 std::shared_ptr<GeomAPI_Pln> aNewPln(new GeomAPI_Pln(aOrig, aDir));
249 aPlane = makeRectangularFace(aFace, aNewPln);
254 //==================================================================================================
255 std::shared_ptr<GeomAPI_Shape> ConstructionPlugin_Plane::createByCoincidentPoint()
258 AttributeSelectionPtr aFaceSelection = selection(PLANE());
259 GeomShapePtr aFaceShape = aFaceSelection->value();
260 if(!aFaceShape.get()) {
261 aFaceShape = aFaceSelection->context()->shape();
263 std::shared_ptr<GeomAPI_Face> aFace(new GeomAPI_Face(aFaceShape));
266 AttributeSelectionPtr aPointSelection = selection(COINCIDENT_POINT());
267 GeomShapePtr aPointShape = aPointSelection->value();
268 if(!aPointShape.get()) {
269 aPointShape = aPointSelection->context()->shape();
271 std::shared_ptr<GeomAPI_Vertex> aVertex(new GeomAPI_Vertex(aPointShape));
273 std::shared_ptr<GeomAPI_Pnt> anOrig = aVertex->point();
274 std::shared_ptr<GeomAPI_Pln> aPln = aFace->getPlane();
275 std::shared_ptr<GeomAPI_Dir> aDir = aPln->direction();
277 std::shared_ptr<GeomAPI_Pln> aNewPln(new GeomAPI_Pln(anOrig, aDir));
279 return makeRectangularFace(aFace, aNewPln);
282 //==================================================================================================
283 std::shared_ptr<GeomAPI_Shape> ConstructionPlugin_Plane::createByRotation()
286 AttributeSelectionPtr aFaceSelection = selection(PLANE());
287 GeomShapePtr aFaceShape = aFaceSelection->value();
288 if(!aFaceShape.get()) {
289 aFaceShape = aFaceSelection->context()->shape();
291 std::shared_ptr<GeomAPI_Face> aFace(new GeomAPI_Face(aFaceShape));
294 AttributeSelectionPtr anAxisSelection = selection(AXIS());
295 GeomShapePtr anAxisShape = anAxisSelection->value();
296 if(!anAxisShape.get()) {
297 anAxisShape = anAxisSelection->context()->shape();
299 std::shared_ptr<GeomAPI_Edge> anEdge(new GeomAPI_Edge(anAxisShape));
301 std::shared_ptr<GeomAPI_Ax1> anAxis = std::shared_ptr<GeomAPI_Ax1>(new GeomAPI_Ax1(anEdge->line()->location(),
302 anEdge->line()->direction()));
305 double anAngle = real(ANGLE())->value();
307 GeomAlgoAPI_Rotation aRotationAlgo(aFace, anAxis, anAngle);
309 std::shared_ptr<GeomAPI_Face> aRes(new GeomAPI_Face(aRotationAlgo.shape()));
310 std::shared_ptr<GeomAPI_Pln> aNewPln = aRes->getPlane();
312 aRes = makeRectangularFace(aRes, aNewPln);
317 //==================================================================================================
318 GeomShapePtr faceByThreeVertices(const std::shared_ptr<GeomAPI_Vertex> theV1,
319 const std::shared_ptr<GeomAPI_Vertex> theV2,
320 const std::shared_ptr<GeomAPI_Vertex> theV3)
322 std::shared_ptr<GeomAPI_Face> aFace = GeomAlgoAPI_FaceBuilder::planarFaceByThreeVertices(theV1, theV2, theV3);
324 ListOfShape anObjects;
325 anObjects.push_back(theV1);
326 anObjects.push_back(theV2);
327 anObjects.push_back(theV3);
328 std::list<std::shared_ptr<GeomAPI_Pnt> > aBoundingPoints = GeomAlgoAPI_ShapeTools::getBoundingBox(anObjects, 1.0);
329 GeomShapePtr aRes = GeomAlgoAPI_ShapeTools::fitPlaneToBox(aFace, aBoundingPoints);
334 //==================================================================================================
335 std::shared_ptr<GeomAPI_Face> makeRectangularFace(const std::shared_ptr<GeomAPI_Face> theFace,
336 const std::shared_ptr<GeomAPI_Pln> thePln)
338 // Create rectangular face close to the selected
339 double aXmin, aYmin, Zmin, aXmax, aYmax, Zmax;
340 theFace->computeSize(aXmin, aYmin, Zmin, aXmax, aYmax, Zmax);
342 // use all 8 points of the bounding box to find the 2D bounds
344 double aMinX2d, aMaxX2d, aMinY2d, aMaxY2d;
345 for(int aXIsMin = 0; aXIsMin < 2; aXIsMin++) {
346 for(int aYIsMin = 0; aYIsMin < 2; aYIsMin++) {
347 for(int aZIsMin = 0; aZIsMin < 2; aZIsMin++) {
348 std::shared_ptr<GeomAPI_Pnt> aPnt(new GeomAPI_Pnt(
349 aXIsMin ? aXmin : aXmax, aYIsMin ? aYmin : aYmax, aZIsMin ? Zmin : Zmax));
350 std::shared_ptr<GeomAPI_Pnt2d> aPnt2d = aPnt->to2D(thePln);
351 if (isFirst || aPnt2d->x() < aMinX2d)
352 aMinX2d = aPnt2d->x();
353 if (isFirst || aPnt2d->y() < aMinY2d)
354 aMinY2d = aPnt2d->y();
355 if (isFirst || aPnt2d->x() > aMaxX2d)
356 aMaxX2d = aPnt2d->x();
357 if (isFirst || aPnt2d->y() > aMaxY2d)
358 aMaxY2d = aPnt2d->y();
364 double aWgap = (aMaxX2d - aMinX2d) * 0.1;
365 double aHgap = (aMaxY2d - aMinY2d) * 0.1;
366 std::shared_ptr<GeomAPI_Face> aResFace = GeomAlgoAPI_FaceBuilder::planarFace(thePln,
367 aMinX2d - aWgap, aMinY2d - aHgap, aMaxX2d - aMinX2d + 2. * aWgap, aMaxY2d - aMinY2d + 2. * aHgap);