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_Error.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 // 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
41 namespace SketcherPrs_Tools {
43 AttributePtr getAttribute(ModelAPI_Feature* theFeature, const std::string& theAttrName)
45 AttributePtr anAttribute;
47 std::shared_ptr<ModelAPI_Data> aData = theFeature->data();
48 if (aData.get() && aData->isValid()) { /// essential check as it is called in openGl thread
49 std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = aData->refattr(theAttrName);
50 if (!anAttr->isObject())
51 anAttribute = anAttr->attr();
57 ObjectPtr getResult(ModelAPI_Feature* theFeature, const std::string& theAttrName)
61 std::shared_ptr<ModelAPI_Data> aData = theFeature->data();
62 if (aData.get() && aData->isValid()) { /// essential check as it is called in openGl thread
63 std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = aData->refattr(theAttrName);
65 anObject = anAttr->object();
72 std::shared_ptr<GeomAPI_Shape> getShape(ObjectPtr theObject)
74 ResultConstructionPtr aRes = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theObject);
75 if (aRes.get() != NULL && aRes->data()->isValid()) {/// essential check as it is called in openGl thread
78 return std::shared_ptr<GeomAPI_Shape>();
82 std::shared_ptr<GeomAPI_Pnt2d> getPoint(ModelAPI_Feature* theFeature,
83 const std::string& theAttribute)
85 std::shared_ptr<GeomDataAPI_Point2D> aPointAttr;
87 /// essential check as it is called in openGl thread
88 if (!theFeature || !theFeature->data().get() || !theFeature->data()->isValid())
89 return std::shared_ptr<GeomAPI_Pnt2d>();
92 std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = std::dynamic_pointer_cast<
93 ModelAPI_AttributeRefAttr>(theFeature->data()->attribute(theAttribute));
95 return std::shared_ptr<GeomAPI_Pnt2d>();
97 aFeature = ModelAPI_Feature::feature(anAttr->object());
99 if (aFeature && aFeature->getKind() == SketchPlugin_Point::ID())
100 aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
101 aFeature->data()->attribute(SketchPlugin_Point::COORD_ID()));
103 else if (anAttr->attr()) {
104 aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttr->attr());
106 if (aPointAttr.get() != NULL)
107 return aPointAttr->pnt();
108 return std::shared_ptr<GeomAPI_Pnt2d>();
111 //*************************************************************************************
112 /// Find an attribute of the given object which corresponds to a vetrex
113 /// defined by a shape
114 /// \param theObject an object
115 /// \param theShape a vertex
116 /// \param thePlane a projection plane (sketcher plane)
117 std::shared_ptr<GeomDataAPI_Point2D> findGeomPoint(ObjectPtr theObject,
118 const TopoDS_Shape& theShape,
119 const std::shared_ptr<GeomAPI_Ax3>& thePlane)
121 std::shared_ptr<GeomDataAPI_Point2D> aGeomPoint;
123 FeaturePtr anObjectFeature = ModelAPI_Feature::feature(theObject);
124 if (anObjectFeature) {
125 if (theShape.ShapeType() == TopAbs_VERTEX) {
126 const TopoDS_Vertex& aShapeVertex = TopoDS::Vertex(theShape);
127 if (!aShapeVertex.IsNull()) {
128 gp_Pnt aShapePoint = BRep_Tool::Pnt(aShapeVertex);
129 std::shared_ptr<GeomAPI_Pnt> aShapeGeomPnt = std::shared_ptr<GeomAPI_Pnt>(
130 new GeomAPI_Pnt(aShapePoint.X(), aShapePoint.Y(), aShapePoint.Z()));
132 // find the given point in the feature attributes
133 std::list<AttributePtr> anObjectAttiributes =
134 anObjectFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
135 std::list<AttributePtr>::const_iterator anIt = anObjectAttiributes.begin(),
136 aLast = anObjectAttiributes.end();
137 for (; anIt != aLast && !aGeomPoint; anIt++) {
138 std::shared_ptr<GeomDataAPI_Point2D> anAttributePoint =
139 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(*anIt);
141 std::shared_ptr<GeomAPI_Pnt> anAttributePnt = thePlane->to3D(anAttributePoint->x(),
142 anAttributePoint->y());
143 if (anAttributePnt.get() &&
144 anAttributePnt->distance(aShapeGeomPnt) < Precision::Confusion()) {
145 aGeomPoint = anAttributePoint;
155 //*************************************************************************************
156 std::shared_ptr<GeomDataAPI_Point2D> getFeaturePoint(DataPtr theData,
157 const std::string& theAttribute,
158 const std::shared_ptr<GeomAPI_Ax3>& thePlane)
160 std::shared_ptr<GeomDataAPI_Point2D> aPointAttr;
162 if (!theData.get() || !theData->isValid()) /// essential check as it is called in openGl thread
166 std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = std::dynamic_pointer_cast<
167 ModelAPI_AttributeRefAttr>(theData->attribute(theAttribute));
169 if (anAttr->isObject()) {
170 ObjectPtr anObject = anAttr->object();
171 aFeature = ModelAPI_Feature::feature(anObject);
172 if (aFeature && aFeature->getKind() == SketchPlugin_Point::ID()) {
173 // Attribute refers to a point
174 aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
175 aFeature->data()->attribute(SketchPlugin_Point::COORD_ID()));
178 // if the attribute refers on another object
179 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
181 GeomShapePtr aShape = aRes->shape();
183 TopoDS_Shape aTDSShape = aShape->impl<TopoDS_Shape>();
184 aPointAttr = findGeomPoint(anObject, aTDSShape, thePlane);
189 else if (anAttr->attr()) {
190 // If attribute is a point
191 aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttr->attr());
197 //*************************************************************************************
198 FeaturePtr getFeatureLine(DataPtr theData,
199 const std::string& theAttribute)
202 if (!theData.get() || !theData->isValid()) /// essential check as it is called in openGl thread)
205 std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr =
206 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theData->attribute(theAttribute));
208 FeaturePtr aFeature = ModelAPI_Feature::feature(anAttr->object());
209 if (aFeature && aFeature->getKind() == SketchPlugin_Line::ID()) {
216 //*************************************************************************************
217 std::shared_ptr<GeomAPI_Pnt2d> getProjectionPoint(const FeaturePtr theLine,
218 const std::shared_ptr<GeomAPI_Pnt2d>& thePoint)
220 DataPtr aData = theLine->data();
221 if (!aData.get() || !aData->isValid())
222 return std::shared_ptr<GeomAPI_Pnt2d>();
224 std::shared_ptr<GeomDataAPI_Point2D> aPoint1 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
225 aData->attribute(SketchPlugin_Line::START_ID()));
226 std::shared_ptr<GeomDataAPI_Point2D> aPoint2 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
227 aData->attribute(SketchPlugin_Line::END_ID()));
229 GeomAPI_Lin2d aLin2d(aPoint1->x(), aPoint1->y(), aPoint2->x(), aPoint2->y());
230 return aLin2d.project(thePoint);
234 static double MyArrowSize = 20;
235 double getArrowSize()
240 void setArrowSize(double theSize)
242 MyArrowSize = theSize;
245 int getDefaultArrowSize()
250 static double MyTextHeight = 16;
251 double getTextHeight()
256 void setTextHeight(double theHeight)
258 MyTextHeight = theHeight;
261 double getDefaultTextHeight()
266 double getFlyoutDistance(const ModelAPI_Feature* theConstraint)
268 std::shared_ptr<GeomDataAPI_Point2D> aFlyoutPoint =
269 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
270 const_cast<ModelAPI_Feature*>(theConstraint)->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
272 return aFlyoutPoint->y();
275 std::shared_ptr<GeomAPI_Pnt> getAnchorPoint(const ModelAPI_Feature* theConstraint,
276 const std::shared_ptr<GeomAPI_Ax3>& thePlane)
278 ModelAPI_Feature* aConstraint = const_cast<ModelAPI_Feature*>(theConstraint);
279 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
280 aConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
281 if (!aRefAttr || !aRefAttr->isObject() || !aRefAttr->object())
282 return std::shared_ptr<GeomAPI_Pnt>();
284 FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttr->object());
285 std::shared_ptr<GeomAPI_Pnt2d> aCenter;
286 if (aFeature->getKind() == SketchPlugin_Arc::ID()) { // arc
287 aCenter = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
288 aFeature->attribute(SketchPlugin_Arc::CENTER_ID()))->pnt();
289 } else if (aFeature->getKind() == SketchPlugin_Circle::ID()) { // circle
290 aCenter = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
291 aFeature->attribute(SketchPlugin_Circle::CENTER_ID()))->pnt();
293 return std::shared_ptr<GeomAPI_Pnt>();
295 std::shared_ptr<GeomAPI_Pnt2d> anOrigin(new GeomAPI_Pnt2d(0.0, 0.0));
296 std::shared_ptr<GeomAPI_Pnt2d> aFlyoutPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
297 aConstraint->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()))->pnt();
298 double aRadius = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
299 aConstraint->attribute(SketchPlugin_Constraint::VALUE()))->value();
300 double aLen = aFlyoutPnt->distance(anOrigin);
301 aRadius *= 1.001; // a gap to make point much closer to the circle, but not lying on it
302 aFlyoutPnt->setX(aCenter->x() + aFlyoutPnt->x() * aRadius / aLen);
303 aFlyoutPnt->setY(aCenter->y() + aFlyoutPnt->y() * aRadius / aLen);
304 return thePlane->to3D(aFlyoutPnt->x(), aFlyoutPnt->y());
307 void setDisplaySpecialSymbol(AIS_Dimension* theDimension, const bool& theToDisplay)
310 theDimension->SetSpecialSymbol(MySigmaSymbol);
311 theDimension->SetDisplaySpecialSymbol(AIS_DSS_Before);
314 theDimension->SetSpecialSymbol(MyEmptySymbol);
315 theDimension->SetDisplaySpecialSymbol(AIS_DSS_No);
319 void setDisplayParameter(AIS_Dimension* theDimension, const std::string& theParameter,
320 const bool& theToDisplay)
323 theDimension->DimensionAspect()->MakeUnitsDisplayed(true);
324 theDimension->SetDisplayUnits(TCollection_AsciiString(theParameter.c_str()));
325 theDimension->DimensionAspect()->SetValueStringFormat("");
328 theDimension->DimensionAspect()->MakeUnitsDisplayed(false);
329 theDimension->SetDisplayUnits(TCollection_AsciiString()); // THE_UNDEFINED_UNITS in AIS_Dimension
330 theDimension->DimensionAspect()->SetValueStringFormat("%g");
334 void sendExpressionShownEvent(const bool& theState)
336 static Events_ID anId = SketcherPrs_ParameterStyleMessage::eventId();
337 std::shared_ptr<SketcherPrs_ParameterStyleMessage> aMessage = std::shared_ptr
338 <SketcherPrs_ParameterStyleMessage>(new SketcherPrs_ParameterStyleMessage(anId, 0));
339 aMessage->setStyle(theState ? SketcherPrs_ParameterStyleMessage::ParameterText
340 : SketcherPrs_ParameterStyleMessage::ParameterValue);
341 Events_Loop::loop()->send(aMessage);
342 Events_Loop::loop()->flush(anId);
345 Handle(Prs3d_DimensionAspect) createDimensionAspect()
347 Handle(Prs3d_DimensionAspect) anAspect = new Prs3d_DimensionAspect();
348 anAspect->MakeArrows3d(false);
349 anAspect->MakeText3d(false);
350 anAspect->MakeTextShaded(false);
351 anAspect->MakeUnitsDisplayed(false);
352 anAspect->TextAspect()->SetHeight(SketcherPrs_Tools::getDefaultTextHeight());
353 anAspect->ArrowAspect()->SetLength(SketcherPrs_Tools::getArrowSize());
358 void updateArrows(Handle(Prs3d_DimensionAspect) theDimAspect, double theDimValue, double theTextSize)
360 double anArrowLength = theDimAspect->ArrowAspect()->Length();
361 // This is not realy correct way to get viewer scale.
362 double aViewerScale = (double) SketcherPrs_Tools::getDefaultArrowSize() / anArrowLength;
364 if(theTextSize > ((theDimValue - 3 * SketcherPrs_Tools::getArrowSize()) * aViewerScale)) {
365 theDimAspect->SetTextHorizontalPosition(Prs3d_DTHP_Left);
366 theDimAspect->SetArrowOrientation(Prs3d_DAO_External);
367 theDimAspect->SetExtensionSize(theTextSize / aViewerScale - SketcherPrs_Tools::getArrowSize() / 2.0);
369 theDimAspect->SetTextHorizontalPosition(Prs3d_DTHP_Center);
370 theDimAspect->SetArrowOrientation(Prs3d_DAO_Internal);
372 theDimAspect->SetArrowTailSize(theDimAspect->ArrowAspect()->Length());
373 // The value of vertical aligment is sometimes changed
374 theDimAspect->TextAspect()->SetVerticalJustification(Graphic3d_VTA_CENTER);
377 void sendEmptyPresentationError(ModelAPI_Feature* theFeature, const std::string theError)
379 Events_Error::throwException("An empty AIS presentation: SketcherPrs_LengthDimension");
380 static const Events_ID anEvent = Events_Loop::eventByName(EVENT_EMPTY_AIS_PRESENTATION);
381 std::shared_ptr<ModelAPI_Object> aConstraintPtr(theFeature);
382 ModelAPI_EventCreator::get()->sendUpdated(aConstraintPtr, anEvent);