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>
19 #include <GeomDataAPI_Point2D.h>
20 #include <GeomAPI_Lin2d.h>
23 #include <TopoDS_Shape.hxx>
24 #include <TopoDS_Vertex.hxx>
26 #include <BRep_Tool.hxx>
27 #include <Precision.hxx>
29 #include <AIS_Dimension.hxx>
31 // it is not possible to use 0x2211 as summ symbol because it is not supported by
32 // debian Linux platform
33 static const Standard_ExtCharacter MyEmptySymbol(' ');
34 static const Standard_ExtCharacter MySigmaSymbol(0x03A3);
36 namespace SketcherPrs_Tools {
38 AttributePtr getAttribute(ModelAPI_Feature* theFeature, const std::string& theAttrName)
40 std::shared_ptr<ModelAPI_Data> aData = theFeature->data();
41 std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = aData->refattr(theAttrName);
42 return !anAttr->isObject() ? anAttr->attr() : AttributePtr();
45 ObjectPtr getResult(ModelAPI_Feature* theFeature, const std::string& theAttrName)
47 std::shared_ptr<ModelAPI_Data> aData = theFeature->data();
48 std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = aData->refattr(theAttrName);
49 return anAttr->object();
53 std::shared_ptr<GeomAPI_Shape> getShape(ObjectPtr theObject)
55 ResultConstructionPtr aRes = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theObject);
56 if (aRes.get() != NULL) {
59 return std::shared_ptr<GeomAPI_Shape>();
63 std::shared_ptr<GeomAPI_Pnt2d> getPoint(ModelAPI_Feature* theFeature,
64 const std::string& theAttribute)
66 std::shared_ptr<GeomDataAPI_Point2D> aPointAttr;
68 if (!theFeature->data())
69 return std::shared_ptr<GeomAPI_Pnt2d>();
72 std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = std::dynamic_pointer_cast<
73 ModelAPI_AttributeRefAttr>(theFeature->data()->attribute(theAttribute));
75 return std::shared_ptr<GeomAPI_Pnt2d>();
77 aFeature = ModelAPI_Feature::feature(anAttr->object());
79 if (aFeature && aFeature->getKind() == SketchPlugin_Point::ID())
80 aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
81 aFeature->data()->attribute(SketchPlugin_Point::COORD_ID()));
83 else if (anAttr->attr()) {
84 aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttr->attr());
86 if (aPointAttr.get() != NULL)
87 return aPointAttr->pnt();
88 return std::shared_ptr<GeomAPI_Pnt2d>();
91 //*************************************************************************************
92 /// Find an attribute of the given object which corresponds to a vetrex
93 /// defined by a shape
94 /// \param theObject an object
95 /// \param theShape a vertex
96 /// \param thePlane a projection plane (sketcher plane)
97 std::shared_ptr<GeomDataAPI_Point2D> findGeomPoint(ObjectPtr theObject,
98 const TopoDS_Shape& theShape,
99 const std::shared_ptr<GeomAPI_Ax3>& thePlane)
101 std::shared_ptr<GeomDataAPI_Point2D> aGeomPoint;
103 FeaturePtr anObjectFeature = ModelAPI_Feature::feature(theObject);
104 if (anObjectFeature) {
105 if (theShape.ShapeType() == TopAbs_VERTEX) {
106 const TopoDS_Vertex& aShapeVertex = TopoDS::Vertex(theShape);
107 if (!aShapeVertex.IsNull()) {
108 gp_Pnt aShapePoint = BRep_Tool::Pnt(aShapeVertex);
109 std::shared_ptr<GeomAPI_Pnt> aShapeGeomPnt = std::shared_ptr<GeomAPI_Pnt>(
110 new GeomAPI_Pnt(aShapePoint.X(), aShapePoint.Y(), aShapePoint.Z()));
112 // find the given point in the feature attributes
113 std::list<AttributePtr> anObjectAttiributes =
114 anObjectFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
115 std::list<AttributePtr>::const_iterator anIt = anObjectAttiributes.begin(),
116 aLast = anObjectAttiributes.end();
117 for (; anIt != aLast && !aGeomPoint; anIt++) {
118 std::shared_ptr<GeomDataAPI_Point2D> anAttributePoint =
119 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(*anIt);
121 std::shared_ptr<GeomAPI_Pnt> anAttributePnt = thePlane->to3D(anAttributePoint->x(),
122 anAttributePoint->y());
123 if (anAttributePnt.get() &&
124 anAttributePnt->distance(aShapeGeomPnt) < Precision::Confusion()) {
125 aGeomPoint = anAttributePoint;
135 //*************************************************************************************
136 std::shared_ptr<GeomDataAPI_Point2D> getFeaturePoint(DataPtr theData,
137 const std::string& theAttribute,
138 const std::shared_ptr<GeomAPI_Ax3>& thePlane)
140 std::shared_ptr<GeomDataAPI_Point2D> aPointAttr;
146 std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = std::dynamic_pointer_cast<
147 ModelAPI_AttributeRefAttr>(theData->attribute(theAttribute));
149 if (anAttr->isObject()) {
150 ObjectPtr anObject = anAttr->object();
151 aFeature = ModelAPI_Feature::feature(anObject);
152 if (aFeature && aFeature->getKind() == SketchPlugin_Point::ID()) {
153 // Attribute refers to a point
154 aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
155 aFeature->data()->attribute(SketchPlugin_Point::COORD_ID()));
158 // if the attribute refers on another object
159 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
161 GeomShapePtr aShape = aRes->shape();
163 TopoDS_Shape aTDSShape = aShape->impl<TopoDS_Shape>();
164 aPointAttr = findGeomPoint(anObject, aTDSShape, thePlane);
169 else if (anAttr->attr()) {
170 // If attribute is a point
171 aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttr->attr());
177 //*************************************************************************************
178 FeaturePtr getFeatureLine(DataPtr theData,
179 const std::string& theAttribute)
185 std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr =
186 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theData->attribute(theAttribute));
188 FeaturePtr aFeature = ModelAPI_Feature::feature(anAttr->object());
189 if (aFeature && aFeature->getKind() == SketchPlugin_Line::ID()) {
196 //*************************************************************************************
197 std::shared_ptr<GeomAPI_Pnt2d> getProjectionPoint(const FeaturePtr theLine,
198 const std::shared_ptr<GeomAPI_Pnt2d>& thePoint)
200 DataPtr aData = theLine->data();
201 std::shared_ptr<GeomDataAPI_Point2D> aPoint1 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
202 aData->attribute(SketchPlugin_Line::START_ID()));
203 std::shared_ptr<GeomDataAPI_Point2D> aPoint2 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
204 aData->attribute(SketchPlugin_Line::END_ID()));
206 GeomAPI_Lin2d aLin2d(aPoint1->x(), aPoint1->y(), aPoint2->x(), aPoint2->y());
207 return aLin2d.project(thePoint);
211 static double MyArrowSize = 20;
212 double getArrowSize()
217 void setArrowSize(double theSize)
219 MyArrowSize = theSize;
222 int getDefaultArrowSize()
227 static double MyTextHeight = 16;
228 double getTextHeight()
233 void setTextHeight(double theHeight)
235 MyTextHeight = theHeight;
238 double getDefaultTextHeight()
243 double getFlyoutDistance(const ModelAPI_Feature* theConstraint)
245 std::shared_ptr<GeomDataAPI_Point2D> aFlyoutPoint =
246 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
247 const_cast<ModelAPI_Feature*>(theConstraint)->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
249 return aFlyoutPoint->y();
252 std::shared_ptr<GeomAPI_Pnt> getAnchorPoint(const ModelAPI_Feature* theConstraint,
253 const std::shared_ptr<GeomAPI_Ax3>& thePlane)
255 ModelAPI_Feature* aConstraint = const_cast<ModelAPI_Feature*>(theConstraint);
256 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
257 aConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
258 if (!aRefAttr || !aRefAttr->isObject() || !aRefAttr->object())
259 return std::shared_ptr<GeomAPI_Pnt>();
261 FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttr->object());
262 std::shared_ptr<GeomAPI_Pnt2d> aCenter;
263 if (aFeature->getKind() == SketchPlugin_Arc::ID()) { // arc
264 aCenter = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
265 aFeature->attribute(SketchPlugin_Arc::CENTER_ID()))->pnt();
266 } else if (aFeature->getKind() == SketchPlugin_Circle::ID()) { // circle
267 aCenter = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
268 aFeature->attribute(SketchPlugin_Circle::CENTER_ID()))->pnt();
270 return std::shared_ptr<GeomAPI_Pnt>();
272 std::shared_ptr<GeomAPI_Pnt2d> anOrigin(new GeomAPI_Pnt2d(0.0, 0.0));
273 std::shared_ptr<GeomAPI_Pnt2d> aFlyoutPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
274 aConstraint->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()))->pnt();
275 double aRadius = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
276 aConstraint->attribute(SketchPlugin_Constraint::VALUE()))->value();
277 double aLen = aFlyoutPnt->distance(anOrigin);
278 aRadius *= 1.001; // a gap to make point much closer to the circle, but not lying on it
279 aFlyoutPnt->setX(aCenter->x() + aFlyoutPnt->x() * aRadius / aLen);
280 aFlyoutPnt->setY(aCenter->y() + aFlyoutPnt->y() * aRadius / aLen);
281 return thePlane->to3D(aFlyoutPnt->x(), aFlyoutPnt->y());
284 void setDisplaySpecialSymbol(AIS_Dimension* theDimension, const bool& theToDisplay)
287 theDimension->SetSpecialSymbol(MySigmaSymbol);
288 theDimension->SetDisplaySpecialSymbol(AIS_DSS_Before);
291 theDimension->SetSpecialSymbol(MyEmptySymbol);
292 theDimension->SetDisplaySpecialSymbol(AIS_DSS_No);
296 void setDisplayParameter(AIS_Dimension* theDimension, const std::string& theParameter,
297 const bool& theToDisplay)
300 theDimension->DimensionAspect()->MakeUnitsDisplayed(true);
301 theDimension->SetDisplayUnits(TCollection_AsciiString(theParameter.c_str()));
302 theDimension->DimensionAspect()->SetValueStringFormat("");
305 theDimension->DimensionAspect()->MakeUnitsDisplayed(false);
306 theDimension->SetDisplayUnits(TCollection_AsciiString()); // THE_UNDEFINED_UNITS in AIS_Dimension
307 theDimension->DimensionAspect()->SetValueStringFormat("%g");
311 void sendExpressionShownEvent(const bool& theState)
313 static Events_ID anId = SketcherPrs_ParameterStyleMessage::eventId();
314 std::shared_ptr<SketcherPrs_ParameterStyleMessage> aMessage = std::shared_ptr
315 <SketcherPrs_ParameterStyleMessage>(new SketcherPrs_ParameterStyleMessage(anId, 0));
316 aMessage->setStyle(theState ? SketcherPrs_ParameterStyleMessage::ParameterText
317 : SketcherPrs_ParameterStyleMessage::ParameterValue);
318 Events_Loop::loop()->send(aMessage);
319 Events_Loop::loop()->flush(anId);