Salome HOME
Coincident with objects
[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 #include <SketchPlugin_Arc.h>
14
15 #include <ModelAPI_ResultConstruction.h>
16 #include <ModelAPI_AttributeRefAttr.h>
17 #include <ModelAPI_AttributeDouble.h>
18
19 #include <GeomDataAPI_Point2D.h>
20 #include <GeomAPI_Lin2d.h>
21
22
23 namespace SketcherPrs_Tools {
24
25 ObjectPtr getResult(ModelAPI_Feature* theFeature, const std::string& theAttrName)
26 {
27   std::shared_ptr<ModelAPI_Data> aData = theFeature->data();
28   std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = aData->refattr(theAttrName);
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 (anAttr->attr()) {
62     aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttr->attr());
63   }
64   if (aPointAttr.get() != NULL)
65     return aPointAttr->pnt();
66   return std::shared_ptr<GeomAPI_Pnt2d>();
67 }
68
69 //*************************************************************************************
70 std::shared_ptr<GeomDataAPI_Point2D> getFeaturePoint(DataPtr theData,
71                                                      const std::string& theAttribute)
72 {
73   std::shared_ptr<GeomDataAPI_Point2D> aPointAttr;
74
75   if (!theData)
76     return aPointAttr;
77
78   FeaturePtr aFeature;
79   std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = std::dynamic_pointer_cast<
80       ModelAPI_AttributeRefAttr>(theData->attribute(theAttribute));
81   if (anAttr)
82     aFeature = ModelAPI_Feature::feature(anAttr->object());
83
84   if (aFeature && aFeature->getKind() == SketchPlugin_Point::ID())
85     aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
86         aFeature->data()->attribute(SketchPlugin_Point::COORD_ID()));
87
88   else if (anAttr->attr()) {
89     aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttr->attr());
90   }
91   return aPointAttr;
92 }
93
94 //*************************************************************************************
95 FeaturePtr getFeatureLine(DataPtr theData,
96                           const std::string& theAttribute)
97 {
98   FeaturePtr aLine;
99   if (!theData)
100     return aLine;
101
102   std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = 
103     std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theData->attribute(theAttribute));
104   if (anAttr) {
105     FeaturePtr aFeature = ModelAPI_Feature::feature(anAttr->object());
106     if (aFeature && aFeature->getKind() == SketchPlugin_Line::ID()) {
107       return aFeature;
108     }
109   }
110   return aLine;
111 }
112
113 //*************************************************************************************
114 std::shared_ptr<GeomAPI_Pnt2d> getProjectionPoint(const FeaturePtr theLine,
115                                                   const std::shared_ptr<GeomAPI_Pnt2d>& thePoint)
116 {
117   DataPtr aData = theLine->data();
118   std::shared_ptr<GeomDataAPI_Point2D> aPoint1 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
119       aData->attribute(SketchPlugin_Line::START_ID()));
120   std::shared_ptr<GeomDataAPI_Point2D> aPoint2 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
121       aData->attribute(SketchPlugin_Line::END_ID()));
122
123   GeomAPI_Lin2d aLin2d(aPoint1->x(), aPoint1->y(), aPoint2->x(), aPoint2->y());
124   return aLin2d.project(thePoint);
125 }
126
127
128 static double MyArrowSize = 30.;
129 double getArrowSize()
130 {
131   return MyArrowSize;
132 }
133
134 void setArrowSize(double theSize)
135 {
136   MyArrowSize = theSize;
137 }
138
139 double getFlyoutDistance(const ModelAPI_Feature* theConstraint)
140 {
141   std::shared_ptr<GeomDataAPI_Point2D> aFlyoutPoint =
142       std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
143       const_cast<ModelAPI_Feature*>(theConstraint)->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
144
145   return aFlyoutPoint->y();
146 }
147
148 std::shared_ptr<GeomAPI_Pnt> getAnchorPoint(const ModelAPI_Feature* theConstraint,
149                                             const std::shared_ptr<GeomAPI_Ax3>& thePlane)
150 {
151   ModelAPI_Feature* aConstraint = const_cast<ModelAPI_Feature*>(theConstraint);
152   AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
153       aConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
154   if (!aRefAttr || !aRefAttr->isObject() || !aRefAttr->object())
155     return std::shared_ptr<GeomAPI_Pnt>();
156
157   FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttr->object());
158   std::shared_ptr<GeomAPI_Pnt2d> aCenter;
159   if (aFeature->getKind() == SketchPlugin_Arc::ID()) {
160     aCenter = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
161         aFeature->attribute(SketchPlugin_Arc::CENTER_ID()))->pnt();
162   } else if (aFeature->getKind() == SketchPlugin_Circle::ID()) {
163     aCenter = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
164         aFeature->attribute(SketchPlugin_Circle::CENTER_ID()))->pnt();
165   } else
166     return std::shared_ptr<GeomAPI_Pnt>();
167
168   std::shared_ptr<GeomAPI_Pnt2d> anOrigin(new GeomAPI_Pnt2d(0.0, 0.0));
169   std::shared_ptr<GeomAPI_Pnt2d> aFlyoutPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
170       aConstraint->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()))->pnt();
171   double aRadius = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
172       aConstraint->attribute(SketchPlugin_Constraint::VALUE()))->value();
173   double aLen = aFlyoutPnt->distance(anOrigin);
174   aRadius *= 1.001; // a gap to make point much closer to the circle, but not lying on it
175   aFlyoutPnt->setX(aCenter->x() + aFlyoutPnt->x() * aRadius / aLen);
176   aFlyoutPnt->setY(aCenter->y() + aFlyoutPnt->y() * aRadius / aLen);
177   return thePlane->to3D(aFlyoutPnt->x(), aFlyoutPnt->y());
178 }
179
180 };