0025307: Problems resulted from re-triangulation
[occt.git] / src / TNaming / TNaming_NamingTool.cxx
CommitLineData
b311480e 1// Created on: 2000-02-14
2// Created by: Denis PASCAL
973c2be1 3// Copyright (c) 2000-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.
7fd59977 15
16#include <TNaming_NamingTool.ixx>
17#include <TNaming_Tool.hxx>
18#include <TNaming_NewShapeIterator.hxx>
19#include <TNaming_Iterator.hxx>
20#include <TopTools_MapIteratorOfMapOfShape.hxx>
21#include <TNaming_OldShapeIterator.hxx>
22#include <TNaming_Tool.hxx>
7dcac1df 23#include <TNaming_Naming.hxx>
24#include <TDF_ChildIterator.hxx>
7fd59977 25#ifdef DEB
26//#define MDTV_DEB_DESC
27//#define MDTV_DEB_APPLY
28#ifdef MDTV_DEB_DESC
29#include <TCollection_AsciiString.hxx>
30#include <TDF_Tool.hxx>
31#include <BRepTools.hxx>
32static void WriteS(const TopoDS_Shape& shape,
33 const Standard_CString filename)
34{
35 char buf[256];
36 if(strlen(filename) > 255) return;
37#ifdef WNT
38 strcpy_s (buf, filename);
39#else
40 strcpy (buf, filename);
41#endif
42 char* p = buf;
43 while (*p) {
44 if(*p == ':')
45 *p = '-';
46 p++;
47 }
48 ofstream save (buf);
49 if(!save)
50 cout << "File " << buf << " was not created: rdstate = " << save.rdstate() << endl;
51 save << "DBRep_DrawableShape" << endl << endl;
52 if(!shape.IsNull()) BRepTools::Write(shape, save);
53 save.close();
54}
55#endif
56#endif
57//=======================================================================
58//function : IsForbiden
59//purpose : ANaming voir NamingTool
60//=======================================================================
61
62static Standard_Boolean IsForbiden(const TDF_LabelMap& Forbiden,
63 const TDF_Label& Lab)
64{
65 if (Lab.IsRoot()) {
66 return Standard_False;
67 }
68 if (Forbiden.Contains(Lab))
69 return Standard_True;
70 else {
71 return IsForbiden(Forbiden,Lab.Father());
72 }
73}
74
75//=======================================================================
76//function : LastModif
77//purpose : ANaming
78//=======================================================================
79static void LastModif( TNaming_NewShapeIterator& it,
80 const TopoDS_Shape& S,
81 TopTools_MapOfShape& MS,
82 const TDF_LabelMap& Updated,
83 const TDF_LabelMap& Forbiden)
84{
85 Standard_Boolean YaModif = Standard_False;
86 for (; it.More(); it.Next()) {
87 const TDF_Label& Lab = it.Label();
88#ifdef MDTV_DEB
89 TCollection_AsciiString entry;
90 TDF_Tool::Entry(Lab, entry);
91 cout << "NamingTool:: LastModif LabelEntry = "<< entry << endl;
92#endif
93 if (!Updated.IsEmpty() && !Updated.Contains(Lab)) continue;
94 if (IsForbiden(Forbiden, Lab)) continue;
95 if (it.IsModification()) {
96 YaModif = Standard_True;
97 TNaming_NewShapeIterator it2(it);
98 if (!it2.More()) {
99 const TopoDS_Shape& S = it.Shape();
100 MS.Add (S); // Modified
101 }
102 else
103 LastModif(it2,it.Shape(),MS,Updated,Forbiden);
104 }
105 }
106 if (!YaModif)
107 MS.Add(S);
108}
109//=======================================================================
110static void ApplyOrientation (TopTools_MapOfShape& MS,
111 const TopAbs_Orientation OrientationToApply)
112{
113 if (!MS.IsEmpty ()) {
114#ifdef MDTV_DEB_APPLY
115 cout <<"OrientationToApply = " <<OrientationToApply <<endl;
116 TopTools_MapIteratorOfMapOfShape it1(MS);
117 for (; it1.More(); it1.Next()) {
118 cout << "ApplyOrientation: TShape = " << it1.Key().TShape()->This() << " OR = " <<it1.Key().Orientation() <<endl;
119 }
120#endif
121 TopTools_MapOfShape aMS;
122 aMS.Assign(MS);
123 TopTools_MapIteratorOfMapOfShape it(aMS);
124 for (; it.More(); it.Next()) {
125 if(it.Key().Orientation() != OrientationToApply) {
126 TopoDS_Shape aS = it.Key();
127 MS.Remove(aS);
128 aS.Orientation(OrientationToApply);
129 MS.Add(aS);
130 }
131 }
132 }
133}
134//=======================================================================
135//function : CurrentShape
136//purpose : ANaming
137//=======================================================================
138void TNaming_NamingTool::CurrentShape(const TDF_LabelMap& Valid,
139 const TDF_LabelMap& Forbiden,
140 const Handle(TNaming_NamedShape)& Att,
141 TopTools_MapOfShape& MS)
142{
143 TDF_Label Lab = Att->Label();
144#ifdef MDTV_DEB
145 TCollection_AsciiString entry;
146 TDF_Tool::Entry(Lab, entry);
147 cout << "NamingTool:: LabelEntry = "<< entry << endl;
148#endif
149 if (!Valid.IsEmpty() && !Valid.Contains(Lab)) {
150#ifdef MDTV_DEB
151 TCollection_AsciiString entry;
152 TDF_Tool::Entry(Lab, entry);
153 cout << "NamingTool:: LabelEntry = "<< entry << " is out of Valid map" << endl;
154#endif
155 return;
156 }
157
158 TNaming_Iterator itL (Att);
159 for (; itL.More(); itL.Next()) {
160 const TopoDS_Shape& S = itL.NewShape();
161 if (S.IsNull()) continue;
162#ifdef MDTV_DEB
163 WriteS(S, "CS_NewShape.brep");
164 if(itL.OldShape().IsNull())
165 cout <<"OldShape is Null" <<endl;
166 else
167 WriteS(itL.OldShape(), "CS_OldShape.brep");
168#endif
169 Standard_Boolean YaOrientationToApply(Standard_False);
170 TopAbs_Orientation OrientationToApply(TopAbs_FORWARD);
171 if(Att->Evolution() == TNaming_SELECTED) {
7dcac1df 172 if (itL.More() && itL.NewShape().ShapeType() != TopAbs_VERTEX) {//OR-N
173 Handle (TNaming_Naming) aNaming;
174 Lab.FindAttribute(TNaming_Naming::GetID(), aNaming);
175 if(!aNaming.IsNull()) {
176 if(aNaming->GetName().Type() == TNaming_ORIENTATION) {
177 OrientationToApply = aNaming->GetName().Orientation();
178 } else {
179 Handle (TNaming_Naming) aNaming2;
180 TDF_ChildIterator it(aNaming->Label());
181 for(;it.More();it.Next()) {
182 const TDF_Label& aLabel = it.Value();
183 aLabel.FindAttribute(TNaming_Naming::GetID(), aNaming2);
184 if(!aNaming2.IsNull()) {
185 if(aNaming2->GetName().Type() == TNaming_ORIENTATION) {
186 OrientationToApply = aNaming2->GetName().Orientation();
187 break;
188 }
189 }
190 }
191 }
192 if(OrientationToApply == TopAbs_FORWARD || OrientationToApply == TopAbs_REVERSED)
193 YaOrientationToApply = Standard_True;
194 }
195 } //
7fd59977 196 }
197 TNaming_NewShapeIterator it(itL);
198 if (!it.More()) {
199 if (YaOrientationToApply)
200 MS.Add(S.Oriented(OrientationToApply));
201 else
202 MS.Add(S);
203 }
204 else {
205// LastModif(it, S, MS, Valid, Forbiden);
206 TopTools_MapOfShape MS2;
207 LastModif(it, S, MS2, Valid, Forbiden);
208 if (YaOrientationToApply) ApplyOrientation (MS2, OrientationToApply);//the solution to be refined
209 for (TopTools_MapIteratorOfMapOfShape itMS2(MS2); itMS2.More();itMS2.Next())
210 MS.Add(itMS2.Key());
211 }
212 }
213}
214
215//=======================================================================
216//function : CurrentShapeFromShape
217//purpose : ANaming
218//=======================================================================
219
220void TNaming_NamingTool::CurrentShapeFromShape(const TDF_LabelMap& Valid,
221 const TDF_LabelMap& Forbiden,
222 const TDF_Label& Acces,
223 const TopoDS_Shape& S,
224 TopTools_MapOfShape& MS)
225{
226 TNaming_NewShapeIterator it(S,Acces);
227
228 Handle(TNaming_NamedShape) NS = it.NamedShape();
229 if(!NS.IsNull() && NS->Evolution() == TNaming_SELECTED)
230 MS.Add(TNaming_Tool::GetShape(NS));
231 else {
232 if (!it.More())
233 MS.Add(S);
234 else
235 LastModif(it, S, MS, Valid, Forbiden);
236 }
237}
238
239//=======================================================================
240//function : MakeDescendants
241//purpose : ANaming
242//=======================================================================
243
244static void MakeDescendants (TNaming_NewShapeIterator& it,
245 TDF_LabelMap& Descendants)
246{
247 for (; it.More(); it.Next()) {
248 Descendants.Add(it.Label());
249#ifdef MDTV_DEB_DESC
250 TCollection_AsciiString entry;
251 TDF_Tool::Entry(it.Label(), entry);
252 cout<< "MakeDescendants: Label = " <<entry <<endl;
253#endif
254 if (!it.Shape().IsNull()) {
255 TNaming_NewShapeIterator it2(it);
256 MakeDescendants (it2,Descendants);
257 }
258 }
259}
260//=======================================================================
261void BuildDescendants2 (const Handle(TNaming_NamedShape)& NS, const TDF_Label& ForbLab, TDF_LabelMap& Descendants)
262{
263 if (NS.IsNull()) return;
264 TNaming_NewShapeIterator it(NS);
265 for(;it.More();it.Next()) {
266 if(!it.NamedShape().IsNull()) {
267#ifdef MDTV_DEB_DESC
268 TCollection_AsciiString entry;
269 TDF_Tool::Entry(it.Label(), entry);
270 cout<< "MakeDescendants2: Label = " <<entry <<endl;
271#endif
272 if(ForbLab == it.Label()) continue;
273 Descendants.Add(it.Label());
274 TNaming_NewShapeIterator it2(it);
275 MakeDescendants (it2,Descendants);
276 }
277 }
278}
279//=======================================================================
280//function : BuildDescendants
281//purpose : ANaming
282//=======================================================================
283
284void TNaming_NamingTool::BuildDescendants (const Handle(TNaming_NamedShape)& NS,
285 TDF_LabelMap& Descendants)
286{
287 if (NS.IsNull()) return;
288 Descendants.Add(NS->Label());
289 TNaming_NewShapeIterator it(NS);
290#ifdef MDTV_DEB_DESC
291 TCollection_AsciiString entry;
292 TDF_Tool::Entry(NS->Label(), entry);
293 cout<< "MakeDescendants: Label = " <<entry <<endl;
294#endif
295 MakeDescendants (it,Descendants);
296 TNaming_OldShapeIterator it2(NS);
297 for (; it2.More(); it2.Next()) {
298 if(!it2.Shape().IsNull()) {
299 Handle(TNaming_NamedShape) ONS = TNaming_Tool::NamedShape(it2.Shape(), NS->Label());
300 if(!ONS.IsNull()) {
301#ifdef MDTV_DEB_DESC
302 TCollection_AsciiString entry;
303 TDF_Tool::Entry(ONS->Label(), entry);
304 cout<< "MakeDescendants_Old: Label = " <<entry <<endl;
305#endif
306 BuildDescendants2(ONS, NS->Label(), Descendants);
307 }
308 }
309 }
310}