Salome HOME
Roll back the modification, not yet approved
[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 #include <TopoDS.hxx>
23 #include <TopoDS_Shape.hxx>
24 #include <TopoDS_Vertex.hxx>
25
26 #include <BRep_Tool.hxx>
27 #include <Precision.hxx>
28
29 namespace SketcherPrs_Tools {
30
31 ObjectPtr getResult(ModelAPI_Feature* theFeature, const std::string& theAttrName)
32 {
33   std::shared_ptr<ModelAPI_Data> aData = theFeature->data();
34   std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = aData->refattr(theAttrName);
35   return anAttr->object();
36 }
37
38
39 std::shared_ptr<GeomAPI_Shape> getShape(ObjectPtr theObject)
40 {
41   ResultConstructionPtr aRes = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theObject);
42   if (aRes.get() != NULL) {
43     return aRes->shape();
44   }
45   return std::shared_ptr<GeomAPI_Shape>();
46 }
47
48
49 std::shared_ptr<GeomAPI_Pnt2d> getPoint(ModelAPI_Feature* theFeature,
50                                         const std::string& theAttribute)
51 {
52   std::shared_ptr<GeomDataAPI_Point2D> aPointAttr;
53
54   if (!theFeature->data())
55     return std::shared_ptr<GeomAPI_Pnt2d>();
56
57   FeaturePtr aFeature;
58   std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = std::dynamic_pointer_cast<
59       ModelAPI_AttributeRefAttr>(theFeature->data()->attribute(theAttribute));
60   if (anAttr)
61     aFeature = ModelAPI_Feature::feature(anAttr->object());
62
63   if (aFeature && aFeature->getKind() == SketchPlugin_Point::ID())
64     aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
65         aFeature->data()->attribute(SketchPlugin_Point::COORD_ID()));
66
67   else if (anAttr->attr()) {
68     aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttr->attr());
69   }
70   if (aPointAttr.get() != NULL)
71     return aPointAttr->pnt();
72   return std::shared_ptr<GeomAPI_Pnt2d>();
73 }
74
75 //*************************************************************************************
76 std::shared_ptr<GeomDataAPI_Point2D> findGeomPoint(ObjectPtr theObject, 
77                                     const TopoDS_Shape& theShape, 
78                                     const std::shared_ptr<GeomAPI_Ax3>& thePlane)
79 {
80   std::shared_ptr<GeomDataAPI_Point2D> aGeomPoint;
81
82   FeaturePtr anObjectFeature = ModelAPI_Feature::feature(theObject);
83   if (anObjectFeature) {
84     if (theShape.ShapeType() == TopAbs_VERTEX) {
85       const TopoDS_Vertex& aShapeVertex = TopoDS::Vertex(theShape);
86       if (!aShapeVertex.IsNull())  {
87         gp_Pnt aShapePoint = BRep_Tool::Pnt(aShapeVertex);
88         std::shared_ptr<GeomAPI_Pnt> aShapeGeomPnt = std::shared_ptr<GeomAPI_Pnt>(
89             new GeomAPI_Pnt(aShapePoint.X(), aShapePoint.Y(), aShapePoint.Z()));
90
91         // find the given point in the feature attributes
92         std::list<AttributePtr> anObjectAttiributes = 
93           anObjectFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
94         std::list<AttributePtr>::const_iterator anIt = anObjectAttiributes.begin(), 
95                                                 aLast = anObjectAttiributes.end();
96         for (; anIt != aLast && !aGeomPoint; anIt++) {
97           std::shared_ptr<GeomDataAPI_Point2D> anAttributePoint = 
98             std::dynamic_pointer_cast<GeomDataAPI_Point2D>(*anIt);
99
100           std::shared_ptr<GeomAPI_Pnt> anAttributePnt = thePlane->to3D(anAttributePoint->x(),
101                                                                        anAttributePoint->y());
102           if (anAttributePnt.get() &&
103               anAttributePnt->distance(aShapeGeomPnt) < Precision::Confusion()) {
104             aGeomPoint = anAttributePoint;
105             break;
106           }
107         }
108       }
109     }
110   }
111   return aGeomPoint;
112 }
113
114 //*************************************************************************************
115 std::shared_ptr<GeomDataAPI_Point2D> getFeaturePoint(DataPtr theData,
116                                                      const std::string& theAttribute,
117                                                      const std::shared_ptr<GeomAPI_Ax3>& thePlane)
118 {
119   std::shared_ptr<GeomDataAPI_Point2D> aPointAttr;
120
121   if (!theData)
122     return aPointAttr;
123
124   FeaturePtr aFeature;
125   std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = std::dynamic_pointer_cast<
126       ModelAPI_AttributeRefAttr>(theData->attribute(theAttribute));
127   if (anAttr) {
128     if (anAttr->isObject()) {
129       ObjectPtr anObject = anAttr->object();
130       aFeature = ModelAPI_Feature::feature(anObject);
131       if (aFeature && aFeature->getKind() == SketchPlugin_Point::ID()) {
132         aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
133                      aFeature->data()->attribute(SketchPlugin_Point::COORD_ID()));
134       }
135       else {
136         ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
137         if (aRes.get()) {
138           GeomShapePtr aShape = aRes->shape();
139           if (aShape.get()) {
140             TopoDS_Shape aTDSShape = aShape->impl<TopoDS_Shape>();
141             aPointAttr = findGeomPoint(anObject, aTDSShape, thePlane);
142           }
143         }
144       }
145     }
146     else if (anAttr->attr()) {
147       aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttr->attr());
148     }
149   }
150   return aPointAttr;
151 }
152
153 //*************************************************************************************
154 FeaturePtr getFeatureLine(DataPtr theData,
155                           const std::string& theAttribute)
156 {
157   FeaturePtr aLine;
158   if (!theData)
159     return aLine;
160
161   std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = 
162     std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theData->attribute(theAttribute));
163   if (anAttr) {
164     FeaturePtr aFeature = ModelAPI_Feature::feature(anAttr->object());
165     if (aFeature && aFeature->getKind() == SketchPlugin_Line::ID()) {
166       return aFeature;
167     }
168   }
169   return aLine;
170 }
171
172 //*************************************************************************************
173 std::shared_ptr<GeomAPI_Pnt2d> getProjectionPoint(const FeaturePtr theLine,
174                                                   const std::shared_ptr<GeomAPI_Pnt2d>& thePoint)
175 {
176   DataPtr aData = theLine->data();
177   std::shared_ptr<GeomDataAPI_Point2D> aPoint1 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
178       aData->attribute(SketchPlugin_Line::START_ID()));
179   std::shared_ptr<GeomDataAPI_Point2D> aPoint2 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
180       aData->attribute(SketchPlugin_Line::END_ID()));
181
182   GeomAPI_Lin2d aLin2d(aPoint1->x(), aPoint1->y(), aPoint2->x(), aPoint2->y());
183   return aLin2d.project(thePoint);
184 }
185
186
187 static double MyArrowSize = 24.;
188 double getArrowSize()
189 {
190   return MyArrowSize;
191 }
192
193 void setArrowSize(double theSize)
194 {
195   MyArrowSize = theSize;
196 }
197
198 static double MyTextHeight = 16;
199 double getTextHeight()
200 {
201   return MyTextHeight;
202 }
203
204 void setTextHeight(double theHeight)
205 {
206   MyTextHeight = theHeight;
207 }
208
209 double getDefaultTextHeight()
210 {
211   return 16;
212 }
213
214 double getFlyoutDistance(const ModelAPI_Feature* theConstraint)
215 {
216   std::shared_ptr<GeomDataAPI_Point2D> aFlyoutPoint =
217       std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
218       const_cast<ModelAPI_Feature*>(theConstraint)->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
219
220   return aFlyoutPoint->y();
221 }
222
223 std::shared_ptr<GeomAPI_Pnt> getAnchorPoint(const ModelAPI_Feature* theConstraint,
224                                             const std::shared_ptr<GeomAPI_Ax3>& thePlane)
225 {
226   ModelAPI_Feature* aConstraint = const_cast<ModelAPI_Feature*>(theConstraint);
227   AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
228       aConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
229   if (!aRefAttr || !aRefAttr->isObject() || !aRefAttr->object())
230     return std::shared_ptr<GeomAPI_Pnt>();
231
232   FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttr->object());
233   std::shared_ptr<GeomAPI_Pnt2d> aCenter;
234   if (aFeature->getKind() == SketchPlugin_Arc::ID()) {
235     aCenter = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
236         aFeature->attribute(SketchPlugin_Arc::CENTER_ID()))->pnt();
237   } else if (aFeature->getKind() == SketchPlugin_Circle::ID()) {
238     aCenter = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
239         aFeature->attribute(SketchPlugin_Circle::CENTER_ID()))->pnt();
240   } else
241     return std::shared_ptr<GeomAPI_Pnt>();
242
243   std::shared_ptr<GeomAPI_Pnt2d> anOrigin(new GeomAPI_Pnt2d(0.0, 0.0));
244   std::shared_ptr<GeomAPI_Pnt2d> aFlyoutPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
245       aConstraint->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()))->pnt();
246   double aRadius = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
247       aConstraint->attribute(SketchPlugin_Constraint::VALUE()))->value();
248   double aLen = aFlyoutPnt->distance(anOrigin);
249   aRadius *= 1.001; // a gap to make point much closer to the circle, but not lying on it
250   aFlyoutPnt->setX(aCenter->x() + aFlyoutPnt->x() * aRadius / aLen);
251   aFlyoutPnt->setY(aCenter->y() + aFlyoutPnt->y() * aRadius / aLen);
252   return thePlane->to3D(aFlyoutPnt->x(), aFlyoutPnt->y());
253 }
254
255 };