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 namespace SketcherPrs_Tools {
25 ObjectPtr getResult(ModelAPI_Feature* theFeature, const std::string& theAttrName)
27 std::shared_ptr<ModelAPI_Data> aData = theFeature->data();
28 std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = aData->refattr(theAttrName);
29 return anAttr->object();
33 std::shared_ptr<GeomAPI_Shape> getShape(ObjectPtr theObject)
35 ResultConstructionPtr aRes = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theObject);
36 if (aRes.get() != NULL) {
39 return std::shared_ptr<GeomAPI_Shape>();
43 std::shared_ptr<GeomAPI_Pnt2d> getPoint(ModelAPI_Feature* theFeature,
44 const std::string& theAttribute)
46 std::shared_ptr<GeomDataAPI_Point2D> aPointAttr;
48 if (!theFeature->data())
49 return std::shared_ptr<GeomAPI_Pnt2d>();
52 std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = std::dynamic_pointer_cast<
53 ModelAPI_AttributeRefAttr>(theFeature->data()->attribute(theAttribute));
55 aFeature = ModelAPI_Feature::feature(anAttr->object());
57 if (aFeature && aFeature->getKind() == SketchPlugin_Point::ID())
58 aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
59 aFeature->data()->attribute(SketchPlugin_Point::COORD_ID()));
61 else if (anAttr->attr()) {
62 aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttr->attr());
64 if (aPointAttr.get() != NULL)
65 return aPointAttr->pnt();
66 return std::shared_ptr<GeomAPI_Pnt2d>();
69 //*************************************************************************************
70 std::shared_ptr<GeomDataAPI_Point2D> getFeaturePoint(DataPtr theData,
71 const std::string& theAttribute)
73 std::shared_ptr<GeomDataAPI_Point2D> aPointAttr;
79 std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = std::dynamic_pointer_cast<
80 ModelAPI_AttributeRefAttr>(theData->attribute(theAttribute));
82 aFeature = ModelAPI_Feature::feature(anAttr->object());
84 if (aFeature && aFeature->getKind() == SketchPlugin_Point::ID())
85 aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
86 aFeature->data()->attribute(SketchPlugin_Point::COORD_ID()));
88 else if (anAttr->attr()) {
89 aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttr->attr());
94 //*************************************************************************************
95 FeaturePtr getFeatureLine(DataPtr theData,
96 const std::string& theAttribute)
102 std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr =
103 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theData->attribute(theAttribute));
105 FeaturePtr aFeature = ModelAPI_Feature::feature(anAttr->object());
106 if (aFeature && aFeature->getKind() == SketchPlugin_Line::ID()) {
113 //*************************************************************************************
114 std::shared_ptr<GeomAPI_Pnt2d> getProjectionPoint(const FeaturePtr theLine,
115 const std::shared_ptr<GeomAPI_Pnt2d>& thePoint)
117 DataPtr aData = theLine->data();
118 std::shared_ptr<GeomDataAPI_Point2D> aPoint1 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
119 aData->attribute(SketchPlugin_Line::START_ID()));
120 std::shared_ptr<GeomDataAPI_Point2D> aPoint2 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
121 aData->attribute(SketchPlugin_Line::END_ID()));
123 GeomAPI_Lin2d aLin2d(aPoint1->x(), aPoint1->y(), aPoint2->x(), aPoint2->y());
124 return aLin2d.project(thePoint);
128 static double MyArrowSize = 30.;
129 double getArrowSize()
134 void setArrowSize(double theSize)
136 MyArrowSize = theSize;
139 double getFlyoutDistance(const ModelAPI_Feature* theConstraint)
141 std::shared_ptr<GeomDataAPI_Point2D> aFlyoutPoint =
142 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
143 const_cast<ModelAPI_Feature*>(theConstraint)->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
145 return aFlyoutPoint->y();
148 std::shared_ptr<GeomAPI_Pnt> getAnchorPoint(const ModelAPI_Feature* theConstraint,
149 const std::shared_ptr<GeomAPI_Ax3>& thePlane)
151 ModelAPI_Feature* aConstraint = const_cast<ModelAPI_Feature*>(theConstraint);
152 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
153 aConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
154 if (!aRefAttr || !aRefAttr->isObject() || !aRefAttr->object())
155 return std::shared_ptr<GeomAPI_Pnt>();
157 FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttr->object());
158 std::shared_ptr<GeomAPI_Pnt2d> aCenter;
159 if (aFeature->getKind() == SketchPlugin_Arc::ID()) {
160 aCenter = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
161 aFeature->attribute(SketchPlugin_Arc::CENTER_ID()))->pnt();
162 } else if (aFeature->getKind() == SketchPlugin_Circle::ID()) {
163 aCenter = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
164 aFeature->attribute(SketchPlugin_Circle::CENTER_ID()))->pnt();
166 return std::shared_ptr<GeomAPI_Pnt>();
168 std::shared_ptr<GeomAPI_Pnt2d> anOrigin(new GeomAPI_Pnt2d(0.0, 0.0));
169 std::shared_ptr<GeomAPI_Pnt2d> aFlyoutPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
170 aConstraint->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()))->pnt();
171 double aRadius = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
172 aConstraint->attribute(SketchPlugin_Constraint::VALUE()))->value();
173 double aLen = aFlyoutPnt->distance(anOrigin);
174 aRadius *= 1.001; // a gap to make point much closer to the circle, but not lying on it
175 aFlyoutPnt->setX(aCenter->x() + aFlyoutPnt->x() * aRadius / aLen);
176 aFlyoutPnt->setY(aCenter->y() + aFlyoutPnt->y() * aRadius / aLen);
177 return thePlane->to3D(aFlyoutPnt->x(), aFlyoutPnt->y());