1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 // File: PartSet_ExternalObjectsMgr.cpp
4 // Created: 15 Apr 2015
5 // Author: Natalia Ermolaeva
7 #include "PartSet_ExternalObjectsMgr.h"
8 #include "PartSet_Tools.h"
10 #include <XGUI_Workshop.h>
11 #include <XGUI_ModuleConnector.h>
13 #include <ModuleBase_ViewerPrs.h>
14 #include <ModuleBase_ISelection.h>
16 #include <SketchPlugin_Feature.h>
20 PartSet_ExternalObjectsMgr::PartSet_ExternalObjectsMgr(const std::string& theExternal,
21 const std::string& theCanCreateExternal,
22 const bool theDefaultValue)
23 : myUseExternal(theDefaultValue), myCanCreateExternal(true)
25 QString aIsExternal(theExternal.c_str());
26 if (!aIsExternal.isEmpty()) {
27 QString aStr = aIsExternal.toUpper();
28 myUseExternal = (aStr == "TRUE") || (aStr == "YES");
31 QString aCanCreateExternal(theCanCreateExternal.c_str());
32 if (!aCanCreateExternal.isEmpty()) {
33 QString aStr = aCanCreateExternal.toUpper();
34 myCanCreateExternal = (aStr == "TRUE") || (aStr == "YES");
38 bool PartSet_ExternalObjectsMgr::isValidObject(const ObjectPtr& theObject)
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);
46 // Do check that we can use external feature
47 if (aSPFeature.get() != NULL && aSPFeature->isExternal() && !useExternal()) {
54 ObjectPtr PartSet_ExternalObjectsMgr::externalObject(const ObjectPtr& theSelectedObject,
55 const GeomShapePtr& theShape,
56 const CompositeFeaturePtr& theSketch,
57 const bool theTemporary)
59 ObjectPtr aSelectedObject = PartSet_Tools::findFixedObjectByExternal(
60 theShape->impl<TopoDS_Shape>(), theSelectedObject, theSketch);
61 FeaturePtr aFeature = ModelAPI_Feature::feature(aSelectedObject);
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())
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;
78 return aSelectedObject;
81 void PartSet_ExternalObjectsMgr::getGeomSelection(const ModuleBase_ViewerPrsPtr& thePrs,
83 GeomShapePtr& theShape,
84 ModuleBase_IWorkshop* theWorkshop,
85 const CompositeFeaturePtr& theSketch,
86 const bool isInValidate)
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();
98 if (canCreateExternal()) {
99 GeomShapePtr aShape = theShape;
101 ResultPtr aResult = theWorkshop->selection()->getResult(thePrs);
103 aShape = aResult->shape();
105 if (aShape.get() != NULL && !aShape->isNull())
107 externalObject(theObject, aShape, theSketch, isInValidate);
109 else { /// use objects of found selection
110 anExternalObject = theObject;
111 anExternalShape = theShape;
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;
121 //********************************************************************
122 void PartSet_ExternalObjectsMgr::removeExternal(const CompositeFeaturePtr& theSketch,
123 const FeaturePtr& theFeature,
124 ModuleBase_IWorkshop* theWorkshop,
125 const bool theTemporary)
128 removeExternalObject(myExternalObjectValidated, theSketch, theFeature, theWorkshop);
131 void PartSet_ExternalObjectsMgr::removeExternalObject(const ObjectPtr& theObject,
132 const CompositeFeaturePtr& /*theSketch*/,
133 const FeaturePtr& theFeature,
134 ModuleBase_IWorkshop* theWorkshop)
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);
149 XGUI_Workshop* PartSet_ExternalObjectsMgr::workshop(ModuleBase_IWorkshop* theWorkshop)
151 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(theWorkshop);
152 return aConnector->workshop();