1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 // File: ModelAPI_ResultConstruction.cpp
4 // Created: 07 Jul 2014
5 // Author: Mikhail PONIKAROV
7 #include <Model_ResultConstruction.h>
9 #include <Model_Data.h>
10 #include <ModelAPI_CompositeFeature.h>
11 #include <Model_SelectionNaming.h>
12 #include <Config_PropManager.h>
13 #include <GeomAPI_PlanarEdges.h>
14 #include <GeomAPI_Shape.h>
15 #include <GeomAlgoAPI_SketchBuilder.h>
16 #include <Events_Loop.h>
17 #include <ModelAPI_Events.h>
19 #include <TDF_ChildIterator.hxx>
20 #include <TNaming_NamedShape.hxx>
21 #include <TNaming_Builder.hxx>
22 #include <TopoDS_Shape.hxx>
23 #include <TDataStd_Integer.hxx>
24 #include <TDataStd_IntPackedMap.hxx>
25 #include <TDataStd_Name.hxx>
26 #include <TColStd_MapOfTransient.hxx>
27 #include <TColStd_MapIteratorOfPackedMapOfInteger.hxx>
28 #include <BRep_Tool.hxx>
29 #include <BRep_Builder.hxx>
31 #include <TopoDS_Edge.hxx>
32 #include <TopoDS_Vertex.hxx>
33 #include <TopExp_Explorer.hxx>
34 #include <Precision.hxx>
37 void Model_ResultConstruction::colorConfigInfo(std::string& theSection, std::string& theName,
38 std::string& theDefault)
40 theSection = "Visualization";
41 theName = "result_construction_color";
42 theDefault = DEFAULT_COLOR();
45 void Model_ResultConstruction::setShape(std::shared_ptr<GeomAPI_Shape> theShape)
47 if (myShape != theShape && (!theShape.get() || !theShape->isEqual(myShape))) {
48 static const Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
49 ModelAPI_EventCreator::get()->sendUpdated(data()->owner(), anEvent);
52 myFacesUpToDate = false;
58 std::shared_ptr<GeomAPI_Shape> Model_ResultConstruction::shape()
63 Model_ResultConstruction::Model_ResultConstruction()
67 myFacesUpToDate = false;
70 void Model_ResultConstruction::setIsInHistory(const bool isInHistory)
72 myIsInHistory = isInHistory;
75 int Model_ResultConstruction::facesNum()
77 if (!myFacesUpToDate) {
78 std::shared_ptr<GeomAPI_PlanarEdges> aWirePtr =
79 std::dynamic_pointer_cast<GeomAPI_PlanarEdges>(myShape);
81 std::list<std::shared_ptr<GeomAPI_Shape> > aFaces;
82 GeomAlgoAPI_SketchBuilder::createFaces(aWirePtr->origin(), aWirePtr->dirX(),
83 aWirePtr->norm(), aWirePtr, aFaces);
84 std::list<std::shared_ptr<GeomAPI_Shape> >::iterator aFIter = aFaces.begin();
85 for(; aFIter != aFaces.end(); aFIter++) {
86 std::shared_ptr<GeomAPI_Face> aFace(new GeomAPI_Face(*aFIter));
87 if (aFace.get() && !aFace->isNull())
88 myFaces.push_back(aFace);
91 myFacesUpToDate = true;
93 return int(myFaces.size());
96 std::shared_ptr<GeomAPI_Face> Model_ResultConstruction::face(const int theIndex)
98 return myFaces[theIndex];
101 bool Model_ResultConstruction::isInfinite()
106 void Model_ResultConstruction::setInfinite(const bool theInfinite)
108 myIsInfinite = theInfinite;
111 void Model_ResultConstruction::setIsConcealed(const bool theValue)
113 // do nothing: the construction element is never consealed
116 static const int kSTART_VERTEX_DELTA = 1000000;
118 static void registerSubShape(TDF_Label theMainLabel, TopoDS_Shape theShape,
119 const int theID, std::shared_ptr<Model_Document> theDoc,
120 std::map<int, int>& theOrientations,
121 // name of sub-elements by ID to be exported instead of indexes
122 std::map<int, std::string>& theSubNames,
123 Handle(TDataStd_IntPackedMap) theRefs = Handle(TDataStd_IntPackedMap)(),
124 const int theOrientation = 0)
126 TDF_Label aLab = theID == 0 ? theMainLabel : theMainLabel.FindChild(theID);
127 if (theOrientation != 0) { // store the orientation of edge relatively to face if needed
128 TDataStd_Integer::Set(aLab, theOrientation);
130 TNaming_Builder aBuilder(aLab);
131 aBuilder.Generated(theShape);
132 std::stringstream aName;
133 // #1839 : do not store name of the feature in the tree, since this name could be changed
134 //aName<<theContextFeature->name();
135 if (theShape.ShapeType() != TopAbs_COMPOUND) { // compound means the whole result for construction
137 if (theShape.ShapeType() == TopAbs_FACE) aName<<"Face";
138 else if (theShape.ShapeType() == TopAbs_WIRE) aName<<"Wire";
139 else if (theShape.ShapeType() == TopAbs_EDGE) aName<<"Edge";
140 else if (theShape.ShapeType() == TopAbs_VERTEX) aName<<"Vertex";
142 if (theRefs.IsNull()) {
144 if (theOrientation == 1)
146 else if (theOrientation == -1)
148 } else { // make a composite name from all sub-elements indexes: "1_2_3_4"
149 TColStd_MapIteratorOfPackedMapOfInteger aRef(theRefs->GetMap());
150 for(; aRef.More(); aRef.Next()) {
151 aName<<"-"<<theSubNames[aRef.Key()];
152 if (theOrientations.find(aRef.Key()) != theOrientations.end()) {
153 if (theOrientations[aRef.Key()] == 1)
155 else if (theOrientations[aRef.Key()] == -1)
162 theDoc->addNamingName(aLab, aName.str());
163 TDataStd_Name::Set(aLab, aName.str().c_str());
166 TDF_Label Model_ResultConstruction::startLabel(
167 const std::shared_ptr<ModelAPI_Document> theExtDoc, bool& theExternal)
169 theExternal = theExtDoc.get() && theExtDoc != document();
170 if (theExternal) { // external document is used
171 std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(theExtDoc);
172 return aDoc->extConstructionsLabel();
174 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(data());
175 return aData->label();
178 int Model_ResultConstruction::select(const std::shared_ptr<GeomAPI_Shape>& theSubShape,
179 const std::shared_ptr<ModelAPI_Document> theExtDoc, const int theIndex)
181 int anIndex; // resulting index of the sub-label
182 TopoDS_Shape aSubShape;
183 if (theSubShape.get()) {
184 aSubShape = theSubShape->impl<TopoDS_Shape>();
185 } else if (shape().get()) {
186 aSubShape = shape()->impl<TopoDS_Shape>();
188 // if external document requires this selection, put the naming structures to this doc
189 // to support the naming mechanism in this document correctly
191 TDF_Label aDataLab = startLabel(theExtDoc, anExternal);
192 if (theIndex == -1) {
193 anIndex = anExternal ? 2 : 1; // for the external doc don't mind about the main shape
195 if (theSubShape.get() || anExternal) { // searching for already selected sub (or whole for ext)
196 // iterate all the already presented shapes to see the same
197 TDF_ChildIterator aSubsIter(aDataLab, Standard_False);
198 for(; aSubsIter.More(); aSubsIter.Next()) {
199 const TDF_Label aLab = aSubsIter.Value();
200 if (aLab.Tag() == 1) // skip the root shape label
202 Handle(TNaming_NamedShape) aNS;
203 if (aLab.FindAttribute(TNaming_NamedShape::GetID(), aNS)) {
204 if (aNS->Get().IsSame(aSubShape)) {
205 return aLab.Tag() - 1; // found exactly the needed shape, nothing else to do
208 anIndex = aLab.Tag(); // searching for the latest index
210 anIndex = (anIndex == 1) ? 2 : (anIndex + 1); // next after 1-root, or next after all
213 anIndex = theIndex + 1;
216 // set the naming structure at index
217 TDF_Label aLab = aDataLab.FindChild(anIndex, Standard_True);
218 TNaming_Builder aBuilder(aLab);
219 if (aSubShape.IsNull())
220 return anIndex - 1; // just keep empty named shape
221 aBuilder.Generated(aSubShape);
223 if (anIndex == 1 && isInfinite()) { // infinitive results has no sub-selection
226 ResultPtr aThisPtr = std::dynamic_pointer_cast<ModelAPI_Result>(data()->owner());
227 FeaturePtr aThisFeature = document()->feature(aThisPtr);
228 CompositeFeaturePtr aComposite =
229 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aThisFeature);
230 if (!aComposite || aComposite->numberOfSubs() == 0) {
231 // saving of context is enough: result construction contains exactly the needed shape
234 // identify the results of sub-object of the composite by edges
235 // save type of the selected shape in integer attribute
236 TopAbs_ShapeEnum aShapeType = aSubShape.ShapeType();
237 TDataStd_Integer::Set(aLab, (int)aShapeType);
239 TColStd_MapOfTransient allCurves;
240 if (aShapeType == TopAbs_VERTEX) { // compare positions
241 aVertexPos = BRep_Tool::Pnt(TopoDS::Vertex(aSubShape));
243 for(TopExp_Explorer anEdgeExp(aSubShape, TopAbs_EDGE); anEdgeExp.More(); anEdgeExp.Next()) {
244 TopoDS_Edge anEdge = TopoDS::Edge(anEdgeExp.Current());
245 Standard_Real aFirst, aLast;
246 Handle(Geom_Curve) aCurve = BRep_Tool::Curve(anEdge, aFirst, aLast);
247 allCurves.Add(aCurve);
250 std::shared_ptr<Model_Document> aMyDoc =
251 std::dynamic_pointer_cast<Model_Document>(document());
252 // iterate and store the result ids of sub-elements and sub-elements to sub-labels
253 Handle(TDataStd_IntPackedMap) aRefs = TDataStd_IntPackedMap::Set(aLab);
254 std::map<int, int> anOrientations; //map from edges IDs to orientations of these edges in face
255 std::map<int, std::string> aSubNames; //map from edges IDs to names of edges
257 const int aSubNum = aComposite->numberOfSubs();
258 for(int a = 0; a < aSubNum; a++) {
259 FeaturePtr aSub = aComposite->subFeature(a);
260 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aSub->results();
261 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRes = aResults.cbegin();
262 // there may be many shapes (circle and center): register if at least one is in selection
263 for(; aRes != aResults.cend(); aRes++) {
264 ResultConstructionPtr aConstr =
265 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*aRes);
266 if (!aConstr->shape()) {
269 if (aShapeType == TopAbs_VERTEX) {
270 if (aConstr->shape()->isVertex()) { // compare vertices positions
271 const TopoDS_Shape& aVertex = aConstr->shape()->impl<TopoDS_Shape>();
272 gp_Pnt aPnt = BRep_Tool::Pnt(TopoDS::Vertex(aVertex));
273 if (aPnt.IsEqual(aVertexPos, Precision::Confusion())) {
274 aRefs->Add(aComposite->subFeatureId(a));
275 aSubNames[aComposite->subFeatureId(a)] = Model_SelectionNaming::shortName(aConstr);
277 } else { // get first or last vertex of the edge: last is stored with additional delta
278 const TopoDS_Shape& anEdge = aConstr->shape()->impl<TopoDS_Shape>();
279 int aDelta = kSTART_VERTEX_DELTA;
280 for(TopExp_Explorer aVExp(anEdge, TopAbs_VERTEX); aVExp.More(); aVExp.Next()) {
281 gp_Pnt aPnt = BRep_Tool::Pnt(TopoDS::Vertex(aVExp.Current()));
282 if (aPnt.IsEqual(aVertexPos, Precision::Confusion())) {
283 aRefs->Add(aDelta + aComposite->subFeatureId(a));
284 aSubNames[aDelta + aComposite->subFeatureId(a)] =
285 Model_SelectionNaming::shortName(aConstr, aDelta / kSTART_VERTEX_DELTA);
288 aDelta += kSTART_VERTEX_DELTA;
292 if (aConstr->shape()->isEdge()) {
293 const TopoDS_Shape& aResShape = aConstr->shape()->impl<TopoDS_Shape>();
294 TopoDS_Edge anEdge = TopoDS::Edge(aResShape);
295 if (!anEdge.IsNull()) {
296 Standard_Real aFirst, aLast;
297 Handle(Geom_Curve) aCurve = BRep_Tool::Curve(anEdge, aFirst, aLast);
298 if (allCurves.Contains(aCurve)) {
299 int anID = aComposite->subFeatureId(a);
301 aSubNames[anID] = Model_SelectionNaming::shortName(aConstr);
302 if (aShapeType != TopAbs_EDGE) { // face needs the sub-edges on sub-labels
303 // add edges to sub-label to support naming for edges selection
304 TopExp_Explorer anEdgeExp(aSubShape, TopAbs_EDGE);
305 for(; anEdgeExp.More(); anEdgeExp.Next()) {
306 TopoDS_Edge anEdge = TopoDS::Edge(anEdgeExp.Current());
307 Standard_Real aFirst, aLast;
308 Handle(Geom_Curve) aFaceCurve = BRep_Tool::Curve(anEdge, aFirst, aLast);
309 if (aFaceCurve == aCurve) {
310 int anOrient = Model_SelectionNaming::edgeOrientation(aSubShape, anEdge);
311 anOrientations[anID] = anOrient;
313 TDF_Label aSubLab = aLab.FindChild(anID);
314 std::string aName = "Edge-" + Model_SelectionNaming::shortName(aConstr, 0);
315 TNaming_Builder aBuilder(aSubLab);
316 aBuilder.Generated(anEdge);
317 aMyDoc->addNamingName(aSubLab, aName.c_str());
318 TDataStd_Name::Set(aSubLab, aName.c_str());
321 // store the orientation of edge relatively to face if needed
322 TDataStd_Integer::Set(aSubLab, anOrient);
326 } else { // put vertices of the selected edge to sub-labels
327 // add edges to sub-label to support naming for edges selection
328 int aDelta = kSTART_VERTEX_DELTA;
329 int aTagIndex = anID + kSTART_VERTEX_DELTA;
330 for(TopExp_Explorer anEdgeExp(aSubShape, TopAbs_VERTEX);
333 aTagIndex += kSTART_VERTEX_DELTA,
334 aDelta += kSTART_VERTEX_DELTA) {
335 TopoDS_Vertex aV = TopoDS::Vertex(anEdgeExp.Current());
337 TDF_Label aSubLab = aLab.FindChild(aTagIndex);
338 std::string aName = "Vertex-"
339 + Model_SelectionNaming::shortName(aConstr, aDelta / kSTART_VERTEX_DELTA);
340 TNaming_Builder aBuilder(aLab);
341 aBuilder.Generated(aV);
342 aMyDoc->addNamingName(aLab, aName.c_str());
343 TDataStd_Name::Set(aLab, aName.c_str());
352 // store the selected as primitive
353 registerSubShape(aLab, aSubShape, 0, aMyDoc, anOrientations, aSubNames, aRefs);
358 std::shared_ptr<GeomAPI_Shape> Model_ResultConstruction::shape(const int theIndex,
359 const std::shared_ptr<ModelAPI_Document> theExtDoc)
361 std::shared_ptr<GeomAPI_Shape> aResult;
363 return aResult; // the whole shape, so, NULL
366 TDF_Label aLab = startLabel(theExtDoc, isExt).FindChild(theIndex + 1);
367 if (!aLab.IsNull()) { // index is not bad
368 Handle(TNaming_NamedShape) aSelection;
369 if (aLab.FindAttribute(TNaming_NamedShape::GetID(), aSelection)) {
370 TopoDS_Shape aSelShape = aSelection->Get();
371 aResult = std::shared_ptr<GeomAPI_Shape>(new GeomAPI_Shape);
372 aResult->setImpl(new TopoDS_Shape(aSelShape));
379 bool Model_ResultConstruction::update(const int theIndex,
380 const std::shared_ptr<ModelAPI_Document> theExtDoc, bool& theModified)
384 TDF_Label aLab = startLabel(theExtDoc, anExt).FindChild(theIndex + 1, Standard_True);
386 // it is just reference to construction, not sub-shape
387 // if there is a sketch, the sketch-naming must be updated
389 BRep_Builder aCompoundBuilder;
390 TopoDS_Compound aComp;
391 aCompoundBuilder.MakeCompound(aComp);
392 for(int a = 0; a < facesNum(); a++) {
393 TopoDS_Shape aFace = face(a)->impl<TopoDS_Shape>();
394 aCompoundBuilder.Add(aComp, aFace);
396 std::shared_ptr<GeomAPI_Shape> aShape(new GeomAPI_Shape);
397 aShape->setImpl<TopoDS_Shape>(new TopoDS_Shape(aComp));
398 select(aShape, theExtDoc, theIndex);
400 // For correct naming selection, put the shape into the naming structure.
401 // It seems sub-shapes are not needed: only this shape is (and can be ) selected.
402 TNaming_Builder aBuilder(aLab);
403 aBuilder.Generated(shape()->impl<TopoDS_Shape>());
405 return shape() && !shape()->isNull();
407 // construction: identification by the results indexes, recompute faces and
408 // take the face that more close by the indexes
409 ResultPtr aThisPtr = std::dynamic_pointer_cast<ModelAPI_Result>(data()->owner());
410 FeaturePtr aContextFeature = document()->feature(aThisPtr);
411 // sketch sub-element
412 if (std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aContextFeature).get())
414 // getting a type of selected shape
415 Handle(TDataStd_Integer) aTypeAttr;
416 if (!aLab.FindAttribute(TDataStd_Integer::GetID(), aTypeAttr)) {
419 TopAbs_ShapeEnum aShapeType = (TopAbs_ShapeEnum)(aTypeAttr->Get());
420 // selected indexes will be needed in each "if"
421 Handle(TDataStd_IntPackedMap) aSubIds;
422 std::shared_ptr<GeomAPI_Shape> aNewSelected;
424 !aLab.FindAttribute(TDataStd_IntPackedMap::GetID(), aSubIds) || aSubIds->Extent() == 0;
425 // for now working only with composite features
426 CompositeFeaturePtr aComposite =
427 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aContextFeature);
428 if (!aComposite.get() || aComposite->numberOfSubs() == 0) {
432 if (aShapeType == TopAbs_FACE || aShapeType == TopAbs_WIRE) {
433 // compound is for the whole sketch selection
434 // If this is a wire with plane defined then it is a sketch-like object
435 if (!facesNum()) // no faces, update can not work correctly
437 // if there is no edges indexes, any face can be used: take the first
438 std::shared_ptr<GeomAPI_Shape> aNewSelected;
440 aNewSelected = face(0);
441 } else { // searching for most looks-like initial face by the indexes
442 // prepare edges of the current result for the fast searching
443 // curves and orientations of edges
444 NCollection_DataMap<Handle(Geom_Curve), int> allCurves;
445 const int aSubNum = aComposite->numberOfSubs();
446 for(int a = 0; a < aSubNum; a++) {
447 int aSubID = aComposite->subFeatureId(a);
448 if (aSubIds->Contains(aSubID)) {
449 FeaturePtr aSub = aComposite->subFeature(a);
450 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aSub->results();
451 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRes;
452 for(aRes = aResults.cbegin(); aRes != aResults.cend(); aRes++) {
453 ResultConstructionPtr aConstr =
454 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*aRes);
455 if (aConstr->shape() && aConstr->shape()->isEdge()) {
456 const TopoDS_Shape& aResShape = aConstr->shape()->impl<TopoDS_Shape>();
457 TopoDS_Edge anEdge = TopoDS::Edge(aResShape);
458 if (!anEdge.IsNull()) {
459 Standard_Real aFirst, aLast;
460 Handle(Geom_Curve) aCurve = BRep_Tool::Curve(anEdge, aFirst, aLast);
461 // searching for orientation information
463 Handle(TDataStd_Integer) anInt;
464 if (aLab.FindChild(aSubID).FindAttribute(TDataStd_Integer::GetID(), anInt)) {
465 anOrient = anInt->Get();
467 allCurves.Bind(aCurve, anOrient);
473 aNewSelected = Model_SelectionNaming::findAppropriateFace(
474 aThisPtr, allCurves, aShapeType == TopAbs_WIRE);
476 if (aNewSelected) { // store this new selection
477 select(aNewSelected, theExtDoc, theIndex);
481 // if the selection is not found, put the empty shape:
482 // it's better to have disappeared shape, than the old, the lost one
483 TNaming_Builder anEmptyBuilder(aLab);
486 } else if (aShapeType == TopAbs_EDGE) {
487 // just reselect the edge by the id
488 const int aSubNum = aComposite->numberOfSubs();
489 for(int a = 0; a < aSubNum; a++) {
490 // if aSubIds take any, the first appropriate
491 if (aSubIds->IsEmpty() || aSubIds->Contains(aComposite->subFeatureId(a))) {
492 // found the appropriate feature
493 FeaturePtr aFeature = aComposite->subFeature(a);
494 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aResIter =
495 aFeature->results().cbegin();
496 for(;aResIter != aFeature->results().cend(); aResIter++) {
497 ResultConstructionPtr aRes =
498 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*aResIter);
499 if (aRes && aRes->shape() && aRes->shape()->isEdge()) { // found!
500 select(aRes->shape(), theExtDoc, theIndex);
507 } else if (aShapeType == TopAbs_VERTEX) {
508 // just reselect the vertex by the id of edge
509 const int aSubNum = aComposite->numberOfSubs();
510 for(int a = 0; a < aSubNum; a++) {
511 // if aSubIds take any, the first appropriate
512 int aFeatureID = aComposite->subFeatureId(a);
513 if (aSubIds->IsEmpty() || aSubIds->Contains(aFeatureID) ||
514 aSubIds->Contains(aFeatureID + kSTART_VERTEX_DELTA) ||
515 aSubIds->Contains(aFeatureID + kSTART_VERTEX_DELTA * 2)) {
516 // searching for deltas
518 if (aSubIds->Contains(aFeatureID + kSTART_VERTEX_DELTA)) aVertexNum = 1;
519 else if (aSubIds->Contains(aFeatureID + kSTART_VERTEX_DELTA * 2)) aVertexNum = 2;
520 // found the feature with appropriate edge
521 FeaturePtr aFeature = aComposite->subFeature(a);
522 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aResIter =
523 aFeature->results().cbegin();
524 for(;aResIter != aFeature->results().cend(); aResIter++) {
525 ResultConstructionPtr aRes =
526 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*aResIter);
527 if (aRes && aRes->shape()) {
528 if (aRes->shape()->isVertex() && aVertexNum == 0) { // found!
529 select(aRes->shape(), theExtDoc, theIndex);
532 } else if (aRes->shape()->isEdge() && aVertexNum > 0) {
533 const TopoDS_Shape& anEdge = aRes->shape()->impl<TopoDS_Shape>();
535 for(TopExp_Explorer aVExp(anEdge, TopAbs_VERTEX); aVExp.More(); aVExp.Next()) {
536 if (aVIndex == aVertexNum) { // found!
537 std::shared_ptr<GeomAPI_Shape> aVertex(new GeomAPI_Shape);
538 aVertex->setImpl(new TopoDS_Shape(aVExp.Current()));
539 select(aVertex, theExtDoc, theIndex);
551 } else { // simple construction element: the selected is that needed
552 select(shape(), theExtDoc, theIndex);
556 return false; // unknown case