Salome HOME
Task 5.1.7: To be able to export a part to a file and import it into an existing...
[modules/shaper.git] / src / Model / Model_Tools.cpp
1 // Copyright (C) 2014-2019  CEA/DEN, EDF R&D
2 //
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.
7 //
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.
12 //
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
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include <Model_Tools.h>
21
22 #include <Standard_GUID.hxx>
23
24 #include <TDF_AttributeIterator.hxx>
25 #include <TDF_ChildIterator.hxx>
26 #include <TDF_Reference.hxx>
27 #include <TDF_RelocationTable.hxx>
28
29 void Model_Tools::copyLabels(TDF_Label theSource, TDF_Label theDestination,
30                              Handle(TDF_RelocationTable) theRelocTable)
31 {
32   theRelocTable->SetRelocation(theSource, theDestination);
33   // copy the sub-labels hierarchy
34   TDF_ChildIterator aSubLabsIter(theSource);
35   for (; aSubLabsIter.More(); aSubLabsIter.Next()) {
36     copyLabels(aSubLabsIter.Value(),
37                theDestination.FindChild(aSubLabsIter.Value().Tag()),
38                theRelocTable);
39   }
40 }
41
42 void Model_Tools::copyAttrs(TDF_Label theSource, TDF_Label theDestination,
43                             Handle(TDF_RelocationTable) theRelocTable)
44 {
45   TDF_AttributeIterator anAttrIter(theSource);
46   for(; anAttrIter.More(); anAttrIter.Next()) {
47     Handle(TDF_Attribute) aTargetAttr;
48     if (!theDestination.FindAttribute(anAttrIter.Value()->ID(), aTargetAttr)) {
49       // create a new attribute if not yet exists in the destination
50             aTargetAttr = anAttrIter.Value()->NewEmpty();
51       theDestination.AddAttribute(aTargetAttr);
52     }
53     // no special relocation, empty map, but self-relocation is on: copy references w/o changes
54     Handle(TDF_RelocationTable) aRelocTable =
55         theRelocTable.IsNull() ? new TDF_RelocationTable(Standard_True) : theRelocTable;
56     anAttrIter.Value()->Paste(aTargetAttr, aRelocTable);
57     // an exception: if a source reference refers itself, a copy must also refer itself
58     if (aTargetAttr->ID() == TDF_Reference::GetID()) {
59       Handle(TDF_Reference) aTargetRef = Handle(TDF_Reference)::DownCast(aTargetAttr);
60       if (aTargetRef->Get().IsEqual(anAttrIter.Value()->Label()))
61         aTargetRef->Set(aTargetRef->Label());
62     }
63   }
64   // copy the sub-labels content
65   TDF_ChildIterator aSubLabsIter(theSource);
66   for(; aSubLabsIter.More(); aSubLabsIter.Next()) {
67     copyAttrs(aSubLabsIter.Value(),
68               theDestination.FindChild(aSubLabsIter.Value().Tag()),
69               theRelocTable);
70   }
71 }