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 ObjectPtr getResult(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->object();
46 std::shared_ptr<GeomAPI_Shape> getShape(ObjectPtr theObject)
48 ResultConstructionPtr aRes = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theObject);
49 if (aRes.get() != NULL) {
52 return std::shared_ptr<GeomAPI_Shape>();
56 std::shared_ptr<GeomAPI_Pnt2d> getPoint(ModelAPI_Feature* theFeature,
57 const std::string& theAttribute)
59 std::shared_ptr<GeomDataAPI_Point2D> aPointAttr;
61 if (!theFeature->data())
62 return std::shared_ptr<GeomAPI_Pnt2d>();
65 std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = std::dynamic_pointer_cast<
66 ModelAPI_AttributeRefAttr>(theFeature->data()->attribute(theAttribute));
68 return std::shared_ptr<GeomAPI_Pnt2d>();
70 aFeature = ModelAPI_Feature::feature(anAttr->object());
72 if (aFeature && aFeature->getKind() == SketchPlugin_Point::ID())
73 aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
74 aFeature->data()->attribute(SketchPlugin_Point::COORD_ID()));
76 else if (anAttr->attr()) {
77 aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttr->attr());
79 if (aPointAttr.get() != NULL)
80 return aPointAttr->pnt();
81 return std::shared_ptr<GeomAPI_Pnt2d>();
84 //*************************************************************************************
85 /// Find an attribute of the given object which corresponds to a vetrex
86 /// defined by a shape
87 /// \param theObject an object
88 /// \param theShape a vertex
89 /// \param thePlane a projection plane (sketcher plane)
90 std::shared_ptr<GeomDataAPI_Point2D> findGeomPoint(ObjectPtr theObject,
91 const TopoDS_Shape& theShape,
92 const std::shared_ptr<GeomAPI_Ax3>& thePlane)
94 std::shared_ptr<GeomDataAPI_Point2D> aGeomPoint;
96 FeaturePtr anObjectFeature = ModelAPI_Feature::feature(theObject);
97 if (anObjectFeature) {
98 if (theShape.ShapeType() == TopAbs_VERTEX) {
99 const TopoDS_Vertex& aShapeVertex = TopoDS::Vertex(theShape);
100 if (!aShapeVertex.IsNull()) {
101 gp_Pnt aShapePoint = BRep_Tool::Pnt(aShapeVertex);
102 std::shared_ptr<GeomAPI_Pnt> aShapeGeomPnt = std::shared_ptr<GeomAPI_Pnt>(
103 new GeomAPI_Pnt(aShapePoint.X(), aShapePoint.Y(), aShapePoint.Z()));
105 // find the given point in the feature attributes
106 std::list<AttributePtr> anObjectAttiributes =
107 anObjectFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
108 std::list<AttributePtr>::const_iterator anIt = anObjectAttiributes.begin(),
109 aLast = anObjectAttiributes.end();
110 for (; anIt != aLast && !aGeomPoint; anIt++) {
111 std::shared_ptr<GeomDataAPI_Point2D> anAttributePoint =
112 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(*anIt);
114 std::shared_ptr<GeomAPI_Pnt> anAttributePnt = thePlane->to3D(anAttributePoint->x(),
115 anAttributePoint->y());
116 if (anAttributePnt.get() &&
117 anAttributePnt->distance(aShapeGeomPnt) < Precision::Confusion()) {
118 aGeomPoint = anAttributePoint;
128 //*************************************************************************************
129 std::shared_ptr<GeomDataAPI_Point2D> getFeaturePoint(DataPtr theData,
130 const std::string& theAttribute,
131 const std::shared_ptr<GeomAPI_Ax3>& thePlane)
133 std::shared_ptr<GeomDataAPI_Point2D> aPointAttr;
139 std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = std::dynamic_pointer_cast<
140 ModelAPI_AttributeRefAttr>(theData->attribute(theAttribute));
142 if (anAttr->isObject()) {
143 ObjectPtr anObject = anAttr->object();
144 aFeature = ModelAPI_Feature::feature(anObject);
145 if (aFeature && aFeature->getKind() == SketchPlugin_Point::ID()) {
146 // Attribute refers to a point
147 aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
148 aFeature->data()->attribute(SketchPlugin_Point::COORD_ID()));
151 // if the attribute refers on another object
152 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
154 GeomShapePtr aShape = aRes->shape();
156 TopoDS_Shape aTDSShape = aShape->impl<TopoDS_Shape>();
157 aPointAttr = findGeomPoint(anObject, aTDSShape, thePlane);
162 else if (anAttr->attr()) {
163 // If attribute is a point
164 aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttr->attr());
170 //*************************************************************************************
171 FeaturePtr getFeatureLine(DataPtr theData,
172 const std::string& theAttribute)
178 std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr =
179 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theData->attribute(theAttribute));
181 FeaturePtr aFeature = ModelAPI_Feature::feature(anAttr->object());
182 if (aFeature && aFeature->getKind() == SketchPlugin_Line::ID()) {
189 //*************************************************************************************
190 std::shared_ptr<GeomAPI_Pnt2d> getProjectionPoint(const FeaturePtr theLine,
191 const std::shared_ptr<GeomAPI_Pnt2d>& thePoint)
193 DataPtr aData = theLine->data();
194 std::shared_ptr<GeomDataAPI_Point2D> aPoint1 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
195 aData->attribute(SketchPlugin_Line::START_ID()));
196 std::shared_ptr<GeomDataAPI_Point2D> aPoint2 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
197 aData->attribute(SketchPlugin_Line::END_ID()));
199 GeomAPI_Lin2d aLin2d(aPoint1->x(), aPoint1->y(), aPoint2->x(), aPoint2->y());
200 return aLin2d.project(thePoint);
204 static double MyArrowSize = 20;
205 double getArrowSize()
210 void setArrowSize(double theSize)
212 MyArrowSize = theSize;
215 int getDefaultArrowSize()
220 static double MyTextHeight = 16;
221 double getTextHeight()
226 void setTextHeight(double theHeight)
228 MyTextHeight = theHeight;
231 double getDefaultTextHeight()
236 double getFlyoutDistance(const ModelAPI_Feature* theConstraint)
238 std::shared_ptr<GeomDataAPI_Point2D> aFlyoutPoint =
239 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
240 const_cast<ModelAPI_Feature*>(theConstraint)->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
242 return aFlyoutPoint->y();
245 std::shared_ptr<GeomAPI_Pnt> getAnchorPoint(const ModelAPI_Feature* theConstraint,
246 const std::shared_ptr<GeomAPI_Ax3>& thePlane)
248 ModelAPI_Feature* aConstraint = const_cast<ModelAPI_Feature*>(theConstraint);
249 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
250 aConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
251 if (!aRefAttr || !aRefAttr->isObject() || !aRefAttr->object())
252 return std::shared_ptr<GeomAPI_Pnt>();
254 FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttr->object());
255 std::shared_ptr<GeomAPI_Pnt2d> aCenter;
256 if (aFeature->getKind() == SketchPlugin_Arc::ID()) { // arc
257 aCenter = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
258 aFeature->attribute(SketchPlugin_Arc::CENTER_ID()))->pnt();
259 } else if (aFeature->getKind() == SketchPlugin_Circle::ID()) { // circle
260 aCenter = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
261 aFeature->attribute(SketchPlugin_Circle::CENTER_ID()))->pnt();
263 return std::shared_ptr<GeomAPI_Pnt>();
265 std::shared_ptr<GeomAPI_Pnt2d> anOrigin(new GeomAPI_Pnt2d(0.0, 0.0));
266 std::shared_ptr<GeomAPI_Pnt2d> aFlyoutPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
267 aConstraint->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()))->pnt();
268 double aRadius = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
269 aConstraint->attribute(SketchPlugin_Constraint::VALUE()))->value();
270 double aLen = aFlyoutPnt->distance(anOrigin);
271 aRadius *= 1.001; // a gap to make point much closer to the circle, but not lying on it
272 aFlyoutPnt->setX(aCenter->x() + aFlyoutPnt->x() * aRadius / aLen);
273 aFlyoutPnt->setY(aCenter->y() + aFlyoutPnt->y() * aRadius / aLen);
274 return thePlane->to3D(aFlyoutPnt->x(), aFlyoutPnt->y());
277 void setDisplaySpecialSymbol(AIS_Dimension* theDimension, const bool& theToDisplay)
280 theDimension->SetSpecialSymbol(MySigmaSymbol);
281 theDimension->SetDisplaySpecialSymbol(AIS_DSS_Before);
284 theDimension->SetSpecialSymbol(MyEmptySymbol);
285 theDimension->SetDisplaySpecialSymbol(AIS_DSS_No);