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>
32 #include <TDF_ChildIDIterator.hxx>
33 #include <TNaming_NamedShape.hxx>
34 #include <TNaming_Builder.hxx>
35 #include <TDataStd_IntPackedMap.hxx>
36 #include <TDataStd_Name.hxx>
37 #include <TDataStd_UAttribute.hxx>
38 #include <BRep_Builder.hxx>
40 #include <TopoDS_Edge.hxx>
41 #include <TopoDS_Vertex.hxx>
42 #include <TopoDS_ListOfShape.hxx>
43 #include <TopExp_Explorer.hxx>
44 #include <TopTools_MapOfShape.hxx>
45 #include <NCollection_IndexedDataMap.hxx>
50 // identifier of the infinite result
51 Standard_GUID kIS_INFINITE("dea8cc5a-53f2-49c1-94e8-a947bed20a9f");
52 // identifier of the result not in history
53 Standard_GUID kIS_IN_HISTORY("a9aec01c-805e-44d1-b5d2-a63f06522f8a");
56 void Model_ResultConstruction::colorConfigInfo(std::string& theSection, std::string& theName,
57 std::string& theDefault)
59 theSection = "Visualization";
60 theName = "result_construction_color";
61 theDefault = DEFAULT_COLOR();
64 void Model_ResultConstruction::setShape(std::shared_ptr<GeomAPI_Shape> theShape)
66 if (myShape != theShape) {
69 if (!theShape.get() || !theShape->isEqual(myShape)) {
70 static const Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
71 ModelAPI_EventCreator::get()->sendUpdated(data()->owner(), anEvent);
77 std::shared_ptr<GeomAPI_Shape> Model_ResultConstruction::shape()
82 static std::string shortName(
83 std::shared_ptr<ModelAPI_ResultConstruction>& theConstr)
85 std::string aName = theConstr->data()->name();
86 // remove "-", "/" and "&" command-symbols
87 aName.erase(std::remove(aName.begin(), aName.end(), '-'), aName.end());
88 aName.erase(std::remove(aName.begin(), aName.end(), '/'), aName.end());
89 aName.erase(std::remove(aName.begin(), aName.end(), '&'), aName.end());
90 // remove the last 's', 'e', 'f' and 'r' symbols:
91 // they are used as markers of start/end/forward/rewersed indicators
92 static const std::string aSyms("sefr");
93 std::string::iterator aSuffix = aName.end() - 1;
94 while(aSyms.find(*aSuffix) != std::string::npos) {
97 aName.erase(aSuffix + 1, aName.end());
102 bool Model_ResultConstruction::updateShape()
104 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(data());
105 if (aData && aData->isValid()) {
106 TDF_Label aShapeLab = aData->shapeLab();
107 Handle(TNaming_NamedShape) aNS;
108 if (aShapeLab.FindAttribute(TNaming_NamedShape::GetID(), aNS)) {
109 TopoDS_Shape aShape = aNS->Get();
110 if (!aShape.IsNull()) {
111 GeomShapePtr aGShape(new GeomAPI_Shape);
112 aGShape->setImpl<TopoDS_Shape>(new TopoDS_Shape(aShape));
113 myShape = aGShape; // restore the sketch sub-components
121 Model_ResultConstruction::Model_ResultConstruction()
125 bool Model_ResultConstruction::isInHistory()
127 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(data());
128 if (aData.get() && aData->isValid()) {
129 return !aData->label().IsAttribute(kIS_IN_HISTORY); // by default no attribute, but in history
131 return true; // unknown case
134 void Model_ResultConstruction::setIsInHistory(const bool isInHistory)
136 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(data());
137 if (aData.get() && aData->isValid()) {
139 TDataStd_UAttribute::Set(aData->label(), kIS_IN_HISTORY);
141 aData->label().ForgetAttribute(kIS_IN_HISTORY);
145 bool Model_ResultConstruction::isInfinite()
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_INFINITE);
151 return false; // unknown case
154 void Model_ResultConstruction::setInfinite(const bool theInfinite)
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_INFINITE);
161 aData->label().ForgetAttribute(kIS_INFINITE);
165 int Model_ResultConstruction::facesNum(const bool theUpdateNaming)
168 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(data());
169 if (aData.get() && aData->isValid()) {
170 TDF_Label aShapeLab = aData->shapeLab();
171 TDF_ChildIDIterator anOldIter(aShapeLab, TDataStd_IntPackedMap::GetID());
172 for (; anOldIter.More(); anOldIter.Next()) {
179 std::shared_ptr<GeomAPI_Face> Model_ResultConstruction::face(const int theIndex)
181 std::shared_ptr<GeomAPI_Face> aResult;
183 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(data());
184 if (aData.get() && aData->isValid()) {
185 TDF_Label aShapeLab = aData->shapeLab();
186 TDF_ChildIDIterator anOldIter(aShapeLab, TDataStd_IntPackedMap::GetID());
187 for (; anOldIter.More(); anOldIter.Next()) {
188 if (anIndex == theIndex) {
189 Handle(TNaming_NamedShape) aNS;
190 anOldIter.Value()->Label().FindAttribute(TNaming_NamedShape::GetID(), aNS);
191 aResult.reset(new GeomAPI_Face);
192 aResult->setImpl(new TopoDS_Shape(aNS->Get()));
201 void Model_ResultConstruction::setIsConcealed(const bool theValue)
203 // do nothing: the construction element is never concealed
206 void Model_ResultConstruction::storeShape(std::shared_ptr<GeomAPI_Shape> theShape)
208 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(data());
209 if (aData && aData->isValid()) {
210 std::string aMyName = data()->name();
211 TDF_Label aShapeLab = aData->shapeLab();
212 if (!theShape.get() || theShape->isNull()) {
213 aShapeLab.ForgetAllAttributes();
214 TDataStd_Name::Set(aShapeLab, aMyName.c_str()); // restore name forgotten
217 std::shared_ptr<Model_Document> aMyDoc =
218 std::dynamic_pointer_cast<Model_Document>(document());
219 const TopoDS_Shape& aShape = theShape->impl<TopoDS_Shape>();
220 if (aShape.ShapeType() == TopAbs_VERTEX) {
221 aShapeLab.ForgetAllAttributes(); // clear all previously stored
222 TNaming_Builder aBuilder(aShapeLab);
223 aBuilder.Generated(aShape);
224 TDataStd_Name::Set(aShapeLab, aMyName.c_str());
225 aMyDoc->addNamingName(aShapeLab, aMyName);
226 } else if (aShape.ShapeType() == TopAbs_EDGE) { // store sub-vertices on sub-labels
227 aShapeLab.ForgetAllAttributes(); // clear all previously stored
228 TNaming_Builder aBuilder(aShapeLab);
229 aBuilder.Generated(aShape);
231 TopExp_Explorer anExp(aShape, TopAbs_VERTEX);
232 for(int anIndex = 1; anExp.More(); anExp.Next(), anIndex++) {
233 TDF_Label aSubLab = aShapeLab.FindChild(anIndex);
234 TNaming_Builder aBuilder(aSubLab);
235 aBuilder.Generated(anExp.Current());
236 std::string aVertexName = aMyName + "_" + (anIndex == 1 ? "StartVertex" : "EndVertex");
237 TDataStd_Name::Set(aSubLab, aVertexName.c_str());
238 aMyDoc->addNamingName(aSubLab, aVertexName);
240 TDataStd_Name::Set(aShapeLab, aMyName.c_str());
241 aMyDoc->addNamingName(aShapeLab, aMyName);
242 } else { // this is probably sketch, so, work with it as with composite
243 std::shared_ptr<GeomAPI_PlanarEdges> aWirePtr =
244 std::dynamic_pointer_cast<GeomAPI_PlanarEdges>(theShape);
246 return; // unknown case
247 ResultPtr aThisPtr = std::dynamic_pointer_cast<ModelAPI_Result>(data()->owner());
248 FeaturePtr aThisFeature = aMyDoc->feature(aThisPtr);
249 CompositeFeaturePtr aComposite =
250 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aThisFeature);
251 if (!aComposite || aComposite->numberOfSubs() == 0)
252 return; // unknown case
253 // collect indices of curves of current composite
254 NCollection_DataMap<Handle(Geom_Curve), int> aCurvesIndices;
255 NCollection_DataMap<int, TopoDS_Edge> anEdgeIndices;
256 std::map<int, std::string> aComponentsNames; // names of components that lay on index
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 for (; aRes != aResults.cend(); aRes++) {
263 ResultConstructionPtr aConstr =
264 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*aRes);
265 if (aConstr->shape() && aConstr->shape()->isEdge()) {
266 TopoDS_Edge anEdge = TopoDS::Edge(aConstr->shape()->impl<TopoDS_Shape>());
267 Standard_Real aFirst, aLast;
268 Handle(Geom_Curve) aCurve = BRep_Tool::Curve(anEdge, aFirst, aLast);
269 aCurvesIndices.Bind(aCurve, a);
270 anEdgeIndices.Bind(a, anEdge);
271 aComponentsNames[a] = shortName(aConstr);
276 std::list<std::shared_ptr<GeomAPI_Shape> > aFaces;
277 GeomAlgoAPI_SketchBuilder::createFaces(aWirePtr->origin(), aWirePtr->dirX(),
278 aWirePtr->norm(), aWirePtr, aFaces);
279 // order is important to store faces in the same order if sketch is created from scratch
280 NCollection_IndexedDataMap<TopoDS_Face, TColStd_ListOfInteger> aNewIndices; // edges indices
281 std::list<std::shared_ptr<GeomAPI_Shape> >::iterator aFIter = aFaces.begin();
282 for (; aFIter != aFaces.end(); aFIter++) {
283 std::shared_ptr<GeomAPI_Face> aFace(new GeomAPI_Face(*aFIter));
284 // put them to a label, trying to keep the same faces on the same labels
285 if (aFace.get() && !aFace->isNull()) {
286 TopoDS_Face aTopoFace = TopoDS::Face(aFace->impl<TopoDS_Shape>());
287 aNewIndices.Add(aTopoFace, TColStd_ListOfInteger());
288 // keep new indices of sub-elements used in this face
289 for (TopExp_Explorer anEdges(aTopoFace, TopAbs_EDGE); anEdges.More(); anEdges.Next()) {
290 TopoDS_Edge anEdge = TopoDS::Edge(anEdges.Current());
291 Standard_Real aFirst, aLast;
292 Handle(Geom_Curve) aCurve = BRep_Tool::Curve(anEdge, aFirst, aLast);
293 if (aCurvesIndices.IsBound(aCurve)) {
294 int anIndex = aCurvesIndices.Find(aCurve);
295 if ((aFirst > aLast) != (anEdge.Orientation() == TopAbs_REVERSED))
297 aNewIndices.ChangeFromKey(aTopoFace).Append(anIndex);
302 NCollection_DataMap<int, TopoDS_Face> aFacesOrder; // faces -> tag where they must be set
303 NCollection_List<TopoDS_Face> anUnorderedFaces; // faces that may be located at any index
304 // searching for the best new candidate to old location
305 NCollection_IndexedDataMap<TopoDS_Face, TColStd_ListOfInteger>::Iterator
306 aNewIter(aNewIndices);
307 for (; aNewIter.More(); aNewIter.Next()) {
308 double aBestFound = 0, aBestNotFound = 1.e+100;
310 const TColStd_ListOfInteger& aNewInd = aNewIter.Value();
311 // old faces indices where they where located
312 TDF_ChildIDIterator anOldIter(aShapeLab, TDataStd_IntPackedMap::GetID());
313 for (; anOldIter.More(); anOldIter.Next()) {
314 int aTag = anOldIter.Value()->Label().Tag();
315 if (aFacesOrder.IsBound(aTag))
316 continue; // already found a best candidate
317 Handle(TDataStd_IntPackedMap) anOldIndices =
318 Handle(TDataStd_IntPackedMap)::DownCast(anOldIter.Value());
319 double aFound = 0, aNotFound = 0;
320 TColStd_ListOfInteger::Iterator aNewIndIter(aNewInd);
321 for (; aNewIndIter.More(); aNewIndIter.Next()) {
322 if (anOldIndices->Contains(aNewIndIter.Value())) {
325 else if (anOldIndices->Contains(-aNewIndIter.Value())) { // different orientation
332 if (aNotFound < aBestNotFound) {
333 if (aFound > aBestFound) {
334 aBestNotFound = aNotFound;
340 if (aBestTag != 0) { // found an appropriate face
341 aFacesOrder.Bind(aBestTag, aNewIter.Key());
343 anUnorderedFaces.Append(aNewIter.Key());
346 aShapeLab.ForgetAllAttributes(); // clear all previously stored
347 TDataStd_Name::Set(aShapeLab, aMyName.c_str()); // restore name forgotten
348 TNaming_Builder aBuilder(aShapeLab); // store the compound to get it ready on open of document
349 aBuilder.Generated(aShape);
350 aMyDoc->addNamingName(aShapeLab, aMyName);
351 // set new faces to the labels
353 NCollection_List<TopoDS_Face>::Iterator anUnordered(anUnorderedFaces);
354 for(int aCurrentTag = 1; !aFacesOrder.IsEmpty() || anUnordered.More(); aCurrentTag++) {
355 TopoDS_Face aFaceToPut;
356 if (aFacesOrder.IsBound(aCurrentTag)) {
357 aFaceToPut = aFacesOrder.Find(aCurrentTag);
358 aFacesOrder.UnBind(aCurrentTag);
359 } else if (anUnordered.More()){
360 aFaceToPut = anUnordered.Value();
364 if (!aFaceToPut.IsNull()) {
365 TopTools_MapOfShape aFaceEdges;
366 for(TopExp_Explorer anEdges(aFaceToPut, TopAbs_EDGE); anEdges.More(); anEdges.Next()) {
367 aFaceEdges.Add(anEdges.Current());
370 TDF_Label aLab = aShapeLab.FindChild(aCurrentTag);
371 TNaming_Builder aFaceBuilder(aLab);
372 aFaceBuilder.Generated(aFaceToPut);
373 // store also indices of the new face edges
374 Handle(TDataStd_IntPackedMap) aNewMap = TDataStd_IntPackedMap::Set(aLab);
375 const TColStd_ListOfInteger& aNewInd = aNewIndices.FindFromKey(aFaceToPut);
376 std::stringstream aName;
378 TopExp_Explorer aPutEdges(aFaceToPut, TopAbs_EDGE);
379 TNaming_Builder* anEdgesBuilder = 0;
380 for(TColStd_ListOfInteger::Iterator anIter(aNewInd); anIter.More(); anIter.Next()) {
381 int anIndex = anIter.Value();
382 int aModIndex = anIndex > 0 ? anIndex : -anIndex;
383 aNewMap->Add(anIndex);
384 aName<<"-"<<aComponentsNames[aModIndex];
385 if (anIter.Value() > 0)
389 // collect all edges of the face which are modified in sub-label of the face
390 if (anEdgeIndices.IsBound(aModIndex) &&
391 !aFaceEdges.Contains(anEdgeIndices.Find(aModIndex))) {
392 if (!anEdgesBuilder) {
393 TDF_Label anEdgesLabel = aLab.FindChild(1);
394 anEdgesBuilder = new TNaming_Builder(anEdgesLabel);
395 TDataStd_Name::Set(anEdgesLabel, "SubEdge");
397 anEdgesBuilder->Modify(anEdgeIndices.Find(aModIndex), aPutEdges.Current());
401 TDataStd_Name::Set(aLab, TCollection_ExtendedString(aName.str().c_str()));
402 aMyDoc->addNamingName(aLab, aName.str());