1 // Copyright (C) 2014-2020 CEA/DEN, EDF R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include <FeaturesPlugin_CompositeSketch.h>
21 #include <FeaturesPlugin_Tools.h>
23 #include <ModelAPI_AttributeSelectionList.h>
24 #include <ModelAPI_AttributeReference.h>
25 #include <ModelAPI_BodyBuilder.h>
26 #include <ModelAPI_ResultConstruction.h>
27 #include <ModelAPI_Session.h>
28 #include <ModelAPI_Validator.h>
30 #include <GeomAlgoAPI_Revolution.h>
32 #include <GeomAPI_ShapeExplorer.h>
35 static void storeSubShape(const std::shared_ptr<GeomAlgoAPI_MakeShape> theMakeShape,
36 ResultBodyPtr theResultBody,
37 const GeomShapePtr theShape,
38 const GeomAPI_Shape::ShapeType theType,
39 const std::string& theName);
41 //=================================================================================================
42 void FeaturesPlugin_CompositeSketch::initCompositeSketchAttribtues(const int theInitFlags)
44 // Initialize sketch launcher.
45 if(theInitFlags & InitSketchLauncher) {
46 data()->addAttribute(SKETCH_ID(), ModelAPI_AttributeReference::typeId());
47 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), SKETCH_ID());
50 // Initialize selection list.
51 if(theInitFlags & InitBaseObjectsList) {
52 data()->addAttribute(BASE_OBJECTS_ID(), ModelAPI_AttributeSelectionList::typeId());
53 myCurrentSelectionType = selectionList(BASE_OBJECTS_ID())->selectionType();
57 //=================================================================================================
58 std::shared_ptr<ModelAPI_Feature> FeaturesPlugin_CompositeSketch::addFeature(std::string theID)
60 FeaturePtr aNew = document()->addFeature(theID, false);
62 data()->reference(SKETCH_ID())->setValue(aNew);
65 // Set as current also after it becomes sub to set correctly enabled for other sketch subs.
66 document()->setCurrentFeature(aNew, false);
70 //=================================================================================================
71 int FeaturesPlugin_CompositeSketch::numberOfSubs(bool /*forTree*/) const
73 ObjectPtr aObj = data()->reference(SKETCH_ID())->value();
74 return aObj.get() ? 1 : 0;
77 //=================================================================================================
78 std::shared_ptr<ModelAPI_Feature> FeaturesPlugin_CompositeSketch::subFeature(const int theIndex,
81 FeaturePtr aSubFeature;
84 std::dynamic_pointer_cast<ModelAPI_Feature>(data()->reference(SKETCH_ID())->value());
89 //=================================================================================================
90 int FeaturesPlugin_CompositeSketch::subFeatureId(const int theIndex) const
94 std::dynamic_pointer_cast<ModelAPI_Feature>(data()->reference(SKETCH_ID())->value());
96 return aFeature->data()->featureId();
103 //=================================================================================================
104 bool FeaturesPlugin_CompositeSketch::isSub(ObjectPtr theObject) const
106 bool isSubFeature = false;
107 // Check is this feature of result
108 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
109 if (aFeature.get()) {
110 ObjectPtr aSub = data()->reference(SKETCH_ID())->value();
111 isSubFeature = aSub == theObject;
116 //=================================================================================================
117 void FeaturesPlugin_CompositeSketch::removeFeature(std::shared_ptr<ModelAPI_Feature> theFeature)
119 AttributeSelectionListPtr aBaseObjectsSelectionList = selectionList(BASE_OBJECTS_ID());
120 if(aBaseObjectsSelectionList.get() && aBaseObjectsSelectionList->size() > 0) {
121 aBaseObjectsSelectionList->clear();
124 reference(SKETCH_ID())->setValue(ObjectPtr());
127 //=================================================================================================
128 void FeaturesPlugin_CompositeSketch::getBaseShapes(ListOfShape& theBaseShapesList,
129 const bool theIsMakeShells)
131 AttributeSelectionListPtr aBaseObjectsSelectionList = selectionList(BASE_OBJECTS_ID());
133 bool isOk = FeaturesPlugin_Tools::getShape(
134 aBaseObjectsSelectionList, theIsMakeShells, theBaseShapesList, anError);
139 //=================================================================================================
140 void FeaturesPlugin_CompositeSketch::storeResult(const GeomShapePtr theBaseShape,
141 const std::shared_ptr<GeomAlgoAPI_MakeShape> theMakeShape,
144 // Create result body.
145 ResultBodyPtr aResultBody = document()->createBody(data(), theIndex);
147 // Store generated shape.
148 aResultBody->storeGenerated(theBaseShape, theMakeShape->shape());
150 // Store generated edges/faces.
151 storeGenerationHistory(aResultBody, theBaseShape, theMakeShape);
153 setResult(aResultBody, theIndex);
156 //=================================================================================================
157 void FeaturesPlugin_CompositeSketch::storeGenerationHistory(ResultBodyPtr theResultBody,
158 const GeomShapePtr theBaseShape,
159 const std::shared_ptr<GeomAlgoAPI_MakeShape> theMakeShape)
161 GeomAPI_Shape::ShapeType aBaseShapeType = theBaseShape->shapeType();
162 GeomAPI_Shape::ShapeType aShapeTypeToExplode = GeomAPI_Shape::SHAPE;
164 switch(aBaseShapeType) {
165 case GeomAPI_Shape::EDGE: {
166 aShapeTypeToExplode = GeomAPI_Shape::VERTEX;
169 case GeomAPI_Shape::WIRE: {
170 aShapeTypeToExplode = GeomAPI_Shape::COMPOUND;
173 case GeomAPI_Shape::FACE:
174 case GeomAPI_Shape::SHELL: {
175 aShapeTypeToExplode = GeomAPI_Shape::EDGE;
178 case GeomAPI_Shape::COMPOUND: {
179 aShapeTypeToExplode = GeomAPI_Shape::COMPOUND;
181 default: // [to avoid compilation warnings]
185 if(aShapeTypeToExplode == GeomAPI_Shape::VERTEX ||
186 aShapeTypeToExplode == GeomAPI_Shape::COMPOUND) {
187 theResultBody->loadGeneratedShapes(theMakeShape, theBaseShape, GeomAPI_Shape::VERTEX);
189 if(aShapeTypeToExplode == GeomAPI_Shape::EDGE ||
190 aShapeTypeToExplode == GeomAPI_Shape::COMPOUND) {
191 theResultBody->loadGeneratedShapes(theMakeShape, theBaseShape, GeomAPI_Shape::EDGE);
193 std::list<std::shared_ptr<GeomAlgoAPI_MakeSweep> > aSweeps; // all sweeps collected
194 std::shared_ptr<GeomAlgoAPI_MakeSweep> aMakeSweep =
195 std::dynamic_pointer_cast<GeomAlgoAPI_MakeSweep>(theMakeShape);
196 if(aMakeSweep.get()) {
197 aSweeps.push_back(aMakeSweep);
199 std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeList =
200 std::dynamic_pointer_cast<GeomAlgoAPI_MakeShapeList>(theMakeShape);
201 if (aMakeList.get()) {
202 ListOfMakeShape::const_iterator anIter = aMakeList->list().cbegin();
203 for(; anIter != aMakeList->list().cend(); anIter++) {
204 std::shared_ptr<GeomAlgoAPI_MakeSweep> aSweep =
205 std::dynamic_pointer_cast<GeomAlgoAPI_MakeSweep>(*anIter);
207 aSweeps.push_back(aSweep);
211 std::list<std::shared_ptr<GeomAlgoAPI_MakeSweep> >::iterator aSweep = aSweeps.begin();
212 for(; aSweep != aSweeps.end(); aSweep++) {
213 // Store from shapes.
214 storeShapes(theMakeShape, theResultBody, aBaseShapeType, (*aSweep)->fromShapes(), "From_");
217 storeShapes(theMakeShape, theResultBody, aBaseShapeType, (*aSweep)->toShapes(), "To_");
221 //=================================================================================================
222 void FeaturesPlugin_CompositeSketch::storeShapes(
223 const std::shared_ptr<GeomAlgoAPI_MakeShape> theMakeShape,
224 ResultBodyPtr theResultBody,
225 const GeomAPI_Shape::ShapeType theBaseShapeType,
226 const ListOfShape& theShapes,
227 const std::string theName)
229 GeomAPI_Shape::ShapeType aShapeTypeToExplore = GeomAPI_Shape::FACE;
230 std::string aShapeTypeStr = "Face";
231 switch(theBaseShapeType) {
232 case GeomAPI_Shape::VERTEX: {
233 aShapeTypeToExplore = GeomAPI_Shape::VERTEX;
234 aShapeTypeStr = "Vertex";
237 case GeomAPI_Shape::EDGE:
238 case GeomAPI_Shape::WIRE: {
239 aShapeTypeToExplore = GeomAPI_Shape::EDGE;
240 aShapeTypeStr = "Edge";
243 case GeomAPI_Shape::FACE:
244 case GeomAPI_Shape::SHELL: {
245 aShapeTypeToExplore = GeomAPI_Shape::FACE;
246 aShapeTypeStr = "Face";
249 case GeomAPI_Shape::COMPOUND: {
250 aShapeTypeToExplore = GeomAPI_Shape::COMPOUND;
253 default: // [to avoid compilation warnings]
258 for(ListOfShape::const_iterator anIt = theShapes.cbegin(); anIt != theShapes.cend(); ++anIt) {
259 GeomShapePtr aShape = *anIt;
261 if(aShapeTypeToExplore == GeomAPI_Shape::COMPOUND) {
262 std::string aName = theName + (aShape->shapeType() == GeomAPI_Shape::EDGE ? "Edge" : "Face");
263 storeSubShape(theMakeShape, theResultBody, aShape, aShape->shapeType(), aName);
265 std::string aName = theName + aShapeTypeStr;
266 storeSubShape(theMakeShape, theResultBody, aShape, aShapeTypeToExplore, aName);
267 if (theBaseShapeType == GeomAPI_Shape::WIRE) { // issue 2289: special names also for vertices
268 aName = theName + "Vertex";
269 storeSubShape(theMakeShape, theResultBody, aShape, GeomAPI_Shape::VERTEX, aName);
276 const std::shared_ptr<GeomAlgoAPI_MakeShape> theMakeShape,
277 ResultBodyPtr theResultBody,
278 const GeomShapePtr theShape,
279 const GeomAPI_Shape::ShapeType theType,
280 const std::string& theName)
282 for(GeomAPI_ShapeExplorer anExp(theShape, theType); anExp.more(); anExp.next()) {
283 GeomShapePtr aSubShape = anExp.current();
284 if (!theResultBody->generated(aSubShape, theName)) {
285 int aNbSubs = theResultBody->numberOfSubs();
287 // check the modified shape is in the result body, don't store it if not
288 ListOfShape aNewShapes;
289 theMakeShape->modified(aSubShape, aNewShapes);
290 for (int i = 0; i < aNbSubs; ++i) {
291 ResultBodyPtr aSubRes = theResultBody->subResult(i);
292 GeomShapePtr aShape = aSubRes->shape();
293 ListOfShape::iterator aNewIt = aNewShapes.begin();
294 for (; aNewIt != aNewShapes.end(); ++aNewIt)
295 if (aShape->isSubShape(*aNewIt, false))
297 if (aNewIt != aNewShapes.end()) {
298 // store from/to shapes as primitives and then store modification of them by the boolean
299 aSubRes->generated(aSubShape, theName, false);
300 aSubRes->loadModifiedShapes(theMakeShape, aSubShape, theType);
305 // store from/to shapes as primitives and then store modification of them by the boolean
306 theResultBody->generated(aSubShape, theName, false);
307 theResultBody->loadModifiedShapes(theMakeShape, aSubShape, theType);