Salome HOME
Merge branch 'master' into cgt/devCEA
[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 #include <ModelAPI_Events.h>
19
20 #include <ModelGeomAlgo_Point2D.h>
21
22 #include <Events_InfoMessage.h>
23
24 #include <GeomDataAPI_Point2D.h>
25 #include <GeomAPI_Lin2d.h>
26
27 #include <TopoDS.hxx>
28 #include <TopoDS_Shape.hxx>
29 #include <TopoDS_Vertex.hxx>
30
31 #include <Prs3d_DimensionAspect.hxx>
32
33 #include <BRep_Tool.hxx>
34 #include <Precision.hxx>
35
36 #include <AIS_Dimension.hxx>
37
38 namespace SketcherPrs_Tools {
39
40 AttributePtr getAttribute(ModelAPI_Feature* theFeature, const std::string& theAttrName)
41 {
42   AttributePtr anAttribute;
43   if (theFeature) {
44     std::shared_ptr<ModelAPI_Data> aData = theFeature->data();
45     if (aData.get() && aData->isValid()) { /// essential check as it is called in openGl thread
46       std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = aData->refattr(theAttrName);
47       if (!anAttr->isObject())
48         anAttribute = anAttr->attr();
49     }
50   }
51   return anAttribute;
52 }
53
54 ObjectPtr getResult(ModelAPI_Feature* theFeature, const std::string& theAttrName)
55 {
56   ObjectPtr anObject;
57   if (theFeature) {
58     std::shared_ptr<ModelAPI_Data> aData = theFeature->data();
59     if (aData.get() && aData->isValid()) { /// essential check as it is called in openGl thread
60       std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = aData->refattr(theAttrName);
61       if (anAttr.get())
62         anObject = anAttr->object();
63     }
64   }
65   return anObject;
66 }
67
68
69 std::shared_ptr<GeomAPI_Shape> getShape(ObjectPtr theObject)
70 {
71   ResultConstructionPtr aRes = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theObject);
72   if (aRes.get() != NULL && aRes->data()->isValid()) {
73     /// essential check as it is called in openGl thread
74     return aRes->shape();
75   }
76   return std::shared_ptr<GeomAPI_Shape>();
77 }
78
79
80 std::shared_ptr<GeomAPI_Pnt2d> getPoint(ModelAPI_Feature* theFeature,
81                                         const std::string& theAttribute)
82 {
83   std::shared_ptr<GeomDataAPI_Point2D> aPointAttr = ModelGeomAlgo_Point2D::getPointOfRefAttr(
84                theFeature, theAttribute, SketchPlugin_Point::ID(), SketchPlugin_Point::COORD_ID());
85   if (aPointAttr.get() != NULL)
86     return aPointAttr->pnt();
87   return std::shared_ptr<GeomAPI_Pnt2d>();
88 }
89
90 //*************************************************************************************
91 /// Find an attribute of the given object which corresponds to a vetrex
92 /// defined by a shape
93 /// \param theObject an object
94 /// \param theShape a vertex
95 /// \param thePlane a projection plane (sketcher plane)
96 std::shared_ptr<GeomDataAPI_Point2D> findGeomPoint(ObjectPtr theObject,
97                                     const TopoDS_Shape& theShape,
98                                     const std::shared_ptr<GeomAPI_Ax3>& thePlane)
99 {
100   std::shared_ptr<GeomDataAPI_Point2D> aGeomPoint;
101
102   FeaturePtr anObjectFeature = ModelAPI_Feature::feature(theObject);
103   if (anObjectFeature) {
104     if (theShape.ShapeType() == TopAbs_VERTEX) {
105       const TopoDS_Vertex& aShapeVertex = TopoDS::Vertex(theShape);
106       if (!aShapeVertex.IsNull())  {
107         gp_Pnt aShapePoint = BRep_Tool::Pnt(aShapeVertex);
108         std::shared_ptr<GeomAPI_Pnt> aShapeGeomPnt = std::shared_ptr<GeomAPI_Pnt>(
109             new GeomAPI_Pnt(aShapePoint.X(), aShapePoint.Y(), aShapePoint.Z()));
110
111         // find the given point in the feature attributes
112         std::list<AttributePtr> anObjectAttiributes =
113           anObjectFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
114         std::list<AttributePtr>::const_iterator anIt = anObjectAttiributes.begin(),
115                                                 aLast = anObjectAttiributes.end();
116         for (; anIt != aLast && !aGeomPoint; anIt++) {
117           std::shared_ptr<GeomDataAPI_Point2D> anAttributePoint =
118             std::dynamic_pointer_cast<GeomDataAPI_Point2D>(*anIt);
119
120           std::shared_ptr<GeomAPI_Pnt> anAttributePnt = thePlane->to3D(anAttributePoint->x(),
121                                                                        anAttributePoint->y());
122           if (anAttributePnt.get() &&
123               anAttributePnt->distance(aShapeGeomPnt) < Precision::Confusion()) {
124             aGeomPoint = anAttributePoint;
125             break;
126           }
127         }
128       }
129     }
130   }
131   return aGeomPoint;
132 }
133
134 //*************************************************************************************
135 std::shared_ptr<GeomDataAPI_Point2D> getFeaturePoint(DataPtr theData,
136                                                      const std::string& theAttribute,
137                                                      const std::shared_ptr<GeomAPI_Ax3>& thePlane)
138 {
139   std::shared_ptr<GeomDataAPI_Point2D> aPointAttr;
140
141   if (!theData.get() || !theData->isValid()) /// essential check as it is called in openGl thread
142     return aPointAttr;
143
144   FeaturePtr aFeature;
145   std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = std::dynamic_pointer_cast<
146       ModelAPI_AttributeRefAttr>(theData->attribute(theAttribute));
147   if (anAttr) {
148     if (anAttr->isObject()) {
149       ObjectPtr anObject = anAttr->object();
150       aFeature = ModelAPI_Feature::feature(anObject);
151       if (aFeature && aFeature->getKind() == SketchPlugin_Point::ID()) {
152         // Attribute refers to a point
153         aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
154                      aFeature->data()->attribute(SketchPlugin_Point::COORD_ID()));
155       }
156       else {
157         // if the attribute refers on another object
158         ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
159         if (aRes.get()) {
160           GeomShapePtr aShape = aRes->shape();
161           if (aShape.get()) {
162             TopoDS_Shape aTDSShape = aShape->impl<TopoDS_Shape>();
163             aPointAttr = findGeomPoint(anObject, aTDSShape, thePlane);
164           }
165         }
166       }
167     }
168     else if (anAttr->attr()) {
169       // If attribute is a point
170       aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttr->attr());
171     }
172   }
173   return aPointAttr;
174 }
175
176 //*************************************************************************************
177 FeaturePtr getFeatureLine(DataPtr theData,
178                           const std::string& theAttribute)
179 {
180   FeaturePtr aLine;
181   if (!theData.get() || !theData->isValid()) /// essential check as it is called in openGl thread)
182     return aLine;
183
184   std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr =
185     std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theData->attribute(theAttribute));
186   if (anAttr) {
187     FeaturePtr aFeature = ModelAPI_Feature::feature(anAttr->object());
188     if (aFeature && aFeature->getKind() == SketchPlugin_Line::ID()) {
189       return aFeature;
190     }
191   }
192   return aLine;
193 }
194
195 //*************************************************************************************
196 std::shared_ptr<GeomAPI_Pnt2d> getProjectionPoint(const FeaturePtr theLine,
197                                                   const std::shared_ptr<GeomAPI_Pnt2d>& thePoint)
198 {
199   DataPtr aData = theLine->data();
200   if (!aData.get() || !aData->isValid())
201     return std::shared_ptr<GeomAPI_Pnt2d>();
202
203   std::shared_ptr<GeomDataAPI_Point2D> aPoint1 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
204       aData->attribute(SketchPlugin_Line::START_ID()));
205   std::shared_ptr<GeomDataAPI_Point2D> aPoint2 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
206       aData->attribute(SketchPlugin_Line::END_ID()));
207
208   GeomAPI_Lin2d aLin2d(aPoint1->x(), aPoint1->y(), aPoint2->x(), aPoint2->y());
209   return aLin2d.project(thePoint);
210 }
211
212
213 static double MyArrowSize = 20;
214 double getArrowSize()
215 {
216   return MyArrowSize;
217 }
218
219 void setArrowSize(double theSize)
220 {
221   MyArrowSize = theSize;
222 }
223
224 int getDefaultArrowSize()
225 {
226   return 20;
227 }
228
229 static double MyTextHeight = 16;
230 double getTextHeight()
231 {
232   return MyTextHeight;
233 }
234
235 void setTextHeight(double theHeight)
236 {
237   MyTextHeight = theHeight;
238 }
239
240 double getDefaultTextHeight()
241 {
242   return 16;
243 }
244
245 double getFlyoutDistance(const ModelAPI_Feature* theConstraint)
246 {
247   std::shared_ptr<GeomDataAPI_Point2D> aFlyoutPoint =
248       std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
249       const_cast<ModelAPI_Feature*>(
250       theConstraint)->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
251   // for not initialized values return zero distance to avoid Presentation crash
252   if (!aFlyoutPoint->isInitialized())
253     return 0;
254   return aFlyoutPoint->y();
255 }
256
257 std::shared_ptr<GeomAPI_Pnt> getAnchorPoint(const ModelAPI_Feature* theConstraint,
258                                             const std::shared_ptr<GeomAPI_Ax3>& thePlane)
259 {
260   ModelAPI_Feature* aConstraint = const_cast<ModelAPI_Feature*>(theConstraint);
261   AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
262       aConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
263   if (!aRefAttr || !aRefAttr->isObject() || !aRefAttr->object())
264     return std::shared_ptr<GeomAPI_Pnt>();
265
266   FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttr->object());
267   std::shared_ptr<GeomAPI_Pnt2d> aCenter;
268   if (aFeature->getKind() == SketchPlugin_Arc::ID()) { // arc
269     aCenter = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
270         aFeature->attribute(SketchPlugin_Arc::CENTER_ID()))->pnt();
271   } else if (aFeature->getKind() == SketchPlugin_Circle::ID()) { // circle
272     aCenter = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
273         aFeature->attribute(SketchPlugin_Circle::CENTER_ID()))->pnt();
274   } else
275     return std::shared_ptr<GeomAPI_Pnt>();
276
277   std::shared_ptr<GeomAPI_Pnt2d> anOrigin(new GeomAPI_Pnt2d(0.0, 0.0));
278   std::shared_ptr<GeomAPI_Pnt2d> aFlyoutPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
279       aConstraint->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()))->pnt();
280   double aRadius = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
281       aConstraint->attribute(SketchPlugin_Constraint::VALUE()))->value();
282   double aLen = aFlyoutPnt->distance(anOrigin);
283   aRadius *= 1.001; // a gap to make point much closer to the circle, but not lying on it
284   aFlyoutPnt->setX(aCenter->x() + aFlyoutPnt->x() * aRadius / aLen);
285   aFlyoutPnt->setY(aCenter->y() + aFlyoutPnt->y() * aRadius / aLen);
286   return thePlane->to3D(aFlyoutPnt->x(), aFlyoutPnt->y());
287 }
288
289 void sendExpressionShownEvent(const bool& theState)
290 {
291   static Events_ID anId = SketcherPrs_ParameterStyleMessage::eventId();
292   std::shared_ptr<SketcherPrs_ParameterStyleMessage> aMessage = std::shared_ptr
293     <SketcherPrs_ParameterStyleMessage>(new SketcherPrs_ParameterStyleMessage(anId, 0));
294   aMessage->setStyle(theState ? SketcherPrs_ParameterStyleMessage::ParameterText
295                               : SketcherPrs_ParameterStyleMessage::ParameterValue);
296   Events_Loop::loop()->send(aMessage);
297   Events_Loop::loop()->flush(anId);
298 }
299
300 void sendEmptyPresentationError(ModelAPI_Feature* theFeature, const std::string theError)
301 {
302   Events_InfoMessage("SketcherPrs_Tools",
303     "An empty AIS presentation: SketcherPrs_LengthDimension").send();
304   static const Events_ID anEvent = Events_Loop::eventByName(EVENT_EMPTY_AIS_PRESENTATION);
305   std::shared_ptr<ModelAPI_Object> aConstraintPtr(theFeature);
306   ModelAPI_EventCreator::get()->sendUpdated(aConstraintPtr, anEvent);
307 }
308 };