]> SALOME platform Git repositories - modules/shaper.git/blob - src/SketchPlugin/SketchPlugin_ConstraintCoincidence.cpp
Salome HOME
Merge branch 'Pre_2.8.0_development'
[modules/shaper.git] / src / SketchPlugin / SketchPlugin_ConstraintCoincidence.cpp
1 // Copyright (C) 2014-2017  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
19 //
20
21 #include "SketchPlugin_ConstraintCoincidence.h"
22
23 #include <SketchPlugin_Point.h>
24 #include <SketchPlugin_Line.h>
25 #include <SketchPlugin_Arc.h>
26 #include <SketchPlugin_Circle.h>
27
28 #include <ModelAPI_Events.h>
29 #include <ModelGeomAlgo_Point2D.h>
30 #include <GeomDataAPI_Point2D.h>
31
32 #include <SketcherPrs_Factory.h>
33
34 #include <Events_Loop.h>
35
36 SketchPlugin_ConstraintCoincidence::SketchPlugin_ConstraintCoincidence()
37 {
38 }
39
40 void SketchPlugin_ConstraintCoincidence::initAttributes()
41 {
42   data()->addAttribute(SketchPlugin_Constraint::ENTITY_A(), ModelAPI_AttributeRefAttr::typeId());
43   data()->addAttribute(SketchPlugin_Constraint::ENTITY_B(), ModelAPI_AttributeRefAttr::typeId());
44 }
45
46 void SketchPlugin_ConstraintCoincidence::execute()
47 {
48 }
49
50 AISObjectPtr SketchPlugin_ConstraintCoincidence::getAISObject(AISObjectPtr thePrevious)
51 {
52   if (!sketch())
53     return thePrevious;
54
55   AISObjectPtr anAIS = SketcherPrs_Factory::coincidentConstraint(this, sketch()->coordinatePlane(),
56                                                                  thePrevious);
57   return anAIS;
58 }
59
60 FeaturePtr SketchPlugin_ConstraintCoincidence::findCoincidenceFeature(const FeaturePtr& theFeature1,
61                                                                       const FeaturePtr& theFeature2)
62 {
63   FeaturePtr aResultFeature;
64
65   std::list<AttributePtr> anAttrList;
66   if (theFeature1->getKind() == SketchPlugin_Circle::ID() ||
67       theFeature2->getKind() == SketchPlugin_Circle::ID())
68     return aResultFeature;
69
70   if (theFeature2->getKind() == SketchPlugin_Line::ID()) {
71     anAttrList.push_back(theFeature2->attribute(SketchPlugin_Line::START_ID()));
72     anAttrList.push_back(theFeature2->attribute(SketchPlugin_Line::END_ID()));
73   } else if (theFeature2->getKind() == SketchPlugin_Arc::ID()) {
74     anAttrList.push_back(theFeature2->attribute(SketchPlugin_Arc::START_ID()));
75     anAttrList.push_back(theFeature2->attribute(SketchPlugin_Arc::END_ID()));
76   }
77
78   const std::set<AttributePtr>& aRefsList = theFeature1->data()->refsToMe();
79   std::set<AttributePtr>::const_iterator aRefIt = aRefsList.begin();
80   for (; aRefIt != aRefsList.end() && !aResultFeature.get(); ++aRefIt) {
81     FeaturePtr aConstrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>((*aRefIt)->owner());
82     if (aConstrFeature->getKind() != SketchPlugin_ConstraintCoincidence::ID())
83       continue;
84     AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(*aRefIt);
85     AttributePtr anAttr = aRefAttr->attr();
86     if (anAttr->id() == SketchPlugin_Arc::CENTER_ID())
87       continue;
88
89     anAttr = aConstrFeature->attribute(SketchPlugin_Constraint::ENTITY_A());
90     if (anAttr == *aRefIt)
91       anAttr = aConstrFeature->attribute(SketchPlugin_Constraint::ENTITY_B());
92
93     aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(anAttr);
94     if (!aRefAttr)
95       continue;
96     anAttr = aRefAttr->attr();
97     for (std::list<AttributePtr>::const_iterator anIt = anAttrList.begin();
98          anIt != anAttrList.end() && !aResultFeature.get(); ++anIt)
99       if (*anIt == anAttr)
100         aResultFeature = aConstrFeature;
101   }
102   return aResultFeature;
103 }
104
105 AttributePoint2DPtr SketchPlugin_ConstraintCoincidence::getPoint(const FeaturePtr& theFeature)
106 {
107   AttributePoint2DPtr aPoint = ModelGeomAlgo_Point2D::getPointOfRefAttr(theFeature.get(),
108                                                          SketchPlugin_Constraint::ENTITY_A(),
109                                  SketchPlugin_Point::ID(), SketchPlugin_Point::COORD_ID());
110   if (!aPoint.get())
111     aPoint = ModelGeomAlgo_Point2D::getPointOfRefAttr(theFeature.get(),
112                                                       SketchPlugin_Constraint::ENTITY_B(),
113                                  SketchPlugin_Point::ID(), SketchPlugin_Point::COORD_ID());
114   return aPoint;
115 }
116
117 void SketchPlugin_ConstraintCoincidence::createCoincidenceFeature(SketchPlugin_Sketch* theSketch,
118                                            const std::shared_ptr<GeomDataAPI_Point2D>& thePoint1,
119                                            const std::shared_ptr<GeomDataAPI_Point2D>& thePoint2)
120 {
121   FeaturePtr aFeature;
122   if (theSketch) {
123     aFeature = theSketch->addFeature(SketchPlugin_ConstraintCoincidence::ID());
124   } else {
125     std::shared_ptr<ModelAPI_Document> aDoc = theSketch->document();
126     aFeature = aDoc->addFeature(SketchPlugin_ConstraintCoincidence::ID());
127   }
128
129   std::shared_ptr<ModelAPI_Data> aData = aFeature->data();
130
131   std::shared_ptr<ModelAPI_AttributeRefAttr> aRef1 = std::dynamic_pointer_cast<
132       ModelAPI_AttributeRefAttr>(aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
133   aRef1->setAttr(thePoint1);
134
135   std::shared_ptr<ModelAPI_AttributeRefAttr> aRef2 = std::dynamic_pointer_cast<
136       ModelAPI_AttributeRefAttr>(aData->attribute(SketchPlugin_Constraint::ENTITY_B()));
137   aRef2->setAttr(thePoint2);
138
139   // we need to flush created signal in order to coincidence is processed by solver
140   Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_CREATED));
141 }