1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 // File: PartSet_Validators.cpp
4 // Created: 09 July 2014
5 // Author: Vitaly SMETANNIKOV
7 #include "PartSet_Validators.h"
10 #include <TopoDS_Edge.hxx>
11 #include <BRep_Tool.hxx>
12 #include <GeomAdaptor_Curve.hxx>
13 #include <GeomAbs_CurveType.hxx>
14 #include <GeomValidators_Tools.h>
15 #include <ModuleBase_ISelection.h>
16 #include <ModuleBase_WidgetShapeSelector.h>
18 #include <ModelAPI_AttributeRefAttr.h>
19 #include <ModelAPI_AttributeSelection.h>
20 #include <ModelAPI_AttributeReference.h>
21 #include <ModelAPI_AttributeSelectionList.h>
22 #include <ModelAPI_Object.h>
24 #include <SketchPlugin_Sketch.h>
31 int shapesNbPoints(const ModuleBase_ISelection* theSelection)
33 QList<ModuleBase_ViewerPrs> aList = theSelection->getSelected();
35 foreach (ModuleBase_ViewerPrs aPrs, aList) {
36 const TopoDS_Shape& aShape = aPrs.shape();
37 if (!aShape.IsNull()) {
38 if (aShape.ShapeType() == TopAbs_VERTEX)
45 int shapesNbLines(const ModuleBase_ISelection* theSelection)
47 QList<ModuleBase_ViewerPrs> aList = theSelection->getSelected();
49 foreach(ModuleBase_ViewerPrs aPrs, aList) {
50 const TopoDS_Shape& aShape = aPrs.shape();
51 if (!aShape.IsNull()) {
52 if (aShape.ShapeType() == TopAbs_EDGE) {
53 TopoDS_Edge aEdge = TopoDS::Edge(aShape);
54 Standard_Real aStart, aEnd;
55 Handle(Geom_Curve) aCurve = BRep_Tool::Curve(aEdge, aStart, aEnd);
56 GeomAdaptor_Curve aAdaptor(aCurve);
57 if (aAdaptor.GetType() == GeomAbs_Line)
65 bool PartSet_DistanceValidator::isValid(const ModuleBase_ISelection* theSelection) const
67 int aCount = shapesNbPoints(theSelection) + shapesNbLines(theSelection);
68 return (aCount > 0) && (aCount < 3);
71 bool PartSet_LengthValidator::isValid(const ModuleBase_ISelection* theSelection) const
73 int aCount = shapesNbLines(theSelection);
74 return (aCount > 0) && (aCount < 2);
77 bool PartSet_PerpendicularValidator::isValid(const ModuleBase_ISelection* theSelection) const
79 int aCount = shapesNbLines(theSelection);
80 return (aCount > 0) && (aCount < 3);
83 bool PartSet_ParallelValidator::isValid(const ModuleBase_ISelection* theSelection) const
85 int aCount = shapesNbLines(theSelection);
86 return (aCount > 0) && (aCount < 3);
89 bool PartSet_RadiusValidator::isValid(const ModuleBase_ISelection* theSelection) const
91 QList<ModuleBase_ViewerPrs> aList = theSelection->getSelected();
92 ModuleBase_ViewerPrs aPrs;
94 foreach (ModuleBase_ViewerPrs aPrs, aList) {
95 const TopoDS_Shape& aShape = aPrs.shape();
96 if (!aShape.IsNull()) {
97 if (aShape.ShapeType() == TopAbs_EDGE) {
98 TopoDS_Edge aEdge = TopoDS::Edge(aShape);
99 Standard_Real aStart, aEnd;
100 Handle(Geom_Curve) aCurve = BRep_Tool::Curve(aEdge, aStart, aEnd);
101 GeomAdaptor_Curve aAdaptor(aCurve);
102 if (aAdaptor.GetType() == GeomAbs_Circle)
107 return (aCount > 0) && (aCount < 2);
110 bool PartSet_RigidValidator::isValid(const ModuleBase_ISelection* theSelection) const
112 int aCount = shapesNbLines(theSelection);
113 return (aCount > 0) && (aCount < 2);
116 bool PartSet_DifferentObjectsValidator::isValid(const AttributePtr& theAttribute,
117 const std::list<std::string>& theArguments) const
119 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
121 // 1. check whether the object of the attribute is not among the feature attributes
122 // find the attribute's object
123 ObjectPtr anObject = GeomValidators_Tools::getObject(theAttribute);
125 // check whether the object is not among other feature attributes
126 if (anObject.get() != NULL) {
127 // Check RefAttr attributes
128 std::list<std::shared_ptr<ModelAPI_Attribute> > anAttrs = aFeature->data()->attributes("");
129 //if (anAttrs.size() > 0) {
130 std::list<std::shared_ptr<ModelAPI_Attribute> >::iterator anIt = anAttrs.begin();
131 for(; anIt != anAttrs.end(); anIt++) {
132 AttributePtr anAttr = *anIt;
133 // the function parameter attribute should be skipped
134 if (anAttr.get() == NULL || anAttr->id() == theAttribute->id())
136 ObjectPtr aCurObject = GeomValidators_Tools::getObject(anAttr);
137 if (aCurObject && aCurObject == anObject)
142 // 2. collect object referenced by theAttribute and ...
143 if (featureHasReferences(theAttribute)) {
144 // 3. check whether the attribute value is not among other feature attributes
145 std::list<std::shared_ptr<ModelAPI_Attribute> > anAttrs =
146 aFeature->data()->attributes(ModelAPI_AttributeRefAttr::type());
147 std::list<std::shared_ptr<ModelAPI_Attribute> >::iterator anAttr = anAttrs.begin();
148 for(; anAttr != anAttrs.end(); anAttr++) {
150 std::shared_ptr<ModelAPI_AttributeRefAttr> aRef =
151 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(*anAttr);
152 // check the object is already presented
153 if (!aRef->isObject() && aRef->attr() == theAttribute)
163 bool PartSet_DifferentObjectsValidator::featureHasReferences(const AttributePtr& theAttribute) const
165 std::list<std::pair<std::string, std::list<ObjectPtr> > > allRefs;
166 if (theAttribute->owner().get() && theAttribute->owner()->data().get())
167 theAttribute->owner()->data()->referencesToObjects(allRefs);
168 // collect object referenced by theAttribute
169 std::list<ObjectPtr>* anAttrObjs = 0;
170 std::list<std::pair<std::string, std::list<ObjectPtr> > >::iterator aRefIter = allRefs.begin();
171 for(; aRefIter != allRefs.end(); aRefIter++) {
172 if (theAttribute->id() == aRefIter->first)
173 anAttrObjs = &(aRefIter->second);
175 if (!anAttrObjs || anAttrObjs->empty())
176 return true; // theAttribute does not references to anything
177 // check with all others
178 for(aRefIter = allRefs.begin(); aRefIter != allRefs.end(); aRefIter++) {
179 if (theAttribute->id() == aRefIter->first)
180 continue; // do not check with myself
181 std::list<ObjectPtr>::iterator aReferenced = aRefIter->second.begin();
182 for(; aReferenced != aRefIter->second.end(); aReferenced++) {
183 std::list<ObjectPtr>::iterator aReferencedByMe = anAttrObjs->begin();
184 for(; aReferencedByMe != anAttrObjs->end(); aReferencedByMe++) {
185 if (*aReferenced == *aReferencedByMe) // found same objects!
193 bool PartSet_SketchEntityValidator::isValid(const AttributePtr& theAttribute,
194 const std::list<std::string>& theArguments) const
196 bool isSketchEntities = true;
197 std::set<std::string> anEntityKinds;
198 std::list<std::string>::const_iterator anIt = theArguments.begin(), aLast = theArguments.end();
199 for (; anIt != aLast; anIt++) {
200 anEntityKinds.insert(*anIt);
203 std::string anAttributeType = theAttribute->attributeType();
204 if (anAttributeType == ModelAPI_AttributeSelectionList::type()) {
205 AttributeSelectionListPtr aSelectionListAttr =
206 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
207 // it filters only selection list attributes
208 std::string aType = aSelectionListAttr->selectionType().c_str();
209 // all context objects should be sketch entities
210 int aSize = aSelectionListAttr->size();
211 for (int i = 0; i < aSelectionListAttr->size() && isSketchEntities; i++) {
212 AttributeSelectionPtr aSelectAttr = aSelectionListAttr->value(i);
213 ObjectPtr anObject = aSelectAttr->context();
214 FeaturePtr aFeature = ModelAPI_Feature::feature(anObject);
215 isSketchEntities = anEntityKinds.find(aFeature->getKind()) != anEntityKinds.end();
218 if (anAttributeType == ModelAPI_AttributeRefAttr::type()) {
219 std::shared_ptr<ModelAPI_AttributeRefAttr> aRef =
220 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
221 isSketchEntities = false;
222 if (aRef->isObject()) {
223 ObjectPtr anObject = aRef->object();
224 if (anObject.get() != NULL) {
225 FeaturePtr aFeature = ModelAPI_Feature::feature(anObject);
226 if (aFeature.get() != NULL)
227 isSketchEntities = anEntityKinds.find(aFeature->getKind()) != anEntityKinds.end();
232 return isSketchEntities;