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