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>
14 #include <ModelAPI_ResultConstruction.h>
15 #include <ModelAPI_AttributeRefAttr.h>
17 #include <GeomDataAPI_Point2D.h>
18 #include <GeomAPI_Lin2d.h>
21 namespace SketcherPrs_Tools {
23 ObjectPtr getResult(ModelAPI_Feature* theFeature, const std::string& theAttrName)
25 std::shared_ptr<ModelAPI_Data> aData = theFeature->data();
26 std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = aData->refattr(theAttrName);
27 return anAttr->object();
31 std::shared_ptr<GeomAPI_Shape> getShape(ObjectPtr theObject)
33 ResultConstructionPtr aRes = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theObject);
34 if (aRes.get() != NULL) {
37 return std::shared_ptr<GeomAPI_Shape>();
41 std::shared_ptr<GeomAPI_Pnt2d> getPoint(ModelAPI_Feature* theFeature,
42 const std::string& theAttribute)
44 std::shared_ptr<GeomDataAPI_Point2D> aPointAttr;
46 if (!theFeature->data())
47 return std::shared_ptr<GeomAPI_Pnt2d>();
50 std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = std::dynamic_pointer_cast<
51 ModelAPI_AttributeRefAttr>(theFeature->data()->attribute(theAttribute));
53 aFeature = ModelAPI_Feature::feature(anAttr->object());
55 if (aFeature && aFeature->getKind() == SketchPlugin_Point::ID())
56 aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
57 aFeature->data()->attribute(SketchPlugin_Point::COORD_ID()));
59 else if (aFeature && aFeature->getKind() == SketchPlugin_Circle::ID())
60 aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
61 aFeature->data()->attribute(SketchPlugin_Circle::CENTER_ID()));
63 else if (anAttr->attr()) {
64 aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttr->attr());
66 if (aPointAttr.get() != NULL)
67 return aPointAttr->pnt();
68 return std::shared_ptr<GeomAPI_Pnt2d>();
71 //*************************************************************************************
72 std::shared_ptr<GeomDataAPI_Point2D> getFeaturePoint(DataPtr theData,
73 const std::string& theAttribute)
75 std::shared_ptr<GeomDataAPI_Point2D> aPointAttr;
81 std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = std::dynamic_pointer_cast<
82 ModelAPI_AttributeRefAttr>(theData->attribute(theAttribute));
84 aFeature = ModelAPI_Feature::feature(anAttr->object());
86 if (aFeature && aFeature->getKind() == SketchPlugin_Point::ID())
87 aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
88 aFeature->data()->attribute(SketchPlugin_Point::COORD_ID()));
90 else if (aFeature && aFeature->getKind() == SketchPlugin_Circle::ID())
91 aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
92 aFeature->data()->attribute(SketchPlugin_Circle::CENTER_ID()));
94 else if (anAttr->attr()) {
95 aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttr->attr());
100 //*************************************************************************************
101 FeaturePtr getFeatureLine(DataPtr theData,
102 const std::string& theAttribute)
108 std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr =
109 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theData->attribute(theAttribute));
111 FeaturePtr aFeature = ModelAPI_Feature::feature(anAttr->object());
112 if (aFeature && aFeature->getKind() == SketchPlugin_Line::ID()) {
119 //*************************************************************************************
120 std::shared_ptr<GeomAPI_Pnt2d> getProjectionPoint(const FeaturePtr theLine,
121 const std::shared_ptr<GeomAPI_Pnt2d>& thePoint)
123 DataPtr aData = theLine->data();
124 std::shared_ptr<GeomDataAPI_Point2D> aPoint1 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
125 aData->attribute(SketchPlugin_Line::START_ID()));
126 std::shared_ptr<GeomDataAPI_Point2D> aPoint2 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
127 aData->attribute(SketchPlugin_Line::END_ID()));
129 GeomAPI_Lin2d aLin2d(aPoint1->x(), aPoint1->y(), aPoint2->x(), aPoint2->y());
130 return aLin2d.project(thePoint);
134 static double MyArrowSize = 30.;
135 double getArrowSize()
140 void setArrowSize(double theSize)
142 MyArrowSize = theSize;
145 double getFlyoutDistance(const ModelAPI_Feature* theConstraint,
146 const std::shared_ptr<GeomAPI_Ax3>& thePlane)
148 std::shared_ptr<GeomDataAPI_Point2D> aFlyoutPoint =
149 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
150 const_cast<ModelAPI_Feature*>(theConstraint)->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
152 return aFlyoutPoint->y();