1 // Copyright (C) 2017 CEA/DEN, EDF R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
21 // File: PartSet_ExternalPointsMgr.cpp
22 // Created: 26 April 2017
23 // Author: Vitaly SMETANNIKOV
25 #include "PartSet_ExternalPointsMgr.h"
26 #include "PartSet_CenterPrs.h"
27 #include "PartSet_Tools.h"
29 #include <ModelAPI_Tools.h>
30 #include <ModelAPI_ResultField.h>
31 #include <ModelAPI_ResultGroup.h>
33 #include <ModuleBase_IWorkshop.h>
34 #include <ModuleBase_ViewerPrs.h>
36 #include <GeomAPI_Circ.h>
37 #include <GeomAPI_Edge.h>
38 #include <GeomAPI_Ellipse.h>
39 #include <GeomAPI_Pnt.h>
40 #include <GeomAPI_ShapeExplorer.h>
42 #include <XGUI_Tools.h>
43 #include <XGUI_Displayer.h>
44 #include <XGUI_Workshop.h>
45 #include <XGUI_SelectionActivate.h>
46 #include <XGUI_SelectionMgr.h>
48 PartSet_ExternalPointsMgr::PartSet_ExternalPointsMgr(ModuleBase_IWorkshop* theWorkshop,
49 const CompositeFeaturePtr& theSketch)
50 : QObject(theWorkshop), myWorkshop(theWorkshop), mySketch(theSketch)
52 XGUI_Workshop* aWorkshop = XGUI_Tools::workshop(myWorkshop);
53 XGUI_Displayer* aDisplayer = aWorkshop->displayer();
54 connect(aDisplayer, SIGNAL(objectDisplayed(ObjectPtr, AISObjectPtr)),
55 SLOT(onDisplayObject(ObjectPtr, AISObjectPtr)));
57 connect(aDisplayer, SIGNAL(beforeObjectErase(ObjectPtr, AISObjectPtr)),
58 SLOT(onEraseObject(ObjectPtr, AISObjectPtr)));
60 updateCenterPresentations();
64 PartSet_ExternalPointsMgr::~PartSet_ExternalPointsMgr()
66 if (myPresentations.isEmpty())
68 XGUI_Workshop* aWorkshop = XGUI_Tools::workshop(myWorkshop);
72 XGUI_Displayer* aDisplayer = aWorkshop->displayer();
73 QMapIterator<ObjectPtr, ListOfAIS> aIt(myPresentations);
74 while (aIt.hasNext()) {
76 ListOfAIS aAISList = aIt.value();
77 foreach (AISObjectPtr aAIS, aAISList) {
78 aDisplayer->eraseAIS(aAIS, false);
84 //******************************************************
85 QList<std::shared_ptr<ModuleBase_ViewerPrs>> PartSet_ExternalPointsMgr::findCircularEdgesInPlane()
87 QList<std::shared_ptr<ModuleBase_ViewerPrs>> aResult;
88 XGUI_Workshop* aWorkshop = XGUI_Tools::workshop(myWorkshop);
89 XGUI_Displayer* aDisplayer = aWorkshop->displayer();
90 QObjectPtrList aDispObjects = aDisplayer->displayedObjects();
92 std::shared_ptr<GeomAPI_Pln> aPlane = plane();
93 foreach(ObjectPtr aObj, aDispObjects) {
94 if (myPresentations.contains(aObj))
97 // Do not process objects of the current sketch
98 if (isSketchObject(aObj))
101 ResultPtr aResObj = std::dynamic_pointer_cast<ModelAPI_Result>(aObj);
103 // Do not use Fields and groups in selection in sketcher
104 if ((aResObj->groupName() == ModelAPI_ResultField::group()) ||
105 (aResObj->groupName() == ModelAPI_ResultGroup::group()))
109 GeomShapePtr aShape = aResObj->shape();
111 GeomAPI_ShapeExplorer aExplorer(aShape, GeomAPI_Shape::EDGE);
112 for(; aExplorer.more(); aExplorer.next()) {
113 GeomShapePtr aEdgeShape = aExplorer.current();
114 GeomAPI_Edge anEdge(aEdgeShape);
115 if ((anEdge.isCircle() || anEdge.isArc() || anEdge.isEllipse()) &&
116 anEdge.isInPlane(aPlane)) {
117 bool isContains = false;
118 // Check that edge is not already used.
119 // It is possible that the same edge will be taken from different faces
120 foreach(std::shared_ptr<ModuleBase_ViewerPrs> aPrs, aResult) {
121 GeomAPI_Edge aUsedEdge(aPrs->shape());
122 if (aUsedEdge.isEqual(aEdgeShape)) {
128 std::shared_ptr<ModuleBase_ViewerPrs>
129 aPrs(new ModuleBase_ViewerPrs(aResObj, aEdgeShape));
130 aResult.append(aPrs);
141 void PartSet_ExternalPointsMgr::updateCenterPresentations()
143 XGUI_Workshop* aWorkshop = XGUI_Tools::workshop(myWorkshop);
144 // Return if there is no plane defined
145 if (!plane().get()) {
146 connect(aWorkshop->selector(), SIGNAL(selectionChanged()),
147 SLOT(onSelectionChanged()));
150 XGUI_Displayer* aDisplayer = aWorkshop->displayer();
152 QList<std::shared_ptr<ModuleBase_ViewerPrs>> aEdgesPrs = findCircularEdgesInPlane();
153 foreach(std::shared_ptr<ModuleBase_ViewerPrs> aPrs, aEdgesPrs) {
154 GeomEdgePtr aEdge(new GeomAPI_Edge(aPrs->shape()));
156 if (aEdge->isArc() || aEdge->isCircle()) {
157 GeomCirclePtr aCircle = aEdge->circle();
158 GeomPointPtr aCenter = aCircle->center();
159 Handle(PartSet_CenterPrs) aCentPrs =
160 new PartSet_CenterPrs(aPrs->object(), aEdge, aCenter->impl<gp_Pnt>(),
161 ModelAPI_AttributeSelection::CIRCLE_CENTER);
163 AISObjectPtr anAIS(new GeomAPI_AISObject());
164 anAIS->setImpl(new Handle(AIS_InteractiveObject)(aCentPrs));
166 } else if (aEdge->isEllipse()) {
167 GeomEllipsePtr aEllipse = aEdge->ellipse();
168 GeomPointPtr aF1 = aEllipse->firstFocus();
169 GeomPointPtr aF2 = aEllipse->secondFocus();
170 Handle(PartSet_CenterPrs) aF1Prs =
171 new PartSet_CenterPrs(aPrs->object(), aEdge, aF1->impl<gp_Pnt>(),
172 ModelAPI_AttributeSelection::ELLIPSE_FIRST_FOCUS);
173 Handle(PartSet_CenterPrs) aF2Prs =
174 new PartSet_CenterPrs(aPrs->object(), aEdge, aF2->impl<gp_Pnt>(),
175 ModelAPI_AttributeSelection::ELLIPSE_SECOND_FOCUS);
177 AISObjectPtr anAIS1(new GeomAPI_AISObject());
178 anAIS1->setImpl(new Handle(AIS_InteractiveObject)(aF1Prs));
179 aList.append(anAIS1);
181 AISObjectPtr anAIS2(new GeomAPI_AISObject());
182 anAIS2->setImpl(new Handle(AIS_InteractiveObject)(aF2Prs));
183 aList.append(anAIS2);
185 if (myPresentations.contains(aPrs->object()))
186 myPresentations[aPrs->object()].append(aList);
188 myPresentations[aPrs->object()] = aList;
189 foreach(AISObjectPtr anAIS, aList) {
190 aDisplayer->displayAIS(anAIS, false);
191 aWorkshop->selectionActivate()->activateAIS(anAIS->impl<Handle(AIS_InteractiveObject)>(),
192 TopAbs_VERTEX, false);
197 std::shared_ptr<GeomAPI_Pln> PartSet_ExternalPointsMgr::plane() const
199 return PartSet_Tools::sketchPlane(mySketch);
202 void PartSet_ExternalPointsMgr::onDisplayObject(ObjectPtr theObj, AISObjectPtr theAIS)
204 updateCenterPresentations();
207 void PartSet_ExternalPointsMgr::onEraseObject(ObjectPtr theObj, AISObjectPtr theAIS)
209 if (myPresentations.contains(theObj)) {
210 XGUI_Workshop* aWorkshop = XGUI_Tools::workshop(myWorkshop);
211 XGUI_Displayer* aDisplayer = aWorkshop->displayer();
212 ListOfAIS aList = myPresentations[theObj];
213 foreach(AISObjectPtr aAIS, aList) {
214 aDisplayer->eraseAIS(aAIS, false);
216 myPresentations.remove(theObj);
217 aDisplayer->updateViewer();
222 bool PartSet_ExternalPointsMgr::isSketchObject(const ObjectPtr& theRes) const
224 FeaturePtr aFeature = ModelAPI_Feature::feature(theRes);
227 CompositeFeaturePtr aComp = ModelAPI_Tools::compositeOwner(aFeature);
228 return aComp == mySketch;
231 void PartSet_ExternalPointsMgr::onSelectionChanged()
234 XGUI_Workshop* aWorkshop = XGUI_Tools::workshop(myWorkshop);
235 disconnect(aWorkshop->selector(), SIGNAL(selectionChanged()),
236 this, SLOT(onSelectionChanged()));
237 updateCenterPresentations();