Test for 0022778: Bug in BRepMesh
[occt.git] / src / Interface / Interface_ShareTool.cxx
1 // Copyright (c) 1999-2012 OPEN CASCADE SAS
2 //
3 // The content of this file is subject to the Open CASCADE Technology Public
4 // License Version 6.5 (the "License"). You may not use the content of this file
5 // except in compliance with the License. Please obtain a copy of the License
6 // at http://www.opencascade.org and read it completely before using this file.
7 //
8 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
9 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
10 //
11 // The Original Code and all software distributed under the License is
12 // distributed on an "AS IS" basis, without warranty of any kind, and the
13 // Initial Developer hereby disclaims all such warranties, including without
14 // limitation, any warranties of merchantability, fitness for a particular
15 // purpose or non-infringement. Please see the License for the specific terms
16 // and conditions governing the rights and limitations under the License.
17
18 #include <Interface_ShareTool.ixx>
19 #include <Interface_InterfaceError.hxx>
20 #include <Interface_IntList.hxx>
21 #include <Interface_GeneralModule.hxx>
22 #include <TColStd_HArray1OfInteger.hxx>
23 #include <TColStd_HSequenceOfTransient.hxx>
24 #include <Message_Messenger.hxx>
25
26 Interface_ShareTool::Interface_ShareTool (const Handle(Interface_InterfaceModel)& amodel,
27                                           const Interface_GeneralLib& lib)
28 {
29   theHGraph = new Interface_HGraph(amodel,lib);
30 }
31
32 Interface_ShareTool::Interface_ShareTool (const Handle(Interface_InterfaceModel)& amodel,
33                                           const Handle(Interface_GTool)& gtool)
34 {
35   theHGraph = new Interface_HGraph(amodel,gtool);
36 }
37
38 Interface_ShareTool::Interface_ShareTool (const Handle(Interface_InterfaceModel)& amodel,
39                                           const Handle(Interface_Protocol)& protocol)
40 {
41   theHGraph = new Interface_HGraph(amodel,protocol);
42 }
43
44 Interface_ShareTool::Interface_ShareTool (const Handle(Interface_InterfaceModel)& amodel)
45 {
46   theHGraph = new Interface_HGraph(amodel);
47 }
48
49 Interface_ShareTool::Interface_ShareTool (const Interface_Graph& agraph)
50 {
51   theHGraph = new Interface_HGraph(agraph.Model());
52 }
53
54 Interface_ShareTool::Interface_ShareTool (const Handle(Interface_HGraph)& ahgraph)
55 {
56   theHGraph = ahgraph;
57 }
58
59 //    Ajout des "Implied" sur toutes les Entites du Graphe
60 /*void Interface_ShareTool::AddImplied (const Handle(Interface_GTool)& gtool)
61 {
62   Interface_Graph& thegraph = theHGraph->CGraph();
63   Standard_Integer nb = thegraph.Size();
64   Standard_Boolean yena = Standard_False;
65   for (Standard_Integer i = 1; i <= nb; i ++) {
66     Handle(Standard_Transient) ent = thegraph.Entity(i);
67     if (ent.IsNull()) continue;
68     Handle(Interface_GeneralModule) module;  Standard_Integer CN;
69     if (gtool->Select(ent,module,CN)) {
70       Interface_EntityIterator iter;
71       module->ListImpliedCase(CN,ent,iter);
72       if (iter.NbEntities() == 0) continue;
73       yena = Standard_True;
74       thegraph.SetShare(ent);
75       for (iter.Start(); iter.More(); iter.Next())
76         thegraph.AddShared(ent,iter.Value());
77     }
78   }
79   if (yena) thegraph.EvalSharings();
80 }*/
81
82
83     Handle(Interface_InterfaceModel) Interface_ShareTool::Model () const
84       {  return theHGraph->Graph().Model();  }
85
86     const Interface_Graph& Interface_ShareTool::Graph () const
87       {  return theHGraph->Graph();  }
88
89     Interface_EntityIterator  Interface_ShareTool::RootEntities () const
90       {  return theHGraph->Graph().RootEntities();  }
91
92     Standard_Boolean  Interface_ShareTool::IsShared
93   (const Handle(Standard_Transient)& ent) const
94 {
95   const Interface_Graph& thegraph = theHGraph->Graph();
96   Handle(TColStd_HSequenceOfTransient) list =
97     thegraph.GetShareds (ent);
98   return (!list.IsNull() && list->Length() > 0);
99 }
100
101     Interface_EntityIterator  Interface_ShareTool::Shareds
102   (const Handle(Standard_Transient)& ent) const
103       {  return theHGraph->Graph().Shareds(ent);  }
104
105     Interface_EntityIterator  Interface_ShareTool::Sharings
106   (const Handle(Standard_Transient)& ent) const
107       {  return theHGraph->Graph().Sharings(ent);  }
108
109
110     Standard_Integer Interface_ShareTool::NbTypedSharings
111   (const Handle(Standard_Transient)& ent,
112    const Handle(Standard_Type)& atype) const
113 {
114   Interface_Graph& thegraph = theHGraph->CGraph();
115   Handle(TColStd_HSequenceOfTransient) list = thegraph.GetSharings (ent);
116   if(list.IsNull())
117     return 0;
118
119   Standard_Integer result = 0;
120   Standard_Integer n = list->Length();
121   for (Standard_Integer i = 1; i <= n; i ++) {
122     Handle(Standard_Transient) entsh = list->Value(i);
123     if (entsh.IsNull()) continue;
124     if (entsh->IsKind(atype)) result ++;
125   }
126   return result;
127 }
128
129     Handle(Standard_Transient) Interface_ShareTool::TypedSharing
130   (const Handle(Standard_Transient)& ent,
131    const Handle(Standard_Type)& atype) const
132 {
133   Interface_Graph& thegraph = theHGraph->CGraph();
134   Handle(TColStd_HSequenceOfTransient) list = thegraph.GetSharings(ent);
135   if(list.IsNull())
136     return 0;
137   Handle(Standard_Transient) entresult;
138   Standard_Integer result = 0;
139   Standard_Integer n = list->Length();
140   for (Standard_Integer i = 1; i <= n; i ++) {
141     Handle(Standard_Transient) entsh = list->Value(i);
142     if (entsh.IsNull()) continue;
143     if (entsh->IsKind(atype)) {
144       entresult = entsh;
145       result ++;
146       if (result > 1)  Interface_InterfaceError::Raise
147         ("Interface ShareTool : TypedSharing, more than one found");
148     }
149   }
150   if (result == 0) Interface_InterfaceError::Raise
151     ("Interface ShareTool : TypedSharing, not found");
152   return entresult;
153 }
154
155     Interface_EntityIterator  Interface_ShareTool::All
156 (const Handle(Standard_Transient)& ent, const Standard_Boolean rootlast) const
157 {
158   Handle(Interface_InterfaceModel) model = Model();
159   Interface_EntityIterator list;
160   Standard_Integer i, n0 = 0, nb = model->NbEntities();
161   Handle(TColStd_HArray1OfInteger) fl = new TColStd_HArray1OfInteger (0,nb);
162   fl->Init(0);
163   if (ent == model) {
164 //    On passe les racines en revue (l ordre de base est conserve)
165     Interface_EntityIterator roots = RootEntities();
166     for (roots.Start(); roots.More(); roots.Next()) {
167       Interface_EntityIterator subl = All(roots.Value(),rootlast);
168       for (subl.Start(); subl.More(); subl.Next()) {
169         Standard_Integer nm = model->Number(subl.Value());
170         if (fl->Value(nm) > 0) continue;
171         n0 ++;  fl->SetValue(nm,n0);
172       }
173     }
174 //    Attention, y a t il des oublis ?      
175     for (i = 1; i <= nb; i ++)
176       if (fl->Value(i) == 0)  {  n0 ++;  fl->SetValue(i,n0);  }
177   } else {
178     Handle(TColStd_HSequenceOfTransient) sq = new TColStd_HSequenceOfTransient();
179     sq->Append(ent);
180 //    processus de type file
181     for (i = 1; i <= sq->Length(); i ++) {    // Length croit
182       Handle(Standard_Transient) en = sq->Value(i);
183       Standard_Integer num = model->Number(en);
184       if (fl->Value(num) != 0) continue;  // deja vu
185       n0 ++;  fl->SetValue (num,n0);
186       Interface_EntityIterator sh = Shareds(en);
187       sq->Append (sh.Content());
188     }
189   }
190 //    Reste a constituer la liste, retourner si necessaire
191   Handle(TColStd_HArray1OfInteger) ord = new TColStd_HArray1OfInteger (0,nb);
192   ord->Init(0);
193   for (i = 1; i <= nb; i ++)  {  n0 = fl->Value(i);  ord->SetValue (n0,i);  }
194   if (rootlast && ent != model)    for (i = 1; i <= nb; i ++)
195       {  if (ord->Value(i) != 0) list.AddItem (model->Value(ord->Value(i)));  }
196   else                             for (i = nb; i  > 0; i --)
197       {  if (ord->Value(i) != 0) list.AddItem (model->Value(ord->Value(i)));  }
198
199   return list;
200 }
201
202     void  Interface_ShareTool::Print
203   (const Interface_EntityIterator& iter, const Handle(Message_Messenger)& S) const
204 {
205   S << " Nb.Entities : " << iter.NbEntities() << " : ";
206   for (iter.Start(); iter.More(); iter.Next()) {
207     Handle(Standard_Transient) ent = iter.Value();
208     S << " n0/id:"; Model()->Print(ent,S);
209   }
210   S<<endl;
211 }