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
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
21 #include <Model_ResultConstruction.h>
23 #include <Model_Data.h>
24 #include <ModelAPI_CompositeFeature.h>
25 #include <Model_SelectionNaming.h>
26 #include <ModelAPI_Events.h>
27 #include <Config_PropManager.h>
28 #include <GeomAPI_PlanarEdges.h>
29 #include <GeomAPI_Shape.h>
30 #include <GeomAlgoAPI_SketchBuilder.h>
31 #include <Events_Loop.h>
33 #include <TDF_Reference.hxx>
34 #include <TDF_ChildIterator.hxx>
35 #include <TNaming_NamedShape.hxx>
36 #include <TNaming_Builder.hxx>
37 #include <TDataStd_Integer.hxx>
38 #include <TDataStd_IntPackedMap.hxx>
39 #include <TDataStd_Name.hxx>
40 #include <TDataStd_UAttribute.hxx>
41 #include <TColStd_MapOfTransient.hxx>
42 #include <TColStd_MapIteratorOfPackedMapOfInteger.hxx>
43 #include <BRep_Tool.hxx>
44 #include <BRep_Builder.hxx>
46 #include <TopoDS_Shape.hxx>
47 #include <TopoDS_Edge.hxx>
48 #include <TopoDS_Vertex.hxx>
49 #include <TopExp_Explorer.hxx>
50 #include <TNaming_Tool.hxx>
51 #include <Precision.hxx>
53 // identifier that it is full result selected, but in external document (for internal index is 0)
54 Standard_GUID kFULL_RESULT_ID("ee87e529-da6f-46af-be25-5e0fefde52f7");
57 void Model_ResultConstruction::colorConfigInfo(std::string& theSection, std::string& theName,
58 std::string& theDefault)
60 theSection = "Visualization";
61 theName = "result_construction_color";
62 theDefault = DEFAULT_COLOR();
65 void Model_ResultConstruction::setShape(std::shared_ptr<GeomAPI_Shape> theShape)
67 if (myShape != theShape) {
68 if (!theShape.get() || !theShape->isEqual(myShape)) {
69 static const Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
70 ModelAPI_EventCreator::get()->sendUpdated(data()->owner(), anEvent);
74 myFacesUpToDate = false;
80 std::shared_ptr<GeomAPI_Shape> Model_ResultConstruction::shape()
85 Model_ResultConstruction::Model_ResultConstruction()
89 myFacesUpToDate = false;
92 void Model_ResultConstruction::setIsInHistory(const bool isInHistory)
94 myIsInHistory = isInHistory;
97 int Model_ResultConstruction::facesNum()
99 if (!myFacesUpToDate) {
100 std::shared_ptr<GeomAPI_PlanarEdges> aWirePtr =
101 std::dynamic_pointer_cast<GeomAPI_PlanarEdges>(myShape);
102 if (aWirePtr.get()) {
103 std::list<std::shared_ptr<GeomAPI_Shape> > aFaces;
104 GeomAlgoAPI_SketchBuilder::createFaces(aWirePtr->origin(), aWirePtr->dirX(),
105 aWirePtr->norm(), aWirePtr, aFaces);
106 std::list<std::shared_ptr<GeomAPI_Shape> >::iterator aFIter = aFaces.begin();
107 for(; aFIter != aFaces.end(); aFIter++) {
108 std::shared_ptr<GeomAPI_Face> aFace(new GeomAPI_Face(*aFIter));
109 if (aFace.get() && !aFace->isNull())
110 myFaces.push_back(aFace);
113 myFacesUpToDate = true;
115 // update all the faces and sub-elements in the naming structure
116 DocumentPtr anEmptyExt;
117 bool aNotExt = false;
118 TDF_Label aDataLab = startLabel(anEmptyExt, aNotExt);
119 TDF_ChildIterator aSubsIter(aDataLab, Standard_False);
120 for(; aSubsIter.More(); aSubsIter.Next()) {
121 const TDF_Label aLab = aSubsIter.Value();
122 if (aLab.Tag() == 1) // skip the root shape label
124 Handle(TNaming_NamedShape) aNS;
125 if (aLab.FindAttribute(TNaming_NamedShape::GetID(), aNS)) {
126 update(aLab.Tag() - 1, anEmptyExt, aNotExt);
130 return int(myFaces.size());
133 std::shared_ptr<GeomAPI_Face> Model_ResultConstruction::face(const int theIndex)
135 return myFaces[theIndex];
138 bool Model_ResultConstruction::isInfinite()
143 void Model_ResultConstruction::setInfinite(const bool theInfinite)
145 myIsInfinite = theInfinite;
148 void Model_ResultConstruction::setIsConcealed(const bool theValue)
150 // do nothing: the construction element is never concealed
153 static const int kSTART_VERTEX_DELTA = 1000000;
155 static void registerSubShape(TDF_Label theMainLabel, TopoDS_Shape theShape, std::string theFullName,
156 const int theID, std::shared_ptr<Model_Document> theDoc,
157 bool theSelectionMode)
159 TDF_Label aLab = theID == 0 ? theMainLabel : theMainLabel.FindChild(theID);
160 TNaming_Builder aBuilder(aLab);
161 // wire never happens as sub, it must be generated to be found
162 // by SelectionNaming TNaming_Tool::NamedShape
163 if (theSelectionMode && theShape.ShapeType() != TopAbs_WIRE)
164 aBuilder.Select(theShape, theShape);
166 aBuilder.Generated(theShape);
168 theDoc->addNamingName(aLab, theFullName);
169 TDataStd_Name::Set(aLab, theFullName.c_str());
172 // generates a full-name for sub-element of the composite feature (sketch)
173 std::string fullName(CompositeFeaturePtr theComposite, const TopoDS_Shape& theSubShape,
174 Handle(TDataStd_IntPackedMap) theRefs = Handle(TDataStd_IntPackedMap)())
176 TopAbs_ShapeEnum aShapeType = theSubShape.ShapeType();
178 TColStd_MapOfTransient allCurves;
179 if (aShapeType == TopAbs_VERTEX) { // compare positions
180 aVertexPos = BRep_Tool::Pnt(TopoDS::Vertex(theSubShape));
182 for(TopExp_Explorer anEdgeExp(theSubShape, TopAbs_EDGE); anEdgeExp.More(); anEdgeExp.Next()) {
183 TopoDS_Edge anEdge = TopoDS::Edge(anEdgeExp.Current());
184 Standard_Real aFirst, aLast;
185 Handle(Geom_Curve) aCurve = BRep_Tool::Curve(anEdge, aFirst, aLast);
186 allCurves.Add(aCurve);
189 std::map<int, int> anOrientations; //map from edges IDs to orientations of these edges in face
190 std::map<int, std::string> aSubNames; //map from edges IDs to names of edges
191 TColStd_PackedMapOfInteger aRefs; // indixes of sub-elements in composite
193 const int aSubNum = theComposite->numberOfSubs();
194 for(int a = 0; a < aSubNum; a++) {
195 FeaturePtr aSub = theComposite->subFeature(a);
196 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aSub->results();
197 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRes = aResults.cbegin();
198 // there may be many shapes (circle and center): register if at least one is in selection
199 for(; aRes != aResults.cend(); aRes++) {
200 ResultConstructionPtr aConstr =
201 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*aRes);
202 if (!aConstr->shape()) {
205 if (aShapeType == TopAbs_VERTEX) {
206 if (aConstr->shape()->isVertex()) { // compare vertices positions
207 const TopoDS_Shape& aVertex = aConstr->shape()->impl<TopoDS_Shape>();
208 gp_Pnt aPnt = BRep_Tool::Pnt(TopoDS::Vertex(aVertex));
209 if (aPnt.IsEqual(aVertexPos, Precision::Confusion())) {
210 aRefs.Add(theComposite->subFeatureId(a));
211 aSubNames[theComposite->subFeatureId(a)] = Model_SelectionNaming::shortName(aConstr);
213 } else { // get first or last vertex of the edge: last is stored with additional delta
214 const TopoDS_Shape& anEdge = aConstr->shape()->impl<TopoDS_Shape>();
215 int aDelta = kSTART_VERTEX_DELTA;
216 for(TopExp_Explorer aVExp(anEdge, TopAbs_VERTEX); aVExp.More(); aVExp.Next()) {
217 gp_Pnt aPnt = BRep_Tool::Pnt(TopoDS::Vertex(aVExp.Current()));
218 if (aPnt.IsEqual(aVertexPos, Precision::Confusion())) {
219 aRefs.Add(aDelta + theComposite->subFeatureId(a));
220 aSubNames[aDelta + theComposite->subFeatureId(a)] =
221 Model_SelectionNaming::shortName(aConstr, aDelta / kSTART_VERTEX_DELTA);
224 aDelta += kSTART_VERTEX_DELTA;
228 if (aConstr->shape()->isEdge()) {
229 const TopoDS_Shape& aResShape = aConstr->shape()->impl<TopoDS_Shape>();
230 TopoDS_Edge anEdge = TopoDS::Edge(aResShape);
231 if (!anEdge.IsNull()) {
232 Standard_Real aFirst, aLast;
233 Handle(Geom_Curve) aCurve = BRep_Tool::Curve(anEdge, aFirst, aLast);
234 if (allCurves.Contains(aCurve)) {
235 int anID = theComposite->subFeatureId(a);
237 aSubNames[anID] = Model_SelectionNaming::shortName(aConstr);
238 if (aShapeType != TopAbs_EDGE) { // face needs the sub-edges on sub-labels
239 // add edges to sub-label to support naming for edges selection
240 TopExp_Explorer anEdgeExp(theSubShape, TopAbs_EDGE);
241 for(; anEdgeExp.More(); anEdgeExp.Next()) {
242 TopoDS_Edge anEdge = TopoDS::Edge(anEdgeExp.Current());
243 Standard_Real aFirst, aLast;
244 Handle(Geom_Curve) aFaceCurve = BRep_Tool::Curve(anEdge, aFirst, aLast);
245 if (aFaceCurve == aCurve) {
246 int anOrient = Model_SelectionNaming::edgeOrientation(theSubShape, anEdge);
247 anOrientations[anID] = anOrient;
257 std::stringstream aName;
258 // #1839 : do not store name of the feature in the tree, since this name could be changed
259 if (theSubShape.ShapeType() != TopAbs_COMPOUND) { // compound means the whole construction result
260 if (theSubShape.ShapeType() == TopAbs_FACE) aName<<"Face";
261 else if (theSubShape.ShapeType() == TopAbs_WIRE) aName<<"Wire";
262 else if (theSubShape.ShapeType() == TopAbs_EDGE) aName<<"Edge";
263 else if (theSubShape.ShapeType() == TopAbs_VERTEX) aName<<"Vertex";
265 // make a composite name from all sub-elements indexes: "1_2_3_4"
266 TColStd_MapIteratorOfPackedMapOfInteger aRef(aRefs);
267 for(; aRef.More(); aRef.Next()) {
268 aName<<"-"<<aSubNames[aRef.Key()];
269 if (anOrientations.find(aRef.Key()) != anOrientations.end()) {
270 if (anOrientations[aRef.Key()] == 1)
272 else if (anOrientations[aRef.Key()] == -1)
277 if (!theRefs.IsNull()) {
278 Handle(TColStd_HPackedMapOfInteger) aMap = new TColStd_HPackedMapOfInteger(aRefs);
279 theRefs->ChangeMap(aMap);
284 // stores shape and name on sub-label of the main stored shape
285 static void saveSubName(CompositeFeaturePtr theComposite,
286 TDF_Label& theLab, const bool isSelectionMode, TopoDS_Shape aSub,
287 std::shared_ptr<Model_Document> theDoc, std::string theFullName)
289 // trying to store the edge of composite result, not sketch sub as it is
290 if (aSub.ShapeType() == TopAbs_EDGE) {
291 ResultPtr aRes = theComposite->firstResult();
292 ResultConstructionPtr aConstr = std::dynamic_pointer_cast<Model_ResultConstruction>(aRes);
294 Standard_Real aSubFirst, aSubLast;
295 TopoDS_Edge aSubEdge = TopoDS::Edge(aSub);
296 Handle(Geom_Curve) aSubCurve = BRep_Tool::Curve(aSubEdge, aSubFirst, aSubLast);
297 for(int aFaceIndex = 0; aFaceIndex < aConstr->facesNum(); aFaceIndex++) {
298 GeomShapePtr aGFace = aConstr->face(aFaceIndex);
299 TopoDS_Shape aFace = aGFace->impl<TopoDS_Shape>();
300 for(TopExp_Explorer anExp(aFace, TopAbs_EDGE); anExp.More(); anExp.Next()) {
301 TopoDS_Edge anEdge = TopoDS::Edge(anExp.Current());
302 Standard_Real aFirst, aLast;
303 Handle(Geom_Curve) aCurve = BRep_Tool::Curve(anEdge, aFirst, aLast);
304 if (aCurve == aSubCurve &&
305 ((fabs(aFirst - aSubFirst) < 1.e-9 && fabs(aLast - aSubLast) < 1.e-9)) ||
306 (fabs(aFirst - aSubLast) < 1.e-9 && fabs(aLast - aSubFirst) < 1.e-9)) {
315 TNaming_Builder aBuilder(theLab);
317 aBuilder.Select(aSub, aSub);
319 aBuilder.Generated(aSub);
320 theDoc->addNamingName(theLab, theFullName.c_str());
321 TDataStd_Name::Set(theLab, theFullName.c_str());
325 TDF_Label Model_ResultConstruction::startLabel(
326 const std::shared_ptr<ModelAPI_Document> theExtDoc, bool& theExternal)
328 theExternal = theExtDoc.get() && theExtDoc != document();
329 if (theExternal) { // external document is used
330 std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(theExtDoc);
331 return aDoc->extConstructionsLabel();
333 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(data());
334 return aData->label();
337 int Model_ResultConstruction::select(const std::shared_ptr<GeomAPI_Shape>& theSubShape,
338 const std::shared_ptr<ModelAPI_Document> theExtDoc, const int theIndex)
340 int anIndex; // resulting index of the sub-label
341 TopoDS_Shape aSubShape;
342 if (theSubShape.get()) {
343 aSubShape = theSubShape->impl<TopoDS_Shape>();
344 } else if (shape().get()) {
345 aSubShape = shape()->impl<TopoDS_Shape>();
347 // if external document requires this selection, put the naming structures to this doc
348 // to support the naming mechanism in this document correctly
350 TDF_Label aDataLab = startLabel(theExtDoc, anExternal);
351 if (theIndex == -1) {
352 anIndex = anExternal ? 2 : 1; // for the external doc don't mind about the main shape
354 if (theSubShape.get() || anExternal) { // searching for already selected sub (or whole for ext)
355 // iterate all the already presented shapes to see the same
356 TDF_ChildIterator aSubsIter(aDataLab, Standard_False);
357 for(; aSubsIter.More(); aSubsIter.Next()) {
358 const TDF_Label aLab = aSubsIter.Value();
359 if (aLab.Tag() == 1) // skip the root shape label
361 Handle(TNaming_NamedShape) aNS;
362 if (aLab.FindAttribute(TNaming_NamedShape::GetID(), aNS)) {
363 if (aNS->Get().IsSame(aSubShape)) {
364 return aLab.Tag() - 1; // found exactly the needed shape, nothing else to do
367 anIndex = aLab.Tag(); // searching for the latest index
369 anIndex = (anIndex == 1) ? 2 : (anIndex + 1); // next after 1-root, or next after all
372 anIndex = theIndex + 1;
375 // set the naming structure at index
376 TDF_Label aLab = aDataLab.FindChild(anIndex, Standard_True);
378 // if the subshape is part of a result face, select the whole face (#1997)
379 bool isSelectionMode = false; // and other don't set shapes - all the naming is in face label
380 if (!aSubShape.IsNull() && aSubShape.ShapeType() > TopAbs_FACE) {
381 // but before check that sub-vertex correctly detected as intersection of sketch edges (#2389)
383 if (aSubShape.ShapeType() == TopAbs_VERTEX) {
385 ResultPtr aThisPtr = std::dynamic_pointer_cast<ModelAPI_Result>(data()->owner());
386 FeaturePtr aThisFeature = document()->feature(aThisPtr);
387 CompositeFeaturePtr aComposite =
388 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aThisFeature);
389 if (aComposite.get()) {
390 const int aSubNum = aComposite->numberOfSubs();
391 for(int a = 0; a < aSubNum; a++) {
392 int aSubID = aComposite->subFeatureId(a);
393 FeaturePtr aSub = aComposite->subFeature(a);
394 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aSub->results();
395 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRes;
396 for(aRes = aResults.cbegin(); aRes != aResults.cend(); aRes++) {
397 ResultConstructionPtr aConstr =
398 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*aRes);
399 if (aConstr->shape() && aConstr->shape()->isEdge()) {
400 TopoDS_Shape aResShape = aConstr->shape()->impl<TopoDS_Shape>();
401 for(TopExp_Explorer anExp(aResShape, TopAbs_VERTEX); anExp.More(); anExp.Next()) {
402 if (aSubShape.IsSame(anExp.Current())) {
412 if (anEdgesNum > 1) {
413 for(int aFaceIndex = 0; aFaceIndex < facesNum(); aFaceIndex++) {
414 TopExp_Explorer anExp(face(aFaceIndex)->impl<TopoDS_Shape>(), aSubShape.ShapeType());
415 for(; anExp.More(); anExp.Next()) {
416 if (aSubShape.IsSame(anExp.Current())) { // this is the case: select the whole face
417 // here just store the face index (to update face if update of edge is needed)
418 TNaming_Builder aBuilder(aLab);
419 aBuilder.Select(aSubShape, aSubShape);
420 int aFaceSelID = select(face(aFaceIndex), theExtDoc, -1);
421 TDF_Reference::Set(aLab, aLab.Father().FindChild(aFaceSelID));
422 isSelectionMode = true;
430 // external full result is not identified by index == 0, so, add here the ID
431 if (!theSubShape.get()) {
432 TDataStd_UAttribute::Set(aLab, kFULL_RESULT_ID);
434 TNaming_Builder aBuilder(aLab);
435 // store all sub-faces naming since faces may be used for extrusion, where all edges are needed
436 Handle(TDataStd_IntPackedMap) anIndices = TDataStd_IntPackedMap::Set(aLab);
437 std::list<int> aFacesIndexes;
438 for(int a = 0; a < facesNum(); a++) {
439 anIndices->Add(select(face(a), theExtDoc, -1));
444 { // this to have erased Builder after the shape was generated (NS on this label may be changed)
445 TNaming_Builder aBuilder(aLab);
446 if (aSubShape.IsNull()) {
447 return anIndex - 1; // just keep empty named shape
449 // wire never happens as sub, it must be generated to be found
450 // by SelectionNaming TNaming_Tool::NamedShape
451 if (isSelectionMode && aSubShape.ShapeType() != TopAbs_WIRE) {
452 aBuilder.Select(aSubShape, aSubShape);
454 aBuilder.Generated(aSubShape);
458 if (anIndex == 1 && isInfinite()) { // infinitive results has no sub-selection
461 ResultPtr aThisPtr = std::dynamic_pointer_cast<ModelAPI_Result>(data()->owner());
462 FeaturePtr aThisFeature = document()->feature(aThisPtr);
463 CompositeFeaturePtr aComposite =
464 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aThisFeature);
465 if (!aComposite || aComposite->numberOfSubs() == 0) {
466 // saving of context is enough: result construction contains exactly the needed shape
470 // identify the results of sub-object of the composite by edges
471 // save type of the selected shape in integer attribute
472 TopAbs_ShapeEnum aShapeType = aSubShape.ShapeType();
473 TDataStd_Integer::Set(aLab, (int)aShapeType);
475 TColStd_MapOfTransient allCurves;
476 if (aShapeType == TopAbs_VERTEX) { // compare positions
477 aVertexPos = BRep_Tool::Pnt(TopoDS::Vertex(aSubShape));
479 for(TopExp_Explorer anEdgeExp(aSubShape, TopAbs_EDGE); anEdgeExp.More(); anEdgeExp.Next()) {
480 TopoDS_Edge anEdge = TopoDS::Edge(anEdgeExp.Current());
481 Standard_Real aFirst, aLast;
482 Handle(Geom_Curve) aCurve = BRep_Tool::Curve(anEdge, aFirst, aLast);
483 allCurves.Add(aCurve);
486 std::shared_ptr<Model_Document> aMyDoc =
487 std::dynamic_pointer_cast<Model_Document>(document());
488 // iterate and store the result ids of sub-elements and sub-elements to sub-labels
489 Handle(TDataStd_IntPackedMap) aRefs = TDataStd_IntPackedMap::Set(aLab);
490 const int aSubNum = aComposite->numberOfSubs();
491 // subs are placed on unique labels because of #2248: sketch curve may produce several edges,
492 // but #2401 - on stable labels
493 NCollection_Map<int> aUsedIDMap; // already used lab tags for placement of shapes
495 for(int a = 0; a < aSubNum; a++) {
496 FeaturePtr aSub = aComposite->subFeature(a);
497 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aSub->results();
498 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRes = aResults.cbegin();
499 // there may be many shapes (circle and center): register if at least one is in selection
500 for(; aRes != aResults.cend(); aRes++) {
501 ResultConstructionPtr aConstr =
502 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*aRes);
503 if (!aConstr->shape()) {
506 if (aShapeType != TopAbs_VERTEX) {
507 if (aConstr->shape()->isEdge()) {
508 const TopoDS_Shape& aResShape = aConstr->shape()->impl<TopoDS_Shape>();
509 TopoDS_Edge anEdge = TopoDS::Edge(aResShape);
510 if (!anEdge.IsNull()) {
511 Standard_Real aFirst, aLast;
512 Handle(Geom_Curve) aCurve = BRep_Tool::Curve(anEdge, aFirst, aLast);
513 if (allCurves.Contains(aCurve)) {
514 int anID = aComposite->subFeatureId(a);
515 if (aShapeType != TopAbs_EDGE) { // face needs the sub-edges on sub-labels
516 // add edges to sub-label to support naming for edges selection
517 TopExp_Explorer anEdgeExp(aSubShape, TopAbs_EDGE);
518 for(; anEdgeExp.More(); anEdgeExp.Next()) {
519 TopoDS_Edge anEdge = TopoDS::Edge(anEdgeExp.Current());
520 Standard_Real aFirst, aLast;
521 Handle(Geom_Curve) aFaceCurve = BRep_Tool::Curve(anEdge, aFirst, aLast);
522 if (aFaceCurve == aCurve) {
523 while(aUsedIDMap.Contains(anID))
525 aUsedIDMap.Add(anID);
526 TDF_Label aSubLab = aLab.FindChild(anID);
527 std::string aFullNameSub = fullName(aComposite, anEdge);
528 saveSubName(aComposite, aSubLab, isSelectionMode, anEdge, aMyDoc, aFullNameSub);
530 int anOrient = Model_SelectionNaming::edgeOrientation(aSubShape, anEdge);
532 // store the orientation of edge relatively to face if needed
533 TDataStd_Integer::Set(aSubLab, anOrient);
537 } else { // put vertices of the selected edge to sub-labels
538 // add edges to sub-label to support naming for edges selection
539 for(TopExp_Explorer anEdgeExp(aSubShape, TopAbs_VERTEX);
540 anEdgeExp.More(); anEdgeExp.Next()) {
541 TopoDS_Vertex aV = TopoDS::Vertex(anEdgeExp.Current());
542 while(aUsedIDMap.Contains(anID))
544 aUsedIDMap.Add(anID);
545 TDF_Label aSubLab = aLab.FindChild(anID);
546 std::string aFullNameSub = fullName(aComposite, aV);
547 saveSubName(aComposite, aSubLab, isSelectionMode, aV, aMyDoc, aFullNameSub);
556 std::string aFullName = fullName(aComposite, aSubShape, aRefs);
557 // store the selected as primitive
558 registerSubShape(aLab, aSubShape, aFullName, 0, aMyDoc, isSelectionMode);
562 std::shared_ptr<GeomAPI_Shape> Model_ResultConstruction::shape(const int theIndex,
563 const std::shared_ptr<ModelAPI_Document> theExtDoc)
565 std::shared_ptr<GeomAPI_Shape> aResult;
567 return aResult; // the whole shape, so, NULL
570 TDF_Label aLab = startLabel(theExtDoc, isExt).FindChild(theIndex + 1);
571 if (!aLab.IsNull()) { // index is not bad
572 Handle(TNaming_NamedShape) aSelection;
573 if (aLab.FindAttribute(TNaming_NamedShape::GetID(), aSelection)) {
574 TopoDS_Shape aSelShape = aSelection->Get();
575 if (aSelShape.IsNull())
576 return aResult; // shape equal to context => null
577 aResult = std::shared_ptr<GeomAPI_Shape>(new GeomAPI_Shape);
578 aResult->setImpl(new TopoDS_Shape(aSelShape));
585 bool Model_ResultConstruction::update(const int theIndex,
586 const std::shared_ptr<ModelAPI_Document> theExtDoc, bool& theModified)
590 TDF_Label aLab = startLabel(theExtDoc, anExt).FindChild(theIndex + 1, Standard_True);
591 if (theIndex == 0 || aLab.IsAttribute(kFULL_RESULT_ID)) { // full for external same as index == 0
592 // it is just reference to construction, not sub-shape
593 // if there is a sketch, the sketch-naming must be updated
595 // update all faces named by the whole result
597 Handle(TDataStd_IntPackedMap) anIndices;
598 if (aLab.FindAttribute(TDataStd_IntPackedMap::GetID(), anIndices)) {
599 NCollection_Map<TopoDS_Shape> aFaces; // collect faces, updated in the tree
600 TColStd_MapIteratorOfPackedMapOfInteger anIndexIter(anIndices->GetMap());
601 Handle(TColStd_HPackedMapOfInteger) aNewPackedMap =
602 new TColStd_HPackedMapOfInteger; // with only faces that are ok
603 // iterate to find existing faces, updated
604 for(; anIndexIter.More(); anIndexIter.Next()) {
605 if (update(anIndexIter.Key(), theExtDoc, theModified)) {
606 GeomShapePtr aFace = shape(anIndexIter.Key(), theExtDoc);
607 if (!aFaces.Contains(aFace->impl<TopoDS_Shape>())) {
608 aNewPackedMap->ChangeMap().Add(anIndexIter.Key());
609 aFaces.Add(aFace->impl<TopoDS_Shape>());
613 // then iterate all existing faces to find new faces
614 int aCurrentFacesNum = facesNum();
615 for(int a = 0; a < aCurrentFacesNum; a++) {
616 GeomShapePtr aFace = face(a);
617 if (!aFaces.Contains(aFace->impl<TopoDS_Shape>())) {
619 int aNewFaceIndex = select(aFace, theExtDoc, -1);
620 if (aNewFaceIndex > 0) {
621 aNewPackedMap->ChangeMap().Add(aNewFaceIndex);
625 anIndices->ChangeMap(aNewPackedMap);
629 // check is this modified or not
630 std::shared_ptr<GeomAPI_Shape> aNewShape = shape();
631 TopoDS_Shape anOldSh;
632 Handle(TNaming_NamedShape) aNS;
633 if (aLab.FindAttribute(TNaming_NamedShape::GetID(), aNS)) {
634 anOldSh = aNS->Get();
636 if (aNewShape.get()) {
637 if (anOldSh.IsNull())
640 std::shared_ptr<GeomAPI_Shape> anOldShape(new GeomAPI_Shape);
641 anOldShape->setImpl<TopoDS_Shape>(new TopoDS_Shape(anOldSh));
642 theModified = !anOldShape->isEqual(aNewShape);
645 else if (!anOldSh.IsNull()) {
649 // For correct naming selection, put the shape into the naming structure.
650 // It seems sub-shapes are not needed: only this shape is (and can be ) selected.
651 TNaming_Builder aBuilder(aLab);
652 aBuilder.Generated(aNewShape->impl<TopoDS_Shape>());
654 return shape() && !shape()->isNull();
656 // construction: identification by the results indexes, recompute faces and
657 // take the face that more close by the indexes
658 ResultPtr aThisPtr = std::dynamic_pointer_cast<ModelAPI_Result>(data()->owner());
659 FeaturePtr aContextFeature = document()->feature(aThisPtr);
661 // sketch sub-element
662 if (std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aContextFeature).get())
664 // update the referenced object if it is sub
665 Handle(TDF_Reference) aRef;
666 if (aLab.FindAttribute(TDF_Reference::GetID(), aRef)) {
667 int aFaceIndex = aRef->Get().Tag();
668 // don't check selection since face may disappear, but the shape stays correct
669 Model_ResultConstruction::update(aFaceIndex, theExtDoc, theModified);
671 // getting a type of selected shape
672 Handle(TDataStd_Integer) aTypeAttr;
673 if (!aLab.FindAttribute(TDataStd_Integer::GetID(), aTypeAttr)) {
676 TopAbs_ShapeEnum aShapeType = (TopAbs_ShapeEnum)(aTypeAttr->Get());
677 // selected indexes will be needed in each "if"
678 Handle(TDataStd_IntPackedMap) aSubIds;
679 std::shared_ptr<GeomAPI_Shape> aNewSelected;
681 !aLab.FindAttribute(TDataStd_IntPackedMap::GetID(), aSubIds) || aSubIds->Extent() == 0;
682 // for now working only with composite features
683 CompositeFeaturePtr aComposite =
684 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aContextFeature);
685 if (!aComposite.get() || aComposite->numberOfSubs() == 0) {
689 if (aShapeType == TopAbs_FACE || aShapeType == TopAbs_WIRE) {
690 // compound is for the whole sketch selection
691 // If this is a wire with plane defined then it is a sketch-like object
692 if (!facesNum()) // no faces, update can not work correctly
694 // if there is no edges indexes, any face can be used: take the first
695 std::shared_ptr<GeomAPI_Shape> aNewSelected;
697 aNewSelected = face(0);
698 } else { // searching for most looks-like initial face by the indexes
699 // prepare edges of the current result for the fast searching
700 // curves and orientations of edges
701 NCollection_DataMap<Handle(Geom_Curve), int> allCurves;
702 const int aSubNum = aComposite->numberOfSubs();
703 for(int a = 0; a < aSubNum; a++) {
704 int aSubID = aComposite->subFeatureId(a);
705 if (aSubIds->Contains(aSubID)) {
706 FeaturePtr aSub = aComposite->subFeature(a);
707 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aSub->results();
708 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRes;
709 for(aRes = aResults.cbegin(); aRes != aResults.cend(); aRes++) {
710 ResultConstructionPtr aConstr =
711 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*aRes);
712 if (aConstr->shape() && aConstr->shape()->isEdge()) {
713 const TopoDS_Shape& aResShape = aConstr->shape()->impl<TopoDS_Shape>();
714 TopoDS_Edge anEdge = TopoDS::Edge(aResShape);
715 if (!anEdge.IsNull()) {
716 Standard_Real aFirst, aLast;
717 Handle(Geom_Curve) aCurve = BRep_Tool::Curve(anEdge, aFirst, aLast);
718 // searching for orientation information
720 Handle(TDataStd_Integer) anInt;
721 if (aLab.FindChild(aSubID).FindAttribute(TDataStd_Integer::GetID(), anInt)) {
722 anOrient = anInt->Get();
724 allCurves.Bind(aCurve, anOrient);
730 aNewSelected = Model_SelectionNaming::findAppropriateFace(
731 aThisPtr, allCurves, aShapeType == TopAbs_WIRE);
733 if (aNewSelected) { // store this new selection
734 select(aNewSelected, theExtDoc, theIndex);
738 // if the selection is not found, put the empty shape:
739 // it's better to have disappeared shape, than the old, the lost one
740 TNaming_Builder anEmptyBuilder(aLab);
743 } else if (aShapeType == TopAbs_EDGE) {
744 // just reselect the edge by the id
745 const int aSubNum = aComposite->numberOfSubs();
746 for(int a = 0; a < aSubNum; a++) {
747 // if aSubIds take any, the first appropriate
748 if (aSubIds->IsEmpty() || aSubIds->Contains(aComposite->subFeatureId(a))) {
749 // found the appropriate feature
750 FeaturePtr aFeature = aComposite->subFeature(a);
751 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aResIter =
752 aFeature->results().cbegin();
753 for(;aResIter != aFeature->results().cend(); aResIter++) {
754 ResultConstructionPtr aRes =
755 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*aResIter);
756 if (aRes && aRes->shape() && aRes->shape()->isEdge()) { // found!
757 select(aRes->shape(), theExtDoc, theIndex);
764 } else if (aShapeType == TopAbs_VERTEX) {
765 // just reselect the vertex by the id of edge
766 const int aSubNum = aComposite->numberOfSubs();
767 for(int a = 0; a < aSubNum; a++) {
768 // if aSubIds take any, the first appropriate
769 int aFeatureID = aComposite->subFeatureId(a);
770 if (aSubIds->IsEmpty() || aSubIds->Contains(aFeatureID) ||
771 aSubIds->Contains(aFeatureID + kSTART_VERTEX_DELTA) ||
772 aSubIds->Contains(aFeatureID + kSTART_VERTEX_DELTA * 2)) {
773 // searching for deltas
775 if (aSubIds->Contains(aFeatureID + kSTART_VERTEX_DELTA)) aVertexNum = 1;
776 else if (aSubIds->Contains(aFeatureID + kSTART_VERTEX_DELTA * 2)) aVertexNum = 2;
777 // found the feature with appropriate edge
778 FeaturePtr aFeature = aComposite->subFeature(a);
779 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aResIter =
780 aFeature->results().cbegin();
781 for(;aResIter != aFeature->results().cend(); aResIter++) {
782 ResultConstructionPtr aRes =
783 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*aResIter);
784 if (aRes && aRes->shape()) {
785 if (aRes->shape()->isVertex() && aVertexNum == 0) { // found!
786 select(aRes->shape(), theExtDoc, theIndex);
789 } else if (aRes->shape()->isEdge() && aVertexNum > 0) {
790 const TopoDS_Shape& anEdge = aRes->shape()->impl<TopoDS_Shape>();
792 for(TopExp_Explorer aVExp(anEdge, TopAbs_VERTEX); aVExp.More(); aVExp.Next()) {
793 if (aVIndex == aVertexNum) { // found!
794 std::shared_ptr<GeomAPI_Shape> aVertex(new GeomAPI_Shape);
795 aVertex->setImpl(new TopoDS_Shape(aVExp.Current()));
796 select(aVertex, theExtDoc, theIndex);
808 } else { // simple construction element: the selected is that needed
809 select(shape(), theExtDoc, theIndex);
813 return false; // unknown case