Salome HOME
Add copyright header according to request of CEA from 06.06.2017
[modules/shaper.git] / src / PartSet / PartSet_ExternalObjectsMgr.cpp
1 // Copyright (C) 2014-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 #include "PartSet_ExternalObjectsMgr.h"
22 #include "PartSet_Tools.h"
23
24 #include <XGUI_Workshop.h>
25 #include <XGUI_ModuleConnector.h>
26
27 #include <ModuleBase_ViewerPrs.h>
28 #include <ModuleBase_ISelection.h>
29
30 #include <SketchPlugin_Feature.h>
31
32 #include <QString>
33
34 PartSet_ExternalObjectsMgr::PartSet_ExternalObjectsMgr(const std::string& theExternal,
35                                                        const std::string& theCanCreateExternal,
36                                                        const bool theDefaultValue)
37 : myUseExternal(theDefaultValue), myCanCreateExternal(true)
38 {
39   QString aIsExternal(theExternal.c_str());
40   if (!aIsExternal.isEmpty()) {
41     QString aStr = aIsExternal.toUpper();
42     myUseExternal = (aStr == "TRUE") || (aStr == "YES");
43   }
44
45   QString aCanCreateExternal(theCanCreateExternal.c_str());
46   if (!aCanCreateExternal.isEmpty()) {
47     QString aStr = aCanCreateExternal.toUpper();
48     myCanCreateExternal = (aStr == "TRUE") || (aStr == "YES");
49   }
50 }
51
52 bool PartSet_ExternalObjectsMgr::isValidObject(const ObjectPtr& theObject)
53 {
54   bool aValid = true;
55   FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
56   // Do check using of external feature
57   std::shared_ptr<SketchPlugin_Feature> aSPFeature =
58           std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
59
60   // Do check that we can use external feature
61   if (aSPFeature.get() != NULL && aSPFeature->isExternal() && !useExternal()) {
62     aValid = false;
63   }
64
65   return aValid;
66 }
67
68 ObjectPtr PartSet_ExternalObjectsMgr::externalObject(const ObjectPtr& theSelectedObject,
69                                                      const GeomShapePtr& theShape,
70                                                      const CompositeFeaturePtr& theSketch,
71                                                      const bool theTemporary)
72 {
73   ObjectPtr aSelectedObject = PartSet_Tools::findFixedObjectByExternal(
74                                   theShape->impl<TopoDS_Shape>(), theSelectedObject, theSketch);
75   FeaturePtr aFeature = ModelAPI_Feature::feature(aSelectedObject);
76   if (aFeature.get()) {
77     std::shared_ptr<SketchPlugin_Feature> aSketchFeature =
78                             std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
79     /// some sketch entities should be never shown, e.g. projection feature
80     /// such external features should not be used in selection
81     if (aSketchFeature.get() && !aSketchFeature->canBeDisplayed())
82       return ObjectPtr();
83   }
84
85   if (!aSelectedObject.get()) {
86     // Processing of external (non-sketch) object
87     aSelectedObject = PartSet_Tools::createFixedObjectByExternal(theShape->impl<TopoDS_Shape>(),
88                                                     theSelectedObject, theSketch, theTemporary);
89     if (aSelectedObject.get() && theTemporary)
90         myExternalObjectValidated = aSelectedObject;
91   }
92   return aSelectedObject;
93 }
94
95 void PartSet_ExternalObjectsMgr::getGeomSelection(const ModuleBase_ViewerPrsPtr& thePrs,
96                                                    ObjectPtr& theObject,
97                                                    GeomShapePtr& theShape,
98                                                    ModuleBase_IWorkshop* theWorkshop,
99                                                    const CompositeFeaturePtr& theSketch,
100                                                    const bool isInValidate)
101 {
102   FeaturePtr aSelectedFeature = ModelAPI_Feature::feature(theObject);
103   std::shared_ptr<SketchPlugin_Feature> aSPFeature =
104           std::dynamic_pointer_cast<SketchPlugin_Feature>(aSelectedFeature);
105   // there is no a sketch feature is selected, but the shape exists,
106   // try to create an exernal object
107   // TODO: unite with the same functionality in PartSet_WidgetShapeSelector
108   if (aSPFeature.get() == NULL) {
109     ObjectPtr anExternalObject = ObjectPtr();
110     GeomShapePtr anExternalShape = GeomShapePtr();
111     if (useExternal()) {
112       if (canCreateExternal()) {
113         GeomShapePtr aShape = theShape;
114         if (!aShape.get()) {
115           ResultPtr aResult = theWorkshop->selection()->getResult(thePrs);
116           if (aResult.get())
117             aShape = aResult->shape();
118         }
119         if (aShape.get() != NULL && !aShape->isNull())
120           anExternalObject =
121             externalObject(theObject, aShape, theSketch, isInValidate);
122       }
123       else { /// use objects of found selection
124         anExternalObject = theObject;
125         anExternalShape = theShape;
126       }
127     }
128     /// the object is null if the selected feature is "external"(not sketch entity feature of the
129     /// current sketch) and it is not created by object manager
130     theObject = anExternalObject;
131     theShape = anExternalShape;
132   }
133 }
134
135 //********************************************************************
136 void PartSet_ExternalObjectsMgr::removeExternal(const CompositeFeaturePtr& theSketch,
137                                                 const FeaturePtr& theFeature,
138                                                 ModuleBase_IWorkshop* theWorkshop,
139                                                 const bool theTemporary)
140 {
141   if (theTemporary)
142     removeExternalObject(myExternalObjectValidated, theSketch, theFeature, theWorkshop);
143 }
144
145 void PartSet_ExternalObjectsMgr::removeExternalObject(const ObjectPtr& theObject,
146                                                       const CompositeFeaturePtr& /*theSketch*/,
147                                                       const FeaturePtr& theFeature,
148                                                       ModuleBase_IWorkshop* theWorkshop)
149 {
150   if (theObject.get()) {
151     DocumentPtr aDoc = theObject->document();
152     FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
153     if (aFeature.get() != NULL) {
154       QObjectPtrList anObjects;
155       anObjects.append(aFeature);
156       // the external feature should be removed with all references,
157       // composite sketch feature will be ignored
158       workshop(theWorkshop)->deleteFeatures(anObjects);
159     }
160   }
161 }
162
163 XGUI_Workshop* PartSet_ExternalObjectsMgr::workshop(ModuleBase_IWorkshop* theWorkshop)
164 {
165   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(theWorkshop);
166   return aConnector->workshop();
167 }