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 = SketcherPrs_Factory::coincidentConstraint(this, sketch()->coordinatePlane(),
53 void SketchPlugin_ConstraintCoincidence::attributeChanged(const std::string& theID)
55 if (theID == ENTITY_A() || theID == ENTITY_B()) {
56 AttributeRefAttrPtr anAttrA =
57 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(attribute(ENTITY_A()));
58 AttributeRefAttrPtr anAttrB =
59 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(attribute(ENTITY_B()));
61 if (!anAttrA || !anAttrB || (!anAttrA->isObject() && !anAttrB->isObject()))
64 if (anAttrA->isObject()) {
65 AttributeRefAttrPtr aTransient = anAttrA;
70 std::shared_ptr<GeomDataAPI_Point2D> aPoint =
71 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttrA->attr());
73 if (anAttrB->object())
74 aFeature = ModelAPI_Feature::feature(anAttrB->object());
76 // Adjust position of a point lying on a line or arc
77 if (aPoint && aFeature)
78 adjustPointOnEntity(aPoint, aFeature);
85 // ======= Auxiliary functions ================================================
86 void adjustPointOnEntity(std::shared_ptr<GeomDataAPI_Point2D> thePoint, FeaturePtr theFeature)
88 if (theFeature->getKind() == SketchPlugin_Line::ID()) {
89 // project point on line
90 std::shared_ptr<GeomDataAPI_Point2D> aStart = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
91 theFeature->attribute(SketchPlugin_Line::START_ID()));
92 std::shared_ptr<GeomDataAPI_Point2D> aEnd = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
93 theFeature->attribute(SketchPlugin_Line::END_ID()));
95 std::shared_ptr<GeomAPI_Lin2d> aLine(new GeomAPI_Lin2d(aStart->pnt(), aEnd->pnt()));
96 std::shared_ptr<GeomAPI_Pnt2d> aProjected = aLine->project(thePoint->pnt());
97 thePoint->setValue(aProjected);
99 else if (theFeature->getKind() == SketchPlugin_Circle::ID()) {
100 // project point on circle
101 std::shared_ptr<GeomDataAPI_Point2D> aCenter = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
102 theFeature->attribute(SketchPlugin_Circle::CENTER_ID()));
103 double aRadius = theFeature->real(SketchPlugin_Circle::RADIUS_ID())->value();
105 std::shared_ptr<GeomAPI_Dir2d> aDir(new GeomAPI_Dir2d(1.0, 0.0));
106 std::shared_ptr<GeomAPI_Circ2d> aCircle(new GeomAPI_Circ2d(aCenter->pnt(), aDir, aRadius));
108 std::shared_ptr<GeomAPI_Pnt2d> aProjected = aCircle->project(thePoint->pnt());
109 thePoint->setValue(aProjected);
111 else if (theFeature->getKind() == SketchPlugin_Arc::ID()) {
112 // project point on arc
113 std::shared_ptr<GeomDataAPI_Point2D> aCenter = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
114 theFeature->attribute(SketchPlugin_Arc::CENTER_ID()));
115 std::shared_ptr<GeomDataAPI_Point2D> aStart = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
116 theFeature->attribute(SketchPlugin_Arc::START_ID()));
118 std::shared_ptr<GeomAPI_Circ2d> aCircle(new GeomAPI_Circ2d(aCenter->pnt(), aStart->pnt()));
119 std::shared_ptr<GeomAPI_Pnt2d> aProjected = aCircle->project(thePoint->pnt());
120 thePoint->setValue(aProjected);