0022627: Change OCCT memory management defaults
[occt.git] / src / QADNaming / QADNaming_SelectionCommands.cxx
CommitLineData
7fd59977 1// File: DDesignPart_SelectionCommands.cxx
2// Created: Mon Oct 20 16:18:02 1997
3// Author: Yves FRICAUD
4// <yfr@claquox.paris1.matra-dtv.fr>
5
6
7#include <QADNaming.hxx>
8
9#include <DDF.hxx>
10
11#include <TNaming_NamedShape.hxx>
12#include <TNaming_ListOfNamedShape.hxx>
13#include <TNaming_ListIteratorOfListOfNamedShape.hxx>
14#include <TNaming_Tool.hxx>
15#include <TDF_Label.hxx>
16#include <TDF_AttributeMap.hxx>
17#include <TDF_MapIteratorOfAttributeMap.hxx>
18#include <TDF_ChildIterator.hxx>
19#include <TDF_Tool.hxx>
20#include <TNaming_Name.hxx>
21#include <TNaming.hxx>
22#include <TNaming_Naming.hxx>
23#include <TNaming_MapOfNamedShape.hxx>
24#include <TNaming_MapIteratorOfMapOfNamedShape.hxx>
25#include <TDF_ChildIterator.hxx>
26#include <TNaming_Selector.hxx>
27#include <TopoDS_Shape.hxx>
28#include <TopAbs.hxx>
29#include <TCollection_AsciiString.hxx>
30#include <TDF_LabelMap.hxx>
31
32#include <DBRep.hxx>
33#include <Draw_Appli.hxx>
34#include <Draw.hxx>
35#include <Draw_Interpretor.hxx>
36#include <Draw_Drawable3D.hxx>
37#include <DBRep.hxx>
38#include <DBRep_DrawableShape.hxx>
39#include <stdio.h>
40
41//=======================================================================
42//function : Display
43//purpose :
44//=======================================================================
45
46static void Display (const Standard_CString Name, const TopoDS_Shape& S)
47{
48 //char* name = Name;
49 static Standard_Integer nbIsos = 2;
50 static Standard_Real size = 100.;
51 static Standard_Integer discret = 30;
52
53 Handle(DBRep_DrawableShape) D = new DBRep_DrawableShape(S,
54 Draw_jaune,
55 Draw_vert,
56 Draw_bleu,
57 Draw_rouge,
58 size,
59 nbIsos,
60 discret);
61 Draw::Set(Name,D);
62}
63
64//=======================================================================
65//function : DumpNaming
66//purpose :
67//=======================================================================
68
69static void DumpNaming (const Handle(TNaming_Naming)& naming, Draw_Interpretor& di)
70{
71 TCollection_AsciiString Entry;
72 const TNaming_Name& AName = naming->GetName();
73
74 //TNaming::Print(AName.Type(),cout);
75 Standard_SStream aSStream1;
76 TNaming::Print(AName.Type(),aSStream1);
77 di << aSStream1;
78 di << " ";
79
80 //TopAbs::Print(AName.ShapeType(),cout);
81 Standard_SStream aSStream2;
82 TopAbs::Print(AName.ShapeType(),aSStream2);
83 di << aSStream2;
84
85 const TNaming_ListOfNamedShape& NSS = AName.Arguments();
86 for (TNaming_ListIteratorOfListOfNamedShape it(NSS); it.More(); it.Next()) {
87 TDF_Tool::Entry(it.Value()->Label(),Entry);
88 di << " " << Entry.ToCString();
89 }
90 if(!AName.StopNamedShape().IsNull()) {
91 TDF_Tool::Entry(AName.StopNamedShape()->Label(),Entry);
92 di << " Stop " << Entry.ToCString();
93 }
94}
95
96//=======================================================================
97//function : SelectShape ou SelectGeometry
98//purpose : "Select DF entry shape [context]",
99//=======================================================================
100
101static Standard_Integer QADNaming_Select (Draw_Interpretor& di, Standard_Integer n, const char** a)
102{
103 if (n == 4 || n == 5) {
104 Standard_Boolean geometry = (strcmp(a[0],"SelectGeometry")==0?1:0);
105 Handle(TDF_Data) DF;
106 if(!DDF::GetDF(a[1], DF)) return 1;
107 TDF_Label L;
108 DDF::AddLabel(DF,a[2],L);
109 TNaming_Selector SL (L);
110 if (n == 4) {
111 TopoDS_Shape S = DBRep::Get(a[3], TopAbs_SHAPE);
112 SL.Select (S, geometry);
113 }
114 if (n == 5) {
115 TopoDS_Shape S = DBRep::Get(a[3], TopAbs_SHAPE);
116 TopoDS_Shape C = DBRep::Get(a[4], TopAbs_SHAPE);
117 SL.Select (S, C, geometry);
118 }
119 return 0;
120 }
121 di << "QADNaming_Select : Error" << "\n";
122 return 1;
123}
124
125//=======================================================================
126//function : SolveSelection
127//purpose : "SolveSelection DF entry",
128//=======================================================================
129
130static Standard_Integer QADNaming_SolveSelection (Draw_Interpretor& di, Standard_Integer n, const char** a)
131{
132 if (n >= 3) {
133 TDF_Label aLabel;
134 if (!QADNaming::Entry(a, aLabel)) return 1;
135 char name[100];
136 TNaming_Selector SL (aLabel);
137 TDF_LabelMap valid;
138 Standard_Integer i;
139 for(i=3;i<n;i++) {
140 TDF_Label aValidLab;
141 if (!DDF::FindLabel(aLabel.Data(),a[i],aValidLab)) di<<"Warning: label "<<a[i]<<" not exists"<<"\n";
142 else valid.Add(aValidLab);
143 }
144 Standard_Boolean done = SL.Solve(valid);
145 TopoDS_Shape Res = TNaming_Tool::CurrentShape(SL.NamedShape());
146 //TopoDS_Shape Res = TNaming_Tool::CurrentShape(NS);
147 sprintf (name,"%s_%s","new",a[2]);
148 Display (name,Res);
149 return done?0:1;
150 }
151 di << "Usage: DumpSelection DF entry [validlabel1 validlabel2 ...]" << "\n";
152 return 1;
153}
154
155//=======================================================================
156//function : DumpSelection
157//purpose : DumpSelection DF entry (R)
158//=======================================================================
159
160static Standard_Integer QADNaming_DumpSelection (Draw_Interpretor& di,
161 Standard_Integer n, const char** a)
162{
163 if (n == 3 || n == 4) {
164 Handle(TDF_Data) DF;
165 if (!DDF::GetDF(a[1],DF)) return 1;
166 TDF_Label L;
167 if (!DDF::FindLabel(DF,a[2],L)) return 1;
168 Handle(TNaming_Naming) naming;
169 if (!L.FindAttribute(TNaming_Naming::GetID(),naming)) {
170 di <<"QADNaming_DumpSelection : not a selection" << "\n";
171 return 1;
172 }
173 DumpNaming(naming, di);
174 di << "\n";
175 if (n == 4) {
176 Standard_Integer depth = L.Depth();
177 Standard_Integer curdepth = 0;
178 TCollection_AsciiString Entry;
179 TDF_ChildIterator it (naming->Label(),Standard_True);
180 for (;it.More();it.Next()) {
181 if (it.Value().FindAttribute(TNaming_Naming::GetID(),naming)) {
182 curdepth = (naming->Label().Depth()- depth);
183 for (Standard_Integer i = 1; i <= curdepth; i++) di << " ";
184 TDF_Tool::Entry (naming->Label(),Entry);
185 di << Entry.ToCString() << " ";
186 DumpNaming(naming, di);
187 di << "\n";
188 }
189 }
190 }
191 return 0;
192 }
193 di << "QADNaming_DumpSelection : Error" << "\n";
194 return 1;
195}
196
197//=======================================================================
198//function : ArgsSelection
199//purpose : ArgsSelection DF entry
200//=======================================================================
201
202static Standard_Integer QADNaming_ArgsSelection (Draw_Interpretor& di,
203 Standard_Integer n, const char** a)
204{
205 if (n == 3) {
206 Handle(TDF_Data) DF;
207 if (!DDF::GetDF(a[1],DF)) return 1;
208 TDF_Label L;
209 if (!DDF::FindLabel(DF,a[2],L)) return 1;
210 Handle(TNaming_Naming) naming;
211 if (!L.FindAttribute(TNaming_Naming::GetID(),naming)) {
212 di <<"QADNaming_DumpSelection : not a selection" << "\n";
213 return 1;
214 }
215 TCollection_AsciiString Entry;
216 TNaming_Selector SL (L);
217 di <<" Selection Arguments : ";
218 TDF_AttributeMap args;
219 SL.Arguments(args);
220 for (TDF_MapIteratorOfAttributeMap it(args); it.More(); it.Next()) {
221 TDF_Tool::Entry(it.Key()->Label(),Entry);
222 di << Entry.ToCString() << " ";
223 }
224 di << "\n";
225 return 0;
226 }
227 di << "QADNaming_ArgsSelection : Error" << "\n";
228 return 1;
229}
230
231//=======================================================================
232//function : CollectAttachment
233//purpose :
234//=======================================================================
235
236static void CollectAttachment (const TDF_Label& root,
237 const Handle(TNaming_Naming)& naming,
238 TNaming_MapOfNamedShape& attachment)
239{
240 TNaming_ListIteratorOfListOfNamedShape itarg;
241 const TNaming_ListOfNamedShape& args = naming->GetName().Arguments();
242 for (itarg.Initialize(args);itarg.More();itarg.Next()) {
243 if (!itarg.Value()->Label().IsDescendant(root)) attachment.Add(itarg.Value());
244 }
245 Handle(TNaming_Naming) subnaming;
246 for (TDF_ChildIterator it(naming->Label(),Standard_True);it.More();it.Next()) {
247 if (it.Value().FindAttribute(TNaming_Naming::GetID(),subnaming)) {
248 const TNaming_ListOfNamedShape& subargs = subnaming->GetName().Arguments();
249 for (itarg.Initialize(subargs);itarg.More();itarg.Next()) {
250 if (!itarg.Value()->Label().IsDescendant(root)) attachment.Add(itarg.Value());
251 }
252 }
253 }
254}
255
256
257//=======================================================================
258//function : Attachment
259//purpose : Attachment DF entry
260//=======================================================================
261
262static Standard_Integer QADNaming_Attachment (Draw_Interpretor& di,
263 Standard_Integer n,
264 const char** a)
265{
266 if (n == 3) {
267 Handle(TDF_Data) DF;
268 if (!DDF::GetDF(a[1],DF)) return 1;
269 TDF_Label L;
270 if (!DDF::FindLabel(DF,a[2],L)) return 1;
271 Handle(TNaming_Naming) naming;
272 TNaming_MapOfNamedShape attachment;
273 if (L.FindAttribute(TNaming_Naming::GetID(),naming)) {
274 CollectAttachment (L,naming,attachment);
275 }
276 else {
277 for (TDF_ChildIterator it (L,Standard_True); it.More();it.Next()) {
278 if (it.Value().FindAttribute(TNaming_Naming::GetID(),naming)) {
279 CollectAttachment (L,naming,attachment);
280 it.NextBrother();
281 }
282 }
283 }
284 TCollection_AsciiString Entry;
285 TDF_Tool::Entry(L,Entry);
286 di << " Attachment of " << Entry.ToCString();
287 di << "\n";
288 for (TNaming_MapIteratorOfMapOfNamedShape ita (attachment); ita.More(); ita.Next()) {
289 TDF_Tool::Entry (ita.Key()->Label(),Entry);
290 di << Entry.ToCString() << " ";
291 }
292 di << "\n";
293 return 0;
294 }
295 di << "QADNaming_Attachment : Error" << "\n";
296 return 1;
297}
298
299//=======================================================================
300//function : SelectionCommands
301//purpose :
302//=======================================================================
303
304void QADNaming::SelectionCommands(Draw_Interpretor& theCommands)
305{
306
307 static Standard_Boolean done = Standard_False;
308 if (done) return;
309 done = Standard_True;
310
311 const char* g = "Naming data commands";
312
313 theCommands.Add("SelectShape",
314 "SelectShape DF entry shape [context]",
315 __FILE__, QADNaming_Select, g);
316
317 theCommands.Add("SelectGeometry",
318 "SelectGeometry DF entry shape [context]",
319 __FILE__, QADNaming_Select, g);
320
321 theCommands.Add("DumpSelection",
322 "DumpSelected DF entry",
323 __FILE__, QADNaming_DumpSelection, g);
324
325 theCommands.Add("ArgsSelection",
326 "ArgsSelection DF entry",
327 __FILE__, QADNaming_ArgsSelection, g);
328
329 theCommands.Add("SolveSelection",
330 "DumpSelection DF entry [validlabel1 validlabel2 ...]",
331 __FILE__, QADNaming_SolveSelection, g);
332
333 theCommands.Add("Attachment",
334 "Attachment DF entry",
335 __FILE__, QADNaming_Attachment, g);
336
337}