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 (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 FeaturePtr getFeatureLine(DataPtr theData,
104 const std::string& theAttribute)
110 std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr =
111 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theData->attribute(theAttribute));
113 FeaturePtr aFeature = ModelAPI_Feature::feature(anAttr->object());
114 if (aFeature && aFeature->getKind() == SketchPlugin_Line::ID()) {
121 //*************************************************************************************
122 std::shared_ptr<GeomAPI_Pnt2d> getProjectionPoint(const FeaturePtr theLine,
123 const std::shared_ptr<GeomAPI_Pnt2d>& thePoint)
125 DataPtr aData = theLine->data();
126 std::shared_ptr<GeomDataAPI_Point2D> aPoint1 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
127 aData->attribute(SketchPlugin_Line::START_ID()));
128 std::shared_ptr<GeomDataAPI_Point2D> aPoint2 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
129 aData->attribute(SketchPlugin_Line::END_ID()));
131 GeomAPI_Lin2d aLin2d(aPoint1->x(), aPoint1->y(), aPoint2->x(), aPoint2->y());
132 return aLin2d.project(thePoint);
136 static double MyArrowSize = 30.;
137 double getArrowSize()
142 void setArrowSize(double theSize)
144 MyArrowSize = theSize;
147 double getFlyoutDistance(const ModelAPI_Feature* theConstraint)
149 std::shared_ptr<GeomDataAPI_Point2D> aFlyoutPoint =
150 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
151 const_cast<ModelAPI_Feature*>(theConstraint)->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
153 return aFlyoutPoint->y();
156 std::shared_ptr<GeomAPI_Pnt> getAnchorPoint(const ModelAPI_Feature* theConstraint,
157 const std::shared_ptr<GeomAPI_Ax3>& thePlane)
159 ModelAPI_Feature* aConstraint = const_cast<ModelAPI_Feature*>(theConstraint);
160 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
161 aConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
162 if (!aRefAttr || !aRefAttr->isObject() || !aRefAttr->object())
163 return std::shared_ptr<GeomAPI_Pnt>();
165 FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttr->object());
166 std::shared_ptr<GeomAPI_Pnt2d> aCenter;
167 if (aFeature->getKind() == SketchPlugin_Arc::ID()) {
168 aCenter = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
169 aFeature->attribute(SketchPlugin_Arc::CENTER_ID()))->pnt();
170 } else if (aFeature->getKind() == SketchPlugin_Circle::ID()) {
171 aCenter = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
172 aFeature->attribute(SketchPlugin_Circle::CENTER_ID()))->pnt();
174 return std::shared_ptr<GeomAPI_Pnt>();
176 std::shared_ptr<GeomAPI_Pnt2d> anOrigin(new GeomAPI_Pnt2d(0.0, 0.0));
177 std::shared_ptr<GeomAPI_Pnt2d> aFlyoutPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
178 aConstraint->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()))->pnt();
179 double aRadius = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
180 aConstraint->attribute(SketchPlugin_Constraint::VALUE()))->value();
181 double aLen = aFlyoutPnt->distance(anOrigin);
182 aFlyoutPnt->setX(aCenter->x() + aFlyoutPnt->x() * aRadius / aLen);
183 aFlyoutPnt->setY(aCenter->y() + aFlyoutPnt->y() * aRadius / aLen);
184 return thePlane->to3D(aFlyoutPnt->x(), aFlyoutPnt->y());