1 // Copyright (C) 2014-2019 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 email : webmaster.salome@opencascade.com
20 #include "PartSet_ExternalObjectsMgr.h"
22 #include "PartSet_CenterPrs.h"
23 #include "PartSet_Tools.h"
25 #include <XGUI_Workshop.h>
26 #include <XGUI_ModuleConnector.h>
28 #include <ModuleBase_ViewerPrs.h>
29 #include <ModuleBase_ISelection.h>
31 #include <SketchPlugin_Feature.h>
35 PartSet_ExternalObjectsMgr::PartSet_ExternalObjectsMgr(const std::string& theExternal,
36 const std::string& theCanCreateExternal,
37 const bool theDefaultValue)
38 : myUseExternal(theDefaultValue), myCanCreateExternal(true)
40 QString aIsExternal(theExternal.c_str());
41 if (!aIsExternal.isEmpty()) {
42 QString aStr = aIsExternal.toUpper();
43 myUseExternal = (aStr == "TRUE") || (aStr == "YES");
46 QString aCanCreateExternal(theCanCreateExternal.c_str());
47 if (!aCanCreateExternal.isEmpty()) {
48 QString aStr = aCanCreateExternal.toUpper();
49 myCanCreateExternal = (aStr == "TRUE") || (aStr == "YES");
53 bool PartSet_ExternalObjectsMgr::isValidObject(const ObjectPtr& theObject)
56 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
57 // Do check using of external feature
58 std::shared_ptr<SketchPlugin_Feature> aSPFeature =
59 std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
61 // Do check that we can use external feature
62 if (aSPFeature.get() != NULL && aSPFeature->isExternal() && !useExternal()) {
69 ObjectPtr PartSet_ExternalObjectsMgr::externalObject(const ObjectPtr& theSelectedObject,
70 const GeomShapePtr& theShape,
71 const CompositeFeaturePtr& theSketch,
72 const bool theTemporary)
74 ObjectPtr aSelectedObject = PartSet_Tools::findFixedObjectByExternal(
75 theShape->impl<TopoDS_Shape>(), theSelectedObject, theSketch);
76 if (!aSelectedObject.get()) {
77 // Processing of external (non-sketch) object
78 FeaturePtr aCreatedFeature;
79 aSelectedObject = PartSet_Tools::createFixedObjectByExternal(theShape,
80 theSelectedObject, theSketch, theTemporary, aCreatedFeature);
81 if (aCreatedFeature.get() && theTemporary)
82 myExternalObjectValidated = aCreatedFeature;
84 return aSelectedObject;
87 ObjectPtr PartSet_ExternalObjectsMgr::externalCenterObject(const ModuleBase_ViewerPrsPtr& thePrs,
88 const CompositeFeaturePtr& theSketch,
89 const bool theTemporary)
91 if (!thePrs.get() || thePrs->interactive().IsNull())
94 Handle(PartSet_CenterPrs) aAIS = Handle(PartSet_CenterPrs)::DownCast(thePrs->interactive());
98 gp_Pnt aPntComp = aAIS->Component()->Pnt();
99 GeomVertexPtr aVertPtr(new GeomAPI_Vertex(aPntComp.X(), aPntComp.Y(), aPntComp.Z()));
100 TopoDS_Shape aShape = aVertPtr->impl<TopoDS_Shape>();
102 ResultPtr aSelectedObject =
103 PartSet_Tools::findFixedObjectByExternal(aShape, aAIS->object(), theSketch);
104 if (!aSelectedObject.get())
106 FeaturePtr aCreatedFeature;
107 aSelectedObject = PartSet_Tools::createFixedByExternalCenter(aAIS->object(), aAIS->edge(),
108 aAIS->centerType(), theSketch, theTemporary, aCreatedFeature);
109 if (aCreatedFeature.get() && theTemporary)
110 myExternalObjectValidated = aCreatedFeature;
112 return aSelectedObject;
115 void PartSet_ExternalObjectsMgr::getGeomSelection(const ModuleBase_ViewerPrsPtr& thePrs,
116 ObjectPtr& theObject,
117 GeomShapePtr& theShape,
118 ModuleBase_IWorkshop* theWorkshop,
119 const CompositeFeaturePtr& theSketch,
120 const bool isInValidate)
122 FeaturePtr aSelectedFeature = ModelAPI_Feature::feature(theObject);
123 std::shared_ptr<SketchPlugin_Feature> aSPFeature =
124 std::dynamic_pointer_cast<SketchPlugin_Feature>(aSelectedFeature);
125 // there is no a sketch feature is selected, but the shape exists,
126 // try to create an exernal object
127 // TODO: unite with the same functionality in PartSet_WidgetShapeSelector
128 if (aSPFeature.get() == NULL) {
129 ObjectPtr anExternalObject = ObjectPtr();
130 GeomShapePtr anExternalShape = GeomShapePtr();
132 if (canCreateExternal()) {
133 GeomShapePtr aShape = theShape;
135 ResultPtr aResult = theWorkshop->selection()->getResult(thePrs);
137 aShape = aResult->shape();
139 if (aShape.get() != NULL && !aShape->isNull())
141 externalObject(theObject, aShape, theSketch, isInValidate);
142 if (!anExternalObject.get()) {
143 anExternalObject = externalCenterObject(thePrs, theSketch, isInValidate);
146 else { /// use objects of found selection
147 anExternalObject = theObject;
148 anExternalShape = theShape;
151 /// the object is null if the selected feature is "external"(not sketch entity feature of the
152 /// current sketch) and it is not created by object manager
153 theObject = anExternalObject;
154 theShape = anExternalShape;
158 //********************************************************************
159 void PartSet_ExternalObjectsMgr::removeExternal(const CompositeFeaturePtr& theSketch,
160 const FeaturePtr& theFeature,
161 ModuleBase_IWorkshop* theWorkshop,
162 const bool theTemporary)
165 removeExternalObject(myExternalObjectValidated, theSketch, theFeature, theWorkshop);
168 void PartSet_ExternalObjectsMgr::removeExternalObject(const ObjectPtr& theObject,
169 const CompositeFeaturePtr& /*theSketch*/,
170 const FeaturePtr& theFeature,
171 ModuleBase_IWorkshop* theWorkshop)
173 if (theObject.get()) {
174 DocumentPtr aDoc = theObject->document();
175 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
176 if (aFeature.get() != NULL) {
177 QObjectPtrList anObjects;
178 anObjects.append(aFeature);
179 // the external feature should be removed with all references,
180 // composite sketch feature will be ignored
181 workshop(theWorkshop)->deleteFeatures(anObjects);
186 XGUI_Workshop* PartSet_ExternalObjectsMgr::workshop(ModuleBase_IWorkshop* theWorkshop)
188 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(theWorkshop);
189 return aConnector->workshop();