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 <GeomAlgoAPI_SketchBuilder.h>
26 #include <ModelAPI_Events.h>
27 #include <Model_Document.h>
28 #include <GeomAPI_PlanarEdges.h>
29 #include <GeomAPI_Shape.h>
30 #include <Events_Loop.h>
31 #include <GeomDataAPI_Point.h>
32 #include <GeomDataAPI_Dir.h>
34 #include <TDF_ChildIDIterator.hxx>
35 #include <TNaming_NamedShape.hxx>
36 #include <TNaming_Builder.hxx>
37 #include <TDataStd_IntPackedMap.hxx>
38 #include <TDataStd_Name.hxx>
39 #include <TDataStd_UAttribute.hxx>
40 #include <BRep_Builder.hxx>
42 #include <TopoDS_Edge.hxx>
43 #include <TopoDS_Vertex.hxx>
44 #include <TopoDS_ListOfShape.hxx>
45 #include <TopExp_Explorer.hxx>
46 #include <TopTools_MapOfShape.hxx>
47 #include <NCollection_IndexedDataMap.hxx>
52 // identifier of the infinite result
53 Standard_GUID kIS_INFINITE("dea8cc5a-53f2-49c1-94e8-a947bed20a9f");
54 // identifier of the result not in history
55 Standard_GUID kIS_IN_HISTORY("a9aec01c-805e-44d1-b5d2-a63f06522f8a");
58 void Model_ResultConstruction::colorConfigInfo(std::string& theSection, std::string& theName,
59 std::string& theDefault)
61 theSection = "Visualization";
62 theName = "result_construction_color";
63 theDefault = DEFAULT_COLOR();
66 void Model_ResultConstruction::setShape(std::shared_ptr<GeomAPI_Shape> theShape)
68 if (myShape != theShape) {
71 if (!theShape.get() || !theShape->isEqual(myShape)) {
72 static const Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
73 ModelAPI_EventCreator::get()->sendUpdated(data()->owner(), anEvent);
79 std::shared_ptr<GeomAPI_Shape> Model_ResultConstruction::shape()
84 static std::string shortName(
85 std::shared_ptr<ModelAPI_ResultConstruction>& theConstr)
87 std::string aName = theConstr->data()->name();
88 // remove "-", "/" and "&" command-symbols
89 aName.erase(std::remove(aName.begin(), aName.end(), '-'), aName.end());
90 aName.erase(std::remove(aName.begin(), aName.end(), '/'), aName.end());
91 aName.erase(std::remove(aName.begin(), aName.end(), '&'), aName.end());
92 // remove the last 's', 'e', 'f' and 'r' symbols:
93 // they are used as markers of start/end/forward/reversed indicators
94 static const std::string aSyms("sefr");
95 std::string::iterator aSuffix = aName.end() - 1;
96 while(aSyms.find(*aSuffix) != std::string::npos) {
99 aName.erase(aSuffix + 1, aName.end());
103 bool Model_ResultConstruction::updateShape()
105 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(data());
106 if (aData && aData->isValid()) {
107 TDF_Label aShapeLab = aData->shapeLab();
108 Handle(TNaming_NamedShape) aNS;
109 if (aShapeLab.FindAttribute(TNaming_NamedShape::GetID(), aNS)) {
110 TopoDS_Shape aShape = aNS->Get();
111 if (!aShape.IsNull()) {
112 if (aShape.ShapeType() == TopAbs_COMPOUND) {
113 // restore the sketch planar edges object
114 std::shared_ptr<GeomAPI_PlanarEdges> aBigWire(new GeomAPI_PlanarEdges);
115 aBigWire->setImpl<TopoDS_Shape>(new TopoDS_Shape(aShape));
117 document()->feature(std::dynamic_pointer_cast<ModelAPI_Result>(data()->owner()));
118 std::shared_ptr<GeomDataAPI_Point> anOrigin =
119 std::dynamic_pointer_cast<GeomDataAPI_Point>(aSketch->data()->attribute("Origin"));
120 std::shared_ptr<GeomDataAPI_Dir> aDirX =
121 std::dynamic_pointer_cast<GeomDataAPI_Dir>(aSketch->data()->attribute("DirX"));
122 std::shared_ptr<GeomDataAPI_Dir> aNorm =
123 std::dynamic_pointer_cast<GeomDataAPI_Dir>(aSketch->data()->attribute("Norm"));
124 if (anOrigin.get() && aDirX.get() && aNorm.get()) {
125 aBigWire->setPlane(anOrigin->pnt(), aDirX->dir(), aNorm->dir());
130 // just restore shape
131 GeomShapePtr aGShape(new GeomAPI_Shape);
132 aGShape->setImpl<TopoDS_Shape>(new TopoDS_Shape(aShape));
133 myShape = aGShape; // restore the sketch sub-components
141 Model_ResultConstruction::Model_ResultConstruction()
145 bool Model_ResultConstruction::isInHistory()
147 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(data());
148 if (aData.get() && aData->isValid()) {
149 return !aData->label().IsAttribute(kIS_IN_HISTORY); // by default no attribute, but in history
151 return true; // unknown case
154 void Model_ResultConstruction::setIsInHistory(const bool isInHistory)
156 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(data());
157 if (aData.get() && aData->isValid()) {
159 TDataStd_UAttribute::Set(aData->label(), kIS_IN_HISTORY);
161 aData->label().ForgetAttribute(kIS_IN_HISTORY);
165 bool Model_ResultConstruction::isInfinite()
167 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(data());
168 if (aData.get() && aData->isValid()) {
169 return aData->label().IsAttribute(kIS_INFINITE);
171 return false; // unknown case
174 void Model_ResultConstruction::setInfinite(const bool theInfinite)
176 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(data());
177 if (aData.get() && aData->isValid()) {
179 TDataStd_UAttribute::Set(aData->label(), kIS_INFINITE);
181 aData->label().ForgetAttribute(kIS_INFINITE);
185 int Model_ResultConstruction::facesNum(const bool theUpdateNaming)
188 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(data());
189 if (aData.get() && aData->isValid()) {
190 TDF_Label aShapeLab = aData->shapeLab();
191 TDF_ChildIDIterator anOldIter(aShapeLab, TDataStd_IntPackedMap::GetID());
192 for (; anOldIter.More(); anOldIter.Next()) {
199 std::shared_ptr<GeomAPI_Face> Model_ResultConstruction::face(const int theIndex)
201 std::shared_ptr<GeomAPI_Face> aResult;
203 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(data());
204 if (aData.get() && aData->isValid()) {
205 TDF_Label aShapeLab = aData->shapeLab();
206 TDF_ChildIDIterator anOldIter(aShapeLab, TDataStd_IntPackedMap::GetID());
207 for (; anOldIter.More(); anOldIter.Next()) {
208 if (anIndex == theIndex) {
209 Handle(TNaming_NamedShape) aNS;
210 anOldIter.Value()->Label().FindAttribute(TNaming_NamedShape::GetID(), aNS);
211 aResult.reset(new GeomAPI_Face);
212 aResult->setImpl(new TopoDS_Shape(aNS->Get()));
221 void Model_ResultConstruction::setIsConcealed(const bool theValue)
223 // do nothing: the construction element is never concealed
226 void Model_ResultConstruction::storeShape(std::shared_ptr<GeomAPI_Shape> theShape)
228 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(data());
229 if (aData && aData->isValid()) {
230 std::string aMyName = data()->name();
231 TDF_Label aShapeLab = aData->shapeLab();
232 if (!theShape.get() || theShape->isNull()) {
233 aShapeLab.ForgetAllAttributes();
234 TDataStd_Name::Set(aShapeLab, aMyName.c_str()); // restore name forgotten
237 std::shared_ptr<Model_Document> aMyDoc =
238 std::dynamic_pointer_cast<Model_Document>(document());
239 const TopoDS_Shape& aShape = theShape->impl<TopoDS_Shape>();
240 if (aShape.ShapeType() == TopAbs_VERTEX) {
241 aShapeLab.ForgetAllAttributes(); // clear all previously stored
242 TNaming_Builder aBuilder(aShapeLab);
243 aBuilder.Generated(aShape);
244 TDataStd_Name::Set(aShapeLab, aMyName.c_str());
245 aMyDoc->addNamingName(aShapeLab, aMyName);
246 } else if (aShape.ShapeType() == TopAbs_EDGE) { // store sub-vertices on sub-labels
247 aShapeLab.ForgetAllAttributes(); // clear all previously stored
248 TNaming_Builder aBuilder(aShapeLab);
249 aBuilder.Generated(aShape);
251 TopExp_Explorer anExp(aShape, TopAbs_VERTEX);
252 for(int anIndex = 1; anExp.More(); anExp.Next(), anIndex++) {
253 TDF_Label aSubLab = aShapeLab.FindChild(anIndex);
254 TNaming_Builder aBuilder(aSubLab);
255 aBuilder.Generated(anExp.Current());
256 std::string aVertexName = aMyName + "_" + (anIndex == 1 ? "StartVertex" : "EndVertex");
257 // check this name is already used
260 static std::string anEmpty;
262 aThisRes = aMyDoc->findByName(aVertexName, anEmpty, aUnique);
263 if (aThisRes.get() && aThisRes.get() != this)
265 } while(aThisRes.get() && aThisRes.get() != this);
266 TDataStd_Name::Set(aSubLab, aVertexName.c_str());
267 aMyDoc->addNamingName(aSubLab, aVertexName);
269 TDataStd_Name::Set(aShapeLab, aMyName.c_str());
270 aMyDoc->addNamingName(aShapeLab, aMyName);
271 } else { // this is probably sketch, so, work with it as with composite
272 std::shared_ptr<GeomAPI_PlanarEdges> aWirePtr =
273 std::dynamic_pointer_cast<GeomAPI_PlanarEdges>(theShape);
275 return; // unknown case
276 ResultPtr aThisPtr = std::dynamic_pointer_cast<ModelAPI_Result>(data()->owner());
277 FeaturePtr aThisFeature = aMyDoc->feature(aThisPtr);
278 CompositeFeaturePtr aComposite =
279 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aThisFeature);
280 if (!aComposite || aComposite->numberOfSubs() == 0)
281 return; // unknown case
282 // collect indices of curves of current composite
283 NCollection_DataMap<Handle(Geom_Curve), int> aCurvesIndices;
284 NCollection_DataMap<int, TopoDS_Edge> anEdgeIndices;
285 std::map<int, std::string> aComponentsNames; // names of components that lay on index
286 const int aSubNum = aComposite->numberOfSubs();
287 for (int a = 0; a < aSubNum; a++) {
288 FeaturePtr aSub = aComposite->subFeature(a);
289 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aSub->results();
290 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRes = aResults.cbegin();
291 for (; aRes != aResults.cend(); aRes++) {
292 ResultConstructionPtr aConstr =
293 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*aRes);
294 if (aConstr->shape() && aConstr->shape()->isEdge()) {
295 TopoDS_Edge anEdge = TopoDS::Edge(aConstr->shape()->impl<TopoDS_Shape>());
296 Standard_Real aFirst, aLast;
297 Handle(Geom_Curve) aCurve = BRep_Tool::Curve(anEdge, aFirst, aLast);
298 aCurvesIndices.Bind(aCurve, a);
299 anEdgeIndices.Bind(a, anEdge);
300 aComponentsNames[a] = shortName(aConstr);
305 std::list<std::shared_ptr<GeomAPI_Shape> > aFaces;
306 GeomAlgoAPI_SketchBuilder::createFaces(aWirePtr->origin(), aWirePtr->dirX(),
307 aWirePtr->norm(), aWirePtr, aFaces);
308 // order is important to store faces in the same order if sketch is created from scratch
309 NCollection_IndexedDataMap<TopoDS_Face, TColStd_ListOfInteger> aNewIndices; // edges indices
310 std::list<std::shared_ptr<GeomAPI_Shape> >::iterator aFIter = aFaces.begin();
311 for (; aFIter != aFaces.end(); aFIter++) {
312 std::shared_ptr<GeomAPI_Face> aFace(new GeomAPI_Face(*aFIter));
313 // put them to a label, trying to keep the same faces on the same labels
314 if (aFace.get() && !aFace->isNull()) {
315 TopoDS_Face aTopoFace = TopoDS::Face(aFace->impl<TopoDS_Shape>());
316 aNewIndices.Add(aTopoFace, TColStd_ListOfInteger());
317 // keep new indices of sub-elements used in this face
318 for (TopExp_Explorer anEdges(aTopoFace, TopAbs_EDGE); anEdges.More(); anEdges.Next()) {
319 TopoDS_Edge anEdge = TopoDS::Edge(anEdges.Current());
320 Standard_Real aFirst, aLast;
321 Handle(Geom_Curve) aCurve = BRep_Tool::Curve(anEdge, aFirst, aLast);
322 if (aCurvesIndices.IsBound(aCurve)) {
323 int anIndex = aCurvesIndices.Find(aCurve);
324 if ((aFirst > aLast) != (anEdge.Orientation() == TopAbs_REVERSED))
326 aNewIndices.ChangeFromKey(aTopoFace).Append(anIndex);
331 NCollection_DataMap<int, TopoDS_Face> aFacesOrder; // faces -> tag where they must be set
332 NCollection_List<TopoDS_Face> anUnorderedFaces; // faces that may be located at any index
333 // searching for the best new candidate to old location
334 NCollection_IndexedDataMap<TopoDS_Face, TColStd_ListOfInteger>::Iterator
335 aNewIter(aNewIndices);
336 for (; aNewIter.More(); aNewIter.Next()) {
337 double aBestFound = 0, aBestNotFound = 1.e+100;
339 const TColStd_ListOfInteger& aNewInd = aNewIter.Value();
340 // old faces indices where they where located
341 TDF_ChildIDIterator anOldIter(aShapeLab, TDataStd_IntPackedMap::GetID());
342 for (; anOldIter.More(); anOldIter.Next()) {
343 int aTag = anOldIter.Value()->Label().Tag();
344 if (aFacesOrder.IsBound(aTag))
345 continue; // already found a best candidate
346 Handle(TDataStd_IntPackedMap) anOldIndices =
347 Handle(TDataStd_IntPackedMap)::DownCast(anOldIter.Value());
348 double aFound = 0, aNotFound = 0;
349 TColStd_ListOfInteger::Iterator aNewIndIter(aNewInd);
350 for (; aNewIndIter.More(); aNewIndIter.Next()) {
351 if (anOldIndices->Contains(aNewIndIter.Value())) {
354 else if (anOldIndices->Contains(-aNewIndIter.Value())) { // different orientation
361 if (aNotFound < aBestNotFound) {
362 if (aFound > aBestFound) {
363 aBestNotFound = aNotFound;
369 if (aBestTag != 0) { // found an appropriate face
370 aFacesOrder.Bind(aBestTag, aNewIter.Key());
372 anUnorderedFaces.Append(aNewIter.Key());
375 aShapeLab.ForgetAllAttributes(); // clear all previously stored
376 TDataStd_Name::Set(aShapeLab, aMyName.c_str()); // restore name forgotten
377 TNaming_Builder aBuilder(aShapeLab); // store the compound to get it ready on open of document
378 aBuilder.Generated(aShape);
379 aMyDoc->addNamingName(aShapeLab, aMyName);
380 // set new faces to the labels
382 NCollection_List<TopoDS_Face>::Iterator anUnordered(anUnorderedFaces);
383 for(int aCurrentTag = 1; !aFacesOrder.IsEmpty() || anUnordered.More(); aCurrentTag++) {
384 TopoDS_Face aFaceToPut;
385 if (aFacesOrder.IsBound(aCurrentTag)) {
386 aFaceToPut = aFacesOrder.Find(aCurrentTag);
387 aFacesOrder.UnBind(aCurrentTag);
388 } else if (anUnordered.More()){
389 aFaceToPut = anUnordered.Value();
393 if (!aFaceToPut.IsNull()) {
394 TopTools_MapOfShape aFaceEdges;
395 for(TopExp_Explorer anEdges(aFaceToPut, TopAbs_EDGE); anEdges.More(); anEdges.Next()) {
396 aFaceEdges.Add(anEdges.Current());
399 TDF_Label aLab = aShapeLab.FindChild(aCurrentTag);
400 TNaming_Builder aFaceBuilder(aLab);
401 aFaceBuilder.Generated(aFaceToPut);
402 // store also indices of the new face edges
403 Handle(TDataStd_IntPackedMap) aNewMap = TDataStd_IntPackedMap::Set(aLab);
404 const TColStd_ListOfInteger& aNewInd = aNewIndices.FindFromKey(aFaceToPut);
405 std::stringstream aName;
407 TopExp_Explorer aPutEdges(aFaceToPut, TopAbs_EDGE);
408 TNaming_Builder* anEdgesBuilder = 0;
409 for(TColStd_ListOfInteger::Iterator anIter(aNewInd); anIter.More(); anIter.Next()) {
410 int anIndex = anIter.Value();
411 int aModIndex = anIndex > 0 ? anIndex : -anIndex;
412 aNewMap->Add(anIndex);
413 aName<<"-"<<aComponentsNames[aModIndex];
414 if (anIter.Value() > 0)
418 // collect all edges of the face which are modified in sub-label of the face
419 if (anEdgeIndices.IsBound(aModIndex) &&
420 !aFaceEdges.Contains(anEdgeIndices.Find(aModIndex))) {
421 if (!anEdgesBuilder) {
422 TDF_Label anEdgesLabel = aLab.FindChild(1);
423 anEdgesBuilder = new TNaming_Builder(anEdgesLabel);
424 std::ostringstream aSubName;
425 // tag is needed for Test1922 to distinguish sub-edges of different faces
426 aSubName<<"SubEdge_"<<aCurrentTag;
427 TDataStd_Name::Set(anEdgesLabel, aSubName.str().c_str());
429 anEdgesBuilder->Modify(anEdgeIndices.Find(aModIndex), aPutEdges.Current());
433 TDataStd_Name::Set(aLab, TCollection_ExtendedString(aName.str().c_str()));
434 aMyDoc->addNamingName(aLab, aName.str());
435 // put also wires to sub-labels to correctly select them instead of collection by edges
436 int aWireTag = 2; // first tag is for SubEdge-s
437 for(TopExp_Explorer aWires(aFaceToPut, TopAbs_WIRE); aWires.More(); aWires.Next()) {
438 TDF_Label aWireLab = aLab.FindChild(aWireTag);
439 TNaming_Builder aWireBuilder(aWireLab);
440 aWireBuilder.Generated(aWires.Current());
441 std::ostringstream aWireName;
442 aWireName<<aName.str()<<"_wire";
444 aWireName<<"_"<<aWireTag - 1;
445 TDataStd_Name::Set(aWireLab, aWireName.str().c_str());
446 aMyDoc->addNamingName(aWireLab, aWireName.str());