1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 // File: ConstructionPlugin_Axis.cpp
4 // Created: 12 Dec 2014
5 // Author: Vitaly Smetannikov
7 #include "ConstructionPlugin_Axis.h"
9 #include <Config_PropManager.h>
11 #include <ModelAPI_AttributeSelection.h>
12 #include <ModelAPI_ResultConstruction.h>
13 #include <ModelAPI_AttributeString.h>
14 #include <ModelAPI_AttributeDouble.h>
15 #include <ModelAPI_Session.h>
16 #include <ModelAPI_Validator.h>
18 #include <GeomAPI_Edge.h>
19 #include <GeomAPI_Vertex.h>
20 #include <GeomAlgoAPI_EdgeBuilder.h>
21 #include <GeomAlgoAPI_PointBuilder.h>
29 ConstructionPlugin_Axis::ConstructionPlugin_Axis()
33 void ConstructionPlugin_Axis::initAttributes()
35 data()->addAttribute(ConstructionPlugin_Axis::METHOD(),
36 ModelAPI_AttributeString::typeId());
37 data()->addAttribute(ConstructionPlugin_Axis::POINT_FIRST(),
38 ModelAPI_AttributeSelection::typeId());
39 data()->addAttribute(ConstructionPlugin_Axis::POINT_SECOND(),
40 ModelAPI_AttributeSelection::typeId());
41 data()->addAttribute(ConstructionPlugin_Axis::CYLINDRICAL_FACE(),
42 ModelAPI_AttributeSelection::typeId());
43 data()->addAttribute(ConstructionPlugin_Axis::X_DIRECTION(),
44 ModelAPI_AttributeDouble::typeId());
45 data()->addAttribute(ConstructionPlugin_Axis::Y_DIRECTION(),
46 ModelAPI_AttributeDouble::typeId());
47 data()->addAttribute(ConstructionPlugin_Axis::Z_DIRECTION(),
48 ModelAPI_AttributeDouble::typeId());
49 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(),
50 ConstructionPlugin_Axis::X_DIRECTION());
51 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(),
52 ConstructionPlugin_Axis::Y_DIRECTION());
53 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(),
54 ConstructionPlugin_Axis::Z_DIRECTION());
57 void ConstructionPlugin_Axis::createAxisByTwoPoints()
59 AttributeSelectionPtr aRef1 = data()->selection(ConstructionPlugin_Axis::POINT_FIRST());
60 AttributeSelectionPtr aRef2 = data()->selection(ConstructionPlugin_Axis::POINT_SECOND());
61 if ((aRef1.get() != NULL) && (aRef2.get() != NULL)) {
62 GeomShapePtr aShape1 = aRef1->value();
64 aShape1 = aRef1->context()->shape();
65 GeomShapePtr aShape2 = aRef2->value();
67 aShape2 = aRef2->context()->shape();
68 if (aShape1->isVertex() && aShape2->isVertex() && (!aShape1->isEqual(aShape2))) {
69 std::shared_ptr<GeomAPI_Pnt> aStart = GeomAlgoAPI_PointBuilder::point(aShape1);
70 std::shared_ptr<GeomAPI_Pnt> anEnd = GeomAlgoAPI_PointBuilder::point(aShape2);
71 if (aStart->distance(anEnd) > ConstructionPlugin_Axis::MINIMAL_LENGTH()) {
72 std::shared_ptr<GeomAPI_Edge> anEdge = GeomAlgoAPI_EdgeBuilder::line(aStart, anEnd);
74 ResultConstructionPtr aConstr = document()->createConstruction(data());
75 aConstr->setInfinite(true);
76 aConstr->setShape(anEdge);
84 void ConstructionPlugin_Axis::createAxisByPointAndDirection()
86 AttributeSelectionPtr aRef1 = data()->selection(ConstructionPlugin_Axis::POINT_FIRST());
87 AttributeDoublePtr aXAttr = data()->real(ConstructionPlugin_Axis::X_DIRECTION());
88 AttributeDoublePtr aYAttr = data()->real(ConstructionPlugin_Axis::Y_DIRECTION());
89 AttributeDoublePtr aZAttr = data()->real(ConstructionPlugin_Axis::Z_DIRECTION());
90 if ((aRef1.get() != NULL) && (aXAttr.get() != NULL) &&
91 (aYAttr.get() != NULL) && (aZAttr.get() != NULL)) {
92 GeomShapePtr aShape1 = aRef1->value();
94 aShape1 = aRef1->context()->shape();
96 std::shared_ptr<GeomAPI_Vertex> aVertex(new GeomAPI_Vertex(aXAttr->value(),
99 if (aShape1->isVertex() && (!aShape1->isEqual(aVertex))) {
100 std::shared_ptr<GeomAPI_Pnt> aStart = GeomAlgoAPI_PointBuilder::point(aShape1);
101 std::shared_ptr<GeomAPI_Pnt> anEnd = aVertex->point();
102 if (aStart->distance(anEnd) > ConstructionPlugin_Axis::MINIMAL_LENGTH()) {
103 std::shared_ptr<GeomAPI_Edge> anEdge = GeomAlgoAPI_EdgeBuilder::line(aStart, anEnd);
105 ResultConstructionPtr aConstr = document()->createConstruction(data());
106 aConstr->setInfinite(true);
107 aConstr->setShape(anEdge);
115 void ConstructionPlugin_Axis::createAxisByCylindricalFace()
117 std::shared_ptr<GeomAPI_Shape> aSelection = data()->selection(CYLINDRICAL_FACE())->value();
118 // update arguments due to the selection value
119 if (aSelection && !aSelection->isNull() && aSelection->isFace()) {
120 std::shared_ptr<GeomAPI_Edge> anEdge = GeomAlgoAPI_EdgeBuilder::cylinderAxis(aSelection);
122 ResultConstructionPtr aConstr = document()->createConstruction(data());
123 aConstr->setInfinite(true);
124 aConstr->setShape(anEdge);
131 void ConstructionPlugin_Axis::execute()
133 AttributeStringPtr aMethodTypeAttr = string(ConstructionPlugin_Axis::METHOD());
134 std::string aMethodType = aMethodTypeAttr->value();
135 if (aMethodType == "AxisByPointsCase") {
136 createAxisByTwoPoints();
137 } else if (aMethodType == "AxisByCylindricalFaceCase") {
138 createAxisByCylindricalFace();
139 } else if (aMethodType == "AxisByPointAndDirection") {
140 createAxisByPointAndDirection();
144 bool ConstructionPlugin_Axis::customisePresentation(ResultPtr theResult, AISObjectPtr thePrs,
145 std::shared_ptr<GeomAPI_ICustomPrs> theDefaultPrs)
147 bool isCustomized = theDefaultPrs.get() != NULL &&
148 theDefaultPrs->customisePresentation(theResult, thePrs, theDefaultPrs);
150 isCustomized = thePrs->setLineStyle(3) || isCustomized;
151 isCustomized = thePrs->setWidth(2) || isCustomized;