0031668: Visualization - WebGL sample doesn't work on Emscripten 1.39
[occt.git] / src / TDF / TDF_CopyTool.cxx
CommitLineData
b311480e 1// Created by: DAUTRY Philippe
2// Copyright (c) 1997-1999 Matra Datavision
973c2be1 3// Copyright (c) 1999-2014 OPEN CASCADE SAS
b311480e 4//
973c2be1 5// This file is part of Open CASCADE Technology software library.
b311480e 6//
d5f74e42 7// This library is free software; you can redistribute it and/or modify it under
8// the terms of the GNU Lesser General Public License version 2.1 as published
973c2be1 9// by the Free Software Foundation, with special exception defined in the file
10// OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11// distribution for complete text of the license and disclaimer of any warranty.
b311480e 12//
973c2be1 13// Alternatively, this file may be used under the terms of Open CASCADE
14// commercial license or contractual agreement.
b311480e 15
7fd59977 16// ---------------------
7fd59977 17// Version: 0.0
b311480e 18//Version Date Purpose
7fd59977 19// 0.0 Mar 11 1997 Creation
20
42cf5bc1 21#include <Standard_TypeMismatch.hxx>
7fd59977 22#include <TDF_Attribute.hxx>
23#include <TDF_AttributeDataMap.hxx>
24#include <TDF_AttributeIterator.hxx>
25#include <TDF_AttributeMap.hxx>
26#include <TDF_ChildIterator.hxx>
42cf5bc1 27#include <TDF_ClosureTool.hxx>
28#include <TDF_CopyTool.hxx>
7fd59977 29#include <TDF_DataMapIteratorOfAttributeDataMap.hxx>
30#include <TDF_DataMapIteratorOfLabelDataMap.hxx>
42cf5bc1 31#include <TDF_DataSet.hxx>
32#include <TDF_IDFilter.hxx>
7fd59977 33#include <TDF_Label.hxx>
34#include <TDF_LabelDataMap.hxx>
35#include <TDF_LabelMap.hxx>
36#include <TDF_ListIteratorOfLabelList.hxx>
37#include <TDF_MapIteratorOfAttributeMap.hxx>
38#include <TDF_MapIteratorOfLabelMap.hxx>
42cf5bc1 39#include <TDF_RelocationTable.hxx>
7fd59977 40
7fd59977 41//=======================================================================
42//function : Copy
43//purpose :
44//=======================================================================
45
46void TDF_CopyTool::Copy
47(const Handle(TDF_DataSet)& aSourceDataSet,
48 const Handle(TDF_RelocationTable)& aRelocationTable)
49{
50 TDF_IDFilter privilegeFilter; // Ignore the target attribute's privilege!
51 TDF_IDFilter refFilter; // Will not be used.
52 TDF_CopyTool::Copy
53 (aSourceDataSet, aRelocationTable, privilegeFilter,
54 refFilter, Standard_False);
55}
56
57
58//=======================================================================
59//function : Copy
60//purpose :
61//=======================================================================
62
63void TDF_CopyTool::Copy
64(const Handle(TDF_DataSet)& aSourceDataSet,
65 const Handle(TDF_RelocationTable)& aRelocationTable,
66 const TDF_IDFilter& aPrivilegeFilter)
67{
68 TDF_IDFilter refFilter; // Will not be used.
69 TDF_CopyTool::Copy
70 (aSourceDataSet, aRelocationTable, aPrivilegeFilter,
71 refFilter, Standard_False);
72
73}
74
75//=======================================================================
76//function : Copy
77//purpose :
78//=======================================================================
79
80void TDF_CopyTool::Copy
81(const Handle(TDF_DataSet)& aSourceDataSet,
82 const Handle(TDF_RelocationTable)& aRelocationTable,
83 const TDF_IDFilter& aPrivilegeFilter,
84 const TDF_IDFilter& /* aRefFilter */,
85 const Standard_Boolean /* setSelfContained */)
86{
87 if (aSourceDataSet->IsEmpty()) return;
88
89 TDF_LabelMap& srcLabs = aSourceDataSet->Labels();
90 TDF_AttributeMap& srcAtts = aSourceDataSet->Attributes();
91 TDF_LabelList& rootLst = aSourceDataSet->Roots();
92
93 TDF_LabelDataMap& theLabMap = aRelocationTable->LabelTable();
94 TDF_AttributeDataMap& theAttMap = aRelocationTable->AttributeTable();
95
96 // Parallel exploration of the root label structures:
97 // * builds the target labels not found;
98 // * binds the source attributes with the target ones;
99 // * binds the source attributes with new target ones if there is none.
100
101 // Label pre-binding is tested before paste.
102 // For it is possible to copy the roots at another place with OTHER TAGS,
103 // we first ask <theLabMap> if each source root label is already bound.
104
105 for (TDF_ListIteratorOfLabelList labLItr(rootLst);
106 labLItr.More(); labLItr.Next()) {
107 const TDF_Label& sLab = labLItr.Value();
108 if (theLabMap.IsBound(sLab)) {
109 TDF_Label tIns(theLabMap.Find(sLab));
110 TDF_CopyTool::CopyLabels(sLab,tIns,
5a1271c8 111 theLabMap,theAttMap,srcLabs,srcAtts);
7fd59977 112 }
113 // if not bound : do nothing!
114 }
115
116 // The relocation attribute table is now ready,
117 // except for the label unattached attributes,
118 // but everybody can update the relocation table...
119
120 // Now: the paste phasis!
121 TDF_DataMapIteratorOfAttributeDataMap attItr2(theAttMap);
122 for (;attItr2.More(); attItr2.Next()) {
123 const Handle(TDF_Attribute)& sAtt = attItr2.Key();
124 if (!sAtt.IsNull()) { // This condition looks superfluous; and below also.
125 const Handle(TDF_Attribute)& tAtt = attItr2.Value();
126 // 1 - No copy on itself.
127 // 2 - The target attribute is present BUT its privilege over the
128 // source one must be ignored. The source attribute can be copied.
129 if ((sAtt != tAtt) && aPrivilegeFilter.IsIgnored(tAtt->ID()))
5a1271c8 130 sAtt->Paste(tAtt,aRelocationTable);
7fd59977 131 }
132 }
133}
134
135
136
137//=======================================================================
138//function : CopyLabels
139//purpose : Internal root label copy recursive method.
140//=======================================================================
141
142void TDF_CopyTool::CopyLabels
143(const TDF_Label& aSLabel,
144 TDF_Label& aTargetLabel,
145 TDF_LabelDataMap& aLabMap,
146 TDF_AttributeDataMap& aAttMap,
147 const TDF_LabelMap& aSrcLabelMap,
148 const TDF_AttributeMap& aSrcAttributeMap)
149{
150 TDF_CopyTool::CopyAttributes(aSLabel,aTargetLabel,
5a1271c8 151 aAttMap,aSrcAttributeMap);
7fd59977 152
153 // Does the same for the children.
154 for (TDF_ChildIterator childItr(aSLabel); childItr.More(); childItr.Next()){
155 const TDF_Label& childSLab = childItr.Value();
6e779194 156 if (aSrcLabelMap.Contains(childSLab))
157 {
158 TDF_Label childTIns;
159 if (aLabMap.IsBound (childSLab))
160 {
161 childTIns = aLabMap.Find (childSLab);
162 }
163 else
164 {
165 childTIns = aTargetLabel.FindChild (childSLab.Tag ());
166 aLabMap.Bind (childSLab, childTIns);
167 }
168
169 TDF_CopyTool::CopyLabels
170 (
171 childSLab,childTIns,
172 aLabMap,aAttMap,
173 aSrcLabelMap,aSrcAttributeMap
174 );
7fd59977 175 }
176 }
177}
178
7fd59977 179//=======================================================================
180//function : CopyAttributes
181//purpose : Internal attribute copy method.
182//=======================================================================
183
184void TDF_CopyTool::CopyAttributes
185(const TDF_Label& aSLabel,
186 TDF_Label& aTargetLabel,
187 TDF_AttributeDataMap& aAttMap,
188 const TDF_AttributeMap& aSrcAttributeMap)
189{
190 Handle(TDF_Attribute) tAtt;
191
192 // Finds the target attributes or creates them empty.
193 for (TDF_AttributeIterator attItr(aSLabel);
194 attItr.More(); attItr.Next()) {
195 const Handle(TDF_Attribute) sAtt = attItr.Value();
196 if (aSrcAttributeMap.Contains(sAtt)) {
197 const Standard_GUID& id = sAtt->ID();
198 if (!aTargetLabel.FindAttribute(id,tAtt)) {
5a1271c8 199 tAtt = sAtt->NewEmpty();
200 if(tAtt->ID() != id)
201 tAtt->SetID(id);//
0f3d1d7c 202 aTargetLabel.AddAttribute(tAtt, Standard_True);
5a1271c8 203 aAttMap.Bind(sAtt,tAtt);
7fd59977 204 }
205 else {
5a1271c8 206 // Some attributes have the same ID, but are different and
207 // exclusive. This obliged to test the dynamic type identity.
208 if (tAtt->IsInstance(sAtt->DynamicType()))
209 aAttMap.Bind(sAtt,tAtt);
210 else
211 throw Standard_TypeMismatch("TDF_CopyTool: Cannot paste to a different type attribute.");
7fd59977 212 }
213 }
214 }
215}