Salome HOME
Coincidence correction concerning the next regression: line to be coincident to anoth...
[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 <Events_Error.h>
21
22 #include <GeomDataAPI_Point2D.h>
23 #include <GeomAPI_Lin2d.h>
24
25 #include <TopoDS.hxx>
26 #include <TopoDS_Shape.hxx>
27 #include <TopoDS_Vertex.hxx>
28
29 #include <Prs3d_DimensionAspect.hxx>
30
31 #include <BRep_Tool.hxx>
32 #include <Precision.hxx>
33
34 #include <AIS_Dimension.hxx>
35
36 // it is not possible to use 0x2211 as summ symbol because it is not supported by
37 // debian Linux platform
38 static const Standard_ExtCharacter MyEmptySymbol(' ');
39 static const Standard_ExtCharacter MySigmaSymbol('=');//0x03A3); // using equal instead of sigma
40
41 namespace SketcherPrs_Tools {
42
43 AttributePtr getAttribute(ModelAPI_Feature* theFeature, const std::string& theAttrName)
44 {
45   AttributePtr anAttribute;
46   if (theFeature) {
47     std::shared_ptr<ModelAPI_Data> aData = theFeature->data();
48     if (aData.get() && aData->isValid()) { /// essential check as it is called in openGl thread
49       std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = aData->refattr(theAttrName);
50       if (!anAttr->isObject())
51         anAttribute = anAttr->attr();
52     }
53   }
54   return anAttribute;
55 }
56
57 ObjectPtr getResult(ModelAPI_Feature* theFeature, const std::string& theAttrName)
58 {
59   ObjectPtr anObject;
60   if (theFeature) {
61     std::shared_ptr<ModelAPI_Data> aData = theFeature->data();
62     if (aData.get() && aData->isValid()) { /// essential check as it is called in openGl thread
63       std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = aData->refattr(theAttrName);
64       if (anAttr.get())
65         anObject = anAttr->object();
66     }
67   }
68   return anObject;
69 }
70
71
72 std::shared_ptr<GeomAPI_Shape> getShape(ObjectPtr theObject)
73 {
74   ResultConstructionPtr aRes = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theObject);
75   if (aRes.get() != NULL && aRes->data()->isValid()) {/// essential check as it is called in openGl thread
76     return aRes->shape();
77   }
78   return std::shared_ptr<GeomAPI_Shape>();
79 }
80
81
82 std::shared_ptr<GeomAPI_Pnt2d> getPoint(ModelAPI_Feature* theFeature,
83                                         const std::string& theAttribute)
84 {
85   std::shared_ptr<GeomDataAPI_Point2D> aPointAttr;
86
87   /// essential check as it is called in openGl thread
88   if (!theFeature || !theFeature->data().get() || !theFeature->data()->isValid())
89     return std::shared_ptr<GeomAPI_Pnt2d>();
90
91   FeaturePtr aFeature;
92   std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = std::dynamic_pointer_cast<
93       ModelAPI_AttributeRefAttr>(theFeature->data()->attribute(theAttribute));
94   if(!anAttr.get()) {
95     return std::shared_ptr<GeomAPI_Pnt2d>();
96   }
97   aFeature = ModelAPI_Feature::feature(anAttr->object());
98
99   if (aFeature && aFeature->getKind() == SketchPlugin_Point::ID())
100     aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
101         aFeature->data()->attribute(SketchPlugin_Point::COORD_ID()));
102
103   else if (anAttr->attr()) {
104     aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttr->attr());
105   }
106   if (aPointAttr.get() != NULL)
107     return aPointAttr->pnt();
108   return std::shared_ptr<GeomAPI_Pnt2d>();
109 }
110
111 //*************************************************************************************
112 /// Find an attribute of the given object which corresponds to a vetrex 
113 /// defined by a shape
114 /// \param theObject an object
115 /// \param theShape a vertex
116 /// \param thePlane a projection plane (sketcher plane)
117 std::shared_ptr<GeomDataAPI_Point2D> findGeomPoint(ObjectPtr theObject, 
118                                     const TopoDS_Shape& theShape, 
119                                     const std::shared_ptr<GeomAPI_Ax3>& thePlane)
120 {
121   std::shared_ptr<GeomDataAPI_Point2D> aGeomPoint;
122
123   FeaturePtr anObjectFeature = ModelAPI_Feature::feature(theObject);
124   if (anObjectFeature) {
125     if (theShape.ShapeType() == TopAbs_VERTEX) {
126       const TopoDS_Vertex& aShapeVertex = TopoDS::Vertex(theShape);
127       if (!aShapeVertex.IsNull())  {
128         gp_Pnt aShapePoint = BRep_Tool::Pnt(aShapeVertex);
129         std::shared_ptr<GeomAPI_Pnt> aShapeGeomPnt = std::shared_ptr<GeomAPI_Pnt>(
130             new GeomAPI_Pnt(aShapePoint.X(), aShapePoint.Y(), aShapePoint.Z()));
131
132         // find the given point in the feature attributes
133         std::list<AttributePtr> anObjectAttiributes = 
134           anObjectFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
135         std::list<AttributePtr>::const_iterator anIt = anObjectAttiributes.begin(), 
136                                                 aLast = anObjectAttiributes.end();
137         for (; anIt != aLast && !aGeomPoint; anIt++) {
138           std::shared_ptr<GeomDataAPI_Point2D> anAttributePoint = 
139             std::dynamic_pointer_cast<GeomDataAPI_Point2D>(*anIt);
140
141           std::shared_ptr<GeomAPI_Pnt> anAttributePnt = thePlane->to3D(anAttributePoint->x(),
142                                                                        anAttributePoint->y());
143           if (anAttributePnt.get() &&
144               anAttributePnt->distance(aShapeGeomPnt) < Precision::Confusion()) {
145             aGeomPoint = anAttributePoint;
146             break;
147           }
148         }
149       }
150     }
151   }
152   return aGeomPoint;
153 }
154
155 //*************************************************************************************
156 std::shared_ptr<GeomDataAPI_Point2D> getFeaturePoint(DataPtr theData,
157                                                      const std::string& theAttribute,
158                                                      const std::shared_ptr<GeomAPI_Ax3>& thePlane)
159 {
160   std::shared_ptr<GeomDataAPI_Point2D> aPointAttr;
161
162   if (!theData.get() || !theData->isValid()) /// essential check as it is called in openGl thread
163     return aPointAttr;
164
165   FeaturePtr aFeature;
166   std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = std::dynamic_pointer_cast<
167       ModelAPI_AttributeRefAttr>(theData->attribute(theAttribute));
168   if (anAttr) {
169     if (anAttr->isObject()) {
170       ObjectPtr anObject = anAttr->object();
171       aFeature = ModelAPI_Feature::feature(anObject);
172       if (aFeature && aFeature->getKind() == SketchPlugin_Point::ID()) {
173         // Attribute refers to a point
174         aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
175                      aFeature->data()->attribute(SketchPlugin_Point::COORD_ID()));
176       }
177       else {
178         // if the attribute refers on another object
179         ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
180         if (aRes.get()) {
181           GeomShapePtr aShape = aRes->shape();
182           if (aShape.get()) {
183             TopoDS_Shape aTDSShape = aShape->impl<TopoDS_Shape>();
184             aPointAttr = findGeomPoint(anObject, aTDSShape, thePlane);
185           }
186         }
187       }
188     }
189     else if (anAttr->attr()) {
190       // If attribute is a point
191       aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttr->attr());
192     }
193   }
194   return aPointAttr;
195 }
196
197 //*************************************************************************************
198 FeaturePtr getFeatureLine(DataPtr theData,
199                           const std::string& theAttribute)
200 {
201   FeaturePtr aLine;
202   if (!theData.get() || !theData->isValid()) /// essential check as it is called in openGl thread)
203     return aLine;
204
205   std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = 
206     std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theData->attribute(theAttribute));
207   if (anAttr) {
208     FeaturePtr aFeature = ModelAPI_Feature::feature(anAttr->object());
209     if (aFeature && aFeature->getKind() == SketchPlugin_Line::ID()) {
210       return aFeature;
211     }
212   }
213   return aLine;
214 }
215
216 //*************************************************************************************
217 std::shared_ptr<GeomAPI_Pnt2d> getProjectionPoint(const FeaturePtr theLine,
218                                                   const std::shared_ptr<GeomAPI_Pnt2d>& thePoint)
219 {
220   DataPtr aData = theLine->data();
221   if (!aData.get() || !aData->isValid())
222     return std::shared_ptr<GeomAPI_Pnt2d>();
223
224   std::shared_ptr<GeomDataAPI_Point2D> aPoint1 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
225       aData->attribute(SketchPlugin_Line::START_ID()));
226   std::shared_ptr<GeomDataAPI_Point2D> aPoint2 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
227       aData->attribute(SketchPlugin_Line::END_ID()));
228
229   GeomAPI_Lin2d aLin2d(aPoint1->x(), aPoint1->y(), aPoint2->x(), aPoint2->y());
230   return aLin2d.project(thePoint);
231 }
232
233
234 static double MyArrowSize = 20;
235 double getArrowSize()
236 {
237   return MyArrowSize;
238 }
239
240 void setArrowSize(double theSize)
241 {
242   MyArrowSize = theSize;
243 }
244
245 int getDefaultArrowSize()
246 {
247   return 20;
248 }
249
250 static double MyTextHeight = 16;
251 double getTextHeight()
252 {
253   return MyTextHeight;
254 }
255
256 void setTextHeight(double theHeight)
257 {
258   MyTextHeight = theHeight;
259 }
260
261 double getDefaultTextHeight()
262 {
263   return 16;
264 }
265
266 double getFlyoutDistance(const ModelAPI_Feature* theConstraint)
267 {
268   std::shared_ptr<GeomDataAPI_Point2D> aFlyoutPoint =
269       std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
270       const_cast<ModelAPI_Feature*>(theConstraint)->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
271
272   return aFlyoutPoint->y();
273 }
274
275 std::shared_ptr<GeomAPI_Pnt> getAnchorPoint(const ModelAPI_Feature* theConstraint,
276                                             const std::shared_ptr<GeomAPI_Ax3>& thePlane)
277 {
278   ModelAPI_Feature* aConstraint = const_cast<ModelAPI_Feature*>(theConstraint);
279   AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
280       aConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
281   if (!aRefAttr || !aRefAttr->isObject() || !aRefAttr->object())
282     return std::shared_ptr<GeomAPI_Pnt>();
283
284   FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttr->object());
285   std::shared_ptr<GeomAPI_Pnt2d> aCenter;
286   if (aFeature->getKind() == SketchPlugin_Arc::ID()) { // arc
287     aCenter = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
288         aFeature->attribute(SketchPlugin_Arc::CENTER_ID()))->pnt();
289   } else if (aFeature->getKind() == SketchPlugin_Circle::ID()) { // circle
290     aCenter = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
291         aFeature->attribute(SketchPlugin_Circle::CENTER_ID()))->pnt();
292   } else 
293     return std::shared_ptr<GeomAPI_Pnt>();
294
295   std::shared_ptr<GeomAPI_Pnt2d> anOrigin(new GeomAPI_Pnt2d(0.0, 0.0));
296   std::shared_ptr<GeomAPI_Pnt2d> aFlyoutPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
297       aConstraint->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()))->pnt();
298   double aRadius = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
299       aConstraint->attribute(SketchPlugin_Constraint::VALUE()))->value();
300   double aLen = aFlyoutPnt->distance(anOrigin);
301   aRadius *= 1.001; // a gap to make point much closer to the circle, but not lying on it
302   aFlyoutPnt->setX(aCenter->x() + aFlyoutPnt->x() * aRadius / aLen);
303   aFlyoutPnt->setY(aCenter->y() + aFlyoutPnt->y() * aRadius / aLen);
304   return thePlane->to3D(aFlyoutPnt->x(), aFlyoutPnt->y());
305 }
306
307 void setDisplaySpecialSymbol(AIS_Dimension* theDimension, const bool& theToDisplay)
308 {
309   if (theToDisplay) {
310     theDimension->SetSpecialSymbol(MySigmaSymbol);
311     theDimension->SetDisplaySpecialSymbol(AIS_DSS_Before);
312   }
313   else {
314     theDimension->SetSpecialSymbol(MyEmptySymbol);
315     theDimension->SetDisplaySpecialSymbol(AIS_DSS_No);
316   }
317 }
318
319 void setDisplayParameter(AIS_Dimension* theDimension, const std::string& theParameter,
320                          const bool& theToDisplay)
321 {
322    if (theToDisplay) {
323     theDimension->DimensionAspect()->MakeUnitsDisplayed(true);
324     theDimension->SetDisplayUnits(TCollection_AsciiString(theParameter.c_str()));
325     theDimension->DimensionAspect()->SetValueStringFormat("");
326   }
327   else {
328     theDimension->DimensionAspect()->MakeUnitsDisplayed(false);
329     theDimension->SetDisplayUnits(TCollection_AsciiString()); // THE_UNDEFINED_UNITS in AIS_Dimension
330     theDimension->DimensionAspect()->SetValueStringFormat("%g");
331   }
332 }
333
334 void sendExpressionShownEvent(const bool& theState)
335 {
336   static Events_ID anId = SketcherPrs_ParameterStyleMessage::eventId();
337   std::shared_ptr<SketcherPrs_ParameterStyleMessage> aMessage = std::shared_ptr
338     <SketcherPrs_ParameterStyleMessage>(new SketcherPrs_ParameterStyleMessage(anId, 0));
339   aMessage->setStyle(theState ? SketcherPrs_ParameterStyleMessage::ParameterText
340                               : SketcherPrs_ParameterStyleMessage::ParameterValue);
341   Events_Loop::loop()->send(aMessage);
342   Events_Loop::loop()->flush(anId);
343 }
344
345 Handle(Prs3d_DimensionAspect) createDimensionAspect()
346 {
347   Handle(Prs3d_DimensionAspect) anAspect = new Prs3d_DimensionAspect();
348   anAspect->MakeArrows3d(false);
349   anAspect->MakeText3d(false);
350   anAspect->MakeTextShaded(false);
351   anAspect->MakeUnitsDisplayed(false);
352   anAspect->TextAspect()->SetHeight(SketcherPrs_Tools::getDefaultTextHeight());
353   anAspect->ArrowAspect()->SetLength(SketcherPrs_Tools::getArrowSize());
354
355   return anAspect;
356 }
357
358 void updateArrows(Handle(Prs3d_DimensionAspect) theDimAspect, double theDimValue, double theTextSize)
359 {
360   double anArrowLength = theDimAspect->ArrowAspect()->Length();
361    // This is not realy correct way to get viewer scale.
362   double aViewerScale = (double) SketcherPrs_Tools::getDefaultArrowSize() / anArrowLength;
363
364   if(theTextSize > ((theDimValue - 3 * SketcherPrs_Tools::getArrowSize()) * aViewerScale)) {
365     theDimAspect->SetTextHorizontalPosition(Prs3d_DTHP_Left);
366     theDimAspect->SetArrowOrientation(Prs3d_DAO_External);
367     theDimAspect->SetExtensionSize(theTextSize / aViewerScale - SketcherPrs_Tools::getArrowSize() / 2.0);
368   } else {
369     theDimAspect->SetTextHorizontalPosition(Prs3d_DTHP_Center);
370     theDimAspect->SetArrowOrientation(Prs3d_DAO_Internal);
371   }
372   theDimAspect->SetArrowTailSize(theDimAspect->ArrowAspect()->Length());
373   // The value of vertical aligment is sometimes changed
374   theDimAspect->TextAspect()->SetVerticalJustification(Graphic3d_VTA_CENTER);
375 }
376
377 void sendEmptyPresentationError(ModelAPI_Feature* theFeature, const std::string theError)
378 {
379   Events_Error::throwException("An empty AIS presentation: SketcherPrs_LengthDimension");
380   static const Events_ID anEvent = Events_Loop::eventByName(EVENT_EMPTY_AIS_PRESENTATION);
381   std::shared_ptr<ModelAPI_Object> aConstraintPtr(theFeature);
382   ModelAPI_EventCreator::get()->sendUpdated(aConstraintPtr, anEvent);
383 }
384 };