Salome HOME
Issue #1369: moved Wire feature to new Build plug-in.
[modules/shaper.git] / src / BuildPlugin / BuildPlugin_Validators.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
2
3 // File:        BuildPlugin_Validators.cpp
4 // Created:     22 March 2016
5 // Author:      Dmitry Bobylev
6
7 #include "BuildPlugin_Validators.h"
8
9 #include <ModelAPI_AttributeSelectionList.h>
10 #include <ModelAPI_ResultConstruction.h>
11
12 #include <GeomAPI_PlanarEdges.h>
13
14 #include <GeomAlgoAPI_WireBuilder.h>
15
16 #include <Events_Error.h>
17
18 //=================================================================================================
19 bool BuildPlugin_ValidatorBaseForWire::isValid(const AttributePtr& theAttribute,
20                                                const std::list<std::string>& theArguments,
21                                                std::string& theError) const
22 {
23   // Get base objects list.
24   if(theAttribute->attributeType() != ModelAPI_AttributeSelectionList::typeId()) {
25     Events_Error::send("Validator does not support attribute type \"" + theAttribute->attributeType()
26       + "\"\n Only \"" + ModelAPI_AttributeSelectionList::typeId() + "\" supported.");
27     return false;
28   }
29   AttributeSelectionListPtr aSelectionList = std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
30   if(!aSelectionList.get()) {
31     theError = "Could not get selection list.";
32     return false;
33   }
34   if(aSelectionList->size() == 0) {
35     theError = "Empty selection list.";
36     return false;
37   }
38
39   // Collect base shapes.
40   ListOfShape aListOfShapes;
41   for(int anIndex = 0; anIndex < aSelectionList->size(); ++anIndex) {
42     AttributeSelectionPtr aSelection = aSelectionList->value(anIndex);
43     if(!aSelection.get()) {
44       theError = "Could not get selection.";
45       return false;
46     }
47     ResultPtr aContext = aSelection->context();
48     if(!aContext.get()) {
49       theError = "Attribute have empty context.";
50       return false;
51     }
52
53     GeomShapePtr aShape = aSelection->value();
54     GeomShapePtr aContextShape = aContext->shape();
55     if(!aShape.get()) {
56       aShape = aContextShape;
57     }
58     if(!aShape.get()) {
59       theError = "Empty shape selected.";
60       return false;
61     }
62
63     // Check that shape has acceptable type.
64     if(aShape->shapeType() != GeomAPI_Shape::EDGE && aShape->shapeType() != GeomAPI_Shape::WIRE) {
65       theError = "Selected shape has wrong type. Only edges and wires acceptable.";
66       return false;
67     }
68
69     // Check that it is edge on sketch.
70     ResultConstructionPtr aConstruction = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aContext);
71     if(aConstruction.get()) {
72       if(aConstruction->isInfinite()) {
73         theError = "Inifinte objects not acceptable.";
74         return false;
75       }
76
77       std::shared_ptr<GeomAPI_PlanarEdges> anEdges = std::dynamic_pointer_cast<GeomAPI_PlanarEdges>(aContextShape);
78       if(!anEdges.get()) {
79         // It is not an edge on the sketch.
80         // Check that it is not local selection.
81         if(!aShape->isEqual(aContextShape)) {
82           // Local selection on body does not allowed.
83           theError = "Selected shape is in the local selection. Only global selection is allowed.";
84           return false;
85         }
86       }
87     }
88
89     aListOfShapes.push_back(aShape);
90   }
91
92   // Create wire.
93   GeomShapePtr aWire = GeomAlgoAPI_WireBuilder::wire(aListOfShapes);
94   if(!aWire.get()) {
95     theError = "Result wire empty. Probably it has disconnected edges or non-manifold.";
96     return false;
97   }
98
99   return true;
100 }