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