1 // Copyright (C) 2014-2016 CEA/DEN, EDF R&D
3 // File: ConstructionPlugin_Axis.cpp
4 // Created: 12 Dec 2014
5 // Author: Vitaly Smetannikov
7 // Modified by CEA (delegation to Alyotech) : 29 Mar 2016
9 #include "ConstructionPlugin_Axis.h"
11 #include <Config_PropManager.h>
13 #include <ModelAPI_AttributeSelection.h>
14 #include <ModelAPI_ResultConstruction.h>
15 #include <ModelAPI_AttributeString.h>
16 #include <ModelAPI_AttributeDouble.h>
17 #include <ModelAPI_Session.h>
18 #include <ModelAPI_Validator.h>
20 #include <GeomAPI_Edge.h>
21 #include <GeomAPI_Vertex.h>
22 #include <GeomAlgoAPI_EdgeBuilder.h>
23 #include <GeomAlgoAPI_PointBuilder.h>
33 ConstructionPlugin_Axis::ConstructionPlugin_Axis()
37 void ConstructionPlugin_Axis::initAttributes()
39 data()->addAttribute(ConstructionPlugin_Axis::METHOD(),
40 ModelAPI_AttributeString::typeId());
42 // Attributes needed to build the axis using the "two points" method
43 data()->addAttribute(ConstructionPlugin_Axis::POINT_FIRST(),
44 ModelAPI_AttributeSelection::typeId());
45 data()->addAttribute(ConstructionPlugin_Axis::POINT_SECOND(),
46 ModelAPI_AttributeSelection::typeId());
48 // Attributes needed to build the axis using the "cylindrical face" method"
49 data()->addAttribute(ConstructionPlugin_Axis::CYLINDRICAL_FACE(),
50 ModelAPI_AttributeSelection::typeId());
51 data()->addAttribute(ConstructionPlugin_Axis::X_DIRECTION(),
52 ModelAPI_AttributeDouble::typeId());
53 data()->addAttribute(ConstructionPlugin_Axis::Y_DIRECTION(),
54 ModelAPI_AttributeDouble::typeId());
55 data()->addAttribute(ConstructionPlugin_Axis::Z_DIRECTION(),
56 ModelAPI_AttributeDouble::typeId());
57 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(),
58 ConstructionPlugin_Axis::X_DIRECTION());
59 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(),
60 ConstructionPlugin_Axis::Y_DIRECTION());
61 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(),
62 ConstructionPlugin_Axis::Z_DIRECTION());
64 //Attributes needed to build the axis using the "three dimensions" method
65 data()->addAttribute(ConstructionPlugin_Axis::DX(),
66 ModelAPI_AttributeDouble::typeId());
67 data()->addAttribute(ConstructionPlugin_Axis::DY(),
68 ModelAPI_AttributeDouble::typeId());
69 data()->addAttribute(ConstructionPlugin_Axis::DZ(),
70 ModelAPI_AttributeDouble::typeId());
73 void ConstructionPlugin_Axis::createAxisByTwoPoints()
75 AttributeSelectionPtr aRef1 = data()->selection(ConstructionPlugin_Axis::POINT_FIRST());
76 AttributeSelectionPtr aRef2 = data()->selection(ConstructionPlugin_Axis::POINT_SECOND());
77 if ((aRef1.get() != NULL) && (aRef2.get() != NULL)) {
78 GeomShapePtr aShape1 = aRef1->value();
80 aShape1 = aRef1->context()->shape();
81 GeomShapePtr aShape2 = aRef2->value();
83 aShape2 = aRef2->context()->shape();
84 if (aShape1->isVertex() && aShape2->isVertex() && (!aShape1->isEqual(aShape2))) {
85 std::shared_ptr<GeomAPI_Pnt> aStart = GeomAlgoAPI_PointBuilder::point(aShape1);
86 std::shared_ptr<GeomAPI_Pnt> anEnd = GeomAlgoAPI_PointBuilder::point(aShape2);
87 if (aStart->distance(anEnd) > ConstructionPlugin_Axis::MINIMAL_LENGTH()) {
88 std::shared_ptr<GeomAPI_Edge> anEdge = GeomAlgoAPI_EdgeBuilder::line(aStart, anEnd);
90 ResultConstructionPtr aConstr = document()->createConstruction(data());
91 aConstr->setInfinite(true);
92 aConstr->setShape(anEdge);
100 void ConstructionPlugin_Axis::createAxisByPointAndDirection()
102 AttributeSelectionPtr aRef1 = data()->selection(ConstructionPlugin_Axis::POINT_FIRST());
103 AttributeDoublePtr aXAttr = data()->real(ConstructionPlugin_Axis::X_DIRECTION());
104 AttributeDoublePtr aYAttr = data()->real(ConstructionPlugin_Axis::Y_DIRECTION());
105 AttributeDoublePtr aZAttr = data()->real(ConstructionPlugin_Axis::Z_DIRECTION());
106 if ((aRef1.get() != NULL) && (aXAttr.get() != NULL) &&
107 (aYAttr.get() != NULL) && (aZAttr.get() != NULL)) {
108 GeomShapePtr aShape1 = aRef1->value();
110 aShape1 = aRef1->context()->shape();
112 std::shared_ptr<GeomAPI_Vertex> aVertex(new GeomAPI_Vertex(aXAttr->value(),
115 if (aShape1->isVertex() && (!aShape1->isEqual(aVertex))) {
116 std::shared_ptr<GeomAPI_Pnt> aStart = GeomAlgoAPI_PointBuilder::point(aShape1);
117 std::shared_ptr<GeomAPI_Pnt> anEnd = aVertex->point();
118 if (aStart->distance(anEnd) > ConstructionPlugin_Axis::MINIMAL_LENGTH()) {
119 std::shared_ptr<GeomAPI_Edge> anEdge = GeomAlgoAPI_EdgeBuilder::line(aStart, anEnd);
121 ResultConstructionPtr aConstr = document()->createConstruction(data());
122 aConstr->setInfinite(true);
123 aConstr->setShape(anEdge);
131 void ConstructionPlugin_Axis::createAxisByCylindricalFace()
133 std::shared_ptr<GeomAPI_Shape> aSelection = data()->selection(CYLINDRICAL_FACE())->value();
134 // update arguments due to the selection value
135 if (aSelection && !aSelection->isNull() && aSelection->isFace()) {
136 std::shared_ptr<GeomAPI_Edge> anEdge = GeomAlgoAPI_EdgeBuilder::cylinderAxis(aSelection);
138 ResultConstructionPtr aConstr = document()->createConstruction(data());
139 aConstr->setInfinite(true);
140 aConstr->setShape(anEdge);
145 void ConstructionPlugin_Axis::createAxisByDimensions()
147 // Start by getting these dimensions
148 double aDX = data()->real(DX())->value();
149 double aDY = data()->real(DY())->value();
150 double aDZ = data()->real(DZ())->value();
152 if (fabs(aDX) < MINIMAL_LENGTH() && fabs(aDY) < MINIMAL_LENGTH() && fabs(aDZ) < MINIMAL_LENGTH()){
153 setError("Axis builder with dimensions :: all dimensions are null", false);
157 // Make the axis, build the ResultConstructionPtr and write it
158 std::shared_ptr<GeomAPI_Edge> anEdge = GeomAlgoAPI_EdgeBuilder::line(aDX, aDY, aDZ);
159 ResultConstructionPtr aConstr = document()->createConstruction(data());
160 aConstr->setInfinite(true);
161 aConstr->setShape(anEdge);
165 void ConstructionPlugin_Axis::execute()
167 AttributeStringPtr aMethodTypeAttr = string(ConstructionPlugin_Axis::METHOD());
168 std::string aMethodType = aMethodTypeAttr->value();
169 if (aMethodType == "AxisByPointsCase") {
170 createAxisByTwoPoints();
171 } else if (aMethodType == "AxisByCylindricalFaceCase") {
172 createAxisByCylindricalFace();
173 } else if (aMethodType == "AxisByPointAndDirection") {
174 createAxisByPointAndDirection();
175 } else if (aMethodType == "AxisByDimensionsCase") {
176 createAxisByDimensions();
180 bool ConstructionPlugin_Axis::customisePresentation(ResultPtr theResult, AISObjectPtr thePrs,
181 std::shared_ptr<GeomAPI_ICustomPrs> theDefaultPrs)
183 bool isCustomized = theDefaultPrs.get() != NULL &&
184 theDefaultPrs->customisePresentation(theResult, thePrs, theDefaultPrs);
186 isCustomized = thePrs->setLineStyle(3) || isCustomized;
187 isCustomized = thePrs->setWidth(2) || isCustomized;