1 // Created on: 1997-10-20
2 // Created by: Yves FRICAUD
3 // Copyright (c) 1997-1999 Matra Datavision
4 // Copyright (c) 1999-2014 OPEN CASCADE SAS
6 // This file is part of Open CASCADE Technology software library.
8 // This library is free software; you can redistribute it and/or modify it under
9 // the terms of the GNU Lesser General Public License version 2.1 as published
10 // by the Free Software Foundation, with special exception defined in the file
11 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
12 // distribution for complete text of the license and disclaimer of any warranty.
14 // Alternatively, this file may be used under the terms of Open CASCADE
15 // commercial license or contractual agreement.
17 #include <DNaming.hxx>
21 #include <TNaming_Tool.hxx>
22 #include <TDF_Label.hxx>
23 #include <TDF_Tool.hxx>
24 #include <TNaming.hxx>
25 #include <TNaming_Naming.hxx>
26 #include <TNaming_NamingTool.hxx>
27 #include <TNaming_MapOfNamedShape.hxx>
28 #include <TDF_ChildIterator.hxx>
29 #include <TNaming_Selector.hxx>
30 #include <TopoDS_Shape.hxx>
32 #include <TCollection_AsciiString.hxx>
33 #include <TDF_LabelMap.hxx>
35 #include <Draw_Appli.hxx>
37 #include <Draw_Interpretor.hxx>
39 #include <DBRep_DrawableShape.hxx>
42 //=======================================================================
45 //=======================================================================
47 static void Display (const Standard_CString Name, const TopoDS_Shape& S)
50 static Standard_Integer nbIsos = 2;
51 static Standard_Real size = 100.;
52 static Standard_Integer discret = 30;
54 Handle(DBRep_DrawableShape) D = new DBRep_DrawableShape(S,
65 //=======================================================================
66 //function : DumpNaming
68 //=======================================================================
70 //static void DumpNaming (const Handle(TNaming_Naming)& naming)
71 static void DumpNaming (const Handle(TNaming_Naming)& naming, Draw_Interpretor& di)
73 TCollection_AsciiString Entry;
74 const TNaming_Name& AName = naming->GetName();
75 //TNaming::Print(AName.Type(),std::cout);
76 Standard_SStream aStream1;
77 TNaming::Print(AName.Type(),aStream1);
80 //TopAbs::Print(AName.ShapeType(),std::cout);
81 Standard_SStream aStream2;
82 TopAbs::Print(AName.ShapeType(),aStream2);
84 const TNaming_ListOfNamedShape& NSS = AName.Arguments();
85 for (TNaming_ListIteratorOfListOfNamedShape it(NSS); it.More(); it.Next()) {
86 TDF_Tool::Entry(it.Value()->Label(),Entry);
87 di << " " << Entry.ToCString();
89 if(!AName.StopNamedShape().IsNull()) {
90 TDF_Tool::Entry(AName.StopNamedShape()->Label(),Entry);
91 di << " Stop " << Entry.ToCString();
95 //=======================================================================
96 //function : SelectShape ou SelectGeometry
97 //purpose : "Select DF entry shape [context [orient]]",
98 //=======================================================================
100 static Standard_Integer DNaming_Select (Draw_Interpretor& di, Standard_Integer n, const char** a)
103 Standard_Boolean geometry = !(strcmp(a[0],"SelectGeometry"));
105 if(!DDF::GetDF(a[1], DF)) return 1;
107 DDF::AddLabel(DF,a[2],L);
108 TNaming_Selector SL (L);
110 TopoDS_Shape S = DBRep::Get(a[3], TopAbs_SHAPE);
111 SL.Select (S, geometry);
114 Standard_Boolean Orient(Standard_False);
115 if(n == 6) Orient = (Draw::Atoi(a[5]) != 0);
116 TopoDS_Shape S = DBRep::Get(a[3], TopAbs_SHAPE);
117 TopoDS_Shape C = DBRep::Get(a[4], TopAbs_SHAPE);
118 SL.Select (S, C, geometry, Orient);
122 di << "DNaming_Select : Error\n";
125 // #define DEB_SELN 1
126 //=======================================================================
127 //function : FillValidMap
129 //=======================================================================
131 Standard_Boolean FillValidMap(const TDF_Label& theLabel, TDF_LabelMap& theValidMap)
133 Standard_Boolean extRefFound = Standard_False;
134 TDF_AttributeMap anExtMap;
135 #ifdef OCCT_DEBUG_SELN
136 TCollection_AsciiString entr1;
137 TDF_Tool::Entry(theLabel, entr1);
138 std::cout<<"\tNaming Attribute at = "<< entr1 <<std::endl;
140 TDF_ChildIterator itr(theLabel, Standard_True);
141 for ( ;itr.More(); itr.Next()) {
142 const TDF_Label& aLabel = itr.Value();
143 Handle(TNaming_Naming) aNaming;
145 aLabel.FindAttribute(TNaming_Naming::GetID(), aNaming);
146 if(aNaming.IsNull()) continue;
147 #ifdef OCCT_DEBUG_SELN
148 TDF_Tool::Entry(aLabel, entr1);
149 std::cout<<"\tNaming Attribute at = "<< entr1 <<std::endl;
151 TDF_Tool::OutReferences(aLabel,anExtMap);
152 for (TDF_MapIteratorOfAttributeMap attMItr(anExtMap);attMItr.More(); attMItr.Next()) {
153 const Handle(TDF_Attribute)& att = attMItr.Key();
154 #ifdef OCCT_DEBUG_SELN
155 TDF_Tool::Entry(att->Label(), entr1);
156 std::cout<<"## References attribute dynamic type = "<<att->DynamicType()<<" at Label = "<<entr1 <<std::endl;
158 if (att->Label().IsDifferent(aLabel) && !att->Label().IsDescendant(theLabel)) {
159 theValidMap.Add(att->Label());
160 Handle(TNaming_NamedShape) aNS;
161 att->Label().FindAttribute(TNaming_NamedShape::GetID(), aNS);
163 TNaming_NamingTool::BuildDescendants(aNS, theValidMap);
164 extRefFound = Standard_True;
171 //=======================================================================
172 //function : SolveSelection
173 //purpose : "SolveSelection DF entry",
174 //=======================================================================
176 static Standard_Integer DNaming_SolveSelection (Draw_Interpretor& di, Standard_Integer n, const char** a)
181 if(!DDF::GetDF(a[1], DF)) return 1;
183 DDF::AddLabel(DF,a[2],L);
185 Handle(TNaming_Naming) naming;
186 if (!L.FindAttribute(TNaming_Naming::GetID(),naming)) {
187 std::cout <<"DNaming__SolveSelection : not a selection" << std::endl;
190 TDF_LabelMap aValidMap;
191 if(!FillValidMap(L,aValidMap))
192 di << "Valid map is empty\n";
193 #ifdef OCCT_DEBUG_SELN
194 std::cout<<"== Valid Label map =="<<std::endl;
195 for (TDF_MapIteratorOfLabelMap mapItr(aValidMap);mapItr.More(); mapItr.Next()) {
196 const TDF_Label& aLab = mapItr.Key();
198 TCollection_AsciiString entr1;
199 TDF_Tool::Entry(aLab, entr1);
200 std::cout<<" Label = "<<entr1 <<std::endl;
204 TNaming_Selector SL (L);
205 Standard_Boolean isSolved = SL.Solve(aValidMap);
207 di << "!!! Solver is failed\n";
208 TopoDS_Shape Res = TNaming_Tool::CurrentShape(SL.NamedShape());
209 Sprintf (name,"%s_%s","new",a[2]);
213 di << "DNaming_SolveSelection : Error\n";
218 //=======================================================================
219 //function : DumpSelection
220 //purpose : DumpSelection DF entry (R)"
221 //=======================================================================
222 static Standard_Integer DNaming_DumpSelection (Draw_Interpretor& di,
223 Standard_Integer n, const char** a)
225 if (n == 3 || n == 4) {
227 if (!DDF::GetDF(a[1],DF)) return 1;
229 if (!DDF::FindLabel(DF,a[2],L)) return 1;
230 Handle(TNaming_Naming) naming;
231 if (!L.FindAttribute(TNaming_Naming::GetID(),naming)) {
232 di <<"DNaming_DumpSelection : not a selection\n";
235 DumpNaming(naming, di);
238 Standard_Integer depth = L.Depth();
239 Standard_Integer curdepth = 0;
240 TCollection_AsciiString Entry;
241 TDF_ChildIterator it (naming->Label(),Standard_True);
242 for (;it.More();it.Next()) {
243 if (it.Value().FindAttribute(TNaming_Naming::GetID(),naming)) {
244 curdepth = (naming->Label().Depth()- depth);
245 for (Standard_Integer i = 1; i <= curdepth; i++) di << " ";
246 TDF_Tool::Entry (naming->Label(),Entry);
247 di << Entry.ToCString() << " ";
248 DumpNaming(naming, di);
255 di << "DNaming_DumpSelection : Error\n";
260 //=======================================================================
261 //function : ArgsSelection
262 //purpose : ArgsSelection DF entry"
263 //=======================================================================
264 static Standard_Integer DNaming_ArgsSelection (Draw_Interpretor& di,
265 Standard_Integer n, const char** a)
269 if (!DDF::GetDF(a[1],DF)) return 1;
271 if (!DDF::FindLabel(DF,a[2],L)) return 1;
272 Handle(TNaming_Naming) naming;
273 if (!L.FindAttribute(TNaming_Naming::GetID(),naming)) {
274 di <<"DNaming_DumpSelection : not a selection\n";
277 TCollection_AsciiString Entry;
278 TNaming_Selector SL (L);
279 di <<" Selection Arguments : ";
280 TDF_AttributeMap args;
282 for (TDF_MapIteratorOfAttributeMap it(args); it.More(); it.Next()) {
283 TDF_Tool::Entry(it.Key()->Label(),Entry);
284 di << Entry.ToCString() << " ";
289 di << "DNaming_ArgsSelection : Error\n";
294 //=======================================================================
295 //function : CollectAttachment
297 //=======================================================================
299 static void CollectAttachment (const TDF_Label& root,
300 const Handle(TNaming_Naming)& naming,
301 TNaming_MapOfNamedShape& attachment)
303 TNaming_ListIteratorOfListOfNamedShape itarg;
304 const TNaming_ListOfNamedShape& args = naming->GetName().Arguments();
305 for (itarg.Initialize(args);itarg.More();itarg.Next()) {
306 if (!itarg.Value()->Label().IsDescendant(root)) attachment.Add(itarg.Value());
308 Handle(TNaming_Naming) subnaming;
309 for (TDF_ChildIterator it(naming->Label(),Standard_True);it.More();it.Next()) {
310 if (it.Value().FindAttribute(TNaming_Naming::GetID(),subnaming)) {
311 const TNaming_ListOfNamedShape& subargs = subnaming->GetName().Arguments();
312 for (itarg.Initialize(subargs);itarg.More();itarg.Next()) {
313 if (!itarg.Value()->Label().IsDescendant(root)) attachment.Add(itarg.Value());
321 //=======================================================================
322 //function : Attachment
323 //purpose : Attachment DF entry"
324 //=======================================================================
326 static Standard_Integer DNaming_Attachment (Draw_Interpretor& di,
332 if (!DDF::GetDF(a[1],DF)) return 1;
334 if (!DDF::FindLabel(DF,a[2],L)) return 1;
335 Handle(TNaming_Naming) naming;
336 TNaming_MapOfNamedShape attachment;
337 if (L.FindAttribute(TNaming_Naming::GetID(),naming)) {
338 CollectAttachment (L,naming,attachment);
341 for (TDF_ChildIterator it (L,Standard_True); it.More();it.Next()) {
342 if (it.Value().FindAttribute(TNaming_Naming::GetID(),naming)) {
343 CollectAttachment (L,naming,attachment);
348 TCollection_AsciiString Entry;
349 TDF_Tool::Entry(L,Entry);
350 di << " Attachment of " << Entry.ToCString();
352 for (TNaming_MapIteratorOfMapOfNamedShape ita (attachment); ita.More(); ita.Next()) {
353 TDF_Tool::Entry (ita.Key()->Label(),Entry);
354 di << Entry.ToCString() << " ";
359 di << "DNaming_Attachment : Error\n";
363 //=======================================================================
364 //function : SelectionCommands
366 //=======================================================================
368 void DNaming::SelectionCommands(Draw_Interpretor& theCommands)
371 static Standard_Boolean done = Standard_False;
373 done = Standard_True;
375 const char* g = "Naming data commands";
377 theCommands.Add("SelectShape",
378 "SelectShape DF entry shape [context [Orient]]",
379 __FILE__, DNaming_Select, g);
381 theCommands.Add("SelectGeometry",
382 "SelectGeometry DF entry shape [context]",
383 __FILE__, DNaming_Select, g);
385 theCommands.Add("DumpSelection",
386 "DumpSelected DF entry",
387 __FILE__, DNaming_DumpSelection, g);
389 theCommands.Add("ArgsSelection",
390 "ArgsSelection DF entry",
391 __FILE__, DNaming_ArgsSelection, g);
393 theCommands.Add("SolveSelection",
394 "DumpSelection DF entry",
395 __FILE__, DNaming_SolveSelection, g);
397 theCommands.Add("Attachment",
398 "Attachment DF entry",
399 __FILE__, DNaming_Attachment, g);