]> SALOME platform Git repositories - modules/shaper.git/blob - src/PartSet/PartSet_ExternalObjectsMgr.cpp
Salome HOME
Prevent crash of small arcs presentations
[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
23 #include "PartSet_CenterPrs.h"
24 #include "PartSet_Tools.h"
25
26 #include <XGUI_Workshop.h>
27 #include <XGUI_ModuleConnector.h>
28
29 #include <ModuleBase_ViewerPrs.h>
30 #include <ModuleBase_ISelection.h>
31
32 #include <SketchPlugin_Feature.h>
33
34 #include <QString>
35
36 PartSet_ExternalObjectsMgr::PartSet_ExternalObjectsMgr(const std::string& theExternal,
37                                                        const std::string& theCanCreateExternal,
38                                                        const bool theDefaultValue)
39 : myUseExternal(theDefaultValue), myCanCreateExternal(true)
40 {
41   QString aIsExternal(theExternal.c_str());
42   if (!aIsExternal.isEmpty()) {
43     QString aStr = aIsExternal.toUpper();
44     myUseExternal = (aStr == "TRUE") || (aStr == "YES");
45   }
46
47   QString aCanCreateExternal(theCanCreateExternal.c_str());
48   if (!aCanCreateExternal.isEmpty()) {
49     QString aStr = aCanCreateExternal.toUpper();
50     myCanCreateExternal = (aStr == "TRUE") || (aStr == "YES");
51   }
52 }
53
54 bool PartSet_ExternalObjectsMgr::isValidObject(const ObjectPtr& theObject)
55 {
56   bool aValid = true;
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);
61
62   // Do check that we can use external feature
63   if (aSPFeature.get() != NULL && aSPFeature->isExternal() && !useExternal()) {
64     aValid = false;
65   }
66
67   return aValid;
68 }
69
70 ObjectPtr PartSet_ExternalObjectsMgr::externalObject(const ObjectPtr& theSelectedObject,
71                                                      const GeomShapePtr& theShape,
72                                                      const CompositeFeaturePtr& theSketch,
73                                                      const bool theTemporary)
74 {
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;
84   }
85   return aSelectedObject;
86 }
87
88 ObjectPtr PartSet_ExternalObjectsMgr::externalCenterObject(const ModuleBase_ViewerPrsPtr& thePrs,
89                                                            const CompositeFeaturePtr& theSketch,
90                                                            const bool theTemporary)
91 {
92   if (!thePrs.get() || thePrs->interactive().IsNull())
93     return ObjectPtr();
94
95   Handle(PartSet_CenterPrs) aAIS = Handle(PartSet_CenterPrs)::DownCast(thePrs->interactive());
96   if (aAIS.IsNull())
97     return ObjectPtr();
98
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>();
102
103   ResultPtr aSelectedObject =
104     PartSet_Tools::findFixedObjectByExternal(aShape, aAIS->object(), theSketch);
105   if (!aSelectedObject.get())
106   {
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;
112   }
113   return aSelectedObject;
114 }
115
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)
122 {
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();
132     if (useExternal()) {
133       if (canCreateExternal()) {
134         GeomShapePtr aShape = theShape;
135         if (!aShape.get()) {
136           ResultPtr aResult = theWorkshop->selection()->getResult(thePrs);
137           if (aResult.get())
138             aShape = aResult->shape();
139         }
140         if (aShape.get() != NULL && !aShape->isNull())
141           anExternalObject =
142             externalObject(theObject, aShape, theSketch, isInValidate);
143         if (!anExternalObject.get()) {
144           anExternalObject = externalCenterObject(thePrs, theSketch, isInValidate);
145         }
146       }
147       else { /// use objects of found selection
148         anExternalObject = theObject;
149         anExternalShape = theShape;
150       }
151     }
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;
156   }
157 }
158
159 //********************************************************************
160 void PartSet_ExternalObjectsMgr::removeExternal(const CompositeFeaturePtr& theSketch,
161                                                 const FeaturePtr& theFeature,
162                                                 ModuleBase_IWorkshop* theWorkshop,
163                                                 const bool theTemporary)
164 {
165   if (theTemporary)
166     removeExternalObject(myExternalObjectValidated, theSketch, theFeature, theWorkshop);
167 }
168
169 void PartSet_ExternalObjectsMgr::removeExternalObject(const ObjectPtr& theObject,
170                                                       const CompositeFeaturePtr& /*theSketch*/,
171                                                       const FeaturePtr& theFeature,
172                                                       ModuleBase_IWorkshop* theWorkshop)
173 {
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);
183     }
184   }
185 }
186
187 XGUI_Workshop* PartSet_ExternalObjectsMgr::workshop(ModuleBase_IWorkshop* theWorkshop)
188 {
189   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(theWorkshop);
190   return aConnector->workshop();
191 }