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 email : webmaster.salome@opencascade.com<mailto: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>
29 #include <GeomAlgoAPI_CompoundBuilder.h>
30 #include <GeomAlgoAPI_PaveFiller.h>
31 #include <GeomAlgoAPI_ShapeBuilder.h>
32 #include <GeomAlgoAPI_ShapeTools.h>
33 #include <GeomAlgoAPI_SketchBuilder.h>
34 #include <GeomAlgoAPI_WireBuilder.h>
36 #include <GeomValidators_FeatureKind.h>
37 #include <GeomValidators_ShapeType.h>
39 #include <Events_InfoMessage.h>
41 //=================================================================================================
42 bool BuildPlugin_ValidatorBaseForBuild::isValid(const AttributePtr& theAttribute,
43 const std::list<std::string>& theArguments,
44 Events_InfoMessage& theError) const
46 // Get base objects list.
47 if(theAttribute->attributeType() != ModelAPI_AttributeSelectionList::typeId()) {
48 std::string aMsg = "Error: BuildPlugin_ValidatorBaseForBuild does "
49 "not support attribute type '%1'\nOnly '%2' is supported.";
50 Events_InfoMessage("BuildPlugin_Validators", aMsg).
51 arg(theAttribute->attributeType()).arg(ModelAPI_AttributeSelectionList::typeId()).send();
54 AttributeSelectionListPtr aSelectionList =
55 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
56 if(!aSelectionList.get()) {
57 theError = "Could not get selection list.";
60 if(aSelectionList->size() == 0) {
61 theError = "Empty selection list.";
65 // Collect base shapes.
66 for(int anIndex = 0; anIndex < aSelectionList->size(); ++anIndex) {
67 AttributeSelectionPtr aSelection = aSelectionList->value(anIndex);
68 if(!aSelection.get()) {
69 theError = "Could not get selection.";
72 ResultPtr aContext = aSelection->context();
74 theError = "Attribute have empty context.";
78 GeomShapePtr aShape = aSelection->value();
79 GeomShapePtr aContextShape = aContext->shape();
81 aShape = aContextShape;
84 theError = "Empty shape selected.";
88 // Check that shapes has acceptable type.
89 GeomValidators_ShapeType aValidatorShapeType;
90 if(!aValidatorShapeType.isValid(aSelection, theArguments, theError)) {
94 // Check that it is shape on sketch.
95 ResultConstructionPtr aConstruction =
96 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aContext);
97 if(aConstruction.get()) {
98 if(aConstruction->isInfinite()) {
99 theError = "Inifinte objects not acceptable.";
103 std::shared_ptr<GeomAPI_PlanarEdges> anEdges =
104 std::dynamic_pointer_cast<GeomAPI_PlanarEdges>(aContextShape);
106 if(aShape->isEqual(aContextShape)) {
107 // It is whole sketch.
115 if(!aShape->isEqual(aContextShape)) {
116 // Local selection on body does not allowed.
117 theError = "Selected shape is in the local selection. Only global selection is allowed.";
125 //=================================================================================================
126 bool BuildPlugin_ValidatorBaseForWire::isValid(const std::shared_ptr<ModelAPI_Feature>& theFeature,
127 const std::list<std::string>& theArguments,
128 Events_InfoMessage& theError) const
131 if(theArguments.size() != 1) {
132 std::string aMsg = "Error: BuildPlugin_ValidatorBaseForWire should be used only "
133 "with 1 parameter (ID of base objects list).";
134 Events_InfoMessage("BuildPlugin_Validators", aMsg).send();
137 AttributeSelectionListPtr aSelectionList = theFeature->selectionList(theArguments.front());
138 if(!aSelectionList.get()) {
139 theError = "Empty attribute \"%1\".";
140 theError.arg(theArguments.front());
145 // Collect base shapes.
146 ListOfShape aListOfShapes;
147 for(int anIndex = 0; anIndex < aSelectionList->size(); ++anIndex) {
148 AttributeSelectionPtr aSelection = aSelectionList->value(anIndex);
149 GeomShapePtr aShape = aSelection->value();
151 if (aSelection->context().get())
152 aShape = aSelection->context()->shape();
155 aListOfShapes.push_back(aShape);
159 GeomShapePtr aWire = GeomAlgoAPI_WireBuilder::wire(aListOfShapes);
161 theError = "Result wire empty. Probably it has disconnected edges or non-manifold.";
168 //=================================================================================================
169 bool BuildPlugin_ValidatorBaseForWire::isNotObligatory(std::string theFeature,
170 std::string theAttribute)
175 //=================================================================================================
176 bool BuildPlugin_ValidatorBaseForFace::isValid(const std::shared_ptr<ModelAPI_Feature>& theFeature,
177 const std::list<std::string>& theArguments,
178 Events_InfoMessage& theError) const
181 if(theArguments.size() != 1) {
182 std::string aMsg = "Error: BuildPlugin_ValidatorBaseForFace should be used only with "
183 "1 parameter (ID of base objects list).";
184 Events_InfoMessage("BuildPlugin_Validators", aMsg).send();
187 AttributeSelectionListPtr aSelectionList = theFeature->selectionList(theArguments.front());
188 if(!aSelectionList.get()) {
189 theError = "Empty attribute \"%1\".";
190 theError.arg(theArguments.front());
194 // Collect base shapes.
196 for(int anIndex = 0; anIndex < aSelectionList->size(); ++anIndex) {
197 AttributeSelectionPtr aSelection = aSelectionList->value(anIndex);
198 GeomShapePtr aShape = aSelection->value();
200 if (!aSelection->context()) {
201 theError = "Objects are not selected.";
204 aShape = aSelection->context()->shape();
206 for(GeomAPI_ShapeExplorer anExp(aShape, GeomAPI_Shape::EDGE); anExp.more(); anExp.next()) {
207 GeomShapePtr anEdge = anExp.current();
208 anEdges.push_back(anEdge);
212 if(anEdges.empty()) {
213 theError = "Objects are not selected.";
217 // Check that edges does not have intersections.
218 if(anEdges.size() > 1) {
219 GeomAlgoAPI_PaveFiller aPaveFiller(anEdges, false);
220 if(!aPaveFiller.isDone()) {
221 theError = "Error while checking if edges intersects.";
224 GeomShapePtr aSectedEdges = aPaveFiller.shape();
227 for(GeomAPI_ShapeExplorer
228 anExp(aSectedEdges, GeomAPI_Shape::EDGE); anExp.more(); anExp.next()) {
231 if(anEdgesNum != anEdges.size()) {
232 theError = "Selected objects have intersections.";
237 // Check that they are planar.
238 std::shared_ptr<GeomAPI_Pln> aPln = GeomAlgoAPI_ShapeTools::findPlane(anEdges);
240 theError = "Selected object(s) should belong to only one plane.";
244 // Check that selected objects have closed contours.
246 GeomAlgoAPI_SketchBuilder::createFaces(aPln->location(), aPln->xDirection(),
247 aPln->direction(), anEdges, aFaces);
249 theError = "Selected objects do not generate closed contour.";
256 //=================================================================================================
257 bool BuildPlugin_ValidatorBaseForFace::isNotObligatory(std::string theFeature,
258 std::string theAttribute)
263 //=================================================================================================
264 bool BuildPlugin_ValidatorSubShapesSelection::isValid(const AttributePtr& theAttribute,
265 const std::list<std::string>& theArguments,
266 Events_InfoMessage& theError) const
268 if(theArguments.size() != 1) {
269 std::string aMsg = "Error: BuildPlugin_ValidatorSubShapesSelection should be used only with "
270 "1 parameter(Sketch feature id).";
271 Events_InfoMessage("BuildPlugin_Validators", aMsg).send();
275 // Get base objects list.
276 if(theAttribute->attributeType() != ModelAPI_AttributeSelectionList::typeId()) {
278 "Error: BuildPlugin_ValidatorSubShapesSelection does not support attribute type \""
279 "%1\"\n Only \"%2\" supported.";
280 Events_InfoMessage("BuildPlugin_Validators", aMsg).
281 arg(theAttribute->attributeType()).arg(ModelAPI_AttributeSelectionList::typeId()).send();
284 AttributeSelectionListPtr aSelectionList =
285 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
286 if(!aSelectionList.get()) {
287 theError = "Could not get selection list.";
292 const std::string aBaseShapeId = "base_shape";
293 FeaturePtr aFeature = ModelAPI_Feature::feature(theAttribute->owner());
294 AttributeSelectionPtr aShapeAttrSelection = aFeature->selection(aBaseShapeId);
296 if(!aShapeAttrSelection.get()) {
297 theError = "Base shape is empty.";
301 ResultPtr aBaseContext = aShapeAttrSelection->context();
303 GeomShapePtr aBaseShape = aShapeAttrSelection->value();
304 if(!aBaseShape.get()) {
305 theError = "Base shape is empty.";
309 GeomAlgoAPI_ShapeBuilder aBuilder;
310 aBuilder.removeInternal(aBaseShape);
311 aBaseShape = aBuilder.shape();
313 // If selected shape is wire allow to select only vertices. If face - allow vertices and edges.
314 std::set<GeomAPI_Shape::ShapeType> anAllowedTypes;
315 switch(aBaseShape->shapeType()) {
316 case GeomAPI_Shape::FACE: anAllowedTypes.insert(GeomAPI_Shape::EDGE);
317 case GeomAPI_Shape::WIRE: anAllowedTypes.insert(GeomAPI_Shape::VERTEX);
321 // Check selected shapes.
322 GeomValidators_FeatureKind aFeatureKindValidator;
323 std::list<std::string> anArguments;
324 anArguments.push_back(theArguments.front());
325 for(int anIndex = 0; anIndex < aSelectionList->size(); ++anIndex) {
326 AttributeSelectionPtr aSelectionAttrInList = aSelectionList->value(anIndex);
327 if(!aSelectionAttrInList.get()) {
328 theError = "Empty attribute in list.";
332 // If context of selection same skip.
333 if(aBaseContext == aSelectionAttrInList->context()) {
337 // Check that it is a selection on Sketch.
338 if(!aFeatureKindValidator.isValid(aSelectionAttrInList, anArguments, theError)) {
343 GeomShapePtr aShapeInList = aSelectionAttrInList->value();
344 if(!aShapeInList.get()) {
345 aShapeInList = aSelectionAttrInList->context()->shape();
347 if(anAllowedTypes.find(aShapeInList->shapeType()) == anAllowedTypes.cend()) {
348 theError = "Selected shape has unacceptable type.";
352 // Check that shape inside wire or face.
353 if(!GeomAlgoAPI_ShapeTools::isSubShapeInsideShape(aShapeInList, aBaseShape)) {
354 theError = "Selected shape is not inside base face.";