1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 // File: SketcherPrs_Tools.cpp
4 // Created: 10 March 2015
5 // Author: Vitaly SMETANNIKOV
7 #include "SketcherPrs_Tools.h"
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>
15 #include <ModelAPI_ResultConstruction.h>
16 #include <ModelAPI_AttributeRefAttr.h>
17 #include <ModelAPI_AttributeDouble.h>
18 #include <ModelAPI_Events.h>
20 #include <ModelGeomAlgo_Point2D.h>
22 #include <Events_InfoMessage.h>
24 #include <GeomDataAPI_Point2D.h>
25 #include <GeomAPI_Lin2d.h>
28 #include <TopoDS_Shape.hxx>
29 #include <TopoDS_Vertex.hxx>
31 #include <Prs3d_DimensionAspect.hxx>
33 #include <BRep_Tool.hxx>
34 #include <Precision.hxx>
36 #include <AIS_Dimension.hxx>
38 namespace SketcherPrs_Tools {
40 AttributePtr getAttribute(ModelAPI_Feature* theFeature, const std::string& theAttrName)
42 AttributePtr anAttribute;
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();
54 ObjectPtr getResult(ModelAPI_Feature* theFeature, const std::string& theAttrName)
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);
62 anObject = anAttr->object();
69 std::shared_ptr<GeomAPI_Shape> getShape(ObjectPtr theObject)
71 ResultConstructionPtr aRes = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theObject);
72 if (aRes.get() != NULL && aRes->data()->isValid()) {
73 /// essential check as it is called in openGl thread
76 return std::shared_ptr<GeomAPI_Shape>();
80 std::shared_ptr<GeomAPI_Pnt2d> getPoint(ModelAPI_Feature* theFeature,
81 const std::string& theAttribute)
83 std::shared_ptr<GeomDataAPI_Point2D> aPointAttr = ModelGeomAlgo_Point2D::getPointOfRefAttr(
84 theFeature, theAttribute, SketchPlugin_Point::ID(), SketchPlugin_Point::COORD_ID());
85 if (aPointAttr.get() != NULL)
86 return aPointAttr->pnt();
87 return std::shared_ptr<GeomAPI_Pnt2d>();
90 //*************************************************************************************
91 /// Find an attribute of the given object which corresponds to a vetrex
92 /// defined by a shape
93 /// \param theObject an object
94 /// \param theShape a vertex
95 /// \param thePlane a projection plane (sketcher plane)
96 std::shared_ptr<GeomDataAPI_Point2D> findGeomPoint(ObjectPtr theObject,
97 const TopoDS_Shape& theShape,
98 const std::shared_ptr<GeomAPI_Ax3>& thePlane)
100 std::shared_ptr<GeomDataAPI_Point2D> aGeomPoint;
102 FeaturePtr anObjectFeature = ModelAPI_Feature::feature(theObject);
103 if (anObjectFeature) {
104 if (theShape.ShapeType() == TopAbs_VERTEX) {
105 const TopoDS_Vertex& aShapeVertex = TopoDS::Vertex(theShape);
106 if (!aShapeVertex.IsNull()) {
107 gp_Pnt aShapePoint = BRep_Tool::Pnt(aShapeVertex);
108 std::shared_ptr<GeomAPI_Pnt> aShapeGeomPnt = std::shared_ptr<GeomAPI_Pnt>(
109 new GeomAPI_Pnt(aShapePoint.X(), aShapePoint.Y(), aShapePoint.Z()));
111 // find the given point in the feature attributes
112 std::list<AttributePtr> anObjectAttiributes =
113 anObjectFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
114 std::list<AttributePtr>::const_iterator anIt = anObjectAttiributes.begin(),
115 aLast = anObjectAttiributes.end();
116 for (; anIt != aLast && !aGeomPoint; anIt++) {
117 std::shared_ptr<GeomDataAPI_Point2D> anAttributePoint =
118 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(*anIt);
120 std::shared_ptr<GeomAPI_Pnt> anAttributePnt = thePlane->to3D(anAttributePoint->x(),
121 anAttributePoint->y());
122 if (anAttributePnt.get() &&
123 anAttributePnt->distance(aShapeGeomPnt) < Precision::Confusion()) {
124 aGeomPoint = anAttributePoint;
134 //*************************************************************************************
135 std::shared_ptr<GeomDataAPI_Point2D> getFeaturePoint(DataPtr theData,
136 const std::string& theAttribute,
137 const std::shared_ptr<GeomAPI_Ax3>& thePlane)
139 std::shared_ptr<GeomDataAPI_Point2D> aPointAttr;
141 if (!theData.get() || !theData->isValid()) /// essential check as it is called in openGl thread
145 std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = std::dynamic_pointer_cast<
146 ModelAPI_AttributeRefAttr>(theData->attribute(theAttribute));
148 if (anAttr->isObject()) {
149 ObjectPtr anObject = anAttr->object();
150 aFeature = ModelAPI_Feature::feature(anObject);
151 if (aFeature && aFeature->getKind() == SketchPlugin_Point::ID()) {
152 // Attribute refers to a point
153 aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
154 aFeature->data()->attribute(SketchPlugin_Point::COORD_ID()));
157 // if the attribute refers on another object
158 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
160 GeomShapePtr aShape = aRes->shape();
162 TopoDS_Shape aTDSShape = aShape->impl<TopoDS_Shape>();
163 aPointAttr = findGeomPoint(anObject, aTDSShape, thePlane);
168 else if (anAttr->attr()) {
169 // If attribute is a point
170 aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttr->attr());
176 //*************************************************************************************
177 FeaturePtr getFeatureLine(DataPtr theData,
178 const std::string& theAttribute)
181 if (!theData.get() || !theData->isValid()) /// essential check as it is called in openGl thread)
184 std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr =
185 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theData->attribute(theAttribute));
187 FeaturePtr aFeature = ModelAPI_Feature::feature(anAttr->object());
188 if (aFeature && aFeature->getKind() == SketchPlugin_Line::ID()) {
195 //*************************************************************************************
196 std::shared_ptr<GeomAPI_Pnt2d> getProjectionPoint(const FeaturePtr theLine,
197 const std::shared_ptr<GeomAPI_Pnt2d>& thePoint)
199 DataPtr aData = theLine->data();
200 if (!aData.get() || !aData->isValid())
201 return std::shared_ptr<GeomAPI_Pnt2d>();
203 std::shared_ptr<GeomDataAPI_Point2D> aPoint1 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
204 aData->attribute(SketchPlugin_Line::START_ID()));
205 std::shared_ptr<GeomDataAPI_Point2D> aPoint2 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
206 aData->attribute(SketchPlugin_Line::END_ID()));
208 GeomAPI_Lin2d aLin2d(aPoint1->x(), aPoint1->y(), aPoint2->x(), aPoint2->y());
209 return aLin2d.project(thePoint);
213 static double MyArrowSize = 20;
214 double getArrowSize()
219 void setArrowSize(double theSize)
221 MyArrowSize = theSize;
224 int getDefaultArrowSize()
229 static double MyTextHeight = 16;
230 double getTextHeight()
235 void setTextHeight(double theHeight)
237 MyTextHeight = theHeight;
240 double getDefaultTextHeight()
245 double getFlyoutDistance(const ModelAPI_Feature* theConstraint)
247 std::shared_ptr<GeomDataAPI_Point2D> aFlyoutPoint =
248 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
249 const_cast<ModelAPI_Feature*>(
250 theConstraint)->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
251 // for not initialized values return zero distance to avoid Presentation crash
252 if (!aFlyoutPoint->isInitialized())
254 return aFlyoutPoint->y();
257 std::shared_ptr<GeomAPI_Pnt> getAnchorPoint(const ModelAPI_Feature* theConstraint,
258 const std::shared_ptr<GeomAPI_Ax3>& thePlane)
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>();
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();
275 return std::shared_ptr<GeomAPI_Pnt>();
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());
289 void sendExpressionShownEvent(const bool& theState)
291 static Events_ID anId = SketcherPrs_ParameterStyleMessage::eventId();
292 std::shared_ptr<SketcherPrs_ParameterStyleMessage> aMessage = std::shared_ptr
293 <SketcherPrs_ParameterStyleMessage>(new SketcherPrs_ParameterStyleMessage(anId, 0));
294 aMessage->setStyle(theState ? SketcherPrs_ParameterStyleMessage::ParameterText
295 : SketcherPrs_ParameterStyleMessage::ParameterValue);
296 Events_Loop::loop()->send(aMessage);
297 Events_Loop::loop()->flush(anId);
300 void sendEmptyPresentationError(ModelAPI_Feature* theFeature, const std::string theError)
302 Events_InfoMessage("SketcherPrs_Tools",
303 "An empty AIS presentation: SketcherPrs_LengthDimension").send();
304 static const Events_ID anEvent = Events_Loop::eventByName(EVENT_EMPTY_AIS_PRESENTATION);
305 std::shared_ptr<ModelAPI_Object> aConstraintPtr(theFeature);
306 ModelAPI_EventCreator::get()->sendUpdated(aConstraintPtr, anEvent);