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