1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
3 // File: BuildPlugin_Validators.cpp
4 // Created: 22 March 2016
5 // Author: Dmitry Bobylev
7 #include "BuildPlugin_Validators.h"
9 #include <ModelAPI_AttributeSelectionList.h>
10 #include <ModelAPI_ResultConstruction.h>
12 #include <GeomAPI_PlanarEdges.h>
13 #include <GeomAPI_Pln.h>
14 #include <GeomAPI_ShapeExplorer.h>
16 #include <GeomAlgoAPI_CompoundBuilder.h>
17 #include <GeomAlgoAPI_PaveFiller.h>
18 #include <GeomAlgoAPI_ShapeTools.h>
19 #include <GeomAlgoAPI_SketchBuilder.h>
20 #include <GeomAlgoAPI_WireBuilder.h>
22 #include <GeomValidators_ShapeType.h>
24 #include <Events_Error.h>
26 //=================================================================================================
27 bool BuildPlugin_ValidatorBaseForBuild::isValid(const AttributePtr& theAttribute,
28 const std::list<std::string>& theArguments,
29 std::string& theError) const
31 // Get base objects list.
32 if(theAttribute->attributeType() != ModelAPI_AttributeSelectionList::typeId()) {
33 Events_Error::send("Error: BuildPlugin_ValidatorBaseForBuild does not support attribute type \"" + theAttribute->attributeType()
34 + "\"\n Only \"" + ModelAPI_AttributeSelectionList::typeId() + "\" supported.");
37 AttributeSelectionListPtr aSelectionList = std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
38 if(!aSelectionList.get()) {
39 theError = "Could not get selection list.";
42 if(aSelectionList->size() == 0) {
43 theError = "Empty selection list.";
47 // Collect base shapes.
48 for(int anIndex = 0; anIndex < aSelectionList->size(); ++anIndex) {
49 AttributeSelectionPtr aSelection = aSelectionList->value(anIndex);
50 if(!aSelection.get()) {
51 theError = "Could not get selection.";
54 ResultPtr aContext = aSelection->context();
56 theError = "Attribute have empty context.";
60 GeomShapePtr aShape = aSelection->value();
61 GeomShapePtr aContextShape = aContext->shape();
63 aShape = aContextShape;
66 theError = "Empty shape selected.";
70 // Check that shapes has acceptable type.
71 GeomValidators_ShapeType aValidatorShapeType;
72 if(!aValidatorShapeType.isValid(aSelection, theArguments, theError)) {
76 // Check that it is shape on sketch.
77 ResultConstructionPtr aConstruction = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aContext);
78 if(aConstruction.get()) {
79 if(aConstruction->isInfinite()) {
80 theError = "Inifinte objects not acceptable.";
84 std::shared_ptr<GeomAPI_PlanarEdges> anEdges = std::dynamic_pointer_cast<GeomAPI_PlanarEdges>(aContextShape);
86 if(aShape->isEqual(aContextShape)) {
87 // It is whole sketch.
95 if(!aShape->isEqual(aContextShape)) {
96 // Local selection on body does not allowed.
97 theError = "Selected shape is in the local selection. Only global selection is allowed.";
105 //=================================================================================================
106 bool BuildPlugin_ValidatorBaseForWire::isValid(const std::shared_ptr<ModelAPI_Feature>& theFeature,
107 const std::list<std::string>& theArguments,
108 std::string& theError) const
111 if(theArguments.size() != 1) {
112 Events_Error::send("Error: BuildPlugin_ValidatorBaseForWire should be used only with 1 parameter (ID of base objects list).");
115 AttributeSelectionListPtr aSelectionList = theFeature->selectionList(theArguments.front());
116 if(!aSelectionList.get()) {
117 theError = "Empty attribute \"" + theArguments.front() + "\".";
122 // Collect base shapes.
123 ListOfShape aListOfShapes;
124 for(int anIndex = 0; anIndex < aSelectionList->size(); ++anIndex) {
125 AttributeSelectionPtr aSelection = aSelectionList->value(anIndex);
126 GeomShapePtr aShape = aSelection->value();
128 aShape = aSelection->context()->shape();
130 aListOfShapes.push_back(aShape);
134 GeomShapePtr aWire = GeomAlgoAPI_WireBuilder::wire(aListOfShapes);
136 theError = "Result wire empty. Probably it has disconnected edges or non-manifold.";
143 //=================================================================================================
144 bool BuildPlugin_ValidatorBaseForWire::isNotObligatory(std::string theFeature, std::string theAttribute)
149 //=================================================================================================
150 bool BuildPlugin_ValidatorBaseForFace::isValid(const std::shared_ptr<ModelAPI_Feature>& theFeature,
151 const std::list<std::string>& theArguments,
152 std::string& theError) const
155 if(theArguments.size() != 1) {
156 Events_Error::send("Error: BuildPlugin_ValidatorBaseForFace should be used only with 1 parameter (ID of base objects list).");
159 AttributeSelectionListPtr aSelectionList = theFeature->selectionList(theArguments.front());
160 if(!aSelectionList.get()) {
161 theError = "Empty attribute \"" + theArguments.front() + "\".";
165 // Collect base shapes.
167 for(int anIndex = 0; anIndex < aSelectionList->size(); ++anIndex) {
168 AttributeSelectionPtr aSelection = aSelectionList->value(anIndex);
169 GeomShapePtr aShape = aSelection->value();
171 aShape = aSelection->context()->shape();
173 for(GeomAPI_ShapeExplorer anExp(aShape, GeomAPI_Shape::EDGE); anExp.more(); anExp.next()) {
174 GeomShapePtr anEdge = anExp.current();
175 anEdges.push_back(anEdge);
179 // Check that edges does not have intersections.
180 if(anEdges.size() > 1) {
181 GeomAlgoAPI_PaveFiller aPaveFiller(anEdges, false);
182 if(!aPaveFiller.isDone()) {
183 theError = "Error while checking if edges intersects.";
186 GeomShapePtr aSectedEdges = aPaveFiller.shape();
189 for(GeomAPI_ShapeExplorer anExp(aSectedEdges, GeomAPI_Shape::EDGE); anExp.more(); anExp.next()) {
192 if(anEdgesNum != anEdges.size()) {
193 theError = "Selected objects have intersections.";
198 // Check that they are planar.
199 std::shared_ptr<GeomAPI_Pln> aPln = GeomAlgoAPI_ShapeTools::findPlane(anEdges);
201 theError = "Selected objects are not planar.";
205 // Check that selected objects have closed contours.
207 GeomAlgoAPI_SketchBuilder::createFaces(aPln->location(), aPln->xDirection(), aPln->direction(), anEdges, aFaces);
209 theError = "Selected objects does not have closed contours.";
216 //=================================================================================================
217 bool BuildPlugin_ValidatorBaseForFace::isNotObligatory(std::string theFeature, std::string theAttribute)