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>
37 #include <GeomValidators_FeatureKind.h>
38 #include <GeomValidators_ShapeType.h>
40 #include <Events_InfoMessage.h>
42 //=================================================================================================
43 bool BuildPlugin_ValidatorBaseForBuild::isValid(const AttributePtr& theAttribute,
44 const std::list<std::string>& theArguments,
45 Events_InfoMessage& theError) const
47 // Get base objects list.
48 if(theAttribute->attributeType() != ModelAPI_AttributeSelectionList::typeId()) {
49 std::string aMsg = "Error: BuildPlugin_ValidatorBaseForBuild does "
50 "not support attribute type '%1'\nOnly '%2' is supported.";
51 Events_InfoMessage("BuildPlugin_Validators", aMsg).
52 arg(theAttribute->attributeType()).arg(ModelAPI_AttributeSelectionList::typeId()).send();
55 AttributeSelectionListPtr aSelectionList =
56 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
57 if(!aSelectionList.get()) {
58 theError = "Could not get selection list.";
61 if(aSelectionList->size() == 0) {
62 theError = "Empty selection list.";
66 // Collect base shapes.
67 for(int anIndex = 0; anIndex < aSelectionList->size(); ++anIndex) {
68 AttributeSelectionPtr aSelection = aSelectionList->value(anIndex);
69 if(!aSelection.get()) {
70 theError = "Could not get selection.";
73 ResultPtr aContext = aSelection->context();
75 theError = "Attribute have empty context.";
79 GeomShapePtr aShape = aSelection->value();
80 GeomShapePtr aContextShape = aContext->shape();
82 aShape = aContextShape;
85 theError = "Empty shape selected.";
89 // Check that shapes has acceptable type.
90 GeomValidators_ShapeType aValidatorShapeType;
91 if(!aValidatorShapeType.isValid(aSelection, theArguments, theError)) {
95 // Check that it is shape on sketch.
96 ResultConstructionPtr aConstruction =
97 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aContext);
98 if(aConstruction.get()) {
99 if(aConstruction->isInfinite()) {
100 theError = "Inifinte objects not acceptable.";
104 std::shared_ptr<GeomAPI_PlanarEdges> anEdges =
105 std::dynamic_pointer_cast<GeomAPI_PlanarEdges>(aContextShape);
107 if(aShape->isEqual(aContextShape)) {
108 // It is whole sketch.
120 //=================================================================================================
121 bool BuildPlugin_ValidatorBaseForWire::isValid(const std::shared_ptr<ModelAPI_Feature>& theFeature,
122 const std::list<std::string>& theArguments,
123 Events_InfoMessage& theError) const
126 if(theArguments.size() != 1) {
127 std::string aMsg = "Error: BuildPlugin_ValidatorBaseForWire should be used only "
128 "with 1 parameter (ID of base objects list).";
129 Events_InfoMessage("BuildPlugin_Validators", aMsg).send();
132 AttributeSelectionListPtr aSelectionList = theFeature->selectionList(theArguments.front());
133 if(!aSelectionList.get()) {
134 theError = "Empty attribute \"%1\".";
135 theError.arg(theArguments.front());
140 // Collect base shapes.
141 ListOfShape aListOfShapes;
142 for(int anIndex = 0; anIndex < aSelectionList->size(); ++anIndex) {
143 AttributeSelectionPtr aSelection = aSelectionList->value(anIndex);
144 GeomShapePtr aShape = aSelection->value();
146 if (aSelection->context().get())
147 aShape = aSelection->context()->shape();
150 aListOfShapes.push_back(aShape);
154 GeomShapePtr aWire = GeomAlgoAPI_WireBuilder::wire(aListOfShapes);
156 theError = "Result wire empty. Probably it has disconnected edges or non-manifold.";
163 //=================================================================================================
164 bool BuildPlugin_ValidatorBaseForWire::isNotObligatory(std::string theFeature,
165 std::string theAttribute)
170 //=================================================================================================
171 bool BuildPlugin_ValidatorBaseForFace::isValid(const std::shared_ptr<ModelAPI_Feature>& theFeature,
172 const std::list<std::string>& theArguments,
173 Events_InfoMessage& theError) const
176 if(theArguments.size() != 1) {
177 std::string aMsg = "Error: BuildPlugin_ValidatorBaseForFace should be used only with "
178 "1 parameter (ID of base objects list).";
179 Events_InfoMessage("BuildPlugin_Validators", aMsg).send();
182 AttributeSelectionListPtr aSelectionList = theFeature->selectionList(theArguments.front());
183 if(!aSelectionList.get()) {
184 theError = "Empty attribute \"%1\".";
185 theError.arg(theArguments.front());
189 bool hasEdgesOrWires = false;
190 bool hasFaces = false;
192 // Collect base shapes.
194 for(int anIndex = 0; anIndex < aSelectionList->size(); ++anIndex) {
195 AttributeSelectionPtr aSelection = aSelectionList->value(anIndex);
196 GeomShapePtr aShape = aSelection->value();
198 if (!aSelection->context()) {
199 theError = "Objects are not selected.";
202 aShape = aSelection->context()->shape();
204 if (aShape->shapeType() == GeomAPI_Shape::FACE) {
205 // skip faces exploding
210 for(GeomAPI_ShapeExplorer anExp(aShape, GeomAPI_Shape::EDGE); anExp.more(); anExp.next()) {
211 hasEdgesOrWires = true;
212 GeomShapePtr anEdge = anExp.current();
213 anEdges.push_back(anEdge);
217 if (hasFaces && hasEdgesOrWires) {
218 theError = "Faces and edges/wires should be selected together.";
220 } else if (hasEdgesOrWires && anEdges.empty()) {
221 theError = "Objects are not selected.";
225 // Check that edges does not have intersections.
226 if(anEdges.size() > 1) {
227 GeomAlgoAPI_PaveFiller aPaveFiller(anEdges, false);
228 if(!aPaveFiller.isDone()) {
229 theError = "Error while checking if edges intersects.";
232 GeomShapePtr aSectedEdges = aPaveFiller.shape();
235 for(GeomAPI_ShapeExplorer
236 anExp(aSectedEdges, GeomAPI_Shape::EDGE); anExp.more(); anExp.next()) {
239 if(anEdgesNum != anEdges.size()) {
240 theError = "Selected objects have intersections.";
245 if (!anEdges.empty()) {
246 // Check that they are planar.
247 std::shared_ptr<GeomAPI_Pln> aPln = GeomAlgoAPI_ShapeTools::findPlane(anEdges);
249 theError = "Selected object(s) should belong to only one plane.";
253 // Check that selected objects have closed contours.
255 GeomAlgoAPI_SketchBuilder::createFaces(aPln->location(), aPln->xDirection(),
256 aPln->direction(), anEdges, aFaces);
258 theError = "Selected objects do not generate closed contour.";
266 //=================================================================================================
267 bool BuildPlugin_ValidatorBaseForFace::isNotObligatory(std::string theFeature,
268 std::string theAttribute)
273 //=================================================================================================
274 bool BuildPlugin_ValidatorSubShapesSelection::isValid(const AttributePtr& theAttribute,
275 const std::list<std::string>& theArguments,
276 Events_InfoMessage& theError) const
278 if(theArguments.size() != 1) {
279 std::string aMsg = "Error: BuildPlugin_ValidatorSubShapesSelection should be used only with "
280 "1 parameter(Sketch feature id).";
281 Events_InfoMessage("BuildPlugin_Validators", aMsg).send();
285 // Get base objects list.
286 if(theAttribute->attributeType() != ModelAPI_AttributeSelectionList::typeId()) {
288 "Error: BuildPlugin_ValidatorSubShapesSelection does not support attribute type \""
289 "%1\"\n Only \"%2\" supported.";
290 Events_InfoMessage("BuildPlugin_Validators", aMsg).
291 arg(theAttribute->attributeType()).arg(ModelAPI_AttributeSelectionList::typeId()).send();
294 AttributeSelectionListPtr aSelectionList =
295 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
296 if(!aSelectionList.get()) {
297 theError = "Could not get selection list.";
302 const std::string aBaseShapeId = "base_shape";
303 FeaturePtr aFeature = ModelAPI_Feature::feature(theAttribute->owner());
304 AttributeSelectionPtr aShapeAttrSelection = aFeature->selection(aBaseShapeId);
306 if(!aShapeAttrSelection.get()) {
307 theError = "Base shape is empty.";
311 ResultPtr aBaseContext = aShapeAttrSelection->context();
313 GeomShapePtr aBaseShape = aShapeAttrSelection->value();
314 if(!aBaseShape.get()) {
315 theError = "Base shape is empty.";
319 GeomAlgoAPI_ShapeBuilder aBuilder;
320 aBuilder.removeInternal(aBaseShape);
321 aBaseShape = aBuilder.shape();
323 // If selected shape is wire allow to select only vertices. If face - allow vertices and edges.
324 std::set<GeomAPI_Shape::ShapeType> anAllowedTypes;
325 switch(aBaseShape->shapeType()) {
326 case GeomAPI_Shape::FACE: anAllowedTypes.insert(GeomAPI_Shape::EDGE);
327 case GeomAPI_Shape::WIRE: anAllowedTypes.insert(GeomAPI_Shape::VERTEX);
331 // Check selected shapes.
332 GeomValidators_FeatureKind aFeatureKindValidator;
333 std::list<std::string> anArguments;
334 anArguments.push_back(theArguments.front());
335 for(int anIndex = 0; anIndex < aSelectionList->size(); ++anIndex) {
336 AttributeSelectionPtr aSelectionAttrInList = aSelectionList->value(anIndex);
337 if(!aSelectionAttrInList.get()) {
338 theError = "Empty attribute in list.";
342 // If context of selection same skip.
343 if(aBaseContext == aSelectionAttrInList->context()) {
347 // Check that it is a selection on Sketch.
348 if(!aFeatureKindValidator.isValid(aSelectionAttrInList, anArguments, theError)) {
353 GeomShapePtr aShapeInList = aSelectionAttrInList->value();
354 if(!aShapeInList.get()) {
355 aShapeInList = aSelectionAttrInList->context()->shape();
357 if(anAllowedTypes.find(aShapeInList->shapeType()) == anAllowedTypes.cend()) {
358 theError = "Selected shape has unacceptable type.";
362 // Check that shape inside wire or face.
363 if(!GeomAlgoAPI_ShapeTools::isSubShapeInsideShape(aShapeInList, aBaseShape)) {
364 theError = "Selected shape is not inside base face.";
373 //=================================================================================================
374 bool BuildPlugin_ValidatorFillingSelection::isValid(const AttributePtr& theAttribute,
375 const std::list<std::string>& theArguments,
376 Events_InfoMessage& theError) const
378 // Get base objects list.
379 if (theAttribute->attributeType() != ModelAPI_AttributeSelectionList::typeId()) {
381 "Error: BuildPlugin_ValidatorFillingSelection does not support attribute type \""
382 "%1\"\n Only \"%2\" supported.";
383 Events_InfoMessage("BuildPlugin_Validators", aMsg).
384 arg(theAttribute->attributeType()).arg(ModelAPI_AttributeSelectionList::typeId()).send();
387 AttributeSelectionListPtr aSelectionList =
388 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
389 if (!aSelectionList.get()) {
390 theError = "Could not get selection list.";
394 FeaturePtr anOwner = ModelAPI_Feature::feature(theAttribute->owner());
396 // Check selected shapes.
397 for (int anIndex = 0; anIndex < aSelectionList->size(); ++anIndex) {
398 AttributeSelectionPtr aSelectionAttrInList = aSelectionList->value(anIndex);
399 if (!aSelectionAttrInList.get()) {
400 theError = "Empty attribute in list.";
404 // Check shape exists.
405 GeomShapePtr aShapeInList = aSelectionAttrInList->value();
406 if (!aShapeInList.get()) {
407 theError = "Object has no shape";
412 GeomAPI_Shape::ShapeType aType = aShapeInList->shapeType();
413 if (aType != GeomAPI_Shape::EDGE && aType != GeomAPI_Shape::WIRE) {
414 theError = "Incorrect objects selected";