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.get() ||
105 (aResObj->groupName() == ModelAPI_ResultField::group()) ||
106 (aResObj->groupName() == ModelAPI_ResultGroup::group()))
110 GeomShapePtr aShape = aResObj->shape();
112 GeomAPI_ShapeExplorer aExplorer(aShape, GeomAPI_Shape::EDGE);
113 for(; aExplorer.more(); aExplorer.next()) {
114 GeomShapePtr aEdgeShape = aExplorer.current();
115 GeomAPI_Edge anEdge(aEdgeShape);
116 if ((anEdge.isCircle() || anEdge.isArc() || anEdge.isEllipse()) &&
117 anEdge.isInPlane(aPlane)) {
118 bool isContains = false;
119 // Check that edge is not already used.
120 // It is possible that the same edge will be taken from different faces
121 foreach(std::shared_ptr<ModuleBase_ViewerPrs> aPrs, aResult) {
122 GeomAPI_Edge aUsedEdge(aPrs->shape());
123 if (aUsedEdge.isEqual(aEdgeShape)) {
129 std::shared_ptr<ModuleBase_ViewerPrs>
130 aPrs(new ModuleBase_ViewerPrs(aResObj, aEdgeShape));
131 aResult.append(aPrs);
142 void PartSet_ExternalPointsMgr::updateCenterPresentations()
144 XGUI_Workshop* aWorkshop = XGUI_Tools::workshop(myWorkshop);
145 // Return if there is no plane defined
146 if (!plane().get()) {
147 connect(aWorkshop->selector(), SIGNAL(selectionChanged()),
148 SLOT(onSelectionChanged()));
151 XGUI_Displayer* aDisplayer = aWorkshop->displayer();
153 QList<std::shared_ptr<ModuleBase_ViewerPrs>> aEdgesPrs = findCircularEdgesInPlane();
154 foreach(std::shared_ptr<ModuleBase_ViewerPrs> aPrs, aEdgesPrs) {
155 GeomEdgePtr aEdge(new GeomAPI_Edge(aPrs->shape()));
157 if (aEdge->isArc() || aEdge->isCircle()) {
158 GeomCirclePtr aCircle = aEdge->circle();
159 GeomPointPtr aCenter = aCircle->center();
160 Handle(PartSet_CenterPrs) aCentPrs =
161 new PartSet_CenterPrs(aPrs->object(), aEdge, aCenter->impl<gp_Pnt>(),
162 ModelAPI_AttributeSelection::CIRCLE_CENTER);
164 AISObjectPtr anAIS(new GeomAPI_AISObject());
165 anAIS->setImpl(new Handle(AIS_InteractiveObject)(aCentPrs));
167 } else if (aEdge->isEllipse()) {
168 GeomEllipsePtr aEllipse = aEdge->ellipse();
169 GeomPointPtr aF1 = aEllipse->firstFocus();
170 GeomPointPtr aF2 = aEllipse->secondFocus();
171 Handle(PartSet_CenterPrs) aF1Prs =
172 new PartSet_CenterPrs(aPrs->object(), aEdge, aF1->impl<gp_Pnt>(),
173 ModelAPI_AttributeSelection::ELLIPSE_FIRST_FOCUS);
174 Handle(PartSet_CenterPrs) aF2Prs =
175 new PartSet_CenterPrs(aPrs->object(), aEdge, aF2->impl<gp_Pnt>(),
176 ModelAPI_AttributeSelection::ELLIPSE_SECOND_FOCUS);
178 AISObjectPtr anAIS1(new GeomAPI_AISObject());
179 anAIS1->setImpl(new Handle(AIS_InteractiveObject)(aF1Prs));
180 aList.append(anAIS1);
182 AISObjectPtr anAIS2(new GeomAPI_AISObject());
183 anAIS2->setImpl(new Handle(AIS_InteractiveObject)(aF2Prs));
184 aList.append(anAIS2);
186 if (myPresentations.contains(aPrs->object()))
187 myPresentations[aPrs->object()].append(aList);
189 myPresentations[aPrs->object()] = aList;
190 foreach(AISObjectPtr anAIS, aList) {
191 aDisplayer->displayAIS(anAIS, false);
192 aWorkshop->selectionActivate()->activateAIS(anAIS->impl<Handle(AIS_InteractiveObject)>(),
193 TopAbs_VERTEX, false);
198 std::shared_ptr<GeomAPI_Pln> PartSet_ExternalPointsMgr::plane() const
200 return PartSet_Tools::sketchPlane(mySketch);
203 void PartSet_ExternalPointsMgr::onDisplayObject(ObjectPtr theObj, AISObjectPtr theAIS)
205 updateCenterPresentations();
208 void PartSet_ExternalPointsMgr::onEraseObject(ObjectPtr theObj, AISObjectPtr theAIS)
210 if (myPresentations.contains(theObj)) {
211 XGUI_Workshop* aWorkshop = XGUI_Tools::workshop(myWorkshop);
212 XGUI_Displayer* aDisplayer = aWorkshop->displayer();
213 ListOfAIS aList = myPresentations[theObj];
214 foreach(AISObjectPtr aAIS, aList) {
215 aDisplayer->eraseAIS(aAIS, false);
217 myPresentations.remove(theObj);
218 aDisplayer->updateViewer();
223 bool PartSet_ExternalPointsMgr::isSketchObject(const ObjectPtr& theRes) const
225 FeaturePtr aFeature = ModelAPI_Feature::feature(theRes);
228 CompositeFeaturePtr aComp = ModelAPI_Tools::compositeOwner(aFeature);
229 return aComp == mySketch;
232 void PartSet_ExternalPointsMgr::onSelectionChanged()
235 XGUI_Workshop* aWorkshop = XGUI_Tools::workshop(myWorkshop);
236 disconnect(aWorkshop->selector(), SIGNAL(selectionChanged()),
237 this, SLOT(onSelectionChanged()));
238 updateCenterPresentations();