1 // Copyright (C) 2014-2019 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>
22 #include <ModelAPI_AttributeSelectionList.h>
23 #include <ModelAPI_AttributeReference.h>
24 #include <ModelAPI_BodyBuilder.h>
25 #include <ModelAPI_ResultConstruction.h>
26 #include <ModelAPI_Session.h>
27 #include <ModelAPI_Validator.h>
29 #include <GeomAlgoAPI_CompoundBuilder.h>
30 #include <GeomAlgoAPI_Revolution.h>
31 #include <GeomAlgoAPI_ShapeTools.h>
32 #include <GeomAlgoAPI_SketchBuilder.h>
34 #include <GeomAPI_PlanarEdges.h>
35 #include <GeomAPI_ShapeExplorer.h>
40 static void storeSubShape(const std::shared_ptr<GeomAlgoAPI_MakeShape> theMakeShape,
41 ResultBodyPtr theResultBody,
42 const GeomShapePtr theShape,
43 const GeomAPI_Shape::ShapeType theType,
44 const std::string& theName);
46 //=================================================================================================
47 void FeaturesPlugin_CompositeSketch::initCompositeSketchAttribtues(const int theInitFlags)
49 // Initialize sketch launcher.
50 if(theInitFlags & InitSketchLauncher) {
51 data()->addAttribute(SKETCH_ID(), ModelAPI_AttributeReference::typeId());
52 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), SKETCH_ID());
55 // Initialize selection list.
56 if(theInitFlags & InitBaseObjectsList) {
57 data()->addAttribute(BASE_OBJECTS_ID(), ModelAPI_AttributeSelectionList::typeId());
61 //=================================================================================================
62 std::shared_ptr<ModelAPI_Feature> FeaturesPlugin_CompositeSketch::addFeature(std::string theID)
64 FeaturePtr aNew = document()->addFeature(theID, false);
66 data()->reference(SKETCH_ID())->setValue(aNew);
69 // Set as current also after it becomes sub to set correctly enabled for other sketch subs.
70 document()->setCurrentFeature(aNew, false);
74 //=================================================================================================
75 int FeaturesPlugin_CompositeSketch::numberOfSubs(bool forTree) const
77 ObjectPtr aObj = data()->reference(SKETCH_ID())->value();
78 return aObj.get() ? 1 : 0;
81 //=================================================================================================
82 std::shared_ptr<ModelAPI_Feature> FeaturesPlugin_CompositeSketch::subFeature(const int theIndex,
85 FeaturePtr aSubFeature;
88 std::dynamic_pointer_cast<ModelAPI_Feature>(data()->reference(SKETCH_ID())->value());
93 //=================================================================================================
94 int FeaturesPlugin_CompositeSketch::subFeatureId(const int theIndex) const
98 std::dynamic_pointer_cast<ModelAPI_Feature>(data()->reference(SKETCH_ID())->value());
100 return aFeature->data()->featureId();
107 //=================================================================================================
108 bool FeaturesPlugin_CompositeSketch::isSub(ObjectPtr theObject) const
110 bool isSubFeature = false;
111 // Check is this feature of result
112 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
113 if (aFeature.get()) {
114 ObjectPtr aSub = data()->reference(SKETCH_ID())->value();
115 isSubFeature = aSub == theObject;
120 //=================================================================================================
121 void FeaturesPlugin_CompositeSketch::removeFeature(std::shared_ptr<ModelAPI_Feature> theFeature)
123 AttributeSelectionListPtr aBaseObjectsSelectionList = selectionList(BASE_OBJECTS_ID());
124 if(aBaseObjectsSelectionList.get() && aBaseObjectsSelectionList->size() > 0) {
125 aBaseObjectsSelectionList->clear();
128 reference(SKETCH_ID())->setValue(ObjectPtr());
131 //=================================================================================================
132 void FeaturesPlugin_CompositeSketch::getBaseShapes(ListOfShape& theBaseShapesList,
133 const bool theIsMakeShells)
135 theBaseShapesList.clear();
137 ListOfShape aBaseFacesList;
138 std::map<ResultConstructionPtr, ListOfShape> aSketchWiresMap;
139 AttributeSelectionListPtr aBaseObjectsSelectionList = selectionList(BASE_OBJECTS_ID());
140 if(!aBaseObjectsSelectionList.get()) {
141 setError("Error: Could not get base objects selection list.");
144 if(aBaseObjectsSelectionList->size() == 0) {
145 setError("Error: Base objects list is empty.");
148 for(int anIndex = 0; anIndex < aBaseObjectsSelectionList->size(); anIndex++) {
149 AttributeSelectionPtr aBaseObjectSelection = aBaseObjectsSelectionList->value(anIndex);
150 if(!aBaseObjectSelection.get()) {
151 setError("Error: Selected base object is empty.");
154 GeomShapePtr aBaseShape = aBaseObjectSelection->value();
155 if(aBaseShape.get() && !aBaseShape->isNull()) {
156 GeomAPI_Shape::ShapeType aST = aBaseShape->shapeType();
157 if(aST == GeomAPI_Shape::SOLID || aST == GeomAPI_Shape::COMPSOLID) {
158 setError("Error: Selected shapes has unsupported type.");
161 ResultConstructionPtr aConstruction =
162 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aBaseObjectSelection->context());
163 if(aConstruction.get() && !aBaseShape->isEqual(aConstruction->shape()) &&
164 aST == GeomAPI_Shape::WIRE) {
165 // It is a wire on the sketch, store it to make face later.
166 aSketchWiresMap[aConstruction].push_back(aBaseShape);
169 aST == GeomAPI_Shape::FACE ? aBaseFacesList.push_back(aBaseShape) :
170 theBaseShapesList.push_back(aBaseShape);
173 // This may be the whole sketch result selected, check and get faces.
174 ResultConstructionPtr aConstruction =
175 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aBaseObjectSelection->context());
176 if(!aConstruction.get()) {
177 setError("Error: Selected sketches does not have results.");
180 int aFacesNum = aConstruction->facesNum();
182 // Probably it can be construction.
183 aBaseShape = aConstruction->shape();
184 if(aBaseShape.get() && !aBaseShape->isNull()) {
185 GeomAPI_Shape::ShapeType aST = aBaseShape->shapeType();
186 if(aST != GeomAPI_Shape::VERTEX && aST != GeomAPI_Shape::EDGE &&
187 aST != GeomAPI_Shape::WIRE &&
188 aST != GeomAPI_Shape::FACE && aST != GeomAPI_Shape::SHELL) {
189 setError("Error: Selected shapes has unsupported type.");
192 aST == GeomAPI_Shape::FACE ? aBaseFacesList.push_back(aBaseShape) :
193 theBaseShapesList.push_back(aBaseShape);
196 for(int aFaceIndex = 0; aFaceIndex < aFacesNum; aFaceIndex++) {
197 GeomShapePtr aBaseFace = aConstruction->face(aFaceIndex);
198 if(!aBaseFace.get() || aBaseFace->isNull()) {
199 setError("Error: One of the faces on selected sketch is null.");
202 aBaseFacesList.push_back(aBaseFace);
208 // Make faces from sketch wires.
209 for(std::map<ResultConstructionPtr, ListOfShape>::const_iterator anIt = aSketchWiresMap.cbegin();
210 anIt != aSketchWiresMap.cend(); ++anIt) {
211 const std::shared_ptr<GeomAPI_PlanarEdges> aSketchPlanarEdges =
212 std::dynamic_pointer_cast<GeomAPI_PlanarEdges>((*anIt).first->shape());
213 const ListOfShape& aWiresList = (*anIt).second;
215 GeomAlgoAPI_ShapeTools::makeFacesWithHoles(aSketchPlanarEdges->origin(),
216 aSketchPlanarEdges->norm(),
219 aBaseFacesList.insert(aBaseFacesList.end(), aFaces.begin(), aFaces.end());
222 // Searching faces with common edges.
223 if(theIsMakeShells && aBaseFacesList.size() > 1) {
224 GeomShapePtr aFacesCompound = GeomAlgoAPI_CompoundBuilder::compound(aBaseFacesList);
225 GeomAlgoAPI_ShapeTools::combineShapes(aFacesCompound, GeomAPI_Shape::SHELL, theBaseShapesList);
227 theBaseShapesList.insert(theBaseShapesList.end(), aBaseFacesList.begin(),
228 aBaseFacesList.end());
232 //=================================================================================================
233 void FeaturesPlugin_CompositeSketch::storeResult(const GeomShapePtr theBaseShape,
234 const std::shared_ptr<GeomAlgoAPI_MakeShape> theMakeShape,
237 // Create result body.
238 ResultBodyPtr aResultBody = document()->createBody(data(), theIndex);
240 // Store generated shape.
241 aResultBody->storeGenerated(theBaseShape, theMakeShape->shape());
243 // Store generated edges/faces.
244 storeGenerationHistory(aResultBody, theBaseShape, theMakeShape);
246 setResult(aResultBody, theIndex);
249 //=================================================================================================
250 void FeaturesPlugin_CompositeSketch::storeGenerationHistory(ResultBodyPtr theResultBody,
251 const GeomShapePtr theBaseShape,
252 const std::shared_ptr<GeomAlgoAPI_MakeShape> theMakeShape)
254 GeomAPI_Shape::ShapeType aBaseShapeType = theBaseShape->shapeType();
255 GeomAPI_Shape::ShapeType aShapeTypeToExplode = GeomAPI_Shape::SHAPE;
257 switch(aBaseShapeType) {
258 case GeomAPI_Shape::EDGE: {
259 aShapeTypeToExplode = GeomAPI_Shape::VERTEX;
262 case GeomAPI_Shape::WIRE: {
263 aShapeTypeToExplode = GeomAPI_Shape::COMPOUND;
266 case GeomAPI_Shape::FACE:
267 case GeomAPI_Shape::SHELL: {
268 aShapeTypeToExplode = GeomAPI_Shape::EDGE;
271 case GeomAPI_Shape::COMPOUND: {
272 aShapeTypeToExplode = GeomAPI_Shape::COMPOUND;
276 int aLateralIndex = 1;
277 int aBaseEdgeIndex = 1;
278 int aVertexIndex = 1;
279 int aBaseVertexIndex = 1;
281 if(aShapeTypeToExplode == GeomAPI_Shape::VERTEX ||
282 aShapeTypeToExplode == GeomAPI_Shape::COMPOUND) {
283 theResultBody->loadGeneratedShapes(theMakeShape, theBaseShape, GeomAPI_Shape::VERTEX);
285 if(aShapeTypeToExplode == GeomAPI_Shape::EDGE ||
286 aShapeTypeToExplode == GeomAPI_Shape::COMPOUND) {
287 theResultBody->loadGeneratedShapes(theMakeShape, theBaseShape, GeomAPI_Shape::EDGE);
289 std::list<std::shared_ptr<GeomAlgoAPI_MakeSweep> > aSweeps; // all sweeps collected
290 std::shared_ptr<GeomAlgoAPI_MakeSweep> aMakeSweep =
291 std::dynamic_pointer_cast<GeomAlgoAPI_MakeSweep>(theMakeShape);
292 if(aMakeSweep.get()) {
293 aSweeps.push_back(aMakeSweep);
295 std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeList =
296 std::dynamic_pointer_cast<GeomAlgoAPI_MakeShapeList>(theMakeShape);
297 if (aMakeList.get()) {
298 ListOfMakeShape::const_iterator anIter = aMakeList->list().cbegin();
299 for(; anIter != aMakeList->list().cend(); anIter++) {
300 std::shared_ptr<GeomAlgoAPI_MakeSweep> aSweep =
301 std::dynamic_pointer_cast<GeomAlgoAPI_MakeSweep>(*anIter);
303 aSweeps.push_back(aSweep);
307 std::list<std::shared_ptr<GeomAlgoAPI_MakeSweep> >::iterator aSweep = aSweeps.begin();
308 for(; aSweep != aSweeps.end(); aSweep++) {
309 // Store from shapes.
310 storeShapes(theMakeShape, theResultBody, aBaseShapeType, (*aSweep)->fromShapes(), "From_");
313 storeShapes(theMakeShape, theResultBody, aBaseShapeType, (*aSweep)->toShapes(), "To_");
317 //=================================================================================================
318 void FeaturesPlugin_CompositeSketch::storeShapes(
319 const std::shared_ptr<GeomAlgoAPI_MakeShape> theMakeShape,
320 ResultBodyPtr theResultBody,
321 const GeomAPI_Shape::ShapeType theBaseShapeType,
322 const ListOfShape& theShapes,
323 const std::string theName)
325 GeomAPI_Shape::ShapeType aShapeTypeToExplore = GeomAPI_Shape::FACE;
326 std::string aShapeTypeStr = "Face";
327 switch(theBaseShapeType) {
328 case GeomAPI_Shape::VERTEX: {
329 aShapeTypeToExplore = GeomAPI_Shape::VERTEX;
330 aShapeTypeStr = "Vertex";
333 case GeomAPI_Shape::EDGE:
334 case GeomAPI_Shape::WIRE: {
335 aShapeTypeToExplore = GeomAPI_Shape::EDGE;
336 aShapeTypeStr = "Edge";
339 case GeomAPI_Shape::FACE:
340 case GeomAPI_Shape::SHELL: {
341 aShapeTypeToExplore = GeomAPI_Shape::FACE;
342 aShapeTypeStr = "Face";
345 case GeomAPI_Shape::COMPOUND: {
346 aShapeTypeToExplore = GeomAPI_Shape::COMPOUND;
352 for(ListOfShape::const_iterator anIt = theShapes.cbegin(); anIt != theShapes.cend(); ++anIt) {
353 GeomShapePtr aShape = *anIt;
355 if(aShapeTypeToExplore == GeomAPI_Shape::COMPOUND) {
356 std::string aName = theName + (aShape->shapeType() == GeomAPI_Shape::EDGE ? "Edge" : "Face");
357 storeSubShape(theMakeShape, theResultBody, aShape, aShape->shapeType(), aName);
359 std::string aName = theName + aShapeTypeStr;
360 storeSubShape(theMakeShape, theResultBody, aShape, aShapeTypeToExplore, aName);
361 if (theBaseShapeType == GeomAPI_Shape::WIRE) { // issue 2289: special names also for vertices
362 aName = theName + "Vertex";
363 storeSubShape(theMakeShape, theResultBody, aShape, GeomAPI_Shape::VERTEX, aName);
370 const std::shared_ptr<GeomAlgoAPI_MakeShape> theMakeShape,
371 ResultBodyPtr theResultBody,
372 const GeomShapePtr theShape,
373 const GeomAPI_Shape::ShapeType theType,
374 const std::string& theName)
376 for(GeomAPI_ShapeExplorer anExp(theShape, theType); anExp.more(); anExp.next()) {
377 GeomShapePtr aSubShape = anExp.current();
378 if (!theResultBody->generated(aSubShape, theName)) {
379 // store from/to shapes as primitives and then store modification of them by the boolean
380 theResultBody->generated(aSubShape, theName, false);
381 theResultBody->loadModifiedShapes(theMakeShape, aSubShape, theType);