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.
15 #include <IFSelect_ContextModif.hxx>
16 #include <IFSelect_Modifier.hxx>
17 #include <IFSelect_Selection.hxx>
18 #include <IFSelect_TransformStandard.hxx>
19 #include <Interface_CheckIterator.hxx>
20 #include <Interface_CopyControl.hxx>
21 #include <Interface_CopyTool.hxx>
22 #include <Interface_EntityIterator.hxx>
23 #include <Interface_Graph.hxx>
24 #include <Interface_InterfaceModel.hxx>
25 #include <Interface_Macros.hxx>
26 #include <Interface_Protocol.hxx>
27 #include <Message.hxx>
28 #include <Message_Messenger.hxx>
29 #include <Standard_Transient.hxx>
30 #include <Standard_Type.hxx>
31 #include <TCollection_AsciiString.hxx>
32 #include <TColStd_HArray1OfInteger.hxx>
35 IMPLEMENT_STANDARD_RTTIEXT(IFSelect_TransformStandard,IFSelect_Transformer)
37 IFSelect_TransformStandard::IFSelect_TransformStandard ()
38 { thecopy = Standard_True; }
40 void IFSelect_TransformStandard::SetCopyOption
41 (const Standard_Boolean option)
44 Standard_Boolean IFSelect_TransformStandard::CopyOption () const
47 void IFSelect_TransformStandard::SetSelection
48 (const Handle(IFSelect_Selection)& sel)
51 Handle(IFSelect_Selection) IFSelect_TransformStandard::Selection () const
54 Standard_Integer IFSelect_TransformStandard::NbModifiers () const
55 { return themodifs.Length(); }
57 Handle(IFSelect_Modifier) IFSelect_TransformStandard::Modifier
58 (const Standard_Integer num) const
59 { return GetCasted(IFSelect_Modifier,themodifs.Value(num)); }
61 Standard_Integer IFSelect_TransformStandard::ModifierRank
62 (const Handle(IFSelect_Modifier)& modif) const
64 for (Standard_Integer i = themodifs.Length(); i >= 1; i --)
65 if (modif == themodifs.Value(i)) return i;
69 Standard_Boolean IFSelect_TransformStandard::AddModifier
70 (const Handle(IFSelect_Modifier)& modif,
71 const Standard_Integer atnum)
73 if (atnum < 0 || atnum > themodifs.Length()) return Standard_False;
74 if (atnum == 0) themodifs.Append(modif);
75 else themodifs.InsertBefore(atnum,modif);
79 Standard_Boolean IFSelect_TransformStandard::RemoveModifier
80 (const Handle(IFSelect_Modifier)& modif)
82 Standard_Integer num = ModifierRank(modif);
83 return RemoveModifier(num);
87 Standard_Boolean IFSelect_TransformStandard::RemoveModifier
88 (const Standard_Integer num)
90 if (num <= 0 || num > themodifs.Length()) return Standard_False;
91 themodifs.Remove(num);
95 // #################################################################
96 // ######## ACTION ########
98 Standard_Boolean IFSelect_TransformStandard::Perform
99 (const Interface_Graph& G, const Handle(Interface_Protocol)& protocol,
100 Interface_CheckIterator& checks,
101 Handle(Interface_InterfaceModel)& newmod)
103 Interface_CopyTool TC(G.Model(),protocol);
104 themap = TC.Control();
106 return ApplyModifiers (G,protocol,TC,checks,newmod);
109 void IFSelect_TransformStandard::Copy
110 (const Interface_Graph& G, Interface_CopyTool& TC,
111 Handle(Interface_InterfaceModel)& newmod) const
113 if (CopyOption()) StandardCopy (G,TC,newmod);
114 else OnTheSpot (G,TC,newmod);
117 void IFSelect_TransformStandard::StandardCopy
118 (const Interface_Graph& G, Interface_CopyTool& TC,
119 Handle(Interface_InterfaceModel)& newmod) const
121 Handle(Interface_InterfaceModel) original = G.Model();
122 newmod = original->NewEmptyModel();
124 Standard_Integer nb = G.Size();
125 Handle(TColStd_HArray1OfInteger) remain =
126 new TColStd_HArray1OfInteger(0,nb+1); remain->Init(0);
127 for (Standard_Integer i = 1; i <= nb; i ++) {
128 // if (G.Status(i) == 0) TC.TransferEntity (original->Value(i));
129 TC.TransferEntity (original->Value(i));
131 TC.FillModel(newmod);
134 void IFSelect_TransformStandard::OnTheSpot
135 (const Interface_Graph& G, Interface_CopyTool& TC,
136 Handle(Interface_InterfaceModel)& newmod) const
138 Standard_Integer nb = G.Size();
139 for (Standard_Integer i = 1; i <= nb; i ++) TC.Bind(G.Entity(i),G.Entity(i));
144 Standard_Boolean IFSelect_TransformStandard::ApplyModifiers
145 (const Interface_Graph& G, const Handle(Interface_Protocol)& protocol,
146 Interface_CopyTool& TC, Interface_CheckIterator& checks,
147 Handle(Interface_InterfaceModel)& newmod) const
149 Handle(Message_Messenger) sout = Message::DefaultMessenger();
150 Standard_Boolean res = Standard_True;
151 Standard_Boolean chg = Standard_False;
152 Standard_Integer nb = NbModifiers();
153 Handle(Interface_InterfaceModel) original = G.Model();
155 for (Standard_Integer i = 1; i <= nb; i ++) {
156 Handle(IFSelect_Modifier) unmod = Modifier(i);
157 if (unmod->MayChangeGraph()) chg = Standard_True;
159 // Appliquer ce Modifier (nb : le Dispatch, on s en moque)
160 // D abord, la Selection
161 IFSelect_ContextModif ctx (G,TC);
162 // Ensuite, la Selection
163 // S il y en a une ici, elle a priorite. Sinon, chaque Modifier a la sienne
165 Handle(IFSelect_Selection) sel = thesel;
166 if ( sel.IsNull()) sel = unmod->Selection();
168 Interface_EntityIterator entiter = sel->UniqueResult(G);
169 ctx.Select (entiter);
171 if (ctx.IsForNone()) continue;
172 unmod->Perform (ctx,newmod,protocol,TC);
174 // Report des Erreurs
175 // Faut-il les enregistrer dans newmod ? bonne question
176 Interface_CheckIterator checklist = ctx.CheckList();
177 if (!checklist.IsEmpty(Standard_False)) {
178 checks.Merge(checklist);
179 sout<<"IFSelect_TransformStandard : Messages from Modifier n0 "<<i<<" of "<<nb<<endl;
180 checklist.Print(sout,newmod,Standard_False);
182 if (!checklist.IsEmpty(Standard_True)) {
183 sout<<" -- Abandon TransformStandard --"<<endl;
184 res = Standard_False; break;
188 // Modele pas modifie et Graphe pas modifie : le dire
189 if (newmod == original && !chg) newmod.Nullify();
193 Standard_Boolean IFSelect_TransformStandard::Updated
194 (const Handle(Standard_Transient)& entfrom,
195 Handle(Standard_Transient)& entto) const
197 if (themap.IsNull()) return Standard_False;
198 return themap->Search(entfrom,entto);
202 TCollection_AsciiString IFSelect_TransformStandard::Label () const
205 TCollection_AsciiString labl("");
206 if (CopyOption()) labl.AssignCat("Standard Copy");
207 else labl.AssignCat("On the spot Edition");
208 Standard_Integer nb = NbModifiers();
209 if (nb == 0) sprintf(lab," (no Modifier)");
210 if (nb == 1) sprintf(lab," - %s",Modifier(1)->Label().ToCString());
211 if (nb > 1) sprintf(lab," - %d Modifiers",nb);