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::typeId());
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)
156 // TODO(nds) v1.0.2 master
157 //bool PartSet_DifferentObjectsValidator::isValid(const FeaturePtr& theFeature,
158 // const std::list<std::string>& theArguments,
159 // const ObjectPtr& theObject,
160 // const GeomShapePtr& theShape) const
162 // // Check RefAttr attributes
163 // std::list<std::shared_ptr<ModelAPI_Attribute> > anAttrs =
164 // theFeature->data()->attributes(ModelAPI_AttributeRefAttr::typeId());
165 // if (anAttrs.size() > 0) {
166 // std::list<std::shared_ptr<ModelAPI_Attribute> >::iterator anAttr = anAttrs.begin();
167 // for(; anAttr != anAttrs.end(); anAttr++) {
169 // std::shared_ptr<ModelAPI_AttributeRefAttr> aRef =
170 // std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(*anAttr);
171 // // check the object is already presented
172 // if (aRef->isObject() && aRef->object() == theObject)
177 // // Check selection attributes
178 // anAttrs = theFeature->data()->attributes(ModelAPI_AttributeSelection::typeId());
179 // if (anAttrs.size() > 0) {
180 // std::list<std::shared_ptr<ModelAPI_Attribute> >::iterator anAttr = anAttrs.begin();
181 // for(; anAttr != anAttrs.end(); anAttr++) {
183 // std::shared_ptr<ModelAPI_AttributeSelection> aRef =
184 // std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(*anAttr);
185 // // check the object is already presented
186 // if (aRef->isInitialized() && aRef->context() == theObject) {
187 // if (theShape.get() != NULL) {
188 // if (aRef->value()->isEqual(theShape))
196 // // Check selection attributes
197 // anAttrs = theFeature->data()->attributes(ModelAPI_AttributeReference::typeId());
198 // if (anAttrs.size() > 0) {
199 // std::list<std::shared_ptr<ModelAPI_Attribute> >::iterator anAttr = anAttrs.begin();
200 // for(; anAttr != anAttrs.end(); anAttr++) {
202 // std::shared_ptr<ModelAPI_AttributeReference> aRef =
203 // std::dynamic_pointer_cast<ModelAPI_AttributeReference>(*anAttr);
204 // // check the object is already presented
205 // if (aRef->isInitialized() && aRef->value() == theObject)
207 // ======= end of todo
215 bool PartSet_DifferentObjectsValidator::featureHasReferences(const AttributePtr& theAttribute) const
216 // TODO(nds) v1.0.2 master
217 //bool PartSet_DifferentObjectsValidator::isValid(const FeaturePtr& theFeature,
218 // const std::list<std::string>& theArguments,
219 // const AttributePtr& theAttribute) const
221 // if (PartSet_DifferentObjectsValidator::isValid(theAttribute, theArguments)) {
222 // std::list<std::shared_ptr<ModelAPI_Attribute> > anAttrs =
223 // theFeature->data()->attributes(ModelAPI_AttributeRefAttr::typeId());
224 // std::list<std::shared_ptr<ModelAPI_Attribute> >::iterator anAttr = anAttrs.begin();
225 // for(; anAttr != anAttrs.end(); anAttr++) {
227 // std::shared_ptr<ModelAPI_AttributeRefAttr> aRef =
228 // std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(*anAttr);
229 // // check the object is already presented
230 // if (!aRef->isObject() && aRef->attr() == theAttribute)
239 //bool PartSet_DifferentObjectsValidator::isValid(const AttributePtr& theAttribute,
240 // const std::list<std::string>& theArguments) const
241 // ======= end of todo
243 std::list<std::pair<std::string, std::list<ObjectPtr> > > allRefs;
244 if (theAttribute->owner().get() && theAttribute->owner()->data().get())
245 theAttribute->owner()->data()->referencesToObjects(allRefs);
246 // collect object referenced by theAttribute
247 std::list<ObjectPtr>* anAttrObjs = 0;
248 std::list<std::pair<std::string, std::list<ObjectPtr> > >::iterator aRefIter = allRefs.begin();
249 for(; aRefIter != allRefs.end(); aRefIter++) {
250 if (theAttribute->id() == aRefIter->first)
251 anAttrObjs = &(aRefIter->second);
253 if (!anAttrObjs || anAttrObjs->empty())
254 return true; // theAttribute does not references to anything
255 // check with all others
256 for(aRefIter = allRefs.begin(); aRefIter != allRefs.end(); aRefIter++) {
257 if (theAttribute->id() == aRefIter->first)
258 continue; // do not check with myself
259 std::list<ObjectPtr>::iterator aReferenced = aRefIter->second.begin();
260 for(; aReferenced != aRefIter->second.end(); aReferenced++) {
261 std::list<ObjectPtr>::iterator aReferencedByMe = anAttrObjs->begin();
262 for(; aReferencedByMe != anAttrObjs->end(); aReferencedByMe++) {
263 if (*aReferenced == *aReferencedByMe) // found same objects!
271 bool PartSet_SketchEntityValidator::isValid(const AttributePtr& theAttribute,
272 const std::list<std::string>& theArguments) const
274 bool isSketchEntities = true;
275 std::set<std::string> anEntityKinds;
276 std::list<std::string>::const_iterator anIt = theArguments.begin(), aLast = theArguments.end();
277 for (; anIt != aLast; anIt++) {
278 anEntityKinds.insert(*anIt);
281 std::string anAttributeType = theAttribute->attributeType();
282 if (anAttributeType == ModelAPI_AttributeSelectionList::typeId()) {
283 AttributeSelectionListPtr aSelectionListAttr =
284 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
285 // it filters only selection list attributes
286 std::string aType = aSelectionListAttr->selectionType().c_str();
287 // all context objects should be sketch entities
288 int aSize = aSelectionListAttr->size();
289 for (int i = 0; i < aSelectionListAttr->size() && isSketchEntities; i++) {
290 AttributeSelectionPtr aSelectAttr = aSelectionListAttr->value(i);
291 ObjectPtr anObject = aSelectAttr->context();
292 FeaturePtr aFeature = ModelAPI_Feature::feature(anObject);
293 isSketchEntities = anEntityKinds.find(aFeature->getKind()) != anEntityKinds.end();
296 if (anAttributeType == ModelAPI_AttributeRefAttr::typeId()) {
297 std::shared_ptr<ModelAPI_AttributeRefAttr> aRef =
298 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
299 isSketchEntities = false;
300 if (aRef->isObject()) {
301 ObjectPtr anObject = aRef->object();
302 if (anObject.get() != NULL) {
303 FeaturePtr aFeature = ModelAPI_Feature::feature(anObject);
304 if (aFeature.get() != NULL)
305 isSketchEntities = anEntityKinds.find(aFeature->getKind()) != anEntityKinds.end();
310 return isSketchEntities;