Salome HOME
Axis feature in Construction plugin is created
[modules/shaper.git] / src / ConstructionPlugin / ConstructionPlugin_Axis.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        ConstructionPlugin_Axis.cpp
4 // Created:     12 Dec 2014
5 // Author:      Vitaly Smetannikov
6
7 #include "ConstructionPlugin_Axis.h"
8
9 #include <ModelAPI_AttributeReference.h>
10 #include <ModelAPI_ResultConstruction.h>
11
12 #include <GeomAPI_Edge.h>
13 #include <GeomAlgoAPI_EdgeBuilder.h>
14 #include <GeomAlgoAPI_PointBuilder.h>
15
16 using namespace std;
17
18 static const double MINIMAL_LENGTH      = 1.e-5;
19
20 ConstructionPlugin_Axis::ConstructionPlugin_Axis()
21 {
22 }
23
24 void ConstructionPlugin_Axis::initAttributes()
25 {
26   data()->addAttribute(POINT_ATTR_FIRST,  ModelAPI_AttributeReference::type());
27   data()->addAttribute(POINT_ATTR_SECOND, ModelAPI_AttributeReference::type());
28 }
29
30 void ConstructionPlugin_Axis::execute()
31 {
32   AttributeReferencePtr aRef1 = data()->reference(POINT_ATTR_FIRST);
33   AttributeReferencePtr aRef2 = data()->reference(POINT_ATTR_SECOND);
34   if ((aRef1.get() != NULL) && (aRef2.get() != NULL)) {
35     ResultConstructionPtr aPntObj1 = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aRef1->value());
36     ResultConstructionPtr aPntObj2 = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aRef2->value());
37     if ((aPntObj1.get() != NULL) && (aPntObj2.get() != NULL)) {
38       GeomShapePtr aShape1 = aPntObj1->shape();
39       GeomShapePtr aShape2 = aPntObj2->shape();
40       if (aShape1->isVertex() && aShape2->isVertex()) {
41         std::shared_ptr<GeomAPI_Pnt> aStart = GeomAlgoAPI_PointBuilder::point(aShape1);
42         std::shared_ptr<GeomAPI_Pnt> anEnd = GeomAlgoAPI_PointBuilder::point(aShape2);
43         if (aStart->distance(anEnd) > MINIMAL_LENGTH) {
44           std::shared_ptr<GeomAPI_Edge> anEdge = GeomAlgoAPI_EdgeBuilder::line(aStart, anEnd);
45
46           ResultConstructionPtr aConstr = document()->createConstruction(data());
47           aConstr->setShape(anEdge);
48           setResult(aConstr);
49         }
50       }
51     }
52   }
53 }
54
55 void ConstructionPlugin_Axis::customisePresentation(AISObjectPtr thePrs)
56 {
57   thePrs->setColor(0, 0, 0);
58   thePrs->setLineStyle(3);
59 }