1 // Copyright (C) 2014-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 #include "PartSet_ExternalObjectsMgr.h"
23 #include "PartSet_CenterPrs.h"
24 #include "PartSet_Tools.h"
26 #include <XGUI_Workshop.h>
27 #include <XGUI_ModuleConnector.h>
29 #include <ModuleBase_ViewerPrs.h>
30 #include <ModuleBase_ISelection.h>
32 #include <SketchPlugin_Feature.h>
36 PartSet_ExternalObjectsMgr::PartSet_ExternalObjectsMgr(const std::string& theExternal,
37 const std::string& theCanCreateExternal,
38 const bool theDefaultValue)
39 : myUseExternal(theDefaultValue), myCanCreateExternal(true)
41 QString aIsExternal(theExternal.c_str());
42 if (!aIsExternal.isEmpty()) {
43 QString aStr = aIsExternal.toUpper();
44 myUseExternal = (aStr == "TRUE") || (aStr == "YES");
47 QString aCanCreateExternal(theCanCreateExternal.c_str());
48 if (!aCanCreateExternal.isEmpty()) {
49 QString aStr = aCanCreateExternal.toUpper();
50 myCanCreateExternal = (aStr == "TRUE") || (aStr == "YES");
54 bool PartSet_ExternalObjectsMgr::isValidObject(const ObjectPtr& theObject)
57 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
58 // Do check using of external feature
59 std::shared_ptr<SketchPlugin_Feature> aSPFeature =
60 std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
62 // Do check that we can use external feature
63 if (aSPFeature.get() != NULL && aSPFeature->isExternal() && !useExternal()) {
70 ObjectPtr PartSet_ExternalObjectsMgr::externalObject(const ObjectPtr& theSelectedObject,
71 const GeomShapePtr& theShape,
72 const CompositeFeaturePtr& theSketch,
73 const bool theTemporary)
75 ObjectPtr aSelectedObject = PartSet_Tools::findFixedObjectByExternal(
76 theShape->impl<TopoDS_Shape>(), theSelectedObject, theSketch);
77 if (!aSelectedObject.get()) {
78 // Processing of external (non-sketch) object
79 FeaturePtr aCreatedFeature;
80 aSelectedObject = PartSet_Tools::createFixedObjectByExternal(theShape,
81 theSelectedObject, theSketch, theTemporary, aCreatedFeature);
82 if (aCreatedFeature.get() && theTemporary)
83 myExternalObjectValidated = aCreatedFeature;
85 return aSelectedObject;
88 ObjectPtr PartSet_ExternalObjectsMgr::externalCenterObject(const ModuleBase_ViewerPrsPtr& thePrs,
89 const CompositeFeaturePtr& theSketch,
90 const bool theTemporary)
92 if (!thePrs.get() || thePrs->interactive().IsNull())
95 Handle(PartSet_CenterPrs) aAIS = Handle(PartSet_CenterPrs)::DownCast(thePrs->interactive());
99 gp_Pnt aPntComp = aAIS->Component()->Pnt();
100 GeomVertexPtr aVertPtr(new GeomAPI_Vertex(aPntComp.X(), aPntComp.Y(), aPntComp.Z()));
101 TopoDS_Shape aShape = aVertPtr->impl<TopoDS_Shape>();
103 ResultPtr aSelectedObject =
104 PartSet_Tools::findFixedObjectByExternal(aShape, aAIS->object(), theSketch);
105 if (!aSelectedObject.get())
107 FeaturePtr aCreatedFeature;
108 aSelectedObject = PartSet_Tools::createFixedByExternalCenter(aAIS->object(), aAIS->edge(),
109 aAIS->centerType(), theSketch, theTemporary, aCreatedFeature);
110 if (aCreatedFeature.get() && theTemporary)
111 myExternalObjectValidated = aCreatedFeature;
113 return aSelectedObject;
116 void PartSet_ExternalObjectsMgr::getGeomSelection(const ModuleBase_ViewerPrsPtr& thePrs,
117 ObjectPtr& theObject,
118 GeomShapePtr& theShape,
119 ModuleBase_IWorkshop* theWorkshop,
120 const CompositeFeaturePtr& theSketch,
121 const bool isInValidate)
123 FeaturePtr aSelectedFeature = ModelAPI_Feature::feature(theObject);
124 std::shared_ptr<SketchPlugin_Feature> aSPFeature =
125 std::dynamic_pointer_cast<SketchPlugin_Feature>(aSelectedFeature);
126 // there is no a sketch feature is selected, but the shape exists,
127 // try to create an exernal object
128 // TODO: unite with the same functionality in PartSet_WidgetShapeSelector
129 if (aSPFeature.get() == NULL) {
130 ObjectPtr anExternalObject = ObjectPtr();
131 GeomShapePtr anExternalShape = GeomShapePtr();
133 if (canCreateExternal()) {
134 GeomShapePtr aShape = theShape;
136 ResultPtr aResult = theWorkshop->selection()->getResult(thePrs);
138 aShape = aResult->shape();
140 if (aShape.get() != NULL && !aShape->isNull())
142 externalObject(theObject, aShape, theSketch, isInValidate);
143 if (!anExternalObject.get()) {
144 anExternalObject = externalCenterObject(thePrs, theSketch, isInValidate);
147 else { /// use objects of found selection
148 anExternalObject = theObject;
149 anExternalShape = theShape;
152 /// the object is null if the selected feature is "external"(not sketch entity feature of the
153 /// current sketch) and it is not created by object manager
154 theObject = anExternalObject;
155 theShape = anExternalShape;
159 //********************************************************************
160 void PartSet_ExternalObjectsMgr::removeExternal(const CompositeFeaturePtr& theSketch,
161 const FeaturePtr& theFeature,
162 ModuleBase_IWorkshop* theWorkshop,
163 const bool theTemporary)
166 removeExternalObject(myExternalObjectValidated, theSketch, theFeature, theWorkshop);
169 void PartSet_ExternalObjectsMgr::removeExternalObject(const ObjectPtr& theObject,
170 const CompositeFeaturePtr& /*theSketch*/,
171 const FeaturePtr& theFeature,
172 ModuleBase_IWorkshop* theWorkshop)
174 if (theObject.get()) {
175 DocumentPtr aDoc = theObject->document();
176 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
177 if (aFeature.get() != NULL) {
178 QObjectPtrList anObjects;
179 anObjects.append(aFeature);
180 // the external feature should be removed with all references,
181 // composite sketch feature will be ignored
182 workshop(theWorkshop)->deleteFeatures(anObjects);
187 XGUI_Workshop* PartSet_ExternalObjectsMgr::workshop(ModuleBase_IWorkshop* theWorkshop)
189 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(theWorkshop);
190 return aConnector->workshop();