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 "BuildPlugin_Validators.h"
22 #include <ModelAPI_AttributeSelectionList.h>
23 #include <ModelAPI_ResultConstruction.h>
25 #include <GeomAPI_PlanarEdges.h>
26 #include <GeomAPI_Pln.h>
27 #include <GeomAPI_ShapeExplorer.h>
28 #include <GeomAPI_ShapeIterator.h>
30 #include <GeomAlgoAPI_CompoundBuilder.h>
31 #include <GeomAlgoAPI_PaveFiller.h>
32 #include <GeomAlgoAPI_ShapeBuilder.h>
33 #include <GeomAlgoAPI_ShapeTools.h>
34 #include <GeomAlgoAPI_SketchBuilder.h>
35 #include <GeomAlgoAPI_WireBuilder.h>
36 #include <GeomAlgoAPI_MakeVolume.h>
37 #include <GeomAlgoAPI_Tools.h>
39 #include <GeomValidators_FeatureKind.h>
40 #include <GeomValidators_ShapeType.h>
42 #include <Events_InfoMessage.h>
44 //=================================================================================================
45 bool BuildPlugin_ValidatorBaseForBuild::isValid(const AttributePtr& theAttribute,
46 const std::list<std::string>& theArguments,
47 Events_InfoMessage& theError) const
49 // Get base objects list.
50 if(theAttribute->attributeType() != ModelAPI_AttributeSelectionList::typeId()) {
51 std::string aMsg = "Error: BuildPlugin_ValidatorBaseForBuild does "
52 "not support attribute type '%1'\nOnly '%2' is supported.";
53 Events_InfoMessage("BuildPlugin_Validators", aMsg).
54 arg(theAttribute->attributeType()).arg(ModelAPI_AttributeSelectionList::typeId()).send();
57 AttributeSelectionListPtr aSelectionList =
58 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
59 if(!aSelectionList.get()) {
60 theError = "Could not get selection list.";
63 if(aSelectionList->size() == 0) {
64 theError = "Empty selection list.";
68 // Collect base shapes.
69 for(int anIndex = 0; anIndex < aSelectionList->size(); ++anIndex) {
70 AttributeSelectionPtr aSelection = aSelectionList->value(anIndex);
71 if(!aSelection.get()) {
72 theError = "Could not get selection.";
75 ResultPtr aContext = aSelection->context();
77 theError = "Attribute have empty context.";
81 GeomShapePtr aShape = aSelection->value();
82 GeomShapePtr aContextShape = aContext->shape();
84 aShape = aContextShape;
87 theError = "Empty shape selected.";
91 // Check that shapes has acceptable type.
92 GeomValidators_ShapeType aValidatorShapeType;
93 if(!aValidatorShapeType.isValid(aSelection, theArguments, theError)) {
97 // Check that it is shape on sketch.
98 ResultConstructionPtr aConstruction =
99 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aContext);
100 if(aConstruction.get()) {
101 if(aConstruction->isInfinite()) {
102 theError = "Inifinte objects not acceptable.";
106 std::shared_ptr<GeomAPI_PlanarEdges> anEdges =
107 std::dynamic_pointer_cast<GeomAPI_PlanarEdges>(aContextShape);
109 if(aShape->isEqual(aContextShape)) {
110 // It is whole sketch.
122 //=================================================================================================
123 bool BuildPlugin_ValidatorBaseForWire::isValid(const std::shared_ptr<ModelAPI_Feature>& theFeature,
124 const std::list<std::string>& theArguments,
125 Events_InfoMessage& theError) const
128 if(theArguments.size() != 1) {
129 std::string aMsg = "Error: BuildPlugin_ValidatorBaseForWire should be used only "
130 "with 1 parameter (ID of base objects list).";
131 Events_InfoMessage("BuildPlugin_Validators", aMsg).send();
134 AttributeSelectionListPtr aSelectionList = theFeature->selectionList(theArguments.front());
135 if(!aSelectionList.get()) {
136 theError = "Empty attribute \"%1\".";
137 theError.arg(theArguments.front());
142 // Collect base shapes.
143 ListOfShape aListOfShapes;
144 for(int anIndex = 0; anIndex < aSelectionList->size(); ++anIndex) {
145 AttributeSelectionPtr aSelection = aSelectionList->value(anIndex);
146 GeomShapePtr aShape = aSelection->value();
148 if (aSelection->context().get())
149 aShape = aSelection->context()->shape();
152 aListOfShapes.push_back(aShape);
156 GeomShapePtr aWire = GeomAlgoAPI_WireBuilder::wire(aListOfShapes);
158 theError = "Result wire empty. Probably it has disconnected edges or non-manifold.";
165 //=================================================================================================
166 bool BuildPlugin_ValidatorBaseForFace::isValid(const std::shared_ptr<ModelAPI_Feature>& theFeature,
167 const std::list<std::string>& theArguments,
168 Events_InfoMessage& theError) const
171 if(theArguments.size() != 1) {
172 std::string aMsg = "Error: BuildPlugin_ValidatorBaseForFace should be used only with "
173 "1 parameter (ID of base objects list).";
174 Events_InfoMessage("BuildPlugin_Validators", aMsg).send();
177 AttributeSelectionListPtr aSelectionList = theFeature->selectionList(theArguments.front());
178 if(!aSelectionList.get()) {
179 theError = "Empty attribute \"%1\".";
180 theError.arg(theArguments.front());
184 bool hasEdgesOrWires = false;
185 bool hasFaces = false;
187 // Collect base shapes.
189 for(int anIndex = 0; anIndex < aSelectionList->size(); ++anIndex) {
190 AttributeSelectionPtr aSelection = aSelectionList->value(anIndex);
191 GeomShapePtr aShape = aSelection->value();
193 if (!aSelection->context()) {
194 theError = "Objects are not selected.";
197 aShape = aSelection->context()->shape();
199 if (aShape->shapeType() == GeomAPI_Shape::FACE) {
200 // skip faces exploding
205 for(GeomAPI_ShapeExplorer anExp(aShape, GeomAPI_Shape::EDGE); anExp.more(); anExp.next()) {
206 hasEdgesOrWires = true;
207 GeomShapePtr anEdge = anExp.current();
208 anEdges.push_back(anEdge);
212 if (hasFaces && hasEdgesOrWires) {
213 theError = "Faces and edges/wires should be selected together.";
215 } else if (hasEdgesOrWires && anEdges.empty()) {
216 theError = "Objects are not selected.";
220 // Check that edges does not have intersections.
221 if(anEdges.size() > 1) {
222 GeomAlgoAPI_PaveFiller aPaveFiller(anEdges, false);
223 if(!aPaveFiller.isDone()) {
224 theError = "Error while checking if edges intersects.";
227 GeomShapePtr aSectedEdges = aPaveFiller.shape();
230 for(GeomAPI_ShapeExplorer
231 anExp(aSectedEdges, GeomAPI_Shape::EDGE); anExp.more(); anExp.next()) {
234 if(anEdgesNum != anEdges.size()) {
235 theError = "Selected objects have intersections.";
240 if (!anEdges.empty()) {
241 // Check that they are planar.
242 std::shared_ptr<GeomAPI_Pln> aPln = GeomAlgoAPI_ShapeTools::findPlane(anEdges);
244 theError = "Selected object(s) should belong to only one plane.";
248 // Check that selected objects have closed contours.
250 GeomAlgoAPI_SketchBuilder::createFaces(aPln->location(), aPln->xDirection(),
251 aPln->direction(), anEdges, aFaces);
253 theError = "Selected objects do not generate closed contour.";
261 //=================================================================================================
262 bool BuildPlugin_ValidatorBaseForSolids::isValid(
263 const std::shared_ptr<ModelAPI_Feature>& theFeature, const std::list<std::string>& theArguments,
264 Events_InfoMessage& theError) const
266 // Get base objects list.
267 AttributeSelectionListPtr aSelectionList = theFeature->selectionList(theArguments.front());
268 if (!aSelectionList.get()) {
269 theError = "Could not get selection list.";
272 if (aSelectionList->size() == 0) {
273 theError = "Empty selection list.";
277 // Collect base shapes.
278 ListOfShape anOriginalShapes;
279 for (int anIndex = 0; anIndex < aSelectionList->size(); ++anIndex) {
280 AttributeSelectionPtr aSelection = aSelectionList->value(anIndex);
281 if (!aSelection->context().get()) {
282 theError = "Invalid selection.";
285 GeomShapePtr aShape = aSelection->value();
287 aShape = aSelection->context()->shape();
288 anOriginalShapes.push_back(aShape);
291 std::shared_ptr<GeomAlgoAPI_MakeVolume> anAlgorithm(
292 new GeomAlgoAPI_MakeVolume(anOriginalShapes, false));
295 if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(anAlgorithm, "MakeVolume", anErr)) {
300 // set of allowed types of results
301 std::set<GeomAPI_Shape::ShapeType> aResultType;
302 std::string aType = theArguments.back();
303 if (aType == "solid")
304 aResultType.insert(GeomAPI_Shape::SOLID);
305 else if (aType == "compsolid") {
306 aResultType.insert(GeomAPI_Shape::COMPSOLID);
307 aResultType.insert(GeomAPI_Shape::SOLID);
310 GeomShapePtr aCompound = anAlgorithm->shape();
311 if (aCompound->shapeType() == GeomAPI_Shape::COMPOUND) {
312 GeomAPI_ShapeIterator anIt(aCompound);
313 GeomShapePtr aFoundSub;
314 for (; anIt.more() && !aFoundSub; anIt.next()) {
315 aFoundSub = anIt.current();
316 if (aResultType.count(aFoundSub->shapeType()) == 0) {
317 theError = "Unable to build a solid";
321 if (anIt.more() || !aFoundSub.get()) {
322 theError = "Unable to build a solid";
325 } else if (aResultType.count(aCompound->shapeType()) == 0) {
326 theError = "Unable to build a solid";
329 // check the internal faces presence
330 for(GeomAPI_ShapeExplorer aFaces(aCompound, GeomAPI_Shape::FACE); aFaces.more(); aFaces.next()) {
331 if (aFaces.current()->orientation() == GeomAPI_Shape::INTERNAL) {
332 theError = "Internal faces are not allowed in the resulting solid";
341 //=================================================================================================
342 bool BuildPlugin_ValidatorSubShapesSelection::isValid(const AttributePtr& theAttribute,
343 const std::list<std::string>& theArguments,
344 Events_InfoMessage& theError) const
346 if(theArguments.size() != 1) {
347 std::string aMsg = "Error: BuildPlugin_ValidatorSubShapesSelection should be used only with "
348 "1 parameter(Sketch feature id).";
349 Events_InfoMessage("BuildPlugin_Validators", aMsg).send();
353 // Get base objects list.
354 if(theAttribute->attributeType() != ModelAPI_AttributeSelectionList::typeId()) {
356 "Error: BuildPlugin_ValidatorSubShapesSelection does not support attribute type \""
357 "%1\"\n Only \"%2\" supported.";
358 Events_InfoMessage("BuildPlugin_Validators", aMsg).
359 arg(theAttribute->attributeType()).arg(ModelAPI_AttributeSelectionList::typeId()).send();
362 AttributeSelectionListPtr aSelectionList =
363 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
364 if(!aSelectionList.get()) {
365 theError = "Could not get selection list.";
370 const std::string aBaseShapeId = "base_shape";
371 FeaturePtr aFeature = ModelAPI_Feature::feature(theAttribute->owner());
372 AttributeSelectionPtr aShapeAttrSelection = aFeature->selection(aBaseShapeId);
374 if(!aShapeAttrSelection.get()) {
375 theError = "Base shape is empty.";
379 ResultPtr aBaseContext = aShapeAttrSelection->context();
381 GeomShapePtr aBaseShape = aShapeAttrSelection->value();
382 if(!aBaseShape.get()) {
383 theError = "Base shape is empty.";
387 GeomAlgoAPI_ShapeBuilder aBuilder;
388 aBuilder.removeInternal(aBaseShape);
389 aBaseShape = aBuilder.shape();
391 // If selected shape is wire allow to select only vertices. If face - allow vertices and edges.
392 std::set<GeomAPI_Shape::ShapeType> anAllowedTypes;
393 switch(aBaseShape->shapeType()) {
394 case GeomAPI_Shape::FACE: anAllowedTypes.insert(GeomAPI_Shape::EDGE);
395 case GeomAPI_Shape::WIRE: anAllowedTypes.insert(GeomAPI_Shape::VERTEX);
399 // Check selected shapes.
400 GeomValidators_FeatureKind aFeatureKindValidator;
401 std::list<std::string> anArguments;
402 anArguments.push_back(theArguments.front());
403 for(int anIndex = 0; anIndex < aSelectionList->size(); ++anIndex) {
404 AttributeSelectionPtr aSelectionAttrInList = aSelectionList->value(anIndex);
405 if(!aSelectionAttrInList.get()) {
406 theError = "Empty attribute in list.";
410 // If context of selection same skip.
411 if(aBaseContext == aSelectionAttrInList->context()) {
415 // Check that it is a selection on Sketch.
416 if(!aFeatureKindValidator.isValid(aSelectionAttrInList, anArguments, theError)) {
421 GeomShapePtr aShapeInList = aSelectionAttrInList->value();
422 if(!aShapeInList.get()) {
423 aShapeInList = aSelectionAttrInList->context()->shape();
425 if(anAllowedTypes.find(aShapeInList->shapeType()) == anAllowedTypes.cend()) {
426 theError = "Selected shape has unacceptable type.";
430 // Check that shape inside wire or face.
431 if(!GeomAlgoAPI_ShapeTools::isSubShapeInsideShape(aShapeInList, aBaseShape)) {
432 theError = "Selected shape is not inside base face.";
441 //=================================================================================================
442 bool BuildPlugin_ValidatorFillingSelection::isValid(const AttributePtr& theAttribute,
443 const std::list<std::string>& theArguments,
444 Events_InfoMessage& theError) const
446 // Get base objects list.
447 if (theAttribute->attributeType() != ModelAPI_AttributeSelectionList::typeId()) {
449 "Error: BuildPlugin_ValidatorFillingSelection does not support attribute type \""
450 "%1\"\n Only \"%2\" supported.";
451 Events_InfoMessage("BuildPlugin_Validators", aMsg).
452 arg(theAttribute->attributeType()).arg(ModelAPI_AttributeSelectionList::typeId()).send();
455 AttributeSelectionListPtr aSelectionList =
456 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
457 if (!aSelectionList.get()) {
458 theError = "Could not get selection list.";
462 FeaturePtr anOwner = ModelAPI_Feature::feature(theAttribute->owner());
464 // Check selected shapes.
465 for (int anIndex = 0; anIndex < aSelectionList->size(); ++anIndex) {
466 AttributeSelectionPtr aSelectionAttrInList = aSelectionList->value(anIndex);
467 if (!aSelectionAttrInList.get()) {
468 theError = "Empty attribute in list.";
472 // Check shape exists.
473 GeomShapePtr aShapeInList = aSelectionAttrInList->value();
474 if (!aShapeInList.get()) {
475 theError = "Object has no shape";
480 GeomAPI_Shape::ShapeType aType = aShapeInList->shapeType();
481 if (aType != GeomAPI_Shape::EDGE && aType != GeomAPI_Shape::WIRE) {
482 theError = "Incorrect objects selected";