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.
90 } else if(!aShape->isEqual(aContextShape)) {
91 // Local selection on body does not allowed.
92 theError = "Selected shape is in the local selection. Only global selection is allowed.";
101 //=================================================================================================
102 bool BuildPlugin_ValidatorBaseForWire::isValid(const std::shared_ptr<ModelAPI_Feature>& theFeature,
103 const std::list<std::string>& theArguments,
104 std::string& theError) const
107 if(theArguments.size() != 1) {
108 Events_Error::send("Error: BuildPlugin_ValidatorBaseForWire should be used only with 1 parameter (ID of base objects list).");
111 AttributeSelectionListPtr aSelectionList = theFeature->selectionList(theArguments.front());
112 if(!aSelectionList.get()) {
113 theError = "Empty attribute \"" + theArguments.front() + "\".";
118 // Collect base shapes.
119 ListOfShape aListOfShapes;
120 for(int anIndex = 0; anIndex < aSelectionList->size(); ++anIndex) {
121 AttributeSelectionPtr aSelection = aSelectionList->value(anIndex);
122 GeomShapePtr aShape = aSelection->value();
124 aShape = aSelection->context()->shape();
126 aListOfShapes.push_back(aShape);
130 GeomShapePtr aWire = GeomAlgoAPI_WireBuilder::wire(aListOfShapes);
132 theError = "Result wire empty. Probably it has disconnected edges or non-manifold.";
139 //=================================================================================================
140 bool BuildPlugin_ValidatorBaseForWire::isNotObligatory(std::string theFeature, std::string theAttribute)
145 //=================================================================================================
146 bool BuildPlugin_ValidatorBaseForFace::isValid(const std::shared_ptr<ModelAPI_Feature>& theFeature,
147 const std::list<std::string>& theArguments,
148 std::string& theError) const
151 if(theArguments.size() != 1) {
152 Events_Error::send("Error: BuildPlugin_ValidatorBaseForFace should be used only with 1 parameter (ID of base objects list).");
155 AttributeSelectionListPtr aSelectionList = theFeature->selectionList(theArguments.front());
156 if(!aSelectionList.get()) {
157 theError = "Empty attribute \"" + theArguments.front() + "\".";
161 // Collect base shapes.
163 for(int anIndex = 0; anIndex < aSelectionList->size(); ++anIndex) {
164 AttributeSelectionPtr aSelection = aSelectionList->value(anIndex);
165 GeomShapePtr aShape = aSelection->value();
167 aShape = aSelection->context()->shape();
169 for(GeomAPI_ShapeExplorer anExp(aShape, GeomAPI_Shape::EDGE); anExp.more(); anExp.next()) {
170 GeomShapePtr anEdge = anExp.current();
171 anEdges.push_back(anEdge);
175 // Check that edges does not have intersections.
176 if(anEdges.size() > 1) {
177 GeomAlgoAPI_PaveFiller aPaveFiller(anEdges, false);
178 if(!aPaveFiller.isDone()) {
179 theError = "Error while checking if edges intersects.";
182 GeomShapePtr aSectedEdges = aPaveFiller.shape();
185 for(GeomAPI_ShapeExplorer anExp(aSectedEdges, GeomAPI_Shape::EDGE); anExp.more(); anExp.next()) {
188 if(anEdgesNum != anEdges.size()) {
189 theError = "Selected objects have intersections.";
194 // Check that they are planar.
195 std::shared_ptr<GeomAPI_Pln> aPln = GeomAlgoAPI_ShapeTools::findPlane(anEdges);
197 theError = "Selected objects are not planar.";
201 // Check that selected objects have closed contours.
203 GeomAlgoAPI_SketchBuilder::createFaces(aPln->location(), aPln->xDirection(), aPln->direction(), anEdges, aFaces);
205 theError = "Selected objects does not have closed contours.";
212 //=================================================================================================
213 bool BuildPlugin_ValidatorBaseForFace::isNotObligatory(std::string theFeature, std::string theAttribute)