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 namespace SketcherPrs_Tools {
31 ObjectPtr getResult(ModelAPI_Feature* theFeature, const std::string& theAttrName)
33 std::shared_ptr<ModelAPI_Data> aData = theFeature->data();
34 std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = aData->refattr(theAttrName);
35 return anAttr->object();
39 std::shared_ptr<GeomAPI_Shape> getShape(ObjectPtr theObject)
41 ResultConstructionPtr aRes = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theObject);
42 if (aRes.get() != NULL) {
45 return std::shared_ptr<GeomAPI_Shape>();
49 std::shared_ptr<GeomAPI_Pnt2d> getPoint(ModelAPI_Feature* theFeature,
50 const std::string& theAttribute)
52 std::shared_ptr<GeomDataAPI_Point2D> aPointAttr;
54 if (!theFeature->data())
55 return std::shared_ptr<GeomAPI_Pnt2d>();
58 std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = std::dynamic_pointer_cast<
59 ModelAPI_AttributeRefAttr>(theFeature->data()->attribute(theAttribute));
61 return std::shared_ptr<GeomAPI_Pnt2d>();
63 aFeature = ModelAPI_Feature::feature(anAttr->object());
65 if (aFeature && aFeature->getKind() == SketchPlugin_Point::ID())
66 aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
67 aFeature->data()->attribute(SketchPlugin_Point::COORD_ID()));
69 else if (anAttr->attr()) {
70 aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttr->attr());
72 if (aPointAttr.get() != NULL)
73 return aPointAttr->pnt();
74 return std::shared_ptr<GeomAPI_Pnt2d>();
77 //*************************************************************************************
78 /// Find an attribute of the given object which corresponds to a vetrex
79 /// defined by a shape
80 /// \param theObject an object
81 /// \param theShape a vertex
82 /// \param thePlane a projection plane (sketcher plane)
83 std::shared_ptr<GeomDataAPI_Point2D> findGeomPoint(ObjectPtr theObject,
84 const TopoDS_Shape& theShape,
85 const std::shared_ptr<GeomAPI_Ax3>& thePlane)
87 std::shared_ptr<GeomDataAPI_Point2D> aGeomPoint;
89 FeaturePtr anObjectFeature = ModelAPI_Feature::feature(theObject);
90 if (anObjectFeature) {
91 if (theShape.ShapeType() == TopAbs_VERTEX) {
92 const TopoDS_Vertex& aShapeVertex = TopoDS::Vertex(theShape);
93 if (!aShapeVertex.IsNull()) {
94 gp_Pnt aShapePoint = BRep_Tool::Pnt(aShapeVertex);
95 std::shared_ptr<GeomAPI_Pnt> aShapeGeomPnt = std::shared_ptr<GeomAPI_Pnt>(
96 new GeomAPI_Pnt(aShapePoint.X(), aShapePoint.Y(), aShapePoint.Z()));
98 // find the given point in the feature attributes
99 std::list<AttributePtr> anObjectAttiributes =
100 anObjectFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
101 std::list<AttributePtr>::const_iterator anIt = anObjectAttiributes.begin(),
102 aLast = anObjectAttiributes.end();
103 for (; anIt != aLast && !aGeomPoint; anIt++) {
104 std::shared_ptr<GeomDataAPI_Point2D> anAttributePoint =
105 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(*anIt);
107 std::shared_ptr<GeomAPI_Pnt> anAttributePnt = thePlane->to3D(anAttributePoint->x(),
108 anAttributePoint->y());
109 if (anAttributePnt.get() &&
110 anAttributePnt->distance(aShapeGeomPnt) < Precision::Confusion()) {
111 aGeomPoint = anAttributePoint;
121 //*************************************************************************************
122 std::shared_ptr<GeomDataAPI_Point2D> getFeaturePoint(DataPtr theData,
123 const std::string& theAttribute,
124 const std::shared_ptr<GeomAPI_Ax3>& thePlane)
126 std::shared_ptr<GeomDataAPI_Point2D> aPointAttr;
132 std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = std::dynamic_pointer_cast<
133 ModelAPI_AttributeRefAttr>(theData->attribute(theAttribute));
135 if (anAttr->isObject()) {
136 ObjectPtr anObject = anAttr->object();
137 aFeature = ModelAPI_Feature::feature(anObject);
138 if (aFeature && aFeature->getKind() == SketchPlugin_Point::ID()) {
139 // Attribute refers to a point
140 aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
141 aFeature->data()->attribute(SketchPlugin_Point::COORD_ID()));
144 // if the attribute refers on another object
145 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
147 GeomShapePtr aShape = aRes->shape();
149 TopoDS_Shape aTDSShape = aShape->impl<TopoDS_Shape>();
150 aPointAttr = findGeomPoint(anObject, aTDSShape, thePlane);
155 else if (anAttr->attr()) {
156 // If attribute is a point
157 aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttr->attr());
163 //*************************************************************************************
164 FeaturePtr getFeatureLine(DataPtr theData,
165 const std::string& theAttribute)
171 std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr =
172 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theData->attribute(theAttribute));
174 FeaturePtr aFeature = ModelAPI_Feature::feature(anAttr->object());
175 if (aFeature && aFeature->getKind() == SketchPlugin_Line::ID()) {
182 //*************************************************************************************
183 std::shared_ptr<GeomAPI_Pnt2d> getProjectionPoint(const FeaturePtr theLine,
184 const std::shared_ptr<GeomAPI_Pnt2d>& thePoint)
186 DataPtr aData = theLine->data();
187 std::shared_ptr<GeomDataAPI_Point2D> aPoint1 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
188 aData->attribute(SketchPlugin_Line::START_ID()));
189 std::shared_ptr<GeomDataAPI_Point2D> aPoint2 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
190 aData->attribute(SketchPlugin_Line::END_ID()));
192 GeomAPI_Lin2d aLin2d(aPoint1->x(), aPoint1->y(), aPoint2->x(), aPoint2->y());
193 return aLin2d.project(thePoint);
197 static double MyArrowSize = 24.;
198 double getArrowSize()
203 void setArrowSize(double theSize)
205 MyArrowSize = theSize;
208 static double MyTextHeight = 16;
209 double getTextHeight()
214 void setTextHeight(double theHeight)
216 MyTextHeight = theHeight;
219 double getDefaultTextHeight()
224 double getFlyoutDistance(const ModelAPI_Feature* theConstraint)
226 std::shared_ptr<GeomDataAPI_Point2D> aFlyoutPoint =
227 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
228 const_cast<ModelAPI_Feature*>(theConstraint)->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
230 return aFlyoutPoint->y();
233 std::shared_ptr<GeomAPI_Pnt> getAnchorPoint(const ModelAPI_Feature* theConstraint,
234 const std::shared_ptr<GeomAPI_Ax3>& thePlane)
236 ModelAPI_Feature* aConstraint = const_cast<ModelAPI_Feature*>(theConstraint);
237 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
238 aConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
239 if (!aRefAttr || !aRefAttr->isObject() || !aRefAttr->object())
240 return std::shared_ptr<GeomAPI_Pnt>();
242 FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttr->object());
243 std::shared_ptr<GeomAPI_Pnt2d> aCenter;
244 if (aFeature->getKind() == SketchPlugin_Arc::ID()) { // arc
245 aCenter = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
246 aFeature->attribute(SketchPlugin_Arc::CENTER_ID()))->pnt();
247 } else if (aFeature->getKind() == SketchPlugin_Circle::ID()) { // circle
248 aCenter = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
249 aFeature->attribute(SketchPlugin_Circle::CENTER_ID()))->pnt();
251 return std::shared_ptr<GeomAPI_Pnt>();
253 std::shared_ptr<GeomAPI_Pnt2d> anOrigin(new GeomAPI_Pnt2d(0.0, 0.0));
254 std::shared_ptr<GeomAPI_Pnt2d> aFlyoutPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
255 aConstraint->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()))->pnt();
256 double aRadius = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
257 aConstraint->attribute(SketchPlugin_Constraint::VALUE()))->value();
258 double aLen = aFlyoutPnt->distance(anOrigin);
259 aRadius *= 1.001; // a gap to make point much closer to the circle, but not lying on it
260 aFlyoutPnt->setX(aCenter->x() + aFlyoutPnt->x() * aRadius / aLen);
261 aFlyoutPnt->setY(aCenter->y() + aFlyoutPnt->y() * aRadius / aLen);
262 return thePlane->to3D(aFlyoutPnt->x(), aFlyoutPnt->y());