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 <Events_InfoMessage.h>
22 #include <GeomDataAPI_Point2D.h>
23 #include <GeomAPI_Lin2d.h>
26 #include <TopoDS_Shape.hxx>
27 #include <TopoDS_Vertex.hxx>
29 #include <Prs3d_DimensionAspect.hxx>
31 #include <BRep_Tool.hxx>
32 #include <Precision.hxx>
34 #include <AIS_Dimension.hxx>
36 namespace SketcherPrs_Tools {
38 AttributePtr getAttribute(ModelAPI_Feature* theFeature, const std::string& theAttrName)
40 AttributePtr anAttribute;
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();
52 ObjectPtr getResult(ModelAPI_Feature* theFeature, const std::string& theAttrName)
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);
60 anObject = anAttr->object();
67 std::shared_ptr<GeomAPI_Shape> getShape(ObjectPtr theObject)
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
73 return std::shared_ptr<GeomAPI_Shape>();
77 std::shared_ptr<GeomAPI_Pnt2d> getPoint(ModelAPI_Feature* theFeature,
78 const std::string& theAttribute)
80 std::shared_ptr<GeomDataAPI_Point2D> aPointAttr;
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>();
87 std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = std::dynamic_pointer_cast<
88 ModelAPI_AttributeRefAttr>(theFeature->data()->attribute(theAttribute));
90 return std::shared_ptr<GeomAPI_Pnt2d>();
92 aFeature = ModelAPI_Feature::feature(anAttr->object());
94 if (aFeature && aFeature->getKind() == SketchPlugin_Point::ID())
95 aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
96 aFeature->data()->attribute(SketchPlugin_Point::COORD_ID()));
98 else if (anAttr->attr()) {
99 aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttr->attr());
101 if (aPointAttr.get() != NULL)
102 return aPointAttr->pnt();
103 return std::shared_ptr<GeomAPI_Pnt2d>();
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)
116 std::shared_ptr<GeomDataAPI_Point2D> aGeomPoint;
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()));
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);
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;
150 //*************************************************************************************
151 std::shared_ptr<GeomDataAPI_Point2D> getFeaturePoint(DataPtr theData,
152 const std::string& theAttribute,
153 const std::shared_ptr<GeomAPI_Ax3>& thePlane)
155 std::shared_ptr<GeomDataAPI_Point2D> aPointAttr;
157 if (!theData.get() || !theData->isValid()) /// essential check as it is called in openGl thread
161 std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = std::dynamic_pointer_cast<
162 ModelAPI_AttributeRefAttr>(theData->attribute(theAttribute));
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()));
173 // if the attribute refers on another object
174 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
176 GeomShapePtr aShape = aRes->shape();
178 TopoDS_Shape aTDSShape = aShape->impl<TopoDS_Shape>();
179 aPointAttr = findGeomPoint(anObject, aTDSShape, thePlane);
184 else if (anAttr->attr()) {
185 // If attribute is a point
186 aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttr->attr());
192 //*************************************************************************************
193 FeaturePtr getFeatureLine(DataPtr theData,
194 const std::string& theAttribute)
197 if (!theData.get() || !theData->isValid()) /// essential check as it is called in openGl thread)
200 std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr =
201 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theData->attribute(theAttribute));
203 FeaturePtr aFeature = ModelAPI_Feature::feature(anAttr->object());
204 if (aFeature && aFeature->getKind() == SketchPlugin_Line::ID()) {
211 //*************************************************************************************
212 std::shared_ptr<GeomAPI_Pnt2d> getProjectionPoint(const FeaturePtr theLine,
213 const std::shared_ptr<GeomAPI_Pnt2d>& thePoint)
215 DataPtr aData = theLine->data();
216 if (!aData.get() || !aData->isValid())
217 return std::shared_ptr<GeomAPI_Pnt2d>();
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()));
224 GeomAPI_Lin2d aLin2d(aPoint1->x(), aPoint1->y(), aPoint2->x(), aPoint2->y());
225 return aLin2d.project(thePoint);
229 static double MyArrowSize = 20;
230 double getArrowSize()
235 void setArrowSize(double theSize)
237 MyArrowSize = theSize;
240 int getDefaultArrowSize()
245 static double MyTextHeight = 16;
246 double getTextHeight()
251 void setTextHeight(double theHeight)
253 MyTextHeight = theHeight;
256 double getDefaultTextHeight()
261 double getFlyoutDistance(const ModelAPI_Feature* theConstraint)
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()));
267 return aFlyoutPoint->y();
270 std::shared_ptr<GeomAPI_Pnt> getAnchorPoint(const ModelAPI_Feature* theConstraint,
271 const std::shared_ptr<GeomAPI_Ax3>& thePlane)
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>();
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();
288 return std::shared_ptr<GeomAPI_Pnt>();
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());
302 void sendExpressionShownEvent(const bool& theState)
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);
313 Handle(Prs3d_DimensionAspect) createDimensionAspect()
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());
326 void updateArrows(Handle(Prs3d_DimensionAspect) theDimAspect, double theDimValue, double theTextSize)
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;
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);
337 theDimAspect->SetTextHorizontalPosition(Prs3d_DTHP_Center);
338 theDimAspect->SetArrowOrientation(Prs3d_DAO_Internal);
340 theDimAspect->SetArrowTailSize(theDimAspect->ArrowAspect()->Length());
341 // The value of vertical aligment is sometimes changed
342 theDimAspect->TextAspect()->SetVerticalJustification(Graphic3d_VTA_CENTER);
345 void sendEmptyPresentationError(ModelAPI_Feature* theFeature, const std::string theError)
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);