1 // Copyright (c) 1999-2014 OPEN CASCADE SAS
3 // This file is part of Open CASCADE Technology software library.
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
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.
11 // Alternatively, this file may be used under the terms of Open CASCADE
12 // commercial license or contractual agreement.
14 #include <IFSelect_ShareOut.ixx>
15 #include <Interface_InterfaceError.hxx>
16 #include <Interface_Macros.hxx>
20 IFSelect_ShareOut::IFSelect_ShareOut ()
22 thedefrt = new TCollection_HAsciiString ("Default");
23 thenbdefs = thelastrun = 0;
27 void IFSelect_ShareOut::Clear (const Standard_Boolean onlydisp)
30 ClearResult(!onlydisp);
32 themodelmodifiers.Clear();
33 thefilemodifiers.Clear();
36 Standard_Boolean IFSelect_ShareOut::RemoveItem
37 (const Handle(Standard_Transient)& item)
39 DeclareAndCast(IFSelect_GeneralModifier,modifier,item);
40 if (!modifier.IsNull()) {
41 Standard_Boolean formodel = modifier->IsKind(STANDARD_TYPE(IFSelect_Modifier));
42 Standard_Integer atnum = ModifierRank(modifier);
43 return RemoveModifier (formodel,atnum);
45 DeclareAndCast(IFSelect_Dispatch,disp,item);
47 Standard_Integer atnum = DispatchRank(disp);
48 return RemoveDispatch(atnum);
50 return Standard_False;
54 void IFSelect_ShareOut::ClearResult (const Standard_Boolean alsoname)
57 if (alsoname) thenbdefs = 0;
60 Standard_Integer IFSelect_ShareOut::LastRun () const
61 { return thelastrun; }
63 void IFSelect_ShareOut::SetLastRun (const Standard_Integer lastrun)
64 { thelastrun = lastrun; }
66 // #######################################################################
67 // #### DISPATCHES (ENVOI DES FICHIERS) ####
69 Standard_Integer IFSelect_ShareOut::NbDispatches () const
70 { return thedisps.Length(); }
72 Standard_Integer IFSelect_ShareOut::DispatchRank
73 (const Handle(IFSelect_Dispatch)& disp) const
75 if (disp.IsNull()) return 0;
76 for (Standard_Integer i = thedisps.Length(); i >= 1; i --)
77 if (disp == thedisps.Value(i)) return i;
81 const Handle(IFSelect_Dispatch)& IFSelect_ShareOut::Dispatch
82 (const Standard_Integer num) const
84 return thedisps.Value(num);
87 void IFSelect_ShareOut::AddDispatch
88 (const Handle(IFSelect_Dispatch)& disp)
90 if (disp.IsNull()) return;
91 thedisps.Append(disp);
95 Standard_Boolean IFSelect_ShareOut::RemoveDispatch
96 (const Standard_Integer rank)
98 if (rank <= thelastrun || rank > thedisps.Length()) return Standard_False;
99 thedisps.Remove(rank);
100 return Standard_True;
103 // ##########################################################################
104 // #### MODIFIERS ####
106 void IFSelect_ShareOut::AddModifier
107 (const Handle(IFSelect_GeneralModifier)& modifier,
108 const Standard_Integer atnum)
110 Standard_Boolean formodel = modifier->IsKind(STANDARD_TYPE(IFSelect_Modifier));
111 if (ModifierRank(modifier) == 0) AddModif (modifier,formodel,atnum);
112 Handle(IFSelect_Dispatch) nuldisp;
113 modifier->SetDispatch(nuldisp);
116 void IFSelect_ShareOut::AddModifier
117 (const Handle(IFSelect_GeneralModifier)& modifier,
118 const Standard_Integer dispnum, const Standard_Integer atnum)
120 Standard_Boolean formodel = modifier->IsKind(STANDARD_TYPE(IFSelect_Modifier));
121 if (ModifierRank(modifier) == 0) AddModif (modifier,formodel,atnum);
122 Handle(IFSelect_Dispatch) disp = Dispatch(dispnum);
123 modifier->SetDispatch(disp);
127 void IFSelect_ShareOut::AddModif
128 (const Handle(IFSelect_GeneralModifier)& modifier,
129 const Standard_Boolean formodel, const Standard_Integer atnum)
132 if (atnum > 0 && atnum <= themodelmodifiers.Length())
133 themodelmodifiers.InsertBefore(atnum,modifier);
134 else themodelmodifiers.Append(modifier);
136 if (atnum > 0 && atnum <= thefilemodifiers.Length())
137 thefilemodifiers.InsertBefore(atnum,modifier);
138 else thefilemodifiers.Append(modifier);
142 Standard_Integer IFSelect_ShareOut::NbModifiers
143 (const Standard_Boolean formodel) const
145 if (formodel) return themodelmodifiers.Length();
146 else return thefilemodifiers.Length();
149 Handle(IFSelect_GeneralModifier) IFSelect_ShareOut::GeneralModifier
150 (const Standard_Boolean formodel, const Standard_Integer atnum) const
152 if (formodel) return themodelmodifiers.Value(atnum);
153 else return thefilemodifiers.Value(atnum);
156 Handle(IFSelect_Modifier) IFSelect_ShareOut::ModelModifier
157 (const Standard_Integer num) const
158 { return Handle(IFSelect_Modifier)::DownCast(themodelmodifiers.Value(num)); }
160 Standard_Integer IFSelect_ShareOut::ModifierRank
161 (const Handle(IFSelect_GeneralModifier)& modifier) const
164 Standard_Boolean formodel = modifier->IsKind(STANDARD_TYPE(IFSelect_Modifier));
166 for (i = themodelmodifiers.Length(); i >= 1; i --)
167 if (modifier == themodelmodifiers.Value(i)) return i;
169 for (i = thefilemodifiers.Length(); i >= 1; i --)
170 if (modifier == thefilemodifiers.Value(i)) return i;
176 Standard_Boolean IFSelect_ShareOut::RemoveModifier
177 (const Standard_Boolean formodel, const Standard_Integer atnum)
179 if (atnum <= 0) return Standard_False;
181 if (atnum > themodelmodifiers.Length()) return Standard_False;
182 themodelmodifiers.Remove(atnum);
184 if (atnum > thefilemodifiers.Length()) return Standard_False;
185 thefilemodifiers.Remove(atnum);
187 return Standard_True;
191 // ChangeModifierRank revient a une permutation circulaire :
192 // before est mis en after, ceux qui sont entre tournent
193 Standard_Boolean IFSelect_ShareOut::ChangeModifierRank
194 (const Standard_Boolean formodel,
195 const Standard_Integer before, const Standard_Integer after)
198 if (before <= 0 || after <= 0) return Standard_False;
199 if (before == after) return Standard_True;
201 nb = themodelmodifiers.Length();
202 if (before > nb || after > nb) return Standard_False;
203 Handle(IFSelect_GeneralModifier) bef = themodelmodifiers.Value(before);
204 themodelmodifiers.Remove(before);
205 if (after == nb) themodelmodifiers.Append(bef);
206 else themodelmodifiers.InsertBefore(after,bef);
208 nb = thefilemodifiers.Length();
209 if (before > nb || after > nb) return Standard_False;
210 Handle(IFSelect_GeneralModifier) bef = thefilemodifiers.Value(before);
211 thefilemodifiers.Remove(before);
212 if (after == nb) thefilemodifiers.Append(bef);
213 else thefilemodifiers.InsertBefore(after,bef);
215 return Standard_True;
218 // #######################################################################
219 // #### NOMINATION DES FICHIERS ####
220 // Rq : thenbdefs s applique tant que l on ne change pas les termes principaux
222 Standard_Boolean IFSelect_ShareOut::SetRootName
223 (const Standard_Integer num, const Handle(TCollection_HAsciiString)& name)
225 if (num < 1 || num > thedisps.Length()) return Standard_False;
226 if (RootNumber(name) != 0) return Standard_False;
227 Dispatch(num)->SetRootName (name);
228 return Standard_True;
231 Standard_Boolean IFSelect_ShareOut::HasRootName
232 (const Standard_Integer num) const
234 if (num < 1 || num > thedisps.Length()) return Standard_False;
235 return Dispatch(num)->HasRootName();
238 Handle(TCollection_HAsciiString) IFSelect_ShareOut::RootName
239 (const Standard_Integer num) const
241 Handle(TCollection_HAsciiString) nulname;
242 if (num < 1 || num > thedisps.Length()) return nulname;
243 return Dispatch(num)->RootName();
246 Standard_Integer IFSelect_ShareOut::RootNumber
247 (const Handle(TCollection_HAsciiString)& name) const
249 if (name.IsNull()) return 0;
250 if (!thedefrt.IsNull()) {
251 if (thedefrt->IsSameString(name)) return -1;
253 for (Standard_Integer i = 1; i <= thedisps.Length(); i ++) {
254 Handle(TCollection_HAsciiString) root = thedisps.Value(i)->RootName();
255 if (root.IsNull()) continue;
256 if (root->IsSameString(name)) return i;
262 void IFSelect_ShareOut::SetPrefix
263 (const Handle(TCollection_HAsciiString)& pref)
264 { thepref = pref; thenbdefs = 0; }
266 Standard_Boolean IFSelect_ShareOut::SetDefaultRootName
267 (const Handle(TCollection_HAsciiString)& defrt)
269 if (RootNumber(defrt) != 0) return Standard_False;
270 if (thedefrt.IsNull() || !thedefrt->IsSameString(defrt)) thenbdefs = 0;
272 return Standard_True;
275 void IFSelect_ShareOut::SetExtension
276 (const Handle(TCollection_HAsciiString)& ext)
277 { theext = ext; thenbdefs = 0; }
279 Handle(TCollection_HAsciiString) IFSelect_ShareOut::Prefix () const
281 if (thepref.IsNull()) return new TCollection_HAsciiString("");
285 Handle(TCollection_HAsciiString) IFSelect_ShareOut::DefaultRootName () const
287 if (thedefrt.IsNull()) return new TCollection_HAsciiString("");
291 Handle(TCollection_HAsciiString) IFSelect_ShareOut::Extension () const
293 if (theext.IsNull()) return new TCollection_HAsciiString("");
298 TCollection_AsciiString IFSelect_ShareOut::FileName
299 (const Standard_Integer dnum, const Standard_Integer pnum,
300 const Standard_Integer nbpack)
302 Handle(TCollection_HAsciiString) rot = RootName(dnum);
303 Standard_Integer num = pnum;
304 Standard_Integer npac = nbpack;
305 Standard_Boolean sufnum = (npac > 1 || num > 1);
308 thenbdefs ++; num = thenbdefs;
310 sufnum = Standard_True; // numeroter sur noms par defaut, des le 1er sans 0
313 TCollection_AsciiString res;
314 if (!thepref.IsNull()) res.AssignCat (thepref->ToCString());
315 if (!rot.IsNull()) res.AssignCat (rot->ToCString());
318 if (sufnum) { // sinon, pas de suffixe numerique
319 // Nom du PacketSuffix : _ suivi du numero <num>
320 // Si nbpack non nul, alors on a un majorant et on peut preceder de zeros
321 // Ex.: nbpack = 50 (donc 2 chiffres), num = 3, cela donnera _03
322 // idnum pas utilise : cette methode peut etre redefinie et utiliser idnum ...
323 // Si nbpack = 0 ou 1, num = 1 pas de suffixe, sinon suffixe "_num" tel quel
324 // MODIF du 3-NOV-1995 -> pour eviter toute confusion, num = 1 donne aussi _1
325 Standard_Integer nbch = 0;
326 char format[10],suffixe[30]; format[1] = ' ';
328 Standard_Integer nbpa = 1;
329 while (nbpa <= npac) { nbpa *= 10; nbch ++; }
332 sprintf(format,"_ %d.%dd",nbch,nbch);
334 } else if (npac >= num || num >= 1) {
335 sprintf(format,"_ d");
338 if (format[1] == '%') {
339 sprintf (suffixe,format,num);
340 res.AssignCat (suffixe);
344 if (!theext.IsNull()) res.AssignCat (theext->ToCString());