]> SALOME platform Git repositories - modules/shaper.git/blob - src/SketcherPrs/SketcherPrs_Tools.cpp
Salome HOME
#1404 Random crash with Shaper: AIS presentations: coincidence/radius constraints...
[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   std::shared_ptr<ModelAPI_Data> aData = theFeature->data();
46   std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = aData->refattr(theAttrName);
47   return !anAttr->isObject() ? anAttr->attr() : AttributePtr();
48 }
49
50 ObjectPtr getResult(ModelAPI_Feature* theFeature, const std::string& theAttrName)
51 {
52   std::shared_ptr<ModelAPI_Data> aData = theFeature->data();
53   std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = aData->refattr(theAttrName);
54   return anAttr->object();
55 }
56
57
58 std::shared_ptr<GeomAPI_Shape> getShape(ObjectPtr theObject)
59 {
60   ResultConstructionPtr aRes = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theObject);
61   if (aRes.get() != NULL) {
62     return aRes->shape();
63   }
64   return std::shared_ptr<GeomAPI_Shape>();
65 }
66
67
68 std::shared_ptr<GeomAPI_Pnt2d> getPoint(ModelAPI_Feature* theFeature,
69                                         const std::string& theAttribute)
70 {
71   std::shared_ptr<GeomDataAPI_Point2D> aPointAttr;
72
73   if (!theFeature->data())
74     return std::shared_ptr<GeomAPI_Pnt2d>();
75
76   FeaturePtr aFeature;
77   std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = std::dynamic_pointer_cast<
78       ModelAPI_AttributeRefAttr>(theFeature->data()->attribute(theAttribute));
79   if(!anAttr.get()) {
80     return std::shared_ptr<GeomAPI_Pnt2d>();
81   }
82   aFeature = ModelAPI_Feature::feature(anAttr->object());
83
84   if (aFeature && aFeature->getKind() == SketchPlugin_Point::ID())
85     aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
86         aFeature->data()->attribute(SketchPlugin_Point::COORD_ID()));
87
88   else if (anAttr->attr()) {
89     aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttr->attr());
90   }
91   if (aPointAttr.get() != NULL)
92     return aPointAttr->pnt();
93   return std::shared_ptr<GeomAPI_Pnt2d>();
94 }
95
96 //*************************************************************************************
97 /// Find an attribute of the given object which corresponds to a vetrex 
98 /// defined by a shape
99 /// \param theObject an object
100 /// \param theShape a vertex
101 /// \param thePlane a projection plane (sketcher plane)
102 std::shared_ptr<GeomDataAPI_Point2D> findGeomPoint(ObjectPtr theObject, 
103                                     const TopoDS_Shape& theShape, 
104                                     const std::shared_ptr<GeomAPI_Ax3>& thePlane)
105 {
106   std::shared_ptr<GeomDataAPI_Point2D> aGeomPoint;
107
108   FeaturePtr anObjectFeature = ModelAPI_Feature::feature(theObject);
109   if (anObjectFeature) {
110     if (theShape.ShapeType() == TopAbs_VERTEX) {
111       const TopoDS_Vertex& aShapeVertex = TopoDS::Vertex(theShape);
112       if (!aShapeVertex.IsNull())  {
113         gp_Pnt aShapePoint = BRep_Tool::Pnt(aShapeVertex);
114         std::shared_ptr<GeomAPI_Pnt> aShapeGeomPnt = std::shared_ptr<GeomAPI_Pnt>(
115             new GeomAPI_Pnt(aShapePoint.X(), aShapePoint.Y(), aShapePoint.Z()));
116
117         // find the given point in the feature attributes
118         std::list<AttributePtr> anObjectAttiributes = 
119           anObjectFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
120         std::list<AttributePtr>::const_iterator anIt = anObjectAttiributes.begin(), 
121                                                 aLast = anObjectAttiributes.end();
122         for (; anIt != aLast && !aGeomPoint; anIt++) {
123           std::shared_ptr<GeomDataAPI_Point2D> anAttributePoint = 
124             std::dynamic_pointer_cast<GeomDataAPI_Point2D>(*anIt);
125
126           std::shared_ptr<GeomAPI_Pnt> anAttributePnt = thePlane->to3D(anAttributePoint->x(),
127                                                                        anAttributePoint->y());
128           if (anAttributePnt.get() &&
129               anAttributePnt->distance(aShapeGeomPnt) < Precision::Confusion()) {
130             aGeomPoint = anAttributePoint;
131             break;
132           }
133         }
134       }
135     }
136   }
137   return aGeomPoint;
138 }
139
140 //*************************************************************************************
141 std::shared_ptr<GeomDataAPI_Point2D> getFeaturePoint(DataPtr theData,
142                                                      const std::string& theAttribute,
143                                                      const std::shared_ptr<GeomAPI_Ax3>& thePlane)
144 {
145   std::shared_ptr<GeomDataAPI_Point2D> aPointAttr;
146
147   if (!theData)
148     return aPointAttr;
149
150   FeaturePtr aFeature;
151   std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = std::dynamic_pointer_cast<
152       ModelAPI_AttributeRefAttr>(theData->attribute(theAttribute));
153   if (anAttr) {
154     if (anAttr->isObject()) {
155       ObjectPtr anObject = anAttr->object();
156       aFeature = ModelAPI_Feature::feature(anObject);
157       if (aFeature && aFeature->getKind() == SketchPlugin_Point::ID()) {
158         // Attribute refers to a point
159         aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
160                      aFeature->data()->attribute(SketchPlugin_Point::COORD_ID()));
161       }
162       else {
163         // if the attribute refers on another object
164         ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
165         if (aRes.get()) {
166           GeomShapePtr aShape = aRes->shape();
167           if (aShape.get()) {
168             TopoDS_Shape aTDSShape = aShape->impl<TopoDS_Shape>();
169             aPointAttr = findGeomPoint(anObject, aTDSShape, thePlane);
170           }
171         }
172       }
173     }
174     else if (anAttr->attr()) {
175       // If attribute is a point
176       aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttr->attr());
177     }
178   }
179   return aPointAttr;
180 }
181
182 //*************************************************************************************
183 FeaturePtr getFeatureLine(DataPtr theData,
184                           const std::string& theAttribute)
185 {
186   FeaturePtr aLine;
187   if (!theData)
188     return aLine;
189
190   std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = 
191     std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theData->attribute(theAttribute));
192   if (anAttr) {
193     FeaturePtr aFeature = ModelAPI_Feature::feature(anAttr->object());
194     if (aFeature && aFeature->getKind() == SketchPlugin_Line::ID()) {
195       return aFeature;
196     }
197   }
198   return aLine;
199 }
200
201 //*************************************************************************************
202 std::shared_ptr<GeomAPI_Pnt2d> getProjectionPoint(const FeaturePtr theLine,
203                                                   const std::shared_ptr<GeomAPI_Pnt2d>& thePoint)
204 {
205   DataPtr aData = theLine->data();
206   std::shared_ptr<GeomDataAPI_Point2D> aPoint1 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
207       aData->attribute(SketchPlugin_Line::START_ID()));
208   std::shared_ptr<GeomDataAPI_Point2D> aPoint2 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
209       aData->attribute(SketchPlugin_Line::END_ID()));
210
211   GeomAPI_Lin2d aLin2d(aPoint1->x(), aPoint1->y(), aPoint2->x(), aPoint2->y());
212   return aLin2d.project(thePoint);
213 }
214
215
216 static double MyArrowSize = 20;
217 double getArrowSize()
218 {
219   return MyArrowSize;
220 }
221
222 void setArrowSize(double theSize)
223 {
224   MyArrowSize = theSize;
225 }
226
227 int getDefaultArrowSize()
228 {
229   return 20;
230 }
231
232 static double MyTextHeight = 16;
233 double getTextHeight()
234 {
235   return MyTextHeight;
236 }
237
238 void setTextHeight(double theHeight)
239 {
240   MyTextHeight = theHeight;
241 }
242
243 double getDefaultTextHeight()
244 {
245   return 16;
246 }
247
248 double getFlyoutDistance(const ModelAPI_Feature* theConstraint)
249 {
250   std::shared_ptr<GeomDataAPI_Point2D> aFlyoutPoint =
251       std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
252       const_cast<ModelAPI_Feature*>(theConstraint)->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
253
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 setDisplaySpecialSymbol(AIS_Dimension* theDimension, const bool& theToDisplay)
290 {
291   if (theToDisplay) {
292     theDimension->SetSpecialSymbol(MySigmaSymbol);
293     theDimension->SetDisplaySpecialSymbol(AIS_DSS_Before);
294   }
295   else {
296     theDimension->SetSpecialSymbol(MyEmptySymbol);
297     theDimension->SetDisplaySpecialSymbol(AIS_DSS_No);
298   }
299 }
300
301 void setDisplayParameter(AIS_Dimension* theDimension, const std::string& theParameter,
302                          const bool& theToDisplay)
303 {
304    if (theToDisplay) {
305     theDimension->DimensionAspect()->MakeUnitsDisplayed(true);
306     theDimension->SetDisplayUnits(TCollection_AsciiString(theParameter.c_str()));
307     theDimension->DimensionAspect()->SetValueStringFormat("");
308   }
309   else {
310     theDimension->DimensionAspect()->MakeUnitsDisplayed(false);
311     theDimension->SetDisplayUnits(TCollection_AsciiString()); // THE_UNDEFINED_UNITS in AIS_Dimension
312     theDimension->DimensionAspect()->SetValueStringFormat("%g");
313   }
314 }
315
316 void sendExpressionShownEvent(const bool& theState)
317 {
318   static Events_ID anId = SketcherPrs_ParameterStyleMessage::eventId();
319   std::shared_ptr<SketcherPrs_ParameterStyleMessage> aMessage = std::shared_ptr
320     <SketcherPrs_ParameterStyleMessage>(new SketcherPrs_ParameterStyleMessage(anId, 0));
321   aMessage->setStyle(theState ? SketcherPrs_ParameterStyleMessage::ParameterText
322                               : SketcherPrs_ParameterStyleMessage::ParameterValue);
323   Events_Loop::loop()->send(aMessage);
324   Events_Loop::loop()->flush(anId);
325 }
326
327 Handle(Prs3d_DimensionAspect) createDimensionAspect()
328 {
329   Handle(Prs3d_DimensionAspect) anAspect = new Prs3d_DimensionAspect();
330   anAspect->MakeArrows3d(false);
331   anAspect->MakeText3d(false);
332   anAspect->MakeTextShaded(false);
333   anAspect->MakeUnitsDisplayed(false);
334   anAspect->TextAspect()->SetHeight(SketcherPrs_Tools::getDefaultTextHeight());
335   anAspect->ArrowAspect()->SetLength(SketcherPrs_Tools::getArrowSize());
336
337   return anAspect;
338 }
339
340 void updateArrows(Handle(Prs3d_DimensionAspect) theDimAspect, double theDimValue, double theTextSize)
341 {
342   double anArrowLength = theDimAspect->ArrowAspect()->Length();
343    // This is not realy correct way to get viewer scale.
344   double aViewerScale = (double) SketcherPrs_Tools::getDefaultArrowSize() / anArrowLength;
345
346   if(theTextSize > ((theDimValue - 3 * SketcherPrs_Tools::getArrowSize()) * aViewerScale)) {
347     theDimAspect->SetTextHorizontalPosition(Prs3d_DTHP_Left);
348     theDimAspect->SetArrowOrientation(Prs3d_DAO_External);
349     theDimAspect->SetExtensionSize(theTextSize / aViewerScale - SketcherPrs_Tools::getArrowSize() / 2.0);
350   } else {
351     theDimAspect->SetTextHorizontalPosition(Prs3d_DTHP_Center);
352     theDimAspect->SetArrowOrientation(Prs3d_DAO_Internal);
353   }
354   theDimAspect->SetArrowTailSize(theDimAspect->ArrowAspect()->Length());
355   // The value of vertical aligment is sometimes changed
356   theDimAspect->TextAspect()->SetVerticalJustification(Graphic3d_VTA_CENTER);
357 }
358
359 void sendEmptyPresentationError(ModelAPI_Feature* theFeature, const std::string theError)
360 {
361   Events_Error::throwException("An empty AIS presentation: SketcherPrs_LengthDimension");
362   static const Events_ID anEvent = Events_Loop::eventByName(EVENT_EMPTY_AIS_PRESENTATION);
363   std::shared_ptr<ModelAPI_Object> aConstraintPtr(theFeature);
364   ModelAPI_EventCreator::get()->sendUpdated(aConstraintPtr, anEvent);
365 }
366 };