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 std::shared_ptr<GeomDataAPI_Point2D> findGeomPoint(ObjectPtr theObject,
77 const TopoDS_Shape& theShape,
78 const std::shared_ptr<GeomAPI_Ax3>& thePlane)
80 std::shared_ptr<GeomDataAPI_Point2D> aGeomPoint;
82 FeaturePtr anObjectFeature = ModelAPI_Feature::feature(theObject);
83 if (anObjectFeature) {
84 if (theShape.ShapeType() == TopAbs_VERTEX) {
85 const TopoDS_Vertex& aShapeVertex = TopoDS::Vertex(theShape);
86 if (!aShapeVertex.IsNull()) {
87 gp_Pnt aShapePoint = BRep_Tool::Pnt(aShapeVertex);
88 std::shared_ptr<GeomAPI_Pnt> aShapeGeomPnt = std::shared_ptr<GeomAPI_Pnt>(
89 new GeomAPI_Pnt(aShapePoint.X(), aShapePoint.Y(), aShapePoint.Z()));
91 // find the given point in the feature attributes
92 std::list<AttributePtr> anObjectAttiributes =
93 anObjectFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
94 std::list<AttributePtr>::const_iterator anIt = anObjectAttiributes.begin(),
95 aLast = anObjectAttiributes.end();
96 for (; anIt != aLast && !aGeomPoint; anIt++) {
97 std::shared_ptr<GeomDataAPI_Point2D> anAttributePoint =
98 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(*anIt);
100 std::shared_ptr<GeomAPI_Pnt> anAttributePnt = thePlane->to3D(anAttributePoint->x(),
101 anAttributePoint->y());
102 if (anAttributePnt.get() &&
103 anAttributePnt->distance(aShapeGeomPnt) < Precision::Confusion()) {
104 aGeomPoint = anAttributePoint;
114 //*************************************************************************************
115 std::shared_ptr<GeomDataAPI_Point2D> getFeaturePoint(DataPtr theData,
116 const std::string& theAttribute,
117 const std::shared_ptr<GeomAPI_Ax3>& thePlane)
119 std::shared_ptr<GeomDataAPI_Point2D> aPointAttr;
125 std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = std::dynamic_pointer_cast<
126 ModelAPI_AttributeRefAttr>(theData->attribute(theAttribute));
128 if (anAttr->isObject()) {
129 ObjectPtr anObject = anAttr->object();
130 aFeature = ModelAPI_Feature::feature(anObject);
131 if (aFeature && aFeature->getKind() == SketchPlugin_Point::ID()) {
132 aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
133 aFeature->data()->attribute(SketchPlugin_Point::COORD_ID()));
136 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
138 GeomShapePtr aShape = aRes->shape();
140 TopoDS_Shape aTDSShape = aShape->impl<TopoDS_Shape>();
141 aPointAttr = findGeomPoint(anObject, aTDSShape, thePlane);
146 else if (anAttr->attr()) {
147 aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttr->attr());
153 //*************************************************************************************
154 FeaturePtr getFeatureLine(DataPtr theData,
155 const std::string& theAttribute)
161 std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr =
162 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theData->attribute(theAttribute));
164 FeaturePtr aFeature = ModelAPI_Feature::feature(anAttr->object());
165 if (aFeature && aFeature->getKind() == SketchPlugin_Line::ID()) {
172 //*************************************************************************************
173 std::shared_ptr<GeomAPI_Pnt2d> getProjectionPoint(const FeaturePtr theLine,
174 const std::shared_ptr<GeomAPI_Pnt2d>& thePoint)
176 DataPtr aData = theLine->data();
177 std::shared_ptr<GeomDataAPI_Point2D> aPoint1 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
178 aData->attribute(SketchPlugin_Line::START_ID()));
179 std::shared_ptr<GeomDataAPI_Point2D> aPoint2 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
180 aData->attribute(SketchPlugin_Line::END_ID()));
182 GeomAPI_Lin2d aLin2d(aPoint1->x(), aPoint1->y(), aPoint2->x(), aPoint2->y());
183 return aLin2d.project(thePoint);
187 static double MyArrowSize = 24.;
188 double getArrowSize()
193 void setArrowSize(double theSize)
195 MyArrowSize = theSize;
198 static double MyTextHeight = 16;
199 double getTextHeight()
204 void setTextHeight(double theHeight)
206 MyTextHeight = theHeight;
209 double getDefaultTextHeight()
214 double getFlyoutDistance(const ModelAPI_Feature* theConstraint)
216 std::shared_ptr<GeomDataAPI_Point2D> aFlyoutPoint =
217 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
218 const_cast<ModelAPI_Feature*>(theConstraint)->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
220 return aFlyoutPoint->y();
223 std::shared_ptr<GeomAPI_Pnt> getAnchorPoint(const ModelAPI_Feature* theConstraint,
224 const std::shared_ptr<GeomAPI_Ax3>& thePlane)
226 ModelAPI_Feature* aConstraint = const_cast<ModelAPI_Feature*>(theConstraint);
227 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
228 aConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
229 if (!aRefAttr || !aRefAttr->isObject() || !aRefAttr->object())
230 return std::shared_ptr<GeomAPI_Pnt>();
232 FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttr->object());
233 std::shared_ptr<GeomAPI_Pnt2d> aCenter;
234 if (aFeature->getKind() == SketchPlugin_Arc::ID()) {
235 aCenter = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
236 aFeature->attribute(SketchPlugin_Arc::CENTER_ID()))->pnt();
237 } else if (aFeature->getKind() == SketchPlugin_Circle::ID()) {
238 aCenter = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
239 aFeature->attribute(SketchPlugin_Circle::CENTER_ID()))->pnt();
241 return std::shared_ptr<GeomAPI_Pnt>();
243 std::shared_ptr<GeomAPI_Pnt2d> anOrigin(new GeomAPI_Pnt2d(0.0, 0.0));
244 std::shared_ptr<GeomAPI_Pnt2d> aFlyoutPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
245 aConstraint->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()))->pnt();
246 double aRadius = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
247 aConstraint->attribute(SketchPlugin_Constraint::VALUE()))->value();
248 double aLen = aFlyoutPnt->distance(anOrigin);
249 aRadius *= 1.001; // a gap to make point much closer to the circle, but not lying on it
250 aFlyoutPnt->setX(aCenter->x() + aFlyoutPnt->x() * aRadius / aLen);
251 aFlyoutPnt->setY(aCenter->y() + aFlyoutPnt->y() * aRadius / aLen);
252 return thePlane->to3D(aFlyoutPnt->x(), aFlyoutPnt->y());