Salome HOME
Exclude links to SketchPlugin
[modules/shaper.git] / src / SketcherPrs / SketcherPrs_Tools.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        SketcherPrs_Tools.cpp
4 // Created:     10 March 2015
5 // Author:      Vitaly SMETANNIKOV
6
7 #include "SketcherPrs_Tools.h"
8
9 #include <SketchPlugin_Constraint.h>
10 #include <SketchPlugin_Point.h>
11 #include <SketchPlugin_Circle.h>
12 #include <SketchPlugin_Line.h>
13
14 #include <ModelAPI_ResultConstruction.h>
15 #include <ModelAPI_AttributeRefAttr.h>
16
17 #include <GeomDataAPI_Point2D.h>
18 #include <GeomAPI_Lin2d.h>
19
20
21 namespace SketcherPrs_Tools {
22
23 ObjectPtr getResult(ModelAPI_Feature* theFeature, const std::string& theAttrName)
24 {
25   std::shared_ptr<ModelAPI_Data> aData = theFeature->data();
26   std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = 
27     std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aData->attribute(theAttrName));
28
29   return anAttr->object();
30 }
31
32
33 std::shared_ptr<GeomAPI_Shape> getShape(ObjectPtr theObject)
34 {
35   ResultConstructionPtr aRes = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theObject);
36   if (aRes.get() != NULL) {
37     return aRes->shape();
38   }
39   return std::shared_ptr<GeomAPI_Shape>();
40 }
41
42
43 std::shared_ptr<GeomAPI_Pnt2d> getPoint(ModelAPI_Feature* theFeature,
44                                         const std::string& theAttribute)
45 {
46   std::shared_ptr<GeomDataAPI_Point2D> aPointAttr;
47
48   if (!theFeature->data())
49     return std::shared_ptr<GeomAPI_Pnt2d>();
50
51   FeaturePtr aFeature;
52   std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = std::dynamic_pointer_cast<
53       ModelAPI_AttributeRefAttr>(theFeature->data()->attribute(theAttribute));
54   if (anAttr)
55     aFeature = ModelAPI_Feature::feature(anAttr->object());
56
57   if (aFeature && aFeature->getKind() == SketchPlugin_Point::ID())
58     aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
59         aFeature->data()->attribute(SketchPlugin_Point::COORD_ID()));
60
61   else if (aFeature && aFeature->getKind() == SketchPlugin_Circle::ID())
62     aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
63         aFeature->data()->attribute(SketchPlugin_Circle::CENTER_ID()));
64
65   else if (anAttr->attr()) {
66     aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttr->attr());
67   }
68   if (aPointAttr.get() != NULL)
69     return aPointAttr->pnt();
70   return std::shared_ptr<GeomAPI_Pnt2d>();
71 }
72
73 //*************************************************************************************
74 std::shared_ptr<GeomDataAPI_Point2D> getFeaturePoint(DataPtr theData,
75                                                      const std::string& theAttribute)
76 {
77   std::shared_ptr<GeomDataAPI_Point2D> aPointAttr;
78
79   if (!theData)
80     return aPointAttr;
81
82   FeaturePtr aFeature;
83   std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = std::dynamic_pointer_cast<
84       ModelAPI_AttributeRefAttr>(theData->attribute(theAttribute));
85   if (anAttr)
86     aFeature = ModelAPI_Feature::feature(anAttr->object());
87
88   if (aFeature && aFeature->getKind() == SketchPlugin_Point::ID())
89     aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
90         aFeature->data()->attribute(SketchPlugin_Point::COORD_ID()));
91
92   else if (aFeature && aFeature->getKind() == SketchPlugin_Circle::ID())
93     aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
94         aFeature->data()->attribute(SketchPlugin_Circle::CENTER_ID()));
95
96   else if (anAttr->attr()) {
97     aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttr->attr());
98   }
99   return aPointAttr;
100 }
101
102 //*************************************************************************************
103 FeaturePtr getFeatureLine(DataPtr theData,
104                           const std::string& theAttribute)
105 {
106   FeaturePtr aLine;
107   if (!theData)
108     return aLine;
109
110   std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = 
111     std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theData->attribute(theAttribute));
112   if (anAttr) {
113     FeaturePtr aFeature = ModelAPI_Feature::feature(anAttr->object());
114     if (aFeature && aFeature->getKind() == SketchPlugin_Line::ID()) {
115       return aFeature;
116     }
117   }
118   return aLine;
119 }
120
121 //*************************************************************************************
122 std::shared_ptr<GeomAPI_Pnt2d> getProjectionPoint(const FeaturePtr theLine,
123                                                   const std::shared_ptr<GeomAPI_Pnt2d>& thePoint)
124 {
125   DataPtr aData = theLine->data();
126   std::shared_ptr<GeomDataAPI_Point2D> aPoint1 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
127       aData->attribute(SketchPlugin_Line::START_ID()));
128   std::shared_ptr<GeomDataAPI_Point2D> aPoint2 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
129       aData->attribute(SketchPlugin_Line::END_ID()));
130
131   GeomAPI_Lin2d aLin2d(aPoint1->x(), aPoint1->y(), aPoint2->x(), aPoint2->y());
132   return aLin2d.project(thePoint);
133 }
134
135
136 static double MyArrowSize = 30.;
137 double getArrowSize()
138 {
139   return MyArrowSize;
140 }
141
142 void setArrowSize(double theSize)
143 {
144   MyArrowSize = theSize;
145 }
146
147 };