Salome HOME
Documentation of the sources
[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 /// Find an attribute of the given object which corresponds to a vetrex 
77 /// defined by a shape
78 /// \param theObject an object
79 /// \param theShape a vertex
80 /// \param thePlane a projection plane (sketcher plane)
81 std::shared_ptr<GeomDataAPI_Point2D> findGeomPoint(ObjectPtr theObject, 
82                                     const TopoDS_Shape& theShape, 
83                                     const std::shared_ptr<GeomAPI_Ax3>& thePlane)
84 {
85   std::shared_ptr<GeomDataAPI_Point2D> aGeomPoint;
86
87   FeaturePtr anObjectFeature = ModelAPI_Feature::feature(theObject);
88   if (anObjectFeature) {
89     if (theShape.ShapeType() == TopAbs_VERTEX) {
90       const TopoDS_Vertex& aShapeVertex = TopoDS::Vertex(theShape);
91       if (!aShapeVertex.IsNull())  {
92         gp_Pnt aShapePoint = BRep_Tool::Pnt(aShapeVertex);
93         std::shared_ptr<GeomAPI_Pnt> aShapeGeomPnt = std::shared_ptr<GeomAPI_Pnt>(
94             new GeomAPI_Pnt(aShapePoint.X(), aShapePoint.Y(), aShapePoint.Z()));
95
96         // find the given point in the feature attributes
97         std::list<AttributePtr> anObjectAttiributes = 
98           anObjectFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
99         std::list<AttributePtr>::const_iterator anIt = anObjectAttiributes.begin(), 
100                                                 aLast = anObjectAttiributes.end();
101         for (; anIt != aLast && !aGeomPoint; anIt++) {
102           std::shared_ptr<GeomDataAPI_Point2D> anAttributePoint = 
103             std::dynamic_pointer_cast<GeomDataAPI_Point2D>(*anIt);
104
105           std::shared_ptr<GeomAPI_Pnt> anAttributePnt = thePlane->to3D(anAttributePoint->x(),
106                                                                        anAttributePoint->y());
107           if (anAttributePnt.get() &&
108               anAttributePnt->distance(aShapeGeomPnt) < Precision::Confusion()) {
109             aGeomPoint = anAttributePoint;
110             break;
111           }
112         }
113       }
114     }
115   }
116   return aGeomPoint;
117 }
118
119 //*************************************************************************************
120 std::shared_ptr<GeomDataAPI_Point2D> getFeaturePoint(DataPtr theData,
121                                                      const std::string& theAttribute,
122                                                      const std::shared_ptr<GeomAPI_Ax3>& thePlane)
123 {
124   std::shared_ptr<GeomDataAPI_Point2D> aPointAttr;
125
126   if (!theData)
127     return aPointAttr;
128
129   FeaturePtr aFeature;
130   std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = std::dynamic_pointer_cast<
131       ModelAPI_AttributeRefAttr>(theData->attribute(theAttribute));
132   if (anAttr) {
133     if (anAttr->isObject()) {
134       ObjectPtr anObject = anAttr->object();
135       aFeature = ModelAPI_Feature::feature(anObject);
136       if (aFeature && aFeature->getKind() == SketchPlugin_Point::ID()) {
137         // Attribute refers to a point
138         aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
139                      aFeature->data()->attribute(SketchPlugin_Point::COORD_ID()));
140       }
141       else {
142         // if the attribute refers on another object
143         ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
144         if (aRes.get()) {
145           GeomShapePtr aShape = aRes->shape();
146           if (aShape.get()) {
147             TopoDS_Shape aTDSShape = aShape->impl<TopoDS_Shape>();
148             aPointAttr = findGeomPoint(anObject, aTDSShape, thePlane);
149           }
150         }
151       }
152     }
153     else if (anAttr->attr()) {
154       // If attribute is a point
155       aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttr->attr());
156     }
157   }
158   return aPointAttr;
159 }
160
161 //*************************************************************************************
162 FeaturePtr getFeatureLine(DataPtr theData,
163                           const std::string& theAttribute)
164 {
165   FeaturePtr aLine;
166   if (!theData)
167     return aLine;
168
169   std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = 
170     std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theData->attribute(theAttribute));
171   if (anAttr) {
172     FeaturePtr aFeature = ModelAPI_Feature::feature(anAttr->object());
173     if (aFeature && aFeature->getKind() == SketchPlugin_Line::ID()) {
174       return aFeature;
175     }
176   }
177   return aLine;
178 }
179
180 //*************************************************************************************
181 std::shared_ptr<GeomAPI_Pnt2d> getProjectionPoint(const FeaturePtr theLine,
182                                                   const std::shared_ptr<GeomAPI_Pnt2d>& thePoint)
183 {
184   DataPtr aData = theLine->data();
185   std::shared_ptr<GeomDataAPI_Point2D> aPoint1 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
186       aData->attribute(SketchPlugin_Line::START_ID()));
187   std::shared_ptr<GeomDataAPI_Point2D> aPoint2 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
188       aData->attribute(SketchPlugin_Line::END_ID()));
189
190   GeomAPI_Lin2d aLin2d(aPoint1->x(), aPoint1->y(), aPoint2->x(), aPoint2->y());
191   return aLin2d.project(thePoint);
192 }
193
194
195 static double MyArrowSize = 24.;
196 double getArrowSize()
197 {
198   return MyArrowSize;
199 }
200
201 void setArrowSize(double theSize)
202 {
203   MyArrowSize = theSize;
204 }
205
206 static double MyTextHeight = 16;
207 double getTextHeight()
208 {
209   return MyTextHeight;
210 }
211
212 void setTextHeight(double theHeight)
213 {
214   MyTextHeight = theHeight;
215 }
216
217 double getDefaultTextHeight()
218 {
219   return 16;
220 }
221
222 double getFlyoutDistance(const ModelAPI_Feature* theConstraint)
223 {
224   std::shared_ptr<GeomDataAPI_Point2D> aFlyoutPoint =
225       std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
226       const_cast<ModelAPI_Feature*>(theConstraint)->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
227
228   return aFlyoutPoint->y();
229 }
230
231 std::shared_ptr<GeomAPI_Pnt> getAnchorPoint(const ModelAPI_Feature* theConstraint,
232                                             const std::shared_ptr<GeomAPI_Ax3>& thePlane)
233 {
234   ModelAPI_Feature* aConstraint = const_cast<ModelAPI_Feature*>(theConstraint);
235   AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
236       aConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
237   if (!aRefAttr || !aRefAttr->isObject() || !aRefAttr->object())
238     return std::shared_ptr<GeomAPI_Pnt>();
239
240   FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttr->object());
241   std::shared_ptr<GeomAPI_Pnt2d> aCenter;
242   if (aFeature->getKind() == SketchPlugin_Arc::ID()) { // arc
243     aCenter = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
244         aFeature->attribute(SketchPlugin_Arc::CENTER_ID()))->pnt();
245   } else if (aFeature->getKind() == SketchPlugin_Circle::ID()) { // circle
246     aCenter = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
247         aFeature->attribute(SketchPlugin_Circle::CENTER_ID()))->pnt();
248   } else 
249     return std::shared_ptr<GeomAPI_Pnt>();
250
251   std::shared_ptr<GeomAPI_Pnt2d> anOrigin(new GeomAPI_Pnt2d(0.0, 0.0));
252   std::shared_ptr<GeomAPI_Pnt2d> aFlyoutPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
253       aConstraint->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()))->pnt();
254   double aRadius = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
255       aConstraint->attribute(SketchPlugin_Constraint::VALUE()))->value();
256   double aLen = aFlyoutPnt->distance(anOrigin);
257   aRadius *= 1.001; // a gap to make point much closer to the circle, but not lying on it
258   aFlyoutPnt->setX(aCenter->x() + aFlyoutPnt->x() * aRadius / aLen);
259   aFlyoutPnt->setY(aCenter->y() + aFlyoutPnt->y() * aRadius / aLen);
260   return thePlane->to3D(aFlyoutPnt->x(), aFlyoutPnt->y());
261 }
262
263 };