]> SALOME platform Git repositories - modules/shaper.git/blob - src/SketcherPrs/SketcherPrs_Tools.cpp
Salome HOME
Add copyright header according to request of CEA from 06.06.2017
[modules/shaper.git] / src / SketcherPrs / SketcherPrs_Tools.cpp
1 // Copyright (C) 2014-2017  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
18 //
19
20 #include "SketcherPrs_Tools.h"
21
22 #include <SketchPlugin_Constraint.h>
23 #include <SketchPlugin_Point.h>
24 #include <SketchPlugin_Circle.h>
25 #include <SketchPlugin_Line.h>
26 #include <SketchPlugin_Arc.h>
27
28 #include <ModelAPI_ResultConstruction.h>
29 #include <ModelAPI_AttributeRefAttr.h>
30 #include <ModelAPI_AttributeDouble.h>
31 #include <ModelAPI_Events.h>
32
33 #include <ModelGeomAlgo_Point2D.h>
34
35 #include <Events_InfoMessage.h>
36
37 #include <GeomDataAPI_Point2D.h>
38 #include <GeomAPI_Lin2d.h>
39
40 #include <TopoDS.hxx>
41 #include <TopoDS_Shape.hxx>
42 #include <TopoDS_Vertex.hxx>
43
44 #include <Prs3d_DimensionAspect.hxx>
45
46 #include <BRep_Tool.hxx>
47 #include <Precision.hxx>
48
49 #include <AIS_Dimension.hxx>
50
51 namespace SketcherPrs_Tools {
52
53 AttributePtr getAttribute(ModelAPI_Feature* theFeature, const std::string& theAttrName)
54 {
55   AttributePtr anAttribute;
56   if (theFeature) {
57     std::shared_ptr<ModelAPI_Data> aData = theFeature->data();
58     if (aData.get() && aData->isValid()) { /// essential check as it is called in openGl thread
59       std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = aData->refattr(theAttrName);
60       if (!anAttr->isObject())
61         anAttribute = anAttr->attr();
62     }
63   }
64   return anAttribute;
65 }
66
67 ObjectPtr getResult(ModelAPI_Feature* theFeature, const std::string& theAttrName)
68 {
69   ObjectPtr anObject;
70   if (theFeature) {
71     std::shared_ptr<ModelAPI_Data> aData = theFeature->data();
72     if (aData.get() && aData->isValid()) { /// essential check as it is called in openGl thread
73       std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = aData->refattr(theAttrName);
74       if (anAttr.get())
75         anObject = anAttr->object();
76     }
77   }
78   return anObject;
79 }
80
81
82 std::shared_ptr<GeomAPI_Shape> getShape(ObjectPtr theObject)
83 {
84   ResultConstructionPtr aRes = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theObject);
85   if (aRes.get() != NULL && aRes->data()->isValid()) {
86     /// essential check as it is called in openGl thread
87     return aRes->shape();
88   }
89   return std::shared_ptr<GeomAPI_Shape>();
90 }
91
92
93 std::shared_ptr<GeomAPI_Pnt2d> getPoint(ModelAPI_Feature* theFeature,
94                                         const std::string& theAttribute)
95 {
96   std::shared_ptr<GeomDataAPI_Point2D> aPointAttr = ModelGeomAlgo_Point2D::getPointOfRefAttr(
97                theFeature, theAttribute, SketchPlugin_Point::ID(), SketchPlugin_Point::COORD_ID());
98   if (aPointAttr.get() != NULL)
99     return aPointAttr->pnt();
100   return std::shared_ptr<GeomAPI_Pnt2d>();
101 }
102
103 //*************************************************************************************
104 /// Find an attribute of the given object which corresponds to a vetrex
105 /// defined by a shape
106 /// \param theObject an object
107 /// \param theShape a vertex
108 /// \param thePlane a projection plane (sketcher plane)
109 std::shared_ptr<GeomDataAPI_Point2D> findGeomPoint(ObjectPtr theObject,
110                                     const TopoDS_Shape& theShape,
111                                     const std::shared_ptr<GeomAPI_Ax3>& thePlane)
112 {
113   std::shared_ptr<GeomDataAPI_Point2D> aGeomPoint;
114
115   FeaturePtr anObjectFeature = ModelAPI_Feature::feature(theObject);
116   if (anObjectFeature) {
117     if (theShape.ShapeType() == TopAbs_VERTEX) {
118       const TopoDS_Vertex& aShapeVertex = TopoDS::Vertex(theShape);
119       if (!aShapeVertex.IsNull())  {
120         gp_Pnt aShapePoint = BRep_Tool::Pnt(aShapeVertex);
121         std::shared_ptr<GeomAPI_Pnt> aShapeGeomPnt = std::shared_ptr<GeomAPI_Pnt>(
122             new GeomAPI_Pnt(aShapePoint.X(), aShapePoint.Y(), aShapePoint.Z()));
123
124         // find the given point in the feature attributes
125         std::list<AttributePtr> anObjectAttiributes =
126           anObjectFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
127         std::list<AttributePtr>::const_iterator anIt = anObjectAttiributes.begin(),
128                                                 aLast = anObjectAttiributes.end();
129         for (; anIt != aLast && !aGeomPoint; anIt++) {
130           std::shared_ptr<GeomDataAPI_Point2D> anAttributePoint =
131             std::dynamic_pointer_cast<GeomDataAPI_Point2D>(*anIt);
132
133           std::shared_ptr<GeomAPI_Pnt> anAttributePnt = thePlane->to3D(anAttributePoint->x(),
134                                                                        anAttributePoint->y());
135           if (anAttributePnt.get() &&
136               anAttributePnt->distance(aShapeGeomPnt) < Precision::Confusion()) {
137             aGeomPoint = anAttributePoint;
138             break;
139           }
140         }
141       }
142     }
143   }
144   return aGeomPoint;
145 }
146
147 //*************************************************************************************
148 std::shared_ptr<GeomDataAPI_Point2D> getFeaturePoint(DataPtr theData,
149                                                      const std::string& theAttribute,
150                                                      const std::shared_ptr<GeomAPI_Ax3>& thePlane)
151 {
152   std::shared_ptr<GeomDataAPI_Point2D> aPointAttr;
153
154   if (!theData.get() || !theData->isValid()) /// essential check as it is called in openGl thread
155     return aPointAttr;
156
157   FeaturePtr aFeature;
158   std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = std::dynamic_pointer_cast<
159       ModelAPI_AttributeRefAttr>(theData->attribute(theAttribute));
160   if (anAttr) {
161     if (anAttr->isObject()) {
162       ObjectPtr anObject = anAttr->object();
163       aFeature = ModelAPI_Feature::feature(anObject);
164       if (aFeature && aFeature->getKind() == SketchPlugin_Point::ID()) {
165         // Attribute refers to a point
166         aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
167                      aFeature->data()->attribute(SketchPlugin_Point::COORD_ID()));
168       }
169       else {
170         // if the attribute refers on another object
171         ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
172         if (aRes.get()) {
173           GeomShapePtr aShape = aRes->shape();
174           if (aShape.get()) {
175             TopoDS_Shape aTDSShape = aShape->impl<TopoDS_Shape>();
176             aPointAttr = findGeomPoint(anObject, aTDSShape, thePlane);
177           }
178         }
179       }
180     }
181     else if (anAttr->attr()) {
182       // If attribute is a point
183       aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttr->attr());
184     }
185   }
186   return aPointAttr;
187 }
188
189 //*************************************************************************************
190 FeaturePtr getFeatureLine(DataPtr theData,
191                           const std::string& theAttribute)
192 {
193   FeaturePtr aLine;
194   if (!theData.get() || !theData->isValid()) /// essential check as it is called in openGl thread)
195     return aLine;
196
197   std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr =
198     std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theData->attribute(theAttribute));
199   if (anAttr) {
200     FeaturePtr aFeature = ModelAPI_Feature::feature(anAttr->object());
201     if (aFeature && aFeature->getKind() == SketchPlugin_Line::ID()) {
202       return aFeature;
203     }
204   }
205   return aLine;
206 }
207
208 //*************************************************************************************
209 std::shared_ptr<GeomAPI_Pnt2d> getProjectionPoint(const FeaturePtr theLine,
210                                                   const std::shared_ptr<GeomAPI_Pnt2d>& thePoint)
211 {
212   DataPtr aData = theLine->data();
213   if (!aData.get() || !aData->isValid())
214     return std::shared_ptr<GeomAPI_Pnt2d>();
215
216   std::shared_ptr<GeomDataAPI_Point2D> aPoint1 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
217       aData->attribute(SketchPlugin_Line::START_ID()));
218   std::shared_ptr<GeomDataAPI_Point2D> aPoint2 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
219       aData->attribute(SketchPlugin_Line::END_ID()));
220
221   GeomAPI_Lin2d aLin2d(aPoint1->x(), aPoint1->y(), aPoint2->x(), aPoint2->y());
222   return aLin2d.project(thePoint);
223 }
224
225
226 static double MyArrowSize = 20;
227 double getArrowSize()
228 {
229   return MyArrowSize;
230 }
231
232 void setArrowSize(double theSize)
233 {
234   MyArrowSize = theSize;
235 }
236
237 int getDefaultArrowSize()
238 {
239   return 20;
240 }
241
242 static double MyTextHeight = 16;
243 double getTextHeight()
244 {
245   return MyTextHeight;
246 }
247
248 void setTextHeight(double theHeight)
249 {
250   MyTextHeight = theHeight;
251 }
252
253 double getDefaultTextHeight()
254 {
255   return 16;
256 }
257
258 double getFlyoutDistance(const ModelAPI_Feature* theConstraint)
259 {
260   std::shared_ptr<GeomDataAPI_Point2D> aFlyoutPoint =
261       std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
262       const_cast<ModelAPI_Feature*>(
263       theConstraint)->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
264   // for not initialized values return zero distance to avoid Presentation crash
265   if (!aFlyoutPoint->isInitialized())
266     return 0;
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 void sendEmptyPresentationError(ModelAPI_Feature* theFeature, const std::string theError)
314 {
315   Events_InfoMessage("SketcherPrs_Tools",
316     "An empty AIS presentation: SketcherPrs_LengthDimension").send();
317   static const Events_ID anEvent = Events_Loop::eventByName(EVENT_EMPTY_AIS_PRESENTATION);
318   std::shared_ptr<ModelAPI_Object> aConstraintPtr(theFeature);
319   ModelAPI_EventCreator::get()->sendUpdated(aConstraintPtr, anEvent);
320 }
321 };