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()) {/// essential check as it is called in openGl thread
75 return std::shared_ptr<GeomAPI_Shape>();
79 std::shared_ptr<GeomAPI_Pnt2d> getPoint(ModelAPI_Feature* theFeature,
80 const std::string& theAttribute)
82 std::shared_ptr<GeomDataAPI_Point2D> aPointAttr = ModelGeomAlgo_Point2D::getPointOfRefAttr(
83 theFeature, theAttribute, SketchPlugin_Point::ID(), SketchPlugin_Point::COORD_ID());
84 if (aPointAttr.get() != NULL)
85 return aPointAttr->pnt();
86 return std::shared_ptr<GeomAPI_Pnt2d>();
89 //*************************************************************************************
90 /// Find an attribute of the given object which corresponds to a vetrex
91 /// defined by a shape
92 /// \param theObject an object
93 /// \param theShape a vertex
94 /// \param thePlane a projection plane (sketcher plane)
95 std::shared_ptr<GeomDataAPI_Point2D> findGeomPoint(ObjectPtr theObject,
96 const TopoDS_Shape& theShape,
97 const std::shared_ptr<GeomAPI_Ax3>& thePlane)
99 std::shared_ptr<GeomDataAPI_Point2D> aGeomPoint;
101 FeaturePtr anObjectFeature = ModelAPI_Feature::feature(theObject);
102 if (anObjectFeature) {
103 if (theShape.ShapeType() == TopAbs_VERTEX) {
104 const TopoDS_Vertex& aShapeVertex = TopoDS::Vertex(theShape);
105 if (!aShapeVertex.IsNull()) {
106 gp_Pnt aShapePoint = BRep_Tool::Pnt(aShapeVertex);
107 std::shared_ptr<GeomAPI_Pnt> aShapeGeomPnt = std::shared_ptr<GeomAPI_Pnt>(
108 new GeomAPI_Pnt(aShapePoint.X(), aShapePoint.Y(), aShapePoint.Z()));
110 // find the given point in the feature attributes
111 std::list<AttributePtr> anObjectAttiributes =
112 anObjectFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
113 std::list<AttributePtr>::const_iterator anIt = anObjectAttiributes.begin(),
114 aLast = anObjectAttiributes.end();
115 for (; anIt != aLast && !aGeomPoint; anIt++) {
116 std::shared_ptr<GeomDataAPI_Point2D> anAttributePoint =
117 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(*anIt);
119 std::shared_ptr<GeomAPI_Pnt> anAttributePnt = thePlane->to3D(anAttributePoint->x(),
120 anAttributePoint->y());
121 if (anAttributePnt.get() &&
122 anAttributePnt->distance(aShapeGeomPnt) < Precision::Confusion()) {
123 aGeomPoint = anAttributePoint;
133 //*************************************************************************************
134 std::shared_ptr<GeomDataAPI_Point2D> getFeaturePoint(DataPtr theData,
135 const std::string& theAttribute,
136 const std::shared_ptr<GeomAPI_Ax3>& thePlane)
138 std::shared_ptr<GeomDataAPI_Point2D> aPointAttr;
140 if (!theData.get() || !theData->isValid()) /// essential check as it is called in openGl thread
144 std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = std::dynamic_pointer_cast<
145 ModelAPI_AttributeRefAttr>(theData->attribute(theAttribute));
147 if (anAttr->isObject()) {
148 ObjectPtr anObject = anAttr->object();
149 aFeature = ModelAPI_Feature::feature(anObject);
150 if (aFeature && aFeature->getKind() == SketchPlugin_Point::ID()) {
151 // Attribute refers to a point
152 aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
153 aFeature->data()->attribute(SketchPlugin_Point::COORD_ID()));
156 // if the attribute refers on another object
157 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
159 GeomShapePtr aShape = aRes->shape();
161 TopoDS_Shape aTDSShape = aShape->impl<TopoDS_Shape>();
162 aPointAttr = findGeomPoint(anObject, aTDSShape, thePlane);
167 else if (anAttr->attr()) {
168 // If attribute is a point
169 aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttr->attr());
175 //*************************************************************************************
176 FeaturePtr getFeatureLine(DataPtr theData,
177 const std::string& theAttribute)
180 if (!theData.get() || !theData->isValid()) /// essential check as it is called in openGl thread)
183 std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr =
184 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theData->attribute(theAttribute));
186 FeaturePtr aFeature = ModelAPI_Feature::feature(anAttr->object());
187 if (aFeature && aFeature->getKind() == SketchPlugin_Line::ID()) {
194 //*************************************************************************************
195 std::shared_ptr<GeomAPI_Pnt2d> getProjectionPoint(const FeaturePtr theLine,
196 const std::shared_ptr<GeomAPI_Pnt2d>& thePoint)
198 DataPtr aData = theLine->data();
199 if (!aData.get() || !aData->isValid())
200 return std::shared_ptr<GeomAPI_Pnt2d>();
202 std::shared_ptr<GeomDataAPI_Point2D> aPoint1 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
203 aData->attribute(SketchPlugin_Line::START_ID()));
204 std::shared_ptr<GeomDataAPI_Point2D> aPoint2 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
205 aData->attribute(SketchPlugin_Line::END_ID()));
207 GeomAPI_Lin2d aLin2d(aPoint1->x(), aPoint1->y(), aPoint2->x(), aPoint2->y());
208 return aLin2d.project(thePoint);
212 static double MyArrowSize = 20;
213 double getArrowSize()
218 void setArrowSize(double theSize)
220 MyArrowSize = theSize;
223 int getDefaultArrowSize()
228 static double MyTextHeight = 16;
229 double getTextHeight()
234 void setTextHeight(double theHeight)
236 MyTextHeight = theHeight;
239 double getDefaultTextHeight()
244 double getFlyoutDistance(const ModelAPI_Feature* theConstraint)
246 std::shared_ptr<GeomDataAPI_Point2D> aFlyoutPoint =
247 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
248 const_cast<ModelAPI_Feature*>(theConstraint)->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
250 return aFlyoutPoint->y();
253 std::shared_ptr<GeomAPI_Pnt> getAnchorPoint(const ModelAPI_Feature* theConstraint,
254 const std::shared_ptr<GeomAPI_Ax3>& thePlane)
256 ModelAPI_Feature* aConstraint = const_cast<ModelAPI_Feature*>(theConstraint);
257 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
258 aConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
259 if (!aRefAttr || !aRefAttr->isObject() || !aRefAttr->object())
260 return std::shared_ptr<GeomAPI_Pnt>();
262 FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttr->object());
263 std::shared_ptr<GeomAPI_Pnt2d> aCenter;
264 if (aFeature->getKind() == SketchPlugin_Arc::ID()) { // arc
265 aCenter = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
266 aFeature->attribute(SketchPlugin_Arc::CENTER_ID()))->pnt();
267 } else if (aFeature->getKind() == SketchPlugin_Circle::ID()) { // circle
268 aCenter = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
269 aFeature->attribute(SketchPlugin_Circle::CENTER_ID()))->pnt();
271 return std::shared_ptr<GeomAPI_Pnt>();
273 std::shared_ptr<GeomAPI_Pnt2d> anOrigin(new GeomAPI_Pnt2d(0.0, 0.0));
274 std::shared_ptr<GeomAPI_Pnt2d> aFlyoutPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
275 aConstraint->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()))->pnt();
276 double aRadius = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
277 aConstraint->attribute(SketchPlugin_Constraint::VALUE()))->value();
278 double aLen = aFlyoutPnt->distance(anOrigin);
279 aRadius *= 1.001; // a gap to make point much closer to the circle, but not lying on it
280 aFlyoutPnt->setX(aCenter->x() + aFlyoutPnt->x() * aRadius / aLen);
281 aFlyoutPnt->setY(aCenter->y() + aFlyoutPnt->y() * aRadius / aLen);
282 return thePlane->to3D(aFlyoutPnt->x(), aFlyoutPnt->y());
285 void sendExpressionShownEvent(const bool& theState)
287 static Events_ID anId = SketcherPrs_ParameterStyleMessage::eventId();
288 std::shared_ptr<SketcherPrs_ParameterStyleMessage> aMessage = std::shared_ptr
289 <SketcherPrs_ParameterStyleMessage>(new SketcherPrs_ParameterStyleMessage(anId, 0));
290 aMessage->setStyle(theState ? SketcherPrs_ParameterStyleMessage::ParameterText
291 : SketcherPrs_ParameterStyleMessage::ParameterValue);
292 Events_Loop::loop()->send(aMessage);
293 Events_Loop::loop()->flush(anId);
296 Handle(Prs3d_DimensionAspect) createDimensionAspect()
298 Handle(Prs3d_DimensionAspect) anAspect = new Prs3d_DimensionAspect();
299 anAspect->MakeArrows3d(false);
300 anAspect->MakeText3d(false);
301 anAspect->MakeTextShaded(false);
302 anAspect->MakeUnitsDisplayed(false);
303 anAspect->TextAspect()->SetHeight(SketcherPrs_Tools::getDefaultTextHeight());
304 anAspect->ArrowAspect()->SetLength(SketcherPrs_Tools::getArrowSize());
309 void updateArrows(Handle(Prs3d_DimensionAspect) theDimAspect, double theDimValue, double theTextSize)
311 double anArrowLength = theDimAspect->ArrowAspect()->Length();
312 // This is not realy correct way to get viewer scale.
313 double aViewerScale = (double) SketcherPrs_Tools::getDefaultArrowSize() / anArrowLength;
315 if(theTextSize > ((theDimValue - 3 * SketcherPrs_Tools::getArrowSize()) * aViewerScale)) {
316 theDimAspect->SetTextHorizontalPosition(Prs3d_DTHP_Left);
317 theDimAspect->SetArrowOrientation(Prs3d_DAO_External);
318 theDimAspect->SetExtensionSize((theTextSize / aViewerScale + SketcherPrs_Tools::getArrowSize()) / 2.0);
320 theDimAspect->SetTextHorizontalPosition(Prs3d_DTHP_Center);
321 theDimAspect->SetArrowOrientation(Prs3d_DAO_Internal);
323 theDimAspect->SetArrowTailSize(theDimAspect->ArrowAspect()->Length());
324 // The value of vertical aligment is sometimes changed
325 theDimAspect->TextAspect()->SetVerticalJustification(Graphic3d_VTA_CENTER);
328 void sendEmptyPresentationError(ModelAPI_Feature* theFeature, const std::string theError)
330 Events_InfoMessage("SketcherPrs_Tools", "An empty AIS presentation: SketcherPrs_LengthDimension").send();
331 static const Events_ID anEvent = Events_Loop::eventByName(EVENT_EMPTY_AIS_PRESENTATION);
332 std::shared_ptr<ModelAPI_Object> aConstraintPtr(theFeature);
333 ModelAPI_EventCreator::get()->sendUpdated(aConstraintPtr, anEvent);