Salome HOME
Precise calculation of the flyout point for Length and Distance constraints
[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 = aData->refattr(theAttrName);
27   return anAttr->object();
28 }
29
30
31 std::shared_ptr<GeomAPI_Shape> getShape(ObjectPtr theObject)
32 {
33   ResultConstructionPtr aRes = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theObject);
34   if (aRes.get() != NULL) {
35     return aRes->shape();
36   }
37   return std::shared_ptr<GeomAPI_Shape>();
38 }
39
40
41 std::shared_ptr<GeomAPI_Pnt2d> getPoint(ModelAPI_Feature* theFeature,
42                                         const std::string& theAttribute)
43 {
44   std::shared_ptr<GeomDataAPI_Point2D> aPointAttr;
45
46   if (!theFeature->data())
47     return std::shared_ptr<GeomAPI_Pnt2d>();
48
49   FeaturePtr aFeature;
50   std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = std::dynamic_pointer_cast<
51       ModelAPI_AttributeRefAttr>(theFeature->data()->attribute(theAttribute));
52   if (anAttr)
53     aFeature = ModelAPI_Feature::feature(anAttr->object());
54
55   if (aFeature && aFeature->getKind() == SketchPlugin_Point::ID())
56     aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
57         aFeature->data()->attribute(SketchPlugin_Point::COORD_ID()));
58
59   else if (aFeature && aFeature->getKind() == SketchPlugin_Circle::ID())
60     aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
61         aFeature->data()->attribute(SketchPlugin_Circle::CENTER_ID()));
62
63   else if (anAttr->attr()) {
64     aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttr->attr());
65   }
66   if (aPointAttr.get() != NULL)
67     return aPointAttr->pnt();
68   return std::shared_ptr<GeomAPI_Pnt2d>();
69 }
70
71 //*************************************************************************************
72 std::shared_ptr<GeomDataAPI_Point2D> getFeaturePoint(DataPtr theData,
73                                                      const std::string& theAttribute)
74 {
75   std::shared_ptr<GeomDataAPI_Point2D> aPointAttr;
76
77   if (!theData)
78     return aPointAttr;
79
80   FeaturePtr aFeature;
81   std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = std::dynamic_pointer_cast<
82       ModelAPI_AttributeRefAttr>(theData->attribute(theAttribute));
83   if (anAttr)
84     aFeature = ModelAPI_Feature::feature(anAttr->object());
85
86   if (aFeature && aFeature->getKind() == SketchPlugin_Point::ID())
87     aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
88         aFeature->data()->attribute(SketchPlugin_Point::COORD_ID()));
89
90   else if (aFeature && aFeature->getKind() == SketchPlugin_Circle::ID())
91     aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
92         aFeature->data()->attribute(SketchPlugin_Circle::CENTER_ID()));
93
94   else if (anAttr->attr()) {
95     aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttr->attr());
96   }
97   return aPointAttr;
98 }
99
100 //*************************************************************************************
101 FeaturePtr getFeatureLine(DataPtr theData,
102                           const std::string& theAttribute)
103 {
104   FeaturePtr aLine;
105   if (!theData)
106     return aLine;
107
108   std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = 
109     std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theData->attribute(theAttribute));
110   if (anAttr) {
111     FeaturePtr aFeature = ModelAPI_Feature::feature(anAttr->object());
112     if (aFeature && aFeature->getKind() == SketchPlugin_Line::ID()) {
113       return aFeature;
114     }
115   }
116   return aLine;
117 }
118
119 //*************************************************************************************
120 std::shared_ptr<GeomAPI_Pnt2d> getProjectionPoint(const FeaturePtr theLine,
121                                                   const std::shared_ptr<GeomAPI_Pnt2d>& thePoint)
122 {
123   DataPtr aData = theLine->data();
124   std::shared_ptr<GeomDataAPI_Point2D> aPoint1 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
125       aData->attribute(SketchPlugin_Line::START_ID()));
126   std::shared_ptr<GeomDataAPI_Point2D> aPoint2 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
127       aData->attribute(SketchPlugin_Line::END_ID()));
128
129   GeomAPI_Lin2d aLin2d(aPoint1->x(), aPoint1->y(), aPoint2->x(), aPoint2->y());
130   return aLin2d.project(thePoint);
131 }
132
133
134 static double MyArrowSize = 30.;
135 double getArrowSize()
136 {
137   return MyArrowSize;
138 }
139
140 void setArrowSize(double theSize)
141 {
142   MyArrowSize = theSize;
143 }
144
145 double getFlyoutDistance(const ModelAPI_Feature* theConstraint, 
146                          const std::shared_ptr<GeomAPI_Ax3>& thePlane)
147 {
148   std::shared_ptr<GeomDataAPI_Point2D> aFlyoutPoint =
149       std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
150       const_cast<ModelAPI_Feature*>(theConstraint)->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
151
152   return aFlyoutPoint->y();
153 }
154
155
156 };