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(SketchPlugin_Constraint* theFeature, const std::string& theAttrName)
25 std::shared_ptr<ModelAPI_Data> aData = theFeature->data();
26 std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr =
27 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aData->attribute(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(SketchPlugin_Constraint* 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 (aFeature && aFeature->getKind() == SketchPlugin_Circle::ID())
62 aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
63 aFeature->data()->attribute(SketchPlugin_Circle::CENTER_ID()));
65 else if (anAttr->attr()) {
66 aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttr->attr());
68 if (aPointAttr.get() != NULL)
69 return aPointAttr->pnt();
70 return std::shared_ptr<GeomAPI_Pnt2d>();
73 //*************************************************************************************
74 std::shared_ptr<GeomDataAPI_Point2D> getFeaturePoint(DataPtr theData,
75 const std::string& theAttribute)
77 std::shared_ptr<GeomDataAPI_Point2D> aPointAttr;
83 std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = std::dynamic_pointer_cast<
84 ModelAPI_AttributeRefAttr>(theData->attribute(theAttribute));
86 aFeature = ModelAPI_Feature::feature(anAttr->object());
88 if (aFeature && aFeature->getKind() == SketchPlugin_Point::ID())
89 aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
90 aFeature->data()->attribute(SketchPlugin_Point::COORD_ID()));
92 else if (aFeature && aFeature->getKind() == SketchPlugin_Circle::ID())
93 aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
94 aFeature->data()->attribute(SketchPlugin_Circle::CENTER_ID()));
96 else if (anAttr->attr()) {
97 aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttr->attr());
102 //*************************************************************************************
103 std::shared_ptr<SketchPlugin_Line> getFeatureLine(DataPtr theData,
104 const std::string& theAttribute)
106 std::shared_ptr<SketchPlugin_Line> aLine;
110 std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = std::dynamic_pointer_cast<
111 ModelAPI_AttributeRefAttr>(theData->attribute(theAttribute));
113 FeaturePtr aFeature = ModelAPI_Feature::feature(anAttr->object());
114 if (aFeature && aFeature->getKind() == SketchPlugin_Line::ID()) {
115 aLine = std::dynamic_pointer_cast<SketchPlugin_Line>(aFeature);
121 //*************************************************************************************
122 std::shared_ptr<GeomAPI_Pnt2d> getProjectionPoint(
123 const std::shared_ptr<SketchPlugin_Line>& theLine,
124 const std::shared_ptr<GeomAPI_Pnt2d>& thePoint)
126 std::shared_ptr<ModelAPI_Data> aData = theLine->data();
127 std::shared_ptr<GeomDataAPI_Point2D> aPoint1 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
128 aData->attribute(SketchPlugin_Line::START_ID()));
129 std::shared_ptr<GeomDataAPI_Point2D> aPoint2 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
130 aData->attribute(SketchPlugin_Line::END_ID()));
132 GeomAPI_Lin2d aLin2d(aPoint1->x(), aPoint1->y(), aPoint2->x(), aPoint2->y());
133 return aLin2d.project(thePoint);