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