0030686: Visualization, SelectMgr_ViewerSelector - sorting issues of transformation...
[occt.git] / src / Interface / Interface_EntityList.cxx
CommitLineData
973c2be1 1// Copyright (c) 1999-2014 OPEN CASCADE SAS
b311480e 2//
973c2be1 3// This file is part of Open CASCADE Technology software library.
b311480e 4//
d5f74e42 5// This library is free software; you can redistribute it and/or modify it under
6// the terms of the GNU Lesser General Public License version 2.1 as published
973c2be1 7// by the Free Software Foundation, with special exception defined in the file
8// OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
9// distribution for complete text of the license and disclaimer of any warranty.
b311480e 10//
973c2be1 11// Alternatively, this file may be used under the terms of Open CASCADE
12// commercial license or contractual agreement.
b311480e 13
42cf5bc1 14
7fd59977 15#include <Interface_EntityCluster.hxx>
42cf5bc1 16#include <Interface_EntityIterator.hxx>
17#include <Interface_EntityList.hxx>
7fd59977 18#include <Interface_InterfaceError.hxx>
7fd59977 19#include <Standard_NullObject.hxx>
42cf5bc1 20#include <Standard_OutOfRange.hxx>
21#include <Standard_Transient.hxx>
7fd59977 22
23// Une EntityList, c est au fond un "Handle" bien entoure :
24// S il est nul, la liste est vide
25// Si c est une Entite, la liste comprend cette entite et rien d autre
26// Si c est un EntityCluster, il definit (avec ses Next eventuels) le contenu
27// de la liste
b311480e 28Interface_EntityList::Interface_EntityList () { }
7fd59977 29
30 void Interface_EntityList::Clear ()
31 { theval.Nullify(); }
32
33// .... EDITIONS (ajout-suppression) ....
34
35 void Interface_EntityList::Append
36 (const Handle(Standard_Transient)& ent)
37{
9775fa61 38 if (ent.IsNull()) throw Standard_NullObject("Interface_EntityList Append");
7fd59977 39 if (theval.IsNull()) { theval = ent; return; }
51740958 40 Handle(Interface_EntityCluster) aValEC =
7fd59977 41 Handle(Interface_EntityCluster)::DownCast(theval);
51740958 42 if (!aValEC.IsNull()) aValEC->Append(ent); // EntityCluster
7fd59977 43 else { // reste InterfaceEntity ...
44 Handle(Interface_EntityCluster) ec = new Interface_EntityCluster(theval);
45 ec->Append(ent);
46 theval = ec;
47 }
48}
49
50// Difference avec Append : on optimise, en evitant la recursivite
51// En effet, quand un EntityCluster est plein, Append transmet au Next
52// Ici, EntityList garde le controle, le temps de traitement reste le meme
53// Moyennant quoi, l ordre n est pas garanti
54
55 void Interface_EntityList::Add
56 (const Handle(Standard_Transient)& ent)
57{
9775fa61 58 if (ent.IsNull()) throw Standard_NullObject("Interface_EntityList Add");
7fd59977 59 if (theval.IsNull()) { theval = ent; return; }
51740958 60 Handle(Interface_EntityCluster) aValEC =
7fd59977 61 Handle(Interface_EntityCluster)::DownCast(theval);
51740958 62 if (!aValEC.IsNull()) { // EntityCluster
63 if (aValEC->IsLocalFull()) theval = new Interface_EntityCluster(ent, aValEC);
64 else aValEC->Append (ent);
7fd59977 65 } else { // reste InterfaceEntity ...
66 Handle(Interface_EntityCluster) ec = new Interface_EntityCluster(theval);
67 ec->Append(ent);
68 theval = ec;
69 }
70}
71
72// Remove : Par Identification d Item a supprimer, ou par Rang
73// Identification : Item supprime ou qu il soit
74// N.B.: La liste peut devenir vide ... cf retour Remove de Cluster
75
76 void Interface_EntityList::Remove (const Handle(Standard_Transient)& ent)
77{
9775fa61 78 if (ent.IsNull()) throw Standard_NullObject("Interface_EntityList Remove");
7fd59977 79 if (theval.IsNull()) return;
80 if (theval == ent) {
81 theval.Nullify();
82 return;
83 }
84 Handle(Interface_EntityCluster) ec =
85 Handle(Interface_EntityCluster)::DownCast(theval);
86 if (ec.IsNull()) return; // Une seule Entite et pas la bonne
87 Standard_Boolean res = ec->Remove(ent);
88 if (res) theval.Nullify();
89}
90
91// Remove par rang : tester OutOfRange
92
93 void Interface_EntityList::Remove (const Standard_Integer num)
94{
9775fa61 95 if (theval.IsNull()) throw Standard_OutOfRange("EntityList : Remove");
7fd59977 96 Handle(Interface_EntityCluster) ec =
97 Handle(Interface_EntityCluster)::DownCast(theval);
98 if (ec.IsNull()) {
9775fa61 99 if (num != 1) throw Standard_OutOfRange("EntityList : Remove");
7fd59977 100 theval.Nullify();
101 return;
102 }
103 Standard_Boolean res = ec->Remove(num);
104 if (res) theval.Nullify();
105}
106
107// .... ACCES Unitaire AUX DONNEES ....
108
109 Standard_Boolean Interface_EntityList::IsEmpty () const
110 { return (theval.IsNull()); }
111
112 Standard_Integer Interface_EntityList::NbEntities () const
113{
114 if (theval.IsNull()) return 0;
115 Handle(Interface_EntityCluster) ec =
116 Handle(Interface_EntityCluster)::DownCast(theval);
117 if (ec.IsNull()) return 1; // Une seuke Entite
118 return ec->NbEntities();
119}
120
121
122 const Handle(Standard_Transient)& Interface_EntityList::Value
123 (const Standard_Integer num) const
124{
9775fa61 125 if (theval.IsNull()) throw Standard_OutOfRange("Interface EntityList : Value");
7fd59977 126 Handle(Interface_EntityCluster) ec =
127 Handle(Interface_EntityCluster)::DownCast(theval);
128 if (!ec.IsNull()) return ec->Value(num); // EntityCluster
9775fa61 129 else if (num != 1) throw Standard_OutOfRange("Interface EntityList : Value");
7fd59977 130 return theval;
131}
132
133 void Interface_EntityList::SetValue
134 (const Standard_Integer num, const Handle(Standard_Transient)& ent)
135{
9775fa61 136 if (ent.IsNull()) throw Standard_NullObject("Interface_EntityList SetValue");
137 if (theval.IsNull()) throw Standard_OutOfRange("Interface EntityList : SetValue");
7fd59977 138 Handle(Interface_EntityCluster) ec =
139 Handle(Interface_EntityCluster)::DownCast(theval);
140 if (!ec.IsNull()) ec->SetValue(num,ent); // EntityCluster
9775fa61 141 else if (num != 1) throw Standard_OutOfRange("Interface EntityList : SetValue");
7fd59977 142 else theval = ent;
143
144}
145
146// .... Interrogations Generales ....
147
148 void Interface_EntityList::FillIterator
149 (Interface_EntityIterator& iter) const
150{
151 if (theval.IsNull()) return;
152 Handle(Interface_EntityCluster) ec =
153 Handle(Interface_EntityCluster)::DownCast(theval);
154 if (!ec.IsNull()) ec->FillIterator(iter); // EntityCluster;
155 else iter.GetOneItem(theval);
156}
157
158
159 Standard_Integer Interface_EntityList::NbTypedEntities
160 (const Handle(Standard_Type)& atype) const
161{
162 Standard_Integer res = 0;
163 if (theval.IsNull()) return 0;
164 Handle(Interface_EntityCluster) ec =
165 Handle(Interface_EntityCluster)::DownCast(theval);
166 if (!ec.IsNull()) { // EntityCluster
167 while (!ec.IsNull()) {
168 for (Standard_Integer i = ec->NbLocal(); i > 0; i --) {
169 if (ec->Value(i)->IsKind(atype)) res ++;
170 }
171 if (!ec->HasNext()) break;
172 ec = ec->Next();
173 }
174 } else { // Une seule Entite
175 if (theval->IsKind(atype)) res = 1;
176 }
177 return res;
178}
179
180 Handle(Standard_Transient) Interface_EntityList::TypedEntity
181 (const Handle(Standard_Type)& atype, const Standard_Integer num) const
182{
183 Standard_Integer res = 0;
184 Handle(Standard_Transient) entres;
9775fa61 185 if (theval.IsNull()) throw Interface_InterfaceError("Interface EntityList : TypedEntity , none found");
7fd59977 186 Handle(Interface_EntityCluster) ec =
187 Handle(Interface_EntityCluster)::DownCast(theval);
188 if (!ec.IsNull()) { // EntityCluster
189 while (!ec.IsNull()) {
190 for (Standard_Integer i = ec->NbLocal(); i > 0; i --) {
191 if (ec->Value(i)->IsKind(atype)) {
192 res ++;
9775fa61 193 if (num == 0 && res > 1) throw Interface_InterfaceError("Interface EntityList : TypedEntity , several found");
7fd59977 194 entres = ec->Value(i);
195 if (res == num) return entres;
196 }
197 }
198 if (!ec->HasNext()) break;
199 ec = ec->Next();
200 }
201 } else if (num > 1) {
9775fa61 202 throw Interface_InterfaceError("Interface EntityList : TypedEntity ,out of range");
7fd59977 203 } else { // InterfaceEntity
9775fa61 204 if (!theval->IsKind(atype)) throw Interface_InterfaceError("Interface EntityList : TypedEntity , none found");
7fd59977 205 entres = theval;
206 }
207 return entres;
208}