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 aFeature = ModelAPI_Feature::feature(anAttr->object());
63 if (aFeature && aFeature->getKind() == SketchPlugin_Point::ID())
64 aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
65 aFeature->data()->attribute(SketchPlugin_Point::COORD_ID()));
67 else if (anAttr->attr()) {
68 aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttr->attr());
70 if (aPointAttr.get() != NULL)
71 return aPointAttr->pnt();
72 return std::shared_ptr<GeomAPI_Pnt2d>();
75 //*************************************************************************************
76 /// Find an attribute of the given object which corresponds to a vetrex
77 /// defined by a shape
78 /// \param theObject an object
79 /// \param theShape a vertex
80 /// \param thePlane a projection plane (sketcher plane)
81 std::shared_ptr<GeomDataAPI_Point2D> findGeomPoint(ObjectPtr theObject,
82 const TopoDS_Shape& theShape,
83 const std::shared_ptr<GeomAPI_Ax3>& thePlane)
85 std::shared_ptr<GeomDataAPI_Point2D> aGeomPoint;
87 FeaturePtr anObjectFeature = ModelAPI_Feature::feature(theObject);
88 if (anObjectFeature) {
89 if (theShape.ShapeType() == TopAbs_VERTEX) {
90 const TopoDS_Vertex& aShapeVertex = TopoDS::Vertex(theShape);
91 if (!aShapeVertex.IsNull()) {
92 gp_Pnt aShapePoint = BRep_Tool::Pnt(aShapeVertex);
93 std::shared_ptr<GeomAPI_Pnt> aShapeGeomPnt = std::shared_ptr<GeomAPI_Pnt>(
94 new GeomAPI_Pnt(aShapePoint.X(), aShapePoint.Y(), aShapePoint.Z()));
96 // find the given point in the feature attributes
97 std::list<AttributePtr> anObjectAttiributes =
98 anObjectFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
99 std::list<AttributePtr>::const_iterator anIt = anObjectAttiributes.begin(),
100 aLast = anObjectAttiributes.end();
101 for (; anIt != aLast && !aGeomPoint; anIt++) {
102 std::shared_ptr<GeomDataAPI_Point2D> anAttributePoint =
103 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(*anIt);
105 std::shared_ptr<GeomAPI_Pnt> anAttributePnt = thePlane->to3D(anAttributePoint->x(),
106 anAttributePoint->y());
107 if (anAttributePnt.get() &&
108 anAttributePnt->distance(aShapeGeomPnt) < Precision::Confusion()) {
109 aGeomPoint = anAttributePoint;
119 //*************************************************************************************
120 std::shared_ptr<GeomDataAPI_Point2D> getFeaturePoint(DataPtr theData,
121 const std::string& theAttribute,
122 const std::shared_ptr<GeomAPI_Ax3>& thePlane)
124 std::shared_ptr<GeomDataAPI_Point2D> aPointAttr;
130 std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = std::dynamic_pointer_cast<
131 ModelAPI_AttributeRefAttr>(theData->attribute(theAttribute));
133 if (anAttr->isObject()) {
134 ObjectPtr anObject = anAttr->object();
135 aFeature = ModelAPI_Feature::feature(anObject);
136 if (aFeature && aFeature->getKind() == SketchPlugin_Point::ID()) {
137 // Attribute refers to a point
138 aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
139 aFeature->data()->attribute(SketchPlugin_Point::COORD_ID()));
142 // if the attribute refers on another object
143 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
145 GeomShapePtr aShape = aRes->shape();
147 TopoDS_Shape aTDSShape = aShape->impl<TopoDS_Shape>();
148 aPointAttr = findGeomPoint(anObject, aTDSShape, thePlane);
153 else if (anAttr->attr()) {
154 // If attribute is a point
155 aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttr->attr());
161 //*************************************************************************************
162 FeaturePtr getFeatureLine(DataPtr theData,
163 const std::string& theAttribute)
169 std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr =
170 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theData->attribute(theAttribute));
172 FeaturePtr aFeature = ModelAPI_Feature::feature(anAttr->object());
173 if (aFeature && aFeature->getKind() == SketchPlugin_Line::ID()) {
180 //*************************************************************************************
181 std::shared_ptr<GeomAPI_Pnt2d> getProjectionPoint(const FeaturePtr theLine,
182 const std::shared_ptr<GeomAPI_Pnt2d>& thePoint)
184 DataPtr aData = theLine->data();
185 std::shared_ptr<GeomDataAPI_Point2D> aPoint1 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
186 aData->attribute(SketchPlugin_Line::START_ID()));
187 std::shared_ptr<GeomDataAPI_Point2D> aPoint2 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
188 aData->attribute(SketchPlugin_Line::END_ID()));
190 GeomAPI_Lin2d aLin2d(aPoint1->x(), aPoint1->y(), aPoint2->x(), aPoint2->y());
191 return aLin2d.project(thePoint);
195 static double MyArrowSize = 24.;
196 double getArrowSize()
201 void setArrowSize(double theSize)
203 MyArrowSize = theSize;
206 static double MyTextHeight = 16;
207 double getTextHeight()
212 void setTextHeight(double theHeight)
214 MyTextHeight = theHeight;
217 double getDefaultTextHeight()
222 double getFlyoutDistance(const ModelAPI_Feature* theConstraint)
224 std::shared_ptr<GeomDataAPI_Point2D> aFlyoutPoint =
225 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
226 const_cast<ModelAPI_Feature*>(theConstraint)->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
228 return aFlyoutPoint->y();
231 std::shared_ptr<GeomAPI_Pnt> getAnchorPoint(const ModelAPI_Feature* theConstraint,
232 const std::shared_ptr<GeomAPI_Ax3>& thePlane)
234 ModelAPI_Feature* aConstraint = const_cast<ModelAPI_Feature*>(theConstraint);
235 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
236 aConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
237 if (!aRefAttr || !aRefAttr->isObject() || !aRefAttr->object())
238 return std::shared_ptr<GeomAPI_Pnt>();
240 FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttr->object());
241 std::shared_ptr<GeomAPI_Pnt2d> aCenter;
242 if (aFeature->getKind() == SketchPlugin_Arc::ID()) { // arc
243 aCenter = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
244 aFeature->attribute(SketchPlugin_Arc::CENTER_ID()))->pnt();
245 } else if (aFeature->getKind() == SketchPlugin_Circle::ID()) { // circle
246 aCenter = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
247 aFeature->attribute(SketchPlugin_Circle::CENTER_ID()))->pnt();
249 return std::shared_ptr<GeomAPI_Pnt>();
251 std::shared_ptr<GeomAPI_Pnt2d> anOrigin(new GeomAPI_Pnt2d(0.0, 0.0));
252 std::shared_ptr<GeomAPI_Pnt2d> aFlyoutPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
253 aConstraint->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()))->pnt();
254 double aRadius = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
255 aConstraint->attribute(SketchPlugin_Constraint::VALUE()))->value();
256 double aLen = aFlyoutPnt->distance(anOrigin);
257 aRadius *= 1.001; // a gap to make point much closer to the circle, but not lying on it
258 aFlyoutPnt->setX(aCenter->x() + aFlyoutPnt->x() * aRadius / aLen);
259 aFlyoutPnt->setY(aCenter->y() + aFlyoutPnt->y() * aRadius / aLen);
260 return thePlane->to3D(aFlyoutPnt->x(), aFlyoutPnt->y());