1 // Copyright (C) 2014-2017 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
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
21 #include "BuildPlugin_Validators.h"
23 #include <ModelAPI_AttributeSelectionList.h>
24 #include <ModelAPI_ResultConstruction.h>
26 #include <GeomAPI_PlanarEdges.h>
27 #include <GeomAPI_Pln.h>
28 #include <GeomAPI_ShapeExplorer.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 <GeomAPI_ShapeIterator.h>
38 #include <GeomAPI_ShapeExplorer.h>
40 #include <GeomValidators_FeatureKind.h>
41 #include <GeomValidators_ShapeType.h>
43 #include <Events_InfoMessage.h>
45 //=================================================================================================
46 bool BuildPlugin_ValidatorBaseForBuild::isValid(const AttributePtr& theAttribute,
47 const std::list<std::string>& theArguments,
48 Events_InfoMessage& theError) const
50 // Get base objects list.
51 if(theAttribute->attributeType() != ModelAPI_AttributeSelectionList::typeId()) {
52 std::string aMsg = "Error: BuildPlugin_ValidatorBaseForBuild does "
53 "not support attribute type '%1'\nOnly '%2' is supported.";
54 Events_InfoMessage("BuildPlugin_Validators", aMsg).
55 arg(theAttribute->attributeType()).arg(ModelAPI_AttributeSelectionList::typeId()).send();
58 AttributeSelectionListPtr aSelectionList =
59 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
60 if(!aSelectionList.get()) {
61 theError = "Could not get selection list.";
64 if(aSelectionList->size() == 0) {
65 theError = "Empty selection list.";
69 // Collect base shapes.
70 for(int anIndex = 0; anIndex < aSelectionList->size(); ++anIndex) {
71 AttributeSelectionPtr aSelection = aSelectionList->value(anIndex);
72 if(!aSelection.get()) {
73 theError = "Could not get selection.";
76 ResultPtr aContext = aSelection->context();
78 theError = "Attribute have empty context.";
82 GeomShapePtr aShape = aSelection->value();
83 GeomShapePtr aContextShape = aContext->shape();
85 aShape = aContextShape;
88 theError = "Empty shape selected.";
92 // Check that shapes has acceptable type.
93 GeomValidators_ShapeType aValidatorShapeType;
94 if(!aValidatorShapeType.isValid(aSelection, theArguments, theError)) {
98 // Check that it is shape on sketch.
99 ResultConstructionPtr aConstruction =
100 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aContext);
101 if(aConstruction.get()) {
102 if(aConstruction->isInfinite()) {
103 theError = "Inifinte objects not acceptable.";
107 std::shared_ptr<GeomAPI_PlanarEdges> anEdges =
108 std::dynamic_pointer_cast<GeomAPI_PlanarEdges>(aContextShape);
110 if(aShape->isEqual(aContextShape)) {
111 // It is whole sketch.
123 //=================================================================================================
124 bool BuildPlugin_ValidatorBaseForWire::isValid(const std::shared_ptr<ModelAPI_Feature>& theFeature,
125 const std::list<std::string>& theArguments,
126 Events_InfoMessage& theError) const
129 if(theArguments.size() != 1) {
130 std::string aMsg = "Error: BuildPlugin_ValidatorBaseForWire should be used only "
131 "with 1 parameter (ID of base objects list).";
132 Events_InfoMessage("BuildPlugin_Validators", aMsg).send();
135 AttributeSelectionListPtr aSelectionList = theFeature->selectionList(theArguments.front());
136 if(!aSelectionList.get()) {
137 theError = "Empty attribute \"%1\".";
138 theError.arg(theArguments.front());
143 // Collect base shapes.
144 ListOfShape aListOfShapes;
145 for(int anIndex = 0; anIndex < aSelectionList->size(); ++anIndex) {
146 AttributeSelectionPtr aSelection = aSelectionList->value(anIndex);
147 GeomShapePtr aShape = aSelection->value();
149 if (aSelection->context().get())
150 aShape = aSelection->context()->shape();
153 aListOfShapes.push_back(aShape);
157 GeomShapePtr aWire = GeomAlgoAPI_WireBuilder::wire(aListOfShapes);
159 theError = "Result wire empty. Probably it has disconnected edges or non-manifold.";
166 //=================================================================================================
167 bool BuildPlugin_ValidatorBaseForFace::isValid(const std::shared_ptr<ModelAPI_Feature>& theFeature,
168 const std::list<std::string>& theArguments,
169 Events_InfoMessage& theError) const
172 if(theArguments.size() != 1) {
173 std::string aMsg = "Error: BuildPlugin_ValidatorBaseForFace should be used only with "
174 "1 parameter (ID of base objects list).";
175 Events_InfoMessage("BuildPlugin_Validators", aMsg).send();
178 AttributeSelectionListPtr aSelectionList = theFeature->selectionList(theArguments.front());
179 if(!aSelectionList.get()) {
180 theError = "Empty attribute \"%1\".";
181 theError.arg(theArguments.front());
185 bool hasEdgesOrWires = false;
186 bool hasFaces = false;
188 // Collect base shapes.
190 for(int anIndex = 0; anIndex < aSelectionList->size(); ++anIndex) {
191 AttributeSelectionPtr aSelection = aSelectionList->value(anIndex);
192 GeomShapePtr aShape = aSelection->value();
194 if (!aSelection->context()) {
195 theError = "Objects are not selected.";
198 aShape = aSelection->context()->shape();
200 if (aShape->shapeType() == GeomAPI_Shape::FACE) {
201 // skip faces exploding
206 for(GeomAPI_ShapeExplorer anExp(aShape, GeomAPI_Shape::EDGE); anExp.more(); anExp.next()) {
207 hasEdgesOrWires = true;
208 GeomShapePtr anEdge = anExp.current();
209 anEdges.push_back(anEdge);
213 if (hasFaces && hasEdgesOrWires) {
214 theError = "Faces and edges/wires should be selected together.";
216 } else if (hasEdgesOrWires && anEdges.empty()) {
217 theError = "Objects are not selected.";
221 // Check that edges does not have intersections.
222 if(anEdges.size() > 1) {
223 GeomAlgoAPI_PaveFiller aPaveFiller(anEdges, false);
224 if(!aPaveFiller.isDone()) {
225 theError = "Error while checking if edges intersects.";
228 GeomShapePtr aSectedEdges = aPaveFiller.shape();
231 for(GeomAPI_ShapeExplorer
232 anExp(aSectedEdges, GeomAPI_Shape::EDGE); anExp.more(); anExp.next()) {
235 if(anEdgesNum != anEdges.size()) {
236 theError = "Selected objects have intersections.";
241 if (!anEdges.empty()) {
242 // Check that they are planar.
243 std::shared_ptr<GeomAPI_Pln> aPln = GeomAlgoAPI_ShapeTools::findPlane(anEdges);
245 theError = "Selected object(s) should belong to only one plane.";
249 // Check that selected objects have closed contours.
251 GeomAlgoAPI_SketchBuilder::createFaces(aPln->location(), aPln->xDirection(),
252 aPln->direction(), anEdges, aFaces);
254 theError = "Selected objects do not generate closed contour.";
262 //=================================================================================================
263 bool BuildPlugin_ValidatorBaseForSolids::isValid(
264 const std::shared_ptr<ModelAPI_Feature>& theFeature, const std::list<std::string>& theArguments,
265 Events_InfoMessage& theError) const
267 // Get base objects list.
268 AttributeSelectionListPtr aSelectionList = theFeature->selectionList(theArguments.front());
269 if (!aSelectionList.get()) {
270 theError = "Could not get selection list.";
273 if (aSelectionList->size() == 0) {
274 theError = "Empty selection list.";
278 // Collect base shapes.
279 ListOfShape anOriginalShapes;
280 for (int anIndex = 0; anIndex < aSelectionList->size(); ++anIndex) {
281 AttributeSelectionPtr aSelection = aSelectionList->value(anIndex);
282 GeomShapePtr aShape = aSelection->value();
284 aShape = aSelection->context()->shape();
285 anOriginalShapes.push_back(aShape);
288 std::shared_ptr<GeomAlgoAPI_MakeVolume> anAlgorithm(new GeomAlgoAPI_MakeVolume(anOriginalShapes));
290 if (!anAlgorithm->isDone()) {
291 theError = "MakeVolume algorithm failed.";
294 if (anAlgorithm->shape()->isNull()) {
295 theError = "Resulting shape of MakeVolume is Null.";
298 if (!anAlgorithm->isValid()) {
299 theError = "Resulting shape of MakeVolume is not valid.";
303 // set of allowed types of results
304 std::set<GeomAPI_Shape::ShapeType> aResultType;
305 std::string aType = theArguments.back();
306 if (aType == "solid")
307 aResultType.insert(GeomAPI_Shape::SOLID);
308 else if (aType == "compsolid") {
309 aResultType.insert(GeomAPI_Shape::COMPSOLID);
310 aResultType.insert(GeomAPI_Shape::SOLID);
313 GeomShapePtr aCompound = anAlgorithm->shape();
314 if (aCompound->shapeType() == GeomAPI_Shape::COMPOUND) {
315 GeomAPI_ShapeIterator anIt(aCompound);
316 GeomShapePtr aFoundSub;
317 for (; anIt.more() && !aFoundSub; anIt.next()) {
318 aFoundSub = anIt.current();
319 if (aResultType.count(aFoundSub->shapeType()) == 0) {
320 theError = "Unable to build a solid";
324 if (anIt.more() || !aFoundSub.get()) {
325 theError = "Unable to build a solid";
328 } else if (aResultType.count(aCompound->shapeType()) == 0) {
329 theError = "Unable to build a solid";
332 // check the internal faces presence
333 for(GeomAPI_ShapeExplorer aFaces(aCompound, GeomAPI_Shape::FACE); aFaces.more(); aFaces.next()) {
334 if (aFaces.current()->orientation() == GeomAPI_Shape::INTERNAL) {
335 theError = "Internal faces are not allowed in the resulting solid";
344 //=================================================================================================
345 bool BuildPlugin_ValidatorSubShapesSelection::isValid(const AttributePtr& theAttribute,
346 const std::list<std::string>& theArguments,
347 Events_InfoMessage& theError) const
349 if(theArguments.size() != 1) {
350 std::string aMsg = "Error: BuildPlugin_ValidatorSubShapesSelection should be used only with "
351 "1 parameter(Sketch feature id).";
352 Events_InfoMessage("BuildPlugin_Validators", aMsg).send();
356 // Get base objects list.
357 if(theAttribute->attributeType() != ModelAPI_AttributeSelectionList::typeId()) {
359 "Error: BuildPlugin_ValidatorSubShapesSelection does not support attribute type \""
360 "%1\"\n Only \"%2\" supported.";
361 Events_InfoMessage("BuildPlugin_Validators", aMsg).
362 arg(theAttribute->attributeType()).arg(ModelAPI_AttributeSelectionList::typeId()).send();
365 AttributeSelectionListPtr aSelectionList =
366 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
367 if(!aSelectionList.get()) {
368 theError = "Could not get selection list.";
373 const std::string aBaseShapeId = "base_shape";
374 FeaturePtr aFeature = ModelAPI_Feature::feature(theAttribute->owner());
375 AttributeSelectionPtr aShapeAttrSelection = aFeature->selection(aBaseShapeId);
377 if(!aShapeAttrSelection.get()) {
378 theError = "Base shape is empty.";
382 ResultPtr aBaseContext = aShapeAttrSelection->context();
384 GeomShapePtr aBaseShape = aShapeAttrSelection->value();
385 if(!aBaseShape.get()) {
386 theError = "Base shape is empty.";
390 GeomAlgoAPI_ShapeBuilder aBuilder;
391 aBuilder.removeInternal(aBaseShape);
392 aBaseShape = aBuilder.shape();
394 // If selected shape is wire allow to select only vertices. If face - allow vertices and edges.
395 std::set<GeomAPI_Shape::ShapeType> anAllowedTypes;
396 switch(aBaseShape->shapeType()) {
397 case GeomAPI_Shape::FACE: anAllowedTypes.insert(GeomAPI_Shape::EDGE);
398 case GeomAPI_Shape::WIRE: anAllowedTypes.insert(GeomAPI_Shape::VERTEX);
402 // Check selected shapes.
403 GeomValidators_FeatureKind aFeatureKindValidator;
404 std::list<std::string> anArguments;
405 anArguments.push_back(theArguments.front());
406 for(int anIndex = 0; anIndex < aSelectionList->size(); ++anIndex) {
407 AttributeSelectionPtr aSelectionAttrInList = aSelectionList->value(anIndex);
408 if(!aSelectionAttrInList.get()) {
409 theError = "Empty attribute in list.";
413 // If context of selection same skip.
414 if(aBaseContext == aSelectionAttrInList->context()) {
418 // Check that it is a selection on Sketch.
419 if(!aFeatureKindValidator.isValid(aSelectionAttrInList, anArguments, theError)) {
424 GeomShapePtr aShapeInList = aSelectionAttrInList->value();
425 if(!aShapeInList.get()) {
426 aShapeInList = aSelectionAttrInList->context()->shape();
428 if(anAllowedTypes.find(aShapeInList->shapeType()) == anAllowedTypes.cend()) {
429 theError = "Selected shape has unacceptable type.";
433 // Check that shape inside wire or face.
434 if(!GeomAlgoAPI_ShapeTools::isSubShapeInsideShape(aShapeInList, aBaseShape)) {
435 theError = "Selected shape is not inside base face.";
444 //=================================================================================================
445 bool BuildPlugin_ValidatorFillingSelection::isValid(const AttributePtr& theAttribute,
446 const std::list<std::string>& theArguments,
447 Events_InfoMessage& theError) const
449 // Get base objects list.
450 if (theAttribute->attributeType() != ModelAPI_AttributeSelectionList::typeId()) {
452 "Error: BuildPlugin_ValidatorFillingSelection does not support attribute type \""
453 "%1\"\n Only \"%2\" supported.";
454 Events_InfoMessage("BuildPlugin_Validators", aMsg).
455 arg(theAttribute->attributeType()).arg(ModelAPI_AttributeSelectionList::typeId()).send();
458 AttributeSelectionListPtr aSelectionList =
459 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
460 if (!aSelectionList.get()) {
461 theError = "Could not get selection list.";
465 FeaturePtr anOwner = ModelAPI_Feature::feature(theAttribute->owner());
467 // Check selected shapes.
468 for (int anIndex = 0; anIndex < aSelectionList->size(); ++anIndex) {
469 AttributeSelectionPtr aSelectionAttrInList = aSelectionList->value(anIndex);
470 if (!aSelectionAttrInList.get()) {
471 theError = "Empty attribute in list.";
475 // Check shape exists.
476 GeomShapePtr aShapeInList = aSelectionAttrInList->value();
477 if (!aShapeInList.get()) {
478 theError = "Object has no shape";
483 GeomAPI_Shape::ShapeType aType = aShapeInList->shapeType();
484 if (aType != GeomAPI_Shape::EDGE && aType != GeomAPI_Shape::WIRE) {
485 theError = "Incorrect objects selected";