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>
14 #include <GeomAlgoAPI_CompoundBuilder.h>
15 #include <GeomAlgoAPI_WireBuilder.h>
17 #include <GeomValidators_ShapeType.h>
19 #include <Events_Error.h>
21 //=================================================================================================
22 bool BuildPlugin_ValidatorBaseForBuild::isValid(const AttributePtr& theAttribute,
23 const std::list<std::string>& theArguments,
24 std::string& theError) const
26 // Get base objects list.
27 if(theAttribute->attributeType() != ModelAPI_AttributeSelectionList::typeId()) {
28 Events_Error::send("Error: BuildPlugin_ValidatorBaseForBuild does not support attribute type \"" + theAttribute->attributeType()
29 + "\"\n Only \"" + ModelAPI_AttributeSelectionList::typeId() + "\" supported.");
32 AttributeSelectionListPtr aSelectionList = std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
33 if(!aSelectionList.get()) {
34 theError = "Could not get selection list.";
37 if(aSelectionList->size() == 0) {
38 theError = "Empty selection list.";
42 // Collect base shapes.
43 for(int anIndex = 0; anIndex < aSelectionList->size(); ++anIndex) {
44 AttributeSelectionPtr aSelection = aSelectionList->value(anIndex);
45 if(!aSelection.get()) {
46 theError = "Could not get selection.";
49 ResultPtr aContext = aSelection->context();
51 theError = "Attribute have empty context.";
55 GeomShapePtr aShape = aSelection->value();
56 GeomShapePtr aContextShape = aContext->shape();
58 aShape = aContextShape;
61 theError = "Empty shape selected.";
65 // Check that shapes has acceptable type.
66 GeomValidators_ShapeType aValidatorShapeType;
67 if(!aValidatorShapeType.isValid(aSelection, theArguments, theError)) {
71 // Check that it is shape on sketch.
72 ResultConstructionPtr aConstruction = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aContext);
73 if(aConstruction.get()) {
74 if(aConstruction->isInfinite()) {
75 theError = "Inifinte objects not acceptable.";
79 std::shared_ptr<GeomAPI_PlanarEdges> anEdges = std::dynamic_pointer_cast<GeomAPI_PlanarEdges>(aContextShape);
81 if(aShape->isEqual(aContextShape)) {
82 // It is whole sketch.
85 } else if(!aShape->isEqual(aContextShape)) {
86 // Local selection on body does not allowed.
87 theError = "Selected shape is in the local selection. Only global selection is allowed.";
96 //=================================================================================================
97 bool BuildPlugin_ValidatorBaseForWire::isValid(const std::shared_ptr<ModelAPI_Feature>& theFeature,
98 const std::list<std::string>& theArguments,
99 std::string& theError) const
102 if(theArguments.size() != 1) {
103 Events_Error::send("Error: BuildPlugin_ValidatorBaseForWire should be used only with 1 parameter (ID of base objects list)");
106 AttributePtr anAttribute = theFeature->attribute(theArguments.front());
108 // Check base objects list.
109 BuildPlugin_ValidatorBaseForBuild aValidatorBaseForBuild;
110 std::list<std::string> anArguments;
111 anArguments.push_back("edge");
112 anArguments.push_back("wire");
113 if(!aValidatorBaseForBuild.isValid(anAttribute, anArguments, theError)) {
117 // Collect base shapes.
118 AttributeSelectionListPtr aSelectionList =
119 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(anAttribute);
120 ListOfShape aListOfShapes;
121 for(int anIndex = 0; anIndex < aSelectionList->size(); ++anIndex) {
122 AttributeSelectionPtr aSelection = aSelectionList->value(anIndex);
123 GeomShapePtr aShape = aSelection->value();
125 aShape = aSelection->context()->shape();
127 aListOfShapes.push_back(aShape);
131 GeomShapePtr aWire = GeomAlgoAPI_WireBuilder::wire(aListOfShapes);
133 theError = "Result wire empty. Probably it has disconnected edges or non-manifold.";
140 //=================================================================================================
141 bool BuildPlugin_ValidatorBaseForWireisNotObligatory(std::string theFeature, std::string theAttribute)
146 //=================================================================================================
147 bool BuildPlugin_ValidatorBaseForFace::isValid(const AttributePtr& theAttribute,
148 const std::list<std::string>& theArguments,
149 std::string& theError) const
151 // Get base objects list.
152 BuildPlugin_ValidatorBaseForBuild aValidatorBaseForBuild;
153 std::list<std::string> anArguments;
154 anArguments.push_back("edge");
155 anArguments.push_back("wire");
156 if(!aValidatorBaseForBuild.isValid(theAttribute, anArguments, theError)) {
160 // Collect base shapes.
161 AttributeSelectionListPtr aSelectionList =
162 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
163 ListOfShape aListOfShapes;
164 for(int anIndex = 0; anIndex < aSelectionList->size(); ++anIndex) {
165 AttributeSelectionPtr aSelection = aSelectionList->value(anIndex);
166 GeomShapePtr aShape = aSelection->value();
168 aShape = aSelection->context()->shape();
170 aListOfShapes.push_back(aShape);
173 // Check that they are planar.
174 GeomShapePtr aCompound = GeomAlgoAPI_CompoundBuilder::compound(aListOfShapes);
176 return aCompound->isPlanar();