1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
3 // File: SketchPlugin_ConstraintCoincidence.cpp
4 // Created: 08 May 2014
5 // Author: Artem ZHIDKOV
7 #include "SketchPlugin_ConstraintCoincidence.h"
9 #include <SketchPlugin_Arc.h>
10 #include <SketchPlugin_Circle.h>
11 #include <SketchPlugin_Line.h>
12 #include <SketchPlugin_Point.h>
14 #include <SketcherPrs_Factory.h>
16 #include <ModelAPI_AttributeDouble.h>
17 #include <ModelAPI_Data.h>
18 #include <GeomDataAPI_Dir.h>
19 #include <GeomDataAPI_Point.h>
20 #include <GeomDataAPI_Point2D.h>
22 #include <GeomAPI_Circ2d.h>
23 #include <GeomAPI_Dir2d.h>
24 #include <GeomAPI_Lin2d.h>
26 /// \brief Update coordinates of the point to be correctly placed on the feature
27 static void adjustPointOnEntity(std::shared_ptr<GeomDataAPI_Point2D> thePoint, FeaturePtr theFeature);
29 SketchPlugin_ConstraintCoincidence::SketchPlugin_ConstraintCoincidence()
33 void SketchPlugin_ConstraintCoincidence::initAttributes()
35 data()->addAttribute(SketchPlugin_Constraint::ENTITY_A(), ModelAPI_AttributeRefAttr::typeId());
36 data()->addAttribute(SketchPlugin_Constraint::ENTITY_B(), ModelAPI_AttributeRefAttr::typeId());
39 void SketchPlugin_ConstraintCoincidence::execute()
43 AISObjectPtr SketchPlugin_ConstraintCoincidence::getAISObject(AISObjectPtr thePrevious)
48 AISObjectPtr anAIS = thePrevious;
50 anAIS = SketcherPrs_Factory::coincidentConstraint(this, sketch()->coordinatePlane());
55 void SketchPlugin_ConstraintCoincidence::attributeChanged(const std::string& theID)
57 if (theID == ENTITY_A() || theID == ENTITY_B()) {
58 AttributeRefAttrPtr anAttrA =
59 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(attribute(ENTITY_A()));
60 AttributeRefAttrPtr anAttrB =
61 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(attribute(ENTITY_B()));
63 if (!anAttrA || !anAttrB || (!anAttrA->isObject() && !anAttrB->isObject()))
66 if (anAttrA->isObject()) {
67 AttributeRefAttrPtr aTransient = anAttrA;
72 std::shared_ptr<GeomDataAPI_Point2D> aPoint =
73 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttrA->attr());
75 if (anAttrB->object())
76 aFeature = ModelAPI_Feature::feature(anAttrB->object());
78 // Adjust position of a point lying on a line or arc
79 if (aPoint && aFeature)
80 adjustPointOnEntity(aPoint, aFeature);
87 // ======= Auxiliary functions ================================================
88 void adjustPointOnEntity(std::shared_ptr<GeomDataAPI_Point2D> thePoint, FeaturePtr theFeature)
90 if (theFeature->getKind() == SketchPlugin_Line::ID()) {
91 // project point on line
92 std::shared_ptr<GeomDataAPI_Point2D> aStart = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
93 theFeature->attribute(SketchPlugin_Line::START_ID()));
94 std::shared_ptr<GeomDataAPI_Point2D> aEnd = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
95 theFeature->attribute(SketchPlugin_Line::END_ID()));
97 std::shared_ptr<GeomAPI_Lin2d> aLine(new GeomAPI_Lin2d(aStart->pnt(), aEnd->pnt()));
98 std::shared_ptr<GeomAPI_Pnt2d> aProjected = aLine->project(thePoint->pnt());
99 thePoint->setValue(aProjected);
101 else if (theFeature->getKind() == SketchPlugin_Circle::ID()) {
102 // project point on circle
103 std::shared_ptr<GeomDataAPI_Point2D> aCenter = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
104 theFeature->attribute(SketchPlugin_Circle::CENTER_ID()));
105 double aRadius = theFeature->real(SketchPlugin_Circle::RADIUS_ID())->value();
107 std::shared_ptr<GeomAPI_Dir2d> aDir(new GeomAPI_Dir2d(1.0, 0.0));
108 std::shared_ptr<GeomAPI_Circ2d> aCircle(new GeomAPI_Circ2d(aCenter->pnt(), aDir, aRadius));
110 std::shared_ptr<GeomAPI_Pnt2d> aProjected = aCircle->project(thePoint->pnt());
111 thePoint->setValue(aProjected);
113 else if (theFeature->getKind() == SketchPlugin_Arc::ID()) {
114 // project point on arc
115 std::shared_ptr<GeomDataAPI_Point2D> aCenter = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
116 theFeature->attribute(SketchPlugin_Arc::CENTER_ID()));
117 std::shared_ptr<GeomDataAPI_Point2D> aStart = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
118 theFeature->attribute(SketchPlugin_Arc::START_ID()));
120 std::shared_ptr<GeomAPI_Circ2d> aCircle(new GeomAPI_Circ2d(aCenter->pnt(), aStart->pnt()));
121 std::shared_ptr<GeomAPI_Pnt2d> aProjected = aCircle->project(thePoint->pnt());
122 thePoint->setValue(aProjected);