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_ShapeBuilder.h>
19 #include <GeomAlgoAPI_ShapeTools.h>
20 #include <GeomAlgoAPI_SketchBuilder.h>
21 #include <GeomAlgoAPI_WireBuilder.h>
23 #include <GeomValidators_FeatureKind.h>
24 #include <GeomValidators_ShapeType.h>
26 #include <Events_Error.h>
28 //=================================================================================================
29 bool BuildPlugin_ValidatorBaseForBuild::isValid(const AttributePtr& theAttribute,
30 const std::list<std::string>& theArguments,
31 std::string& theError) const
33 // Get base objects list.
34 if(theAttribute->attributeType() != ModelAPI_AttributeSelectionList::typeId()) {
35 Events_Error::send("Error: BuildPlugin_ValidatorBaseForBuild does not support attribute type \""
36 + theAttribute->attributeType() + "\"\n Only \"" + ModelAPI_AttributeSelectionList::typeId()
40 AttributeSelectionListPtr aSelectionList =
41 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
42 if(!aSelectionList.get()) {
43 theError = "Could not get selection list.";
46 if(aSelectionList->size() == 0) {
47 theError = "Empty selection list.";
51 // Collect base shapes.
52 for(int anIndex = 0; anIndex < aSelectionList->size(); ++anIndex) {
53 AttributeSelectionPtr aSelection = aSelectionList->value(anIndex);
54 if(!aSelection.get()) {
55 theError = "Could not get selection.";
58 ResultPtr aContext = aSelection->context();
60 theError = "Attribute have empty context.";
64 GeomShapePtr aShape = aSelection->value();
65 GeomShapePtr aContextShape = aContext->shape();
67 aShape = aContextShape;
70 theError = "Empty shape selected.";
74 // Check that shapes has acceptable type.
75 GeomValidators_ShapeType aValidatorShapeType;
76 if(!aValidatorShapeType.isValid(aSelection, theArguments, theError)) {
80 // Check that it is shape on sketch.
81 ResultConstructionPtr aConstruction = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aContext);
82 if(aConstruction.get()) {
83 if(aConstruction->isInfinite()) {
84 theError = "Inifinte objects not acceptable.";
88 std::shared_ptr<GeomAPI_PlanarEdges> anEdges = std::dynamic_pointer_cast<GeomAPI_PlanarEdges>(aContextShape);
90 if(aShape->isEqual(aContextShape)) {
91 // It is whole sketch.
99 if(!aShape->isEqual(aContextShape)) {
100 // Local selection on body does not allowed.
101 theError = "Selected shape is in the local selection. Only global selection is allowed.";
109 //=================================================================================================
110 bool BuildPlugin_ValidatorBaseForWire::isValid(const std::shared_ptr<ModelAPI_Feature>& theFeature,
111 const std::list<std::string>& theArguments,
112 std::string& theError) const
115 if(theArguments.size() != 1) {
116 Events_Error::send("Error: BuildPlugin_ValidatorBaseForWire should be used only with "
117 "1 parameter (ID of base objects list).");
120 AttributeSelectionListPtr aSelectionList = theFeature->selectionList(theArguments.front());
121 if(!aSelectionList.get()) {
122 theError = "Empty attribute \"" + theArguments.front() + "\".";
127 // Collect base shapes.
128 ListOfShape aListOfShapes;
129 for(int anIndex = 0; anIndex < aSelectionList->size(); ++anIndex) {
130 AttributeSelectionPtr aSelection = aSelectionList->value(anIndex);
131 GeomShapePtr aShape = aSelection->value();
133 aShape = aSelection->context()->shape();
135 aListOfShapes.push_back(aShape);
139 GeomShapePtr aWire = GeomAlgoAPI_WireBuilder::wire(aListOfShapes);
141 theError = "Result wire empty. Probably it has disconnected edges or non-manifold.";
148 //=================================================================================================
149 bool BuildPlugin_ValidatorBaseForWire::isNotObligatory(std::string theFeature, std::string theAttribute)
154 //=================================================================================================
155 bool BuildPlugin_ValidatorBaseForFace::isValid(const std::shared_ptr<ModelAPI_Feature>& theFeature,
156 const std::list<std::string>& theArguments,
157 std::string& theError) const
160 if(theArguments.size() != 1) {
161 Events_Error::send("Error: BuildPlugin_ValidatorBaseForFace should be used only with "
162 "1 parameter (ID of base objects list).");
165 AttributeSelectionListPtr aSelectionList = theFeature->selectionList(theArguments.front());
166 if(!aSelectionList.get()) {
167 theError = "Empty attribute \"" + theArguments.front() + "\".";
171 // Collect base shapes.
173 for(int anIndex = 0; anIndex < aSelectionList->size(); ++anIndex) {
174 AttributeSelectionPtr aSelection = aSelectionList->value(anIndex);
175 GeomShapePtr aShape = aSelection->value();
177 aShape = aSelection->context()->shape();
179 for(GeomAPI_ShapeExplorer anExp(aShape, GeomAPI_Shape::EDGE); anExp.more(); anExp.next()) {
180 GeomShapePtr anEdge = anExp.current();
181 anEdges.push_back(anEdge);
185 // Check that edges does not have intersections.
186 if(anEdges.size() > 1) {
187 GeomAlgoAPI_PaveFiller aPaveFiller(anEdges, false);
188 if(!aPaveFiller.isDone()) {
189 theError = "Error while checking if edges intersects.";
192 GeomShapePtr aSectedEdges = aPaveFiller.shape();
195 for(GeomAPI_ShapeExplorer anExp(aSectedEdges, GeomAPI_Shape::EDGE); anExp.more(); anExp.next()) {
198 if(anEdgesNum != anEdges.size()) {
199 theError = "Selected objects have intersections.";
204 // Check that they are planar.
205 std::shared_ptr<GeomAPI_Pln> aPln = GeomAlgoAPI_ShapeTools::findPlane(anEdges);
207 theError = "Selected objects are not planar.";
211 // Check that selected objects have closed contours.
213 GeomAlgoAPI_SketchBuilder::createFaces(aPln->location(), aPln->xDirection(), aPln->direction(), anEdges, aFaces);
215 theError = "Selected objects does not have closed contours.";
222 //=================================================================================================
223 bool BuildPlugin_ValidatorBaseForFace::isNotObligatory(std::string theFeature, std::string theAttribute)
228 //=================================================================================================
229 bool BuildPlugin_ValidatorSubShapesSelection::isValid(const AttributePtr& theAttribute,
230 const std::list<std::string>& theArguments,
231 std::string& theError) const
233 if(theArguments.size() != 1) {
234 Events_Error::send("Error: BuildPlugin_ValidatorSubShapesSelection should be used only with "
235 "1 parameter(Sketch feature id).");
239 // Get base objects list.
240 if(theAttribute->attributeType() != ModelAPI_AttributeSelectionList::typeId()) {
241 Events_Error::send("Error: BuildPlugin_ValidatorSubShapesSelection does not support attribute type \""
242 + theAttribute->attributeType() + "\"\n Only \"" + ModelAPI_AttributeSelectionList::typeId()
246 AttributeSelectionListPtr aSelectionList =
247 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
248 if(!aSelectionList.get()) {
249 theError = "Could not get selection list.";
254 const std::string aBaseShapeId = "base_shape";
255 FeaturePtr aFeature = ModelAPI_Feature::feature(theAttribute->owner());
256 AttributeSelectionPtr aShapeAttrSelection = aFeature->selection(aBaseShapeId);
258 if(!aShapeAttrSelection.get()) {
259 theError = "Base shape is empty.";
263 ResultPtr aBaseContext = aShapeAttrSelection->context();
265 GeomShapePtr aBaseShape = aShapeAttrSelection->value();
266 if(!aBaseShape.get()) {
267 theError = "Base shape is empty.";
271 GeomAlgoAPI_ShapeBuilder aBuilder;
272 aBuilder.removeInternal(aBaseShape);
273 aBaseShape = aBuilder.shape();
275 // If selected shape is wire allow to select only vertices. If face - allow vertices and edges.
276 std::set<GeomAPI_Shape::ShapeType> anAllowedTypes;
277 switch(aBaseShape->shapeType()) {
278 case GeomAPI_Shape::FACE: anAllowedTypes.insert(GeomAPI_Shape::EDGE);
279 case GeomAPI_Shape::WIRE: anAllowedTypes.insert(GeomAPI_Shape::VERTEX);
283 // Check selected shapes.
284 GeomValidators_FeatureKind aFeatureKindValidator;
285 std::list<std::string> anArguments;
286 anArguments.push_back(theArguments.front());
287 for(int anIndex = 0; anIndex < aSelectionList->size(); ++anIndex) {
288 AttributeSelectionPtr aSelectionAttrInList = aSelectionList->value(anIndex);
289 if(!aSelectionAttrInList.get()) {
290 theError = "Empty attribute in list.";
294 // If context of selection same skip.
295 if(aBaseContext == aSelectionAttrInList->context()) {
299 // Check that it is a selection on Sketch.
300 if(!aFeatureKindValidator.isValid(aSelectionAttrInList, anArguments, theError)) {
305 GeomShapePtr aShapeInList = aSelectionAttrInList->value();
306 if(!aShapeInList.get()) {
307 aShapeInList = aSelectionAttrInList->context()->shape();
309 if(anAllowedTypes.find(aShapeInList->shapeType()) == anAllowedTypes.cend()) {
310 theError = "Selected shape has unacceptable type.";
314 // Check that shape inside wire or face.
315 if(!GeomAlgoAPI_ShapeTools::isSubShapeInsideShape(aShapeInList, aBaseShape)) {
316 theError = "Selected shape is not inside base face.";