- if (aShapeType == TopAbs_FACE) { // compound is for the whole sketch selection
- // If this is a wire with plane defined thin it is a sketch-like object
- if (!aConstructionContext->facesNum()) // no faces, update can not work correctly
- return false;
- // if there is no edges indexes, any face can be used: take the first
- std::shared_ptr<GeomAPI_Shape> aNewSelected;
- if (aNoIndexes) {
- aNewSelected = aConstructionContext->face(0);
- } else { // searching for most looks-like initial face by the indexes
- // prepare edges of the current resut for the fast searching
- TColStd_MapOfTransient allCurves;
- const int aSubNum = aComposite->numberOfSubs();
- for(int a = 0; a < aSubNum; a++) {
- if (aSubIds->Contains(aComposite->subFeatureId(a))) {
- FeaturePtr aSub = aComposite->subFeature(a);
- const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aSub->results();
- std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRes;
- for(aRes = aResults.cbegin(); aRes != aResults.cend(); aRes++) {
- ResultConstructionPtr aConstr =
- std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*aRes);
- if (aConstr->shape() && aConstr->shape()->isEdge()) {
- const TopoDS_Shape& aResShape = aConstr->shape()->impl<TopoDS_Shape>();
- TopoDS_Edge anEdge = TopoDS::Edge(aResShape);
- if (!anEdge.IsNull()) {
- Standard_Real aFirst, aLast;
- Handle(Geom_Curve) aCurve = BRep_Tool::Curve(anEdge, aFirst, aLast);
- allCurves.Add(aCurve);
- }
- }
- }
- }
- }
- double aBestFound = 0; // best percentage of found edges
- for(int aFaceIndex = 0; aFaceIndex < aConstructionContext->facesNum(); aFaceIndex++) {
- int aFound = 0, aNotFound = 0;
- TopExp_Explorer anEdgesExp(
- aConstructionContext->face(aFaceIndex)->impl<TopoDS_Shape>(), TopAbs_EDGE);
- for(; anEdgesExp.More(); anEdgesExp.Next()) {
- TopoDS_Edge anEdge = TopoDS::Edge(anEdgesExp.Current());
- if (!anEdge.IsNull()) {
- Standard_Real aFirst, aLast;
- Handle(Geom_Curve) aCurve = BRep_Tool::Curve(anEdge, aFirst, aLast);
- if (allCurves.Contains(aCurve)) {
- aFound++;
- } else {
- aNotFound++;
- }
- }
- }
- if (aFound + aNotFound != 0) {
- double aPercentage = double(aFound) / double(aFound + aNotFound);
- if (aPercentage > aBestFound) {
- aBestFound = aPercentage;
- aNewSelected = aConstructionContext->face(aFaceIndex);
- }
- }
- }
- }
- if (aNewSelected) { // store this new selection
- selectConstruction(aContext, aNewSelected);
- owner()->data()->sendAttributeUpdated(this);
- return true;
- }
- } else if (aShapeType == TopAbs_EDGE) {
- // just reselect the edge by the id
- const int aSubNum = aComposite->numberOfSubs();
- for(int a = 0; a < aSubNum; a++) {
- // if aSubIds take any, the first appropriate
- if (aSubIds->IsEmpty() || aSubIds->Contains(aComposite->subFeatureId(a))) {
- // found the appropriate feature
- FeaturePtr aFeature = aComposite->subFeature(a);
- std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aResIter =
- aFeature->results().cbegin();
- for(;aResIter != aFeature->results().cend(); aResIter++) {
- ResultConstructionPtr aRes =
- std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*aResIter);
- if (aRes && aRes->shape() && aRes->shape()->isEdge()) { // found!
- selectConstruction(aContext, aRes->shape());
- owner()->data()->sendAttributeUpdated(this);
- return true;
- }
- }
- }
- }
- } else if (aShapeType == TopAbs_VERTEX) {
- // just reselect the vertex by the id of edge
- const int aSubNum = aComposite->numberOfSubs();
- for(int a = 0; a < aSubNum; a++) {
- // if aSubIds take any, the first appropriate
- int aFeatureID = aComposite->subFeatureId(a);
- if (aSubIds->IsEmpty() || aSubIds->Contains(aFeatureID) ||
- aSubIds->Contains(aFeatureID + kSTART_VERTEX_DELTA) ||
- aSubIds->Contains(aFeatureID + kSTART_VERTEX_DELTA * 2)) {
- // searching for deltas
- int aVertexNum = 0;
- if (aSubIds->Contains(aFeatureID + kSTART_VERTEX_DELTA)) aVertexNum = 1;
- else if (aSubIds->Contains(aFeatureID + kSTART_VERTEX_DELTA * 2)) aVertexNum = 2;
- // found the feature with appropriate edge
- FeaturePtr aFeature = aComposite->subFeature(a);
- std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aResIter =
- aFeature->results().cbegin();
- for(;aResIter != aFeature->results().cend(); aResIter++) {
- ResultConstructionPtr aRes =
- std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*aResIter);
- if (aRes && aRes->shape()) {
- if (aRes->shape()->isVertex() && aVertexNum == 0) { // found!
- selectConstruction(aContext, aRes->shape());
- owner()->data()->sendAttributeUpdated(this);
- return true;
- } else if (aRes->shape()->isEdge() && aVertexNum > 0) {
- const TopoDS_Shape& anEdge = aRes->shape()->impl<TopoDS_Shape>();
- int aVIndex = 1;
- for(TopExp_Explorer aVExp(anEdge, TopAbs_VERTEX); aVExp.More(); aVExp.Next()) {
- if (aVIndex == aVertexNum) { // found!
- std::shared_ptr<GeomAPI_Shape> aVertex(new GeomAPI_Shape);
- aVertex->setImpl(new TopoDS_Shape(aVExp.Current()));
- selectConstruction(aContext, aVertex);
- owner()->data()->sendAttributeUpdated(this);
- return true;
- }
- aVIndex++;
- }
- }
- }
- }
- }
- }