Salome HOME
Merge branch 'Pre_2.8.0_development'
[modules/shaper.git] / src / PartSet / PartSet_ExternalPointsMgr.cpp
1 // Copyright (C) 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 // File:        PartSet_ExternalPointsMgr.cpp
22 // Created:     26 April 2017
23 // Author:      Vitaly SMETANNIKOV
24
25 #include "PartSet_ExternalPointsMgr.h"
26 #include "PartSet_CenterPrs.h"
27 #include "PartSet_Tools.h"
28
29 #include <ModelAPI_Tools.h>
30
31 #include <ModuleBase_IWorkshop.h>
32 #include <ModuleBase_ViewerPrs.h>
33
34 #include <GeomAPI_Circ.h>
35 #include <GeomAPI_Edge.h>
36 #include <GeomAPI_Ellipse.h>
37 #include <GeomAPI_Pnt.h>
38 #include <GeomAPI_ShapeExplorer.h>
39
40 #include <XGUI_Tools.h>
41 #include <XGUI_Displayer.h>
42 #include <XGUI_Workshop.h>
43 #include <XGUI_SelectionMgr.h>
44
45 PartSet_ExternalPointsMgr::PartSet_ExternalPointsMgr(ModuleBase_IWorkshop* theWorkshop,
46                                                      const CompositeFeaturePtr& theSketch)
47   : QObject(theWorkshop), myWorkshop(theWorkshop), mySketch(theSketch)
48 {
49   XGUI_Workshop* aWorkshop = XGUI_Tools::workshop(myWorkshop);
50   XGUI_Displayer* aDisplayer = aWorkshop->displayer();
51   connect(aDisplayer, SIGNAL(objectDisplayed(ObjectPtr, AISObjectPtr)),
52                       SLOT(onDisplayObject(ObjectPtr, AISObjectPtr)));
53
54   connect(aDisplayer, SIGNAL(beforeObjectErase(ObjectPtr, AISObjectPtr)),
55                       SLOT(onEraseObject(ObjectPtr, AISObjectPtr)));
56
57   updateCenterPresentations();
58 }
59
60
61 PartSet_ExternalPointsMgr::~PartSet_ExternalPointsMgr()
62 {
63   if (myPresentations.isEmpty())
64     return;
65   XGUI_Workshop* aWorkshop = XGUI_Tools::workshop(myWorkshop);
66   if (!aWorkshop)
67     return;
68
69   XGUI_Displayer* aDisplayer = aWorkshop->displayer();
70   QMapIterator<ObjectPtr, ListOfAIS> aIt(myPresentations);
71   while (aIt.hasNext()) {
72     aIt.next();
73     ListOfAIS aAISList = aIt.value();
74     foreach (AISObjectPtr aAIS, aAISList) {
75       aDisplayer->eraseAIS(aAIS, false);
76     }
77   }
78 }
79
80
81 //******************************************************
82 QList<std::shared_ptr<ModuleBase_ViewerPrs>> PartSet_ExternalPointsMgr::findCircularEdgesInPlane()
83 {
84   QList<std::shared_ptr<ModuleBase_ViewerPrs>> aResult;
85   XGUI_Workshop* aWorkshop = XGUI_Tools::workshop(myWorkshop);
86   XGUI_Displayer* aDisplayer = aWorkshop->displayer();
87   QObjectPtrList aDispObjects = aDisplayer->displayedObjects();
88
89   std::shared_ptr<GeomAPI_Pln> aPlane = plane();
90   foreach(ObjectPtr aObj, aDispObjects) {
91     if (myPresentations.contains(aObj))
92       continue;
93
94     // Do not process objects of the current sketch
95     if (isSketchObject(aObj))
96       continue;
97
98     ResultPtr aResObj = std::dynamic_pointer_cast<ModelAPI_Result>(aObj);
99     if (aResObj.get()) {
100       GeomShapePtr aShape = aResObj->shape();
101       if (aShape.get()) {
102         GeomAPI_ShapeExplorer aExplorer(aShape, GeomAPI_Shape::EDGE);
103         for(; aExplorer.more(); aExplorer.next()) {
104           GeomShapePtr aEdgeShape = aExplorer.current();
105           GeomAPI_Edge anEdge(aEdgeShape);
106           if ((anEdge.isCircle() || anEdge.isArc() || anEdge.isEllipse()) &&
107                anEdge.isInPlane(aPlane)) {
108             bool isContains = false;
109             // Check that edge is not already used.
110             // It is possible that the same edge will be taken from different faces
111             foreach(std::shared_ptr<ModuleBase_ViewerPrs> aPrs, aResult) {
112               GeomAPI_Edge aUsedEdge(aPrs->shape());
113               if (aUsedEdge.isEqual(aEdgeShape)) {
114                 isContains = true;
115                 break;
116               }
117             }
118             if (!isContains) {
119               std::shared_ptr<ModuleBase_ViewerPrs>
120                 aPrs(new ModuleBase_ViewerPrs(aResObj, aEdgeShape));
121               aResult.append(aPrs);
122             }
123           }
124         }
125       }
126     }
127   }
128   return aResult;
129 }
130
131
132 void PartSet_ExternalPointsMgr::updateCenterPresentations()
133 {
134   XGUI_Workshop* aWorkshop = XGUI_Tools::workshop(myWorkshop);
135   // Return if there is no plane defined
136   if (!plane().get()) {
137     connect(aWorkshop->selector(), SIGNAL(selectionChanged()),
138                                    SLOT(onSelectionChanged()));
139     return;
140   }
141   XGUI_Displayer* aDisplayer = aWorkshop->displayer();
142
143   QList<std::shared_ptr<ModuleBase_ViewerPrs>> aEdgesPrs = findCircularEdgesInPlane();
144   foreach(std::shared_ptr<ModuleBase_ViewerPrs> aPrs, aEdgesPrs) {
145     GeomEdgePtr aEdge(new GeomAPI_Edge(aPrs->shape()));
146     ListOfAIS aList;
147     if (aEdge->isArc() || aEdge->isCircle()) {
148       GeomCirclePtr aCircle = aEdge->circle();
149       GeomPointPtr aCenter = aCircle->center();
150       Handle(PartSet_CenterPrs) aCentPrs =
151         new PartSet_CenterPrs(aPrs->object(), aEdge, aCenter->impl<gp_Pnt>(),
152                               ModelAPI_AttributeSelection::CIRCLE_CENTER);
153
154       AISObjectPtr anAIS(new GeomAPI_AISObject());
155       anAIS->setImpl(new Handle(AIS_InteractiveObject)(aCentPrs));
156       aList.append(anAIS);
157     } else if (aEdge->isEllipse()) {
158       GeomEllipsePtr aEllipse = aEdge->ellipse();
159       GeomPointPtr aF1 = aEllipse->firstFocus();
160       GeomPointPtr aF2 = aEllipse->secondFocus();
161       Handle(PartSet_CenterPrs) aF1Prs =
162         new PartSet_CenterPrs(aPrs->object(), aEdge, aF1->impl<gp_Pnt>(),
163                               ModelAPI_AttributeSelection::ELLIPSE_FIRST_FOCUS);
164       Handle(PartSet_CenterPrs) aF2Prs =
165         new PartSet_CenterPrs(aPrs->object(), aEdge, aF2->impl<gp_Pnt>(),
166                               ModelAPI_AttributeSelection::ELLIPSE_SECOND_FOCUS);
167
168       AISObjectPtr anAIS1(new GeomAPI_AISObject());
169       anAIS1->setImpl(new Handle(AIS_InteractiveObject)(aF1Prs));
170       aList.append(anAIS1);
171
172       AISObjectPtr anAIS2(new GeomAPI_AISObject());
173       anAIS2->setImpl(new Handle(AIS_InteractiveObject)(aF2Prs));
174       aList.append(anAIS2);
175     }
176     if (myPresentations.contains(aPrs->object()))
177       myPresentations[aPrs->object()].append(aList);
178     else
179       myPresentations[aPrs->object()] = aList;
180     foreach(AISObjectPtr anAIS, aList) {
181       aDisplayer->displayAIS(anAIS, false);
182       aDisplayer->activateAIS(anAIS->impl<Handle(AIS_InteractiveObject)>(), TopAbs_VERTEX, false);
183     }
184   }
185 }
186
187 std::shared_ptr<GeomAPI_Pln> PartSet_ExternalPointsMgr::plane() const
188 {
189   return PartSet_Tools::sketchPlane(mySketch);
190 }
191
192 void PartSet_ExternalPointsMgr::onDisplayObject(ObjectPtr theObj, AISObjectPtr theAIS)
193 {
194   updateCenterPresentations();
195 }
196
197 void PartSet_ExternalPointsMgr::onEraseObject(ObjectPtr theObj, AISObjectPtr theAIS)
198 {
199   if (myPresentations.contains(theObj)) {
200     XGUI_Workshop* aWorkshop = XGUI_Tools::workshop(myWorkshop);
201     XGUI_Displayer* aDisplayer = aWorkshop->displayer();
202     ListOfAIS aList = myPresentations[theObj];
203     foreach(AISObjectPtr aAIS, aList) {
204       aDisplayer->eraseAIS(aAIS, false);
205     }
206     myPresentations.remove(theObj);
207     aDisplayer->updateViewer();
208   }
209 }
210
211
212 bool PartSet_ExternalPointsMgr::isSketchObject(const ObjectPtr& theRes) const
213 {
214   FeaturePtr aFeature = ModelAPI_Feature::feature(theRes);
215   if (!aFeature.get())
216     return false;
217   CompositeFeaturePtr aComp = ModelAPI_Tools::compositeOwner(aFeature);
218   return aComp == mySketch;
219 }
220
221 void PartSet_ExternalPointsMgr::onSelectionChanged()
222 {
223   if (plane().get()) {
224     XGUI_Workshop* aWorkshop = XGUI_Tools::workshop(myWorkshop);
225     disconnect(aWorkshop->selector(), SIGNAL(selectionChanged()),
226                this, SLOT(onSelectionChanged()));
227     updateCenterPresentations();
228   }
229 }