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