0022972: Eliminate macro definitions that has compiler-provided analogs (WNT and...
[occt.git] / src / DNaming / DNaming_ModelingCommands.cxx
1 // Created on: 2010-01-15
2 // Created by: Sergey ZARITCHNY
3 // Copyright (c) 2010-2014 OPEN CASCADE SAS
4 //
5 // This file is part of Open CASCADE Technology software library.
6 //
7 // This library is free software; you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License version 2.1 as published
9 // by the Free Software Foundation, with special exception defined in the file
10 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11 // distribution for complete text of the license and disclaimer of any warranty.
12 //
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
15
16 #include <Draw.hxx>
17 #include <Draw_Interpretor.hxx>
18 #include <Standard_ErrorHandler.hxx>
19 #include <DNaming.hxx>
20 #include <DBRep.hxx>
21
22 #include <BRepTools.hxx>
23 #include <BRep_Builder.hxx>
24 #include <gp_Trsf.hxx>
25 #include <TopAbs.hxx>
26 #include <TopoDS.hxx>
27 #include <TopoDS_Shape.hxx>
28 #include <TopoDS_Iterator.hxx>
29 #include <TopExp_Explorer.hxx>
30 #include <TopTools_MapOfShape.hxx>
31 #include <TopTools_MapIteratorOfMapOfShape.hxx>
32 #include <TopTools_MapOfOrientedShape.hxx>
33 #include <TopTools_DataMapOfOrientedShapeShape.hxx>
34 #include <TopTools_ListOfShape.hxx>
35 #include <TopTools_ListIteratorOfListOfShape.hxx>
36 #include <TCollection_AsciiString.hxx>
37 #include <Standard_GUID.hxx>
38
39 #include <DDF.hxx>
40 #include <DDocStd.hxx>
41 #include <TDF_TagSource.hxx>
42 #include <TDF_Tool.hxx>
43 #include <TDF_Reference.hxx>
44 #include <TDF_ChildIterator.hxx>
45 #include <TDF_ChildIDIterator.hxx>
46 #include <TDF_MapIteratorOfLabelMap.hxx>
47 #include <TDataStd_Real.hxx>
48 #include <TDataStd_Integer.hxx>
49 #include <TDataStd_TreeNode.hxx>
50 #include <TDataStd_Name.hxx>
51 #include <TDataStd_UAttribute.hxx>
52 #include <TFunction_Function.hxx>
53 #include <TFunction_Logbook.hxx>
54 #include <TFunction_Driver.hxx>
55 #include <TFunction_DriverTable.hxx>
56 #include <TNaming_NamedShape.hxx>
57 #include <TNaming_Selector.hxx>
58 #include <TNaming_Builder.hxx>
59 #include <TNaming_Naming.hxx>
60 #include <TNaming_Name.hxx>
61 #include <TDocStd_Document.hxx>
62
63 #include <DNaming_BoxDriver.hxx>
64 #include <DNaming_CylinderDriver.hxx>
65 #include <DNaming_SelectionDriver.hxx>
66 #include <DNaming_BooleanOperationDriver.hxx>
67 #include <DNaming_FilletDriver.hxx>
68 #include <DNaming_TransformationDriver.hxx>
69 #include <DNaming_PrismDriver.hxx>
70 #include <DNaming_RevolutionDriver.hxx>
71 #include <DNaming_SphereDriver.hxx>
72 #include <DNaming_PointDriver.hxx>
73 #include <DNaming_Line3DDriver.hxx>
74 #ifdef _WIN32
75 #define EXCEPTION ...
76 #else
77 #define EXCEPTION Standard_Failure
78 #endif          
79 #include <ModelDefinitions.hxx>
80 //#define DEBUG
81 //=======================================================================
82 static Handle(TDataStd_UAttribute) AddObject(const Handle(TDocStd_Document)& aDoc)
83 {
84   Handle(TDataStd_TreeNode) aRootNode =  TDataStd_TreeNode::Set(aDoc->Main());
85   const TDF_Label& aLabel = TDF_TagSource::NewChild(aDoc->Main());
86
87   Handle(TDataStd_UAttribute) anObj = TDataStd_UAttribute::Set(aLabel,GEOMOBJECT_GUID);
88   Handle(TDataStd_TreeNode) aNode = TDataStd_TreeNode::Set(aLabel);
89   aRootNode->Append(aNode);
90   return anObj;
91 }
92 //=======================================================================
93 //function : DNaming_AddObject
94 //purpose  : AddObject D [Name]
95 //           - adds new object (UAttribute) under main label
96 //=======================================================================
97 static Standard_Integer  DNaming_AddObject(Draw_Interpretor& di,
98                                               Standard_Integer nb, 
99                                               const char** a)
100 {
101   if(nb > 1) {
102     Handle(TDocStd_Document) aDoc;  
103     if (!DDocStd::GetDocument(a[1],aDoc)) return 1; 
104     Handle(TDataStd_UAttribute) anObj = AddObject (aDoc);
105     if(!anObj.IsNull()) {
106       if(nb == 3) 
107         TDataStd_Name::Set(anObj->Label(), a[2]);    
108       DDF::ReturnLabel(di, anObj->Label());
109       return 0;
110     }
111   }
112   di << "DNaming_AddObject: Error" << "\n";
113   return 1;
114 }
115
116 #include <NCollection_DataMap.hxx>
117 typedef NCollection_DataMap <TCollection_AsciiString, 
118   Standard_GUID> DataMapOfAStringGUID;
119   static Standard_Boolean isBuilt(Standard_False);
120   static DataMapOfAStringGUID aDMap;
121 //=======================================================================
122 static Standard_Boolean GetFuncGUID(Standard_CString aKey,Standard_GUID& GUID)
123 {
124   Standard_Boolean aRes(Standard_False);
125   
126   if(!isBuilt) {
127     aDMap.Bind("PntXYZ",   PNTXYZ_GUID);
128     aDMap.Bind("PntRLT",   PNTRLT_GUID);
129     aDMap.Bind("Line3D",   LINE3D_GUID);
130     aDMap.Bind("Box",      BOX_GUID);
131     aDMap.Bind("Sph",      SPH_GUID);
132     aDMap.Bind("Cyl",      CYL_GUID);
133     aDMap.Bind("Cut",      CUT_GUID);
134     aDMap.Bind("Fuse",     FUSE_GUID);
135     aDMap.Bind("Comm",     COMMON_GUID);
136     aDMap.Bind("Prism",    PRISM_GUID);
137     aDMap.Bind("FulRevol", FULREVOL_GUID);
138     aDMap.Bind("SecRevol", SECREVOL_GUID);
139     aDMap.Bind("PMirr",    PMIRR_GUID);
140     aDMap.Bind("PTxyz",    PTXYZ_GUID);
141     aDMap.Bind("PTALine",  PTALINE_GUID);
142     aDMap.Bind("PRLine",   PRRLINE_GUID);
143     aDMap.Bind("Fillet",   FILLT_GUID);
144     aDMap.Bind("Attach",   ATTCH_GUID);
145     aDMap.Bind("XAttach",  XTTCH_GUID);
146     aDMap.Bind("Section",  SECTION_GUID);
147     isBuilt = Standard_True;
148   }
149
150   if(aDMap.IsBound(aKey)) {
151     GUID = aDMap.Find(aKey);
152     aRes = Standard_True;
153   }
154   return aRes;
155 }
156 //=======================================================================
157 static Handle(TFunction_Driver) GetDriver(const TCollection_AsciiString& name)
158 {
159   Handle(TFunction_Driver) aDrv;
160   if(name == "Box") 
161     aDrv = new DNaming_BoxDriver();
162   else if(name == "Cyl") 
163     aDrv = new DNaming_CylinderDriver();
164   else if(name == "Sph") 
165     aDrv = new DNaming_SphereDriver();
166   else if(name == "Cut") 
167     aDrv = new DNaming_BooleanOperationDriver();
168   else if(name == "Fuse") 
169     aDrv = new DNaming_BooleanOperationDriver();
170   else if(name == "Comm") 
171     aDrv = new DNaming_BooleanOperationDriver();
172   else if(name == "Prism") 
173     aDrv = new DNaming_PrismDriver();
174   else if(name == "FulRevol") 
175     aDrv = new DNaming_RevolutionDriver();
176   else if(name == "SecRevol") 
177     aDrv = new DNaming_RevolutionDriver();
178   else if(name == "PTxyz") 
179     aDrv = new DNaming_TransformationDriver();
180   else if(name == "PTALine") 
181     aDrv = new DNaming_TransformationDriver();
182   else if(name == "PRLine") 
183     aDrv = new DNaming_TransformationDriver();
184   else if(name == "PMirr") 
185     aDrv = new DNaming_TransformationDriver();
186   else if(name == "Fillet") 
187     aDrv = new DNaming_FilletDriver();
188   else if(name == "Attach") 
189     aDrv = new DNaming_SelectionDriver();
190   else if(name == "XAttach") 
191     aDrv = new DNaming_SelectionDriver();
192   else if(name == "PntXYZ") 
193     aDrv = new DNaming_PointDriver();
194   else if(name == "PntRLT") 
195     aDrv = new DNaming_PointDriver();
196   else if(name == "Line3D") 
197     aDrv = new DNaming_Line3DDriver();
198   else if(name == "Section") 
199     aDrv = new DNaming_BooleanOperationDriver();
200   else 
201     cout << "the specified driver is not supported" <<endl;
202   return aDrv;
203 }
204 //=======================================================================
205 //function : DNaming_AddDriver
206 //purpose  : "AddDriver Doc Name1 Name2 ..."
207 //=======================================================================
208 static Standard_Integer DNaming_AddDriver (Draw_Interpretor& /*theDI*/,
209                                         Standard_Integer theNb, 
210                                         const char** theArg)
211 {
212   if (theNb >= 3) {
213     Handle(TDocStd_Document) aDoc;   
214     Standard_CString aDocS(theArg[1]);
215     if (!DDocStd::GetDocument(aDocS, aDoc)) return 1;
216     Handle(TFunction_DriverTable) aFunctionDrvTable = TFunction_DriverTable::Get();
217     for(int i = 2; i < theNb; i++) {
218       Standard_GUID drvGUID;
219       if(!GetFuncGUID(theArg[i],drvGUID)) continue;      
220       aFunctionDrvTable->AddDriver(drvGUID, GetDriver(theArg[i]));
221 #ifdef OCCT_DEBUG
222       cout << "DNaming_AddDriver : " << theArg[i] << " driver is added" <<endl;
223 #endif
224     }
225     return 0;
226   }
227 #ifdef OCCT_DEBUG
228   cout << "DNaming_AddDriver : Error" << endl;
229 #endif
230   return 1;  
231 }
232
233 //=======================================================================
234 static Handle(TFunction_Function) SetFunctionDS(const TDF_Label& objLabel, 
235                                              const Standard_GUID& funGUID)
236 {
237   //set function
238   const TDF_Label& aLabel = TDF_TagSource::NewChild(objLabel);
239   Handle(TFunction_Function) aFun = TFunction_Function::Set(aLabel,funGUID);
240
241   Handle(TDataStd_TreeNode) aNode =  TDataStd_TreeNode::Set(aLabel);
242   Handle(TDataStd_TreeNode) objNode;
243   objLabel.FindAttribute(TDataStd_TreeNode::GetDefaultTreeID(), objNode);
244   if(!objNode.IsNull()) 
245     objNode->Append(aNode);
246   
247   //set function data sub-structure
248   const TDF_Label& aLab1 = TDF_TagSource::NewChild(aLabel);
249   Handle(TDataStd_TreeNode) aNode1 =  TDataStd_TreeNode::Set(aLab1);
250   TDataStd_Name::Set(aLab1, "Arguments");
251   if(!aNode.IsNull()) 
252     aNode->Append(aNode1);
253   
254   const TDF_Label& aLab2 = TDF_TagSource::NewChild(aLabel);
255   Handle(TDataStd_TreeNode) aNode2 =  TDataStd_TreeNode::Set(aLab2);
256   TDataStd_Name::Set(aLab2, "Result");
257   if(!aNode.IsNull()) 
258     aNode->Append(aNode2);
259   return aFun;
260 }
261 //=======================================================================
262 //function : DNaming_AddFunction
263 //purpose  : AddFunction D ObjEntry FunNane 
264 //         :  - adds new function specified by FunName to an object
265 //         :  specified by ObjEntry 
266 //=======================================================================
267 static Standard_Integer  DNaming_AddFunction(Draw_Interpretor& di,
268                                               Standard_Integer nb, 
269                                               const char** a)
270 {
271   if(nb == 4) {
272     Handle(TDocStd_Document) aDoc;  
273     if (!DDocStd::GetDocument(a[1],aDoc)) return 1; 
274     TDF_Label objLabel;
275     if (!DDF::FindLabel(aDoc->GetData(),a[2], objLabel)) return 1;
276
277     Handle(TDataStd_UAttribute) anObj;
278     if(!objLabel.FindAttribute(GEOMOBJECT_GUID, anObj)) {
279       di << "DNaming_AddFunction: Object is not found at the specified label: "<< a[2] << "\n";
280       return 1;
281     }
282     Standard_GUID funGUID;
283     if(!GetFuncGUID(a[3],funGUID)) {
284       di << "DNaming_AddFunction: the specified function is not supported: "<< a[3] << "\n";
285       return 1;
286     }
287     Handle(TFunction_Function) aFun = SetFunctionDS(objLabel, funGUID);
288     if(!aFun.IsNull()) {
289       TCollection_AsciiString aFName = TCollection_AsciiString(a[3]) + "_Function";
290       TDataStd_Name::Set(aFun->Label(), aFName);
291       TDF_Reference::Set(objLabel, aFun->Label().FindChild(FUNCTION_RESULT_LABEL)); //result is here
292       DDF::ReturnLabel(di, aFun->Label());
293       return 0;
294     }
295   }
296   di << "DNaming_AddObject: Error" << "\n";
297   return 1;
298 }
299
300
301 //=======================================================================
302 //function : DNaming_AddBox
303 //purpose  : "AddBox Doc dx dy dz"
304 //=======================================================================
305 static Standard_Integer DNaming_AddBox (Draw_Interpretor& theDI,
306                                          Standard_Integer theNb, 
307                                          const char** theArg)
308 {
309   if (theNb >= 4) {
310
311     Handle(TDocStd_Document) aDocument;
312     Standard_CString aDocS(theArg[1]);
313     if (!DDocStd::GetDocument(aDocS, aDocument)) return 1;
314     Handle(TDataStd_UAttribute) anObj = AddObject(aDocument);
315     if(anObj.IsNull()) return 1;
316     Standard_GUID funGUID;
317     if(!GetFuncGUID("Box",funGUID)) return 1;
318
319     Handle(TFunction_Function) aFun = SetFunctionDS(anObj->Label(), funGUID);
320     if(aFun.IsNull()) return 1;
321     TDataStd_Name::Set(aFun->Label(), "Box_Function");
322     
323     TDF_Reference::Set(anObj->Label(), aFun->Label().FindChild(FUNCTION_RESULT_LABEL)); //result is here 
324     Standard_Real dx,dy,dz;
325     dx = Draw::Atof(theArg[2]);
326     dy = Draw::Atof(theArg[3]);
327     dz = Draw::Atof(theArg[4]);
328
329     DNaming::GetReal(aFun,BOX_DX)->Set(dx);
330     DNaming::GetReal(aFun,BOX_DY)->Set(dy);
331     DNaming::GetReal(aFun,BOX_DZ)->Set(dz);
332     
333     DDF::ReturnLabel(theDI, anObj->Label());
334     return 0;
335   }
336   cout << "DNaming_AddBox : Error" << endl;
337   return 1;  
338 }
339
340 //=======================================================================
341 static Handle(TFunction_Function) GetFunction(const TDF_Label& objLabel, const Standard_GUID& funGUID) {
342   Handle(TFunction_Function) aFun;
343   Handle(TDataStd_TreeNode) aNode;
344   objLabel.FindAttribute(TDataStd_TreeNode::GetDefaultTreeID(), aNode);
345   if(aNode.IsNull()) return aFun;
346   if(!aNode->HasFirst()) return aFun;
347   else 
348     aNode = aNode->First();
349   while(!aNode.IsNull()) {      
350     if(aNode->FindAttribute(TFunction_Function::GetID(), aFun)) {
351       const Standard_GUID& aGUID = aFun->GetDriverGUID();
352       if(aGUID == funGUID) break;
353       else aFun.Nullify();
354     }
355     aNode = aNode->Next();
356   }
357   return aFun; 
358 }
359 //=======================================================================
360 //function : DNaming_BoxDX
361 //purpose  : "BoxDX Doc BoxLabel NewDX"
362 //=======================================================================
363
364 static Standard_Integer DNaming_BoxDX (Draw_Interpretor& theDI,
365                                         Standard_Integer theNb, 
366                                         const char** theArg)
367 {
368   if (theNb == 4) {
369     Handle(TDocStd_Document) aDoc;   
370     Standard_CString aDocS(theArg[1]);
371     if (!DDocStd::GetDocument(aDocS, aDoc)) return 1;
372     TDF_Label objLabel;
373     if (!DDF::FindLabel(aDoc->GetData(),theArg[2], objLabel)) return 1;
374
375     Handle(TDataStd_UAttribute) anObj;
376     if(!objLabel.FindAttribute(GEOMOBJECT_GUID, anObj)) return 1;       
377     Standard_GUID funGUID;
378     if(!GetFuncGUID("Box",funGUID)) return 1;       
379
380     Handle(TFunction_Function) aFun = GetFunction(objLabel,funGUID);
381     if(!aFun.IsNull()) {
382       Standard_Real value = Draw::Atof(theArg[3]);
383       DNaming::GetReal(aFun,BOX_DX)->Set(value);
384       DDF::ReturnLabel(theDI, DNaming::GetReal(aFun,BOX_DX)->Label());
385       return 0;
386     }
387   }
388   cout << "DNaming_BoxDX : Error" << endl;
389   return 1;  
390 }
391
392 //=======================================================================
393 //function : DNaming_BoxDY
394 //purpose  : "BoxDY Doc BoxLabel NewDY"
395 //=======================================================================
396
397 static Standard_Integer DNaming_BoxDY (Draw_Interpretor& theDI,
398                                         Standard_Integer theNb, 
399                                         const char** theArg)
400 {
401   if (theNb == 4) {
402     Handle(TDocStd_Document) aDoc;   
403     Standard_CString aDocS(theArg[1]);
404     if (!DDocStd::GetDocument(aDocS, aDoc)) return 1;
405     TDF_Label objLabel;
406     if (!DDF::FindLabel(aDoc->GetData(),theArg[2], objLabel)) return 1;
407     
408     Handle(TDataStd_UAttribute) anObj;
409     if(!objLabel.FindAttribute(GEOMOBJECT_GUID, anObj)) return 1;       
410     Standard_GUID funGUID;
411     if(!GetFuncGUID("Box",funGUID)) return 1;       
412     
413     Handle(TFunction_Function) aFun = GetFunction(objLabel,funGUID);
414     if(!aFun.IsNull()) {
415       Standard_Real value = Draw::Atof(theArg[3]);
416       DNaming::GetReal(aFun,BOX_DY)->Set(value);
417       DDF::ReturnLabel(theDI, DNaming::GetReal(aFun,BOX_DY)->Label());
418       return 0;
419     }
420   }
421   cout << "DNaming_BoxDY : Error" << endl;
422   return 1;  
423 }
424
425 //=======================================================================
426 //function : DNaming_BoxDZ
427 //purpose  : "BoxDZ Doc BoxLabel NewDZ"
428 //=======================================================================
429
430 static Standard_Integer DNaming_BoxDZ (Draw_Interpretor& theDI,
431                                         Standard_Integer theNb, 
432                                         const char** theArg)
433 {
434   if (theNb == 4) {
435     Handle(TDocStd_Document) aDoc;   
436     Standard_CString aDocS(theArg[1]);
437     if (!DDocStd::GetDocument(aDocS, aDoc)) return 1;
438     TDF_Label objLabel;
439     if (!DDF::FindLabel(aDoc->GetData(),theArg[2], objLabel)) return 1;
440     
441     Handle(TDataStd_UAttribute) anObj;
442     if(!objLabel.FindAttribute(GEOMOBJECT_GUID, anObj)) return 1;       
443     Standard_GUID funGUID;
444     if(!GetFuncGUID("Box",funGUID)) return 1;       
445     
446     Handle(TFunction_Function) aFun = GetFunction(objLabel,funGUID);
447     if(!aFun.IsNull()) {
448       Standard_Real value = Draw::Atof(theArg[3]);
449       DNaming::GetReal(aFun,BOX_DZ)->Set(value);
450       DDF::ReturnLabel(theDI, DNaming::GetReal(aFun,BOX_DZ)->Label());
451       return 0;
452     }
453   }
454   cout << "DNaming_BoxDZ : Error" << endl;
455   return 1;  
456 }
457
458 //=======================================================================
459 static TFunction_Logbook& GetLogBook ()
460 {
461   static TFunction_Logbook myLog;
462   return myLog;
463 }
464 //=======================================================================
465 //function : Compute
466 //purpose  : Performs the calling to a driver with the given Function ID.
467 //=======================================================================
468 static Standard_Integer ComputeFunction(const Handle(TFunction_Function)& theFun, 
469                                         TFunction_Logbook& theLog)
470 {
471   Handle(TFunction_DriverTable) aTable = TFunction_DriverTable::Get();
472   Handle(TFunction_Driver) aDriver;
473   Standard_Integer aRes(-1);
474   if (aTable->FindDriver(theFun->GetDriverGUID(),aDriver))
475       {
476         aDriver->Init(theFun->Label());
477         aRes = aDriver->Execute(theLog);
478       }
479   else
480     aRes = 1;
481   return aRes;
482 }
483
484
485 //=======================================================================
486 //function : DNaming_SolveFlatFrom
487 //purpose  : Performs sequential recomputation of all Aux Object starting from the specified.
488 //         : "SolveFlatFrom Doc FistAuxObjLabel"
489 //=======================================================================
490
491 static Standard_Integer DNaming_SolveFlatFrom (Draw_Interpretor& /*theDI*/,
492                                         Standard_Integer theNb, 
493                                         const char** theArg)
494 {
495   if (theNb == 3) {
496     Handle(TDocStd_Document) aDoc;   
497     Standard_CString aDocS(theArg[1]);
498     if (!DDocStd::GetDocument(aDocS, aDoc)) return 1;
499     TDF_Label ObjLabel;
500     if (!DDF::FindLabel(aDoc->GetData(),theArg[2], ObjLabel)) return 1;
501     const TDF_Label& FatherLab = ObjLabel.Father();
502     if(FatherLab.IsNull())
503       goto ERR;
504     TCollection_AsciiString entry;   
505     TDF_Tool::Entry(FatherLab, entry);
506 #ifdef OCCT_DEBUG
507     cout << "DNaming_SolveFlatFrom: Father label = " << entry << endl;
508 #endif
509     Standard_Boolean found(Standard_False);
510     TDF_ChildIterator it(FatherLab, Standard_False);  
511     for(;it.More(); it.Next()) {
512       const TDF_Label& aLabel = it.Value();
513       if(!found) {
514         if(aLabel == ObjLabel) {
515           found = Standard_True;        
516         } else 
517           continue;
518       }
519       const TDF_Label& funLabel = aLabel.FindChild(FUNCTION_ARGUMENTS_LABEL, Standard_True);
520       Handle(TFunction_Function) aFun;
521       funLabel.FindAttribute(TFunction_Function::GetID(), aFun);
522       if(aFun.IsNull()) {
523         cout << "DNaming_SolveFlatFrom:: Null function is found!" << endl;
524         continue;
525       } 
526       else {
527         TDF_Tool::Entry(funLabel, entry);
528         try {
529           Standard_Integer aRes = ComputeFunction(aFun, GetLogBook());
530           if(aRes != 0) {
531             cout << "DNaming_SolveFlatFrom: Driver failed at label = " << entry << endl;
532             return 1;
533           }
534 #ifdef OCCT_DEBUG
535           cout <<"DNaming_SolveFlatFrom : function from label " << entry << " is recomputed" << endl;
536 #endif
537         } catch (EXCEPTION) {
538           cout <<"DNaming_SolveFlatFrom : Exception computing function at label " << entry << endl;
539         }
540       }
541     }
542     return 0;
543   }   
544  ERR:
545   cout << "DNaming_SolveFlatFrom : Error" << endl;
546   return 1;  
547 }
548
549 //=======================================================================
550 //function : DNaming_InitLogBook
551 //purpose  : "InitLogBook Doc "
552 //=======================================================================
553 static Standard_Integer DNaming_InitLogBook (Draw_Interpretor& /*theDI*/,
554                                              Standard_Integer theNb, 
555                                              const char** theArg)
556 {
557   if (theNb == 2) {
558     Handle(TDocStd_Document) aDoc;   
559     Standard_CString aDocS(theArg[1]);
560     if (!DDocStd::GetDocument(aDocS, aDoc)) return 1;  
561     if(GetLogBook().IsEmpty()) {
562 #ifdef OCCT_DEBUG
563       cout << "DNaming_InitLogBook : is empty" <<endl;
564 #endif
565     }
566     else {
567       GetLogBook().Clear();
568 #ifdef OCCT_DEBUG
569       cout << "DNaming_InitLogBook : cleaned" <<endl;
570 #endif
571     }
572     return 0;
573   }
574   cout << "DNaming_InitLogBook : Error - No document ==> " <<theNb <<endl;
575   return 1;  
576 }
577
578 //=======================================================================
579 //function : DNaming_CheckLogBook
580 //purpose  : "CheckLogBook Doc"
581 //=======================================================================
582 static Standard_Integer DNaming_CheckLogBook (Draw_Interpretor& /*theDI*/,
583                                              Standard_Integer theNb, 
584                                              const char** theArg)
585 {
586   if (theNb == 2) {
587     Handle(TDocStd_Document) aDoc;   
588     Standard_CString aDocS(theArg[1]);
589     if (!DDocStd::GetDocument(aDocS, aDoc)) return 1;  
590     if(GetLogBook().IsEmpty())
591       cout << "DNaming_CheckLogBook : is empty" <<endl;
592     else {
593       TDF_LabelMap aMap;
594       aMap = GetLogBook().GetValid();
595       TDF_MapIteratorOfLabelMap it(aMap);
596       TCollection_AsciiString entry;
597       cout << "DNaming_CheckLogBook : LogBook current state:" <<endl;
598       for (;it.More();it.Next()) {
599         TDF_Tool::Entry(it.Key(), entry);
600         cout << entry <<endl;
601       }
602     }
603     return 0;
604   }
605   cout << "DNaming_CheckLogBook : Error - No document ==> " <<theNb <<endl;
606   return 1;  
607 }
608
609 //=======================================================================
610 //function : DNaming_ComputeFun
611 //purpose  : "ComputeFun Doc FunctionLabel"
612 //=======================================================================
613 static Standard_Integer DNaming_ComputeFun (Draw_Interpretor& /*theDI*/,
614                                         Standard_Integer theNb, 
615                                         const char** theArg)
616 {
617   if (theNb == 3) {
618     Handle(TDocStd_Document) aDoc;   
619     Standard_CString aDocS(theArg[1]);
620     if (!DDocStd::GetDocument(aDocS, aDoc)) return 1;
621     TDF_Label funLabel;
622     if (!DDF::FindLabel(aDoc->GetData(),theArg[2], funLabel)) return 1;
623     
624     Handle(TFunction_Function) aFun;
625     funLabel.FindAttribute(TFunction_Function::GetID(), aFun);
626     if(aFun.IsNull()) return 1;
627     if(!aFun.IsNull()) {
628       Standard_Integer aRes = ComputeFunction(aFun, GetLogBook());
629       if(aRes != 0) {
630          cout << "DNaming_ComputeFun : No Driver or Driver failed" << endl;
631          return 1;
632        }
633 #ifdef OCCT_DEBUG
634       cout <<"DNaming_ComputeFun : function from label " << theArg[2] << " is recomputed" << endl;
635 #endif
636       return 0;
637     }
638   }
639   cout << "DNaming_ComputeFun : Error" << endl;
640   return 1;  
641 }
642
643 //=======================================================================
644 //function : DNaming_AttachShape
645 //purpose  : "AttachShape Doc Shape Context [Container [KeepOrientation [Geometry]]]"
646 //=======================================================================
647 static Standard_Integer DNaming_AttachShape (Draw_Interpretor& di,
648                                             Standard_Integer nb, 
649                                             const char** a)
650 {
651   if (nb >= 4) {
652     Handle(TDocStd_Document) aDoc;   
653     Standard_CString aDocS(a[1]);
654     if (!DDocStd::GetDocument(aDocS, aDoc)) return 1;
655     Standard_CString aSS(a[2]);
656     const TopoDS_Shape& aShape = DBRep::Get(aSS); //shape to be attached
657     if(aShape.IsNull()) return 1;
658
659     Handle(TDataStd_UAttribute) aContainer, aContext; 
660     if (!DDocStd::Find(aDoc, a[3], GEOMOBJECT_GUID, aContext)) return 1;
661     if(nb > 4) 
662       DDocStd::Find(aDoc, a[4], GEOMOBJECT_GUID, aContainer);
663
664     if(aContainer.IsNull()) aContainer = aContext;
665
666     Handle(TDataStd_UAttribute) anObj = AddObject (aDoc);
667     if(!anObj.IsNull()) {
668       Handle(TDataStd_TreeNode) aNode,RNode;
669       anObj->Label().FindAttribute(TDataStd_TreeNode::GetDefaultTreeID(), aNode);
670       if(aNode.IsNull())
671         aNode = TDataStd_TreeNode::Set(anObj->Label());
672       aNode->Remove();
673       if(aContainer->Label().FindAttribute(TDataStd_TreeNode::GetDefaultTreeID(), RNode))
674         RNode->Append(aNode);
675       TDataStd_Name::Set(anObj->Label(), "Auxiliary_Object");
676       Standard_GUID funGUID;
677       if(GetFuncGUID("Attach",funGUID)) {
678         Handle(TFunction_Function) aFun = SetFunctionDS(anObj->Label(), funGUID);
679         if(!aFun.IsNull()) {
680           TDataStd_Name::Set(aFun->Label(), "ISelection");        
681           TDF_Label aResultLabel =  aFun->Label().FindChild(FUNCTION_RESULT_LABEL, Standard_True); 
682           TDF_Reference::Set(anObj->Label(), aResultLabel ); //result of the object
683           aResultLabel.ForgetAllAttributes(Standard_True);
684           Standard_Boolean aKeepOrientation(Standard_False);
685           if (nb >= 6) 
686             aKeepOrientation = (Standard_Boolean) Draw::Atoi(a[5]);
687           Standard_Boolean aGeometry(Standard_False);
688           if (nb == 7) 
689             aGeometry = (Standard_Boolean) Draw::Atoi(a[6]);
690           Handle(TNaming_NamedShape) aCont =  DNaming::GetObjectValue(aContext);
691 #ifdef OCCT_DEBUG
692           if(aCont.IsNull() || aCont->IsEmpty())
693             cout <<"Wrong Context ..." <<endl;
694 #endif
695           try{
696             TopoDS_Shape aCONTEXT = aCont->Get();       
697             TNaming_Selector aSelector(aResultLabel);
698             if(!aSelector.Select(aShape, aCONTEXT, aGeometry, aKeepOrientation))
699               return 1;
700           }
701           catch (Standard_Failure) {
702             cout << "EXCEPTION: SELECTION_IMPOSSIBLE" <<endl;
703           }
704     
705           if(!aCont.IsNull()) {
706 #ifdef OCCT_DEBUG
707             TCollection_AsciiString entry;
708             TDF_Tool::Entry(aCont->Label(), entry);
709             cout << "ContextNS Label = " << entry <<endl;
710 #endif
711             Handle(TFunction_Function) aCntFun;
712             if(aCont->Label().Father().FindAttribute(TFunction_Function::GetID(), aCntFun)) { //Fun:2 ==> result
713               // First argument of Selection function refers to father function (of context object) 
714               // to which selection is attached (because sel obj. itself already has refrence to result NS
715               TDF_Reference::Set(aFun->Label().FindChild(FUNCTION_ARGUMENTS_LABEL).FindChild(ATTACH_ARG), 
716                                  aCntFun->Label()); //ref to function produced Context shape
717               
718             }
719           }
720           
721           DDF::ReturnLabel(di, anObj->Label());
722           return 0;
723         }
724       }
725     } //###
726   }
727   cout << "DNaming_AttachShape : Error" << endl;
728   return 1;  
729 }
730
731 //=======================================================================
732 //function : DNaming_XAttachShape
733 //purpose  : "AttachShape Doc Shape Context [KeepOrientation [Geometry]]"
734 //=======================================================================
735 static Standard_Integer DNaming_XAttachShape (Draw_Interpretor& di,
736                                             Standard_Integer nb, 
737                                             const char** a)
738 {
739   if (nb >= 4) {
740     Handle(TDocStd_Document) aDoc;   
741     Standard_CString aDocS(a[1]);
742     if (!DDocStd::GetDocument(aDocS, aDoc)) return 1;
743     Standard_CString aSS(a[2]);
744     const TopoDS_Shape& aShape = DBRep::Get(aSS);
745     if(aShape.IsNull()) return 1;
746
747     Handle(TDataStd_UAttribute) aContext; 
748     if (!DDocStd::Find(aDoc, a[3], GEOMOBJECT_GUID, aContext)) return 1;
749     
750     Handle(TDataStd_UAttribute) anObj = AddObject (aDoc);
751     if(!anObj.IsNull()) {
752       TDataStd_Name::Set(anObj->Label(), "Auxiliary_Object");
753       Standard_GUID funGUID;
754       if(GetFuncGUID("XAttach",funGUID)) {
755         Handle(TFunction_Function) aFun = SetFunctionDS(anObj->Label(), funGUID);
756         if(!aFun.IsNull()) {
757           TDataStd_Name::Set(aFun->Label(), "XSelection");        
758           TDF_Label aResultLabel =  aFun->Label().FindChild(FUNCTION_RESULT_LABEL, Standard_True); 
759           TDF_Reference::Set(anObj->Label(), aResultLabel ); //result
760           aResultLabel.ForgetAllAttributes(Standard_True);
761           Standard_Boolean aKeepOrientation(Standard_False);
762           if (nb >= 5) 
763             aKeepOrientation = (Standard_Boolean) Draw::Atoi(a[4]);
764           Standard_Boolean aGeometry(Standard_False);
765           if (nb == 6) 
766             aGeometry = (Standard_Boolean) Draw::Atoi(a[5]);
767           Handle(TNaming_NamedShape) aCont =  DNaming::GetObjectValue(aContext);
768
769           if(aCont.IsNull() || aCont->IsEmpty())
770             cout <<"Wrong Context ..." <<endl;
771           else {
772             TopoDS_Shape aCONTEXT = aCont->Get();
773             try{
774               TNaming_Selector aSelector(aResultLabel);
775               if(!aSelector.Select(aShape, aCONTEXT, aGeometry, aKeepOrientation))
776                 return 1;
777             }
778             catch (Standard_Failure) {
779               cout << "EXCEPTION: SELECTION_IMPOSSIBLE" <<endl;
780             }
781     
782             TDF_Reference::Set(aFun->Label().FindChild(FUNCTION_ARGUMENTS_LABEL).FindChild(ATTACH_ARG), 
783                                aContext->Label()); //ref to Context object
784
785             DDF::ReturnLabel(di, anObj->Label());
786             return 0;
787           }       
788         }
789       }
790     }
791   }
792   cout << "DNaming_XAttachShape : Error" << endl;
793   return 1;  
794 }
795
796 //=======================================================================
797 //function : DNaming_AddCyl
798 //purpose  : "AddCyl Doc Radius Height Axis
799 //=======================================================================
800 static Standard_Integer DNaming_AddCylinder (Draw_Interpretor& theDI,
801                                              Standard_Integer theNb, 
802                                              const char** theArg)
803 {
804   if (theNb == 5) {
805
806     Handle(TDocStd_Document) aDocument;
807     Standard_CString aDocS(theArg[1]);
808     if (!DDocStd::GetDocument(aDocS, aDocument)) return 1;
809     Handle(TDataStd_UAttribute) anObj = AddObject(aDocument);
810     if(anObj.IsNull()) return 1;
811     Standard_GUID funGUID;
812     if(!GetFuncGUID("Cyl",funGUID)) return 1;
813
814     Handle(TFunction_Function) aFun = SetFunctionDS(anObj->Label(), funGUID);
815     if(aFun.IsNull()) return 1;
816     TDataStd_Name::Set(aFun->Label(), "Cyl_Function");
817     
818     TDF_Reference::Set(anObj->Label(), aFun->Label().FindChild(FUNCTION_RESULT_LABEL)); //result is here 
819
820     Standard_Real aR, aH;
821     aR = Draw::Atof(theArg[2]);
822     aH = Draw::Atof(theArg[3]);
823
824     Handle(TDataStd_UAttribute) Axis; 
825     if (!DDocStd::Find(aDocument, theArg[4], GEOMOBJECT_GUID, Axis)) return 1;
826     DNaming::GetReal(aFun,CYL_RADIUS)->Set(aR);
827     DNaming::GetReal(aFun,CYL_HEIGHT)->Set(aH);
828     DNaming::SetObjectArg(aFun, CYL_AXIS, Axis); 
829     
830     DDF::ReturnLabel(theDI, anObj->Label());
831     return 0;
832   }
833   cout << "DNaming_AddCylinder : Error" << endl;
834   return 1;  
835 }
836
837 //=======================================================================
838 //function : DNaming_CylRad
839 //purpose  : "CylRad Doc CylLabel NewRad"
840 //=======================================================================
841
842 static Standard_Integer DNaming_CylRad (Draw_Interpretor& theDI,
843                                         Standard_Integer theNb, 
844                                         const char** theArg)
845 {
846   if (theNb == 4) {
847     Handle(TDocStd_Document) aDoc;   
848     Standard_CString aDocS(theArg[1]);
849     if (!DDocStd::GetDocument(aDocS, aDoc)) return 1;
850     TDF_Label objLabel;
851     if (!DDF::FindLabel(aDoc->GetData(),theArg[2], objLabel)) return 1;
852     
853     Handle(TDataStd_UAttribute) anObj;
854     if(!objLabel.FindAttribute(GEOMOBJECT_GUID, anObj)) return 1;       
855     Standard_GUID funGUID;
856     if(!GetFuncGUID("Cyl",funGUID)) return 1;       
857     
858     Handle(TFunction_Function) aFun = GetFunction(objLabel,funGUID);
859     if(!aFun.IsNull()) {
860       Standard_Real value = Draw::Atof(theArg[3]);
861       DNaming::GetReal(aFun,CYL_RADIUS)->Set(value);
862       DDF::ReturnLabel(theDI, DNaming::GetReal(aFun,CYL_RADIUS)->Label());
863       return 0;
864     }
865   }
866   cout << "DNaming_CylRadius : Error" << endl;
867   return 1;  
868 }
869
870 //=======================================================================
871 //function : DNaming_AddFuse
872 //purpose  : "AddFuse Doc Object Tool"
873 //=======================================================================
874
875  static Standard_Integer DNaming_AddFuse (Draw_Interpretor& theDI,
876                                           Standard_Integer theNb, 
877                                           const char** theArg)
878  {
879    if (theNb == 4) {
880
881     Handle(TDocStd_Document) aDocument;
882     Standard_CString aDocS(theArg[1]);
883     if (!DDocStd::GetDocument(aDocS, aDocument)) return 1;
884
885     Handle(TDataStd_UAttribute) anObject, aTool;
886     if (!DDocStd::Find(aDocument, theArg[2], GEOMOBJECT_GUID, anObject)) return 1;
887     if (!DDocStd::Find(aDocument, theArg[3], GEOMOBJECT_GUID, aTool)) return 1;
888     Standard_GUID funGUID;
889     if(!GetFuncGUID("Fuse",funGUID)) return 1;
890     Handle(TFunction_Function) aFun = SetFunctionDS(anObject->Label(), funGUID);
891     if(aFun.IsNull()) return 1;
892     TDataStd_Name::Set(aFun->Label(), "Fuse");
893     
894     TDF_Reference::Set(anObject->Label(), aFun->Label().FindChild(FUNCTION_RESULT_LABEL)); //result is here 
895     DNaming::SetObjectArg(aFun, BOOL_TOOL, aTool); 
896     DDF::ReturnLabel(theDI, aFun->Label());
897     return 0;
898   }
899    cout << "DModel_AddFuse : Error" << endl;
900    return 1;  
901  }
902
903
904 //=======================================================================
905 //function : DNaming_AddCut
906 //purpose  : "AddCut Doc Object Tool"
907 //=======================================================================
908
909  static Standard_Integer DNaming_AddCut (Draw_Interpretor& theDI,
910                                           Standard_Integer theNb, 
911                                           const char** theArg)
912  {
913    if (theNb == 4) {
914
915     Handle(TDocStd_Document) aDocument;   
916     Standard_CString aDocS(theArg[1]);
917     if (!DDocStd::GetDocument(aDocS, aDocument)) return 1;
918
919     Handle(TDataStd_UAttribute) anObject, aTool;
920     if (!DDocStd::Find(aDocument, theArg[2], GEOMOBJECT_GUID, anObject)) return 1;
921     if (!DDocStd::Find(aDocument, theArg[3], GEOMOBJECT_GUID, aTool)) return 1;
922     Standard_GUID funGUID;
923     if(!GetFuncGUID("Cut",funGUID)) return 1;
924     Handle(TFunction_Function) aFun = SetFunctionDS(anObject->Label(), funGUID);
925     if(aFun.IsNull()) return 1;
926     TDataStd_Name::Set(aFun->Label(), "Cut");
927     
928     TDF_Reference::Set(anObject->Label(), aFun->Label().FindChild(FUNCTION_RESULT_LABEL)); //result is here 
929     DNaming::SetObjectArg(aFun, BOOL_TOOL, aTool); 
930     DDF::ReturnLabel(theDI, aFun->Label());
931     return 0;
932   }
933    cout << "DModel_AddCut : Error" << endl;
934    return 1;  
935  }
936
937 //=======================================================================
938 //function : DNaming_AddCommon
939 //purpose  : "AddCommon Doc Object Tool"
940 //=======================================================================
941
942  static Standard_Integer DNaming_AddCommon (Draw_Interpretor& theDI,
943                                           Standard_Integer theNb, 
944                                           const char** theArg)
945  {
946    if (theNb == 4) {
947
948     Handle(TDocStd_Document) aDocument;   
949     Standard_CString aDocS(theArg[1]);
950     if (!DDocStd::GetDocument(aDocS, aDocument)) return 1;
951
952     Handle(TDataStd_UAttribute) anObject, aTool;
953     if (!DDocStd::Find(aDocument, theArg[2], GEOMOBJECT_GUID, anObject)) return 1;
954     if (!DDocStd::Find(aDocument, theArg[3], GEOMOBJECT_GUID, aTool)) return 1;
955     Standard_GUID funGUID;
956     if(!GetFuncGUID("Comm",funGUID)) return 1;
957     Handle(TFunction_Function) aFun = SetFunctionDS(anObject->Label(), funGUID);
958     if(aFun.IsNull()) return 1;
959     TDataStd_Name::Set(aFun->Label(), "Common");
960     
961     TDF_Reference::Set(anObject->Label(), aFun->Label().FindChild(FUNCTION_RESULT_LABEL)); //result is here 
962     DNaming::SetObjectArg(aFun, BOOL_TOOL, aTool); 
963     DDF::ReturnLabel(theDI, aFun->Label());
964     return 0;
965   }
966    cout << "DModel_AddComm : Error" << endl;
967    return 1;  
968  }
969
970 //=======================================================================
971 //function : DNaming_AddSection
972 //purpose  : "AddSection Doc Object Tool"
973 //=======================================================================
974
975  static Standard_Integer DNaming_AddSection (Draw_Interpretor& theDI,
976                                                      Standard_Integer theNb, const char** theArg)
977  {
978    if (theNb == 4) {
979
980     Handle(TDocStd_Document) aDocument;   
981     Standard_CString aDocS(theArg[1]);
982     if (!DDocStd::GetDocument(aDocS, aDocument)) return 1;
983
984     Handle(TDataStd_UAttribute) anObject, aTool;
985     if (!DDocStd::Find(aDocument, theArg[2], GEOMOBJECT_GUID, anObject)) return 1;
986     if (!DDocStd::Find(aDocument, theArg[3], GEOMOBJECT_GUID, aTool)) return 1;
987     Standard_GUID funGUID;
988     if(!GetFuncGUID("Section",funGUID)) return 1;
989     Handle(TFunction_Function) aFun = SetFunctionDS(anObject->Label(), funGUID);
990     if(aFun.IsNull()) return 1;
991     TDataStd_Name::Set(aFun->Label(), "Section");
992     
993     TDF_Reference::Set(anObject->Label(), aFun->Label().FindChild(FUNCTION_RESULT_LABEL)); //result is here 
994     DNaming::SetObjectArg(aFun, BOOL_TOOL, aTool); 
995     DDF::ReturnLabel(theDI, aFun->Label());
996     return 0;
997   }
998    cout << "DModel_AddSection : Error" << endl;
999    return 1;  
1000  }
1001
1002 //=======================================================================
1003 //function : DNaming_AddFillet
1004 //purpose  : "AddFillet Doc Object Radius Path "
1005 //=======================================================================
1006 static Standard_Integer DNaming_AddFillet (Draw_Interpretor& theDI,
1007                                            Standard_Integer theNb, 
1008                                            const char** theArg)
1009 {
1010   if (theNb < 5) {
1011     cout<<"DNaming_AddFillet(): Wrong number of arguments"<<endl;
1012     return 1;
1013   }
1014
1015   Handle(TDocStd_Document) aDocument;   
1016   Standard_CString aDocS(theArg[1]);
1017   if (!DDocStd::GetDocument(aDocS, aDocument)) return 1;
1018
1019   Handle(TDataStd_UAttribute) anObject;
1020   if (!DDocStd::Find(aDocument, theArg[2], GEOMOBJECT_GUID, anObject)) return 1;
1021   Standard_GUID funGUID;
1022   if(!GetFuncGUID("Fillet",funGUID)) return 1;
1023   Handle(TFunction_Function) aFun = SetFunctionDS(anObject->Label(), funGUID);
1024   if(aFun.IsNull()) return 1;
1025   TDataStd_Name::Set(aFun->Label(), "Fillet");
1026
1027   TDF_Reference::Set(anObject->Label(), aFun->Label().FindChild(FUNCTION_RESULT_LABEL)); //result is here 
1028
1029   Standard_Real aRadius = Draw::Atof(theArg[3]);
1030   DNaming::GetReal(aFun,FILLET_RADIUS)->Set(aRadius);  
1031
1032   Handle(TDataStd_UAttribute) aPath;
1033   if (!DDocStd::Find(aDocument, theArg[4], GEOMOBJECT_GUID, aPath)) return 1;
1034   DNaming::SetObjectArg(aFun, FILLET_PATH, aPath); 
1035   DDF::ReturnLabel(theDI, aFun->Label());
1036   return 0;
1037 }
1038
1039 //=======================================================================
1040 //function : DNaming_TranslatePar
1041 //purpose  : "TranslatePar Doc ShapeEntry dx dy dz"
1042 //=======================================================================
1043 static Standard_Integer DNaming_PTranslateDXYZ (Draw_Interpretor& di,
1044                                                 Standard_Integer nb, 
1045                                                 const char** a)
1046 {
1047   if (nb > 3) {
1048 #ifdef OCCT_DEBUG
1049     cout << "NB = " << nb <<endl;
1050 #endif
1051     Handle(TDocStd_Document) aDocument;   
1052     Standard_CString aDocS(a[1]);
1053     if (!DDocStd::GetDocument(aDocS, aDocument)) return 1; 
1054     
1055     Handle(TDataStd_UAttribute) anObject;// aShape;
1056     if (!DDocStd::Find(aDocument, a[2], GEOMOBJECT_GUID, anObject)) return 1;
1057
1058     Standard_GUID funGUID;
1059     if(!GetFuncGUID("PTxyz",funGUID)) return 1;
1060     Handle(TFunction_Function) aFun = SetFunctionDS(anObject->Label(), funGUID);
1061     if(aFun.IsNull()) return 1;
1062     TDataStd_Name::Set(aFun->Label(), "ParTranslation");
1063     
1064     Standard_Real aDx=0., aDy=0., aDz=0.;
1065     aDx = Draw::Atof(a[3]);
1066     //cout << "DX = " << aDx<<endl;
1067     if(nb > 4) {
1068       aDy = Draw::Atof(a[4]);
1069       //cout << "DY = " << aDy<<endl;
1070       if(nb > 5) {
1071         aDz = Draw::Atof(a[5]);
1072         //cout << "DZ = " << aDz<<endl;
1073       }
1074     }
1075
1076     DNaming::GetReal(aFun,PTRANSF_DX)->Set(aDx);  
1077     DNaming::GetReal(aFun,PTRANSF_DY)->Set(aDy);  
1078     DNaming::GetReal(aFun,PTRANSF_DZ)->Set(aDz);  
1079     TDF_Reference::Set(anObject->Label(), aFun->Label().FindChild(FUNCTION_RESULT_LABEL));
1080     DDF::ReturnLabel(di, aFun->Label());
1081     return 0;
1082   }
1083   cout << "DNaming_Translate : Error" << endl;
1084   return 1;  
1085 }
1086 //=======================================================================
1087 //function : DNaming_PTranslateLine
1088 //purpose  : "PTranslateAlongLine Doc ShapeEntry Line off"
1089 //=======================================================================
1090 static Standard_Integer DNaming_PTranslateLine (Draw_Interpretor& di,
1091                                                 Standard_Integer nb, 
1092                                                 const char** a)
1093 {
1094   if (nb > 4) {
1095 #ifdef OCCT_DEBUG
1096     cout << "NB = " << nb <<endl;
1097 #endif
1098     Handle(TDocStd_Document) aDocument;   
1099     Standard_CString aDocS(a[1]);
1100     if (!DDocStd::GetDocument(aDocS, aDocument)) return 1;
1101  
1102     Handle(TDataStd_UAttribute) anObject, aLine;// aShape, aLine;
1103     if (!DDocStd::Find(aDocument, a[2], GEOMOBJECT_GUID, anObject)) return 1;
1104     if (!DDocStd::Find(aDocument, a[3], GEOMOBJECT_GUID, aLine)) return 1;
1105
1106     Standard_GUID funGUID;
1107     if(!GetFuncGUID("PTALine",funGUID)) return 1;
1108     Handle(TFunction_Function) aFun = SetFunctionDS(anObject->Label(), funGUID);
1109     if(aFun.IsNull()) return 1;
1110     TDataStd_Name::Set(aFun->Label(), "ParTranslationAlongLine");
1111         
1112     Standard_Real anOff = 0.;
1113     anOff =  Draw::Atof(a[4]);
1114     DNaming::GetReal(aFun,PTRANSF_OFF)->Set(anOff);
1115   
1116     DNaming::SetObjectArg(aFun, PTRANSF_LINE, aLine); 
1117     TDF_Reference::Set(anObject->Label(), aFun->Label().FindChild(FUNCTION_RESULT_LABEL));
1118
1119     DDF::ReturnLabel(di, aFun->Label());
1120     return 0;
1121   }
1122   cout << "DNaming_PTranslateAlongLine : Error" << endl;
1123   return 1;  
1124 }
1125
1126 //=======================================================================
1127 //function : DNaming_PRotateLine
1128 //purpose  : "PRotateRoundLine Doc ShapeEntry Line Angle"
1129 //=======================================================================
1130 static Standard_Integer DNaming_PRotateLine(Draw_Interpretor& di,
1131                                              Standard_Integer nb, 
1132                                              const char** a)
1133 {
1134   if (nb > 4) {
1135     Handle(TDocStd_Document) aDocument;
1136     Standard_CString aDocS(a[1]);
1137     if (!DDocStd::GetDocument(aDocS, aDocument)) return 1; 
1138
1139     Handle(TDataStd_UAttribute) anObject, aLine;// aShape, aLine;
1140     if (!DDocStd::Find(aDocument, a[2], GEOMOBJECT_GUID, anObject)) return 1;
1141     if (!DDocStd::Find(aDocument, a[3], GEOMOBJECT_GUID, aLine)) return 1;
1142
1143     Standard_GUID funGUID;
1144     if(!GetFuncGUID("PRLine",funGUID)) return 1;
1145     Handle(TFunction_Function) aFun = SetFunctionDS(anObject->Label(), funGUID);
1146     if(aFun.IsNull()) return 1;
1147     TDataStd_Name::Set(aFun->Label(), "ParRotationAroundLine");
1148         
1149     DNaming::SetObjectArg(aFun, PTRANSF_LINE, aLine); 
1150     TDF_Reference::Set(anObject->Label(), aFun->Label().FindChild(FUNCTION_RESULT_LABEL));
1151
1152     Standard_Real anAngle = 0.;
1153     anAngle =  Draw::Atof(a[4]);
1154     Standard_Real aK = 2*M_PI/360;
1155     anAngle = anAngle * aK;
1156     DNaming::GetReal(aFun,PTRANSF_ANG)->Set(anAngle);
1157
1158     DDF::ReturnLabel(di, aFun->Label());
1159     return 0;    
1160   }
1161   cout << "DNaming_PRotateRoundLine : Error" << endl;
1162   return 1;  
1163 }
1164
1165 //=======================================================================
1166 //function : DNaming_PMirrorObject
1167 //purpose  : "PMirror Doc ShapeEntry PlaneObj"
1168 //=======================================================================
1169 static Standard_Integer DNaming_PMirrorObject(Draw_Interpretor& di,
1170                                               Standard_Integer nb, 
1171                                               const char** a)
1172 {
1173   if (nb > 3) {
1174     Handle(TDocStd_Document) aDocument;   
1175     Standard_CString aDocS(a[1]);
1176     if (!DDocStd::GetDocument(aDocS, aDocument)) return 1; 
1177     
1178     Handle(TDataStd_UAttribute) anObject, aPlane;
1179     if (!DDocStd::Find(aDocument, a[2], GEOMOBJECT_GUID, anObject)) return 1;
1180     if (!DDocStd::Find(aDocument, a[3], GEOMOBJECT_GUID, aPlane)) return 1;
1181
1182     Standard_GUID funGUID;
1183     if(!GetFuncGUID("PMirr",funGUID)) return 1;
1184     Handle(TFunction_Function) aFun = SetFunctionDS(anObject->Label(), funGUID);
1185     if(aFun.IsNull()) return 1;
1186     TDataStd_Name::Set(aFun->Label(), "ParMirror");
1187            
1188     DNaming::SetObjectArg(aFun, PTRANSF_PLANE, aPlane); 
1189     TDF_Reference::Set(anObject->Label(), aFun->Label().FindChild(FUNCTION_RESULT_LABEL));
1190     
1191     DDF::ReturnLabel(di, aFun->Label());
1192     return 0;
1193   }
1194   cout << "DNaming_PMirrorObject : Error" << endl;
1195   return 1;  
1196 }
1197 //=======================================================================
1198 //function : DModel_AddPrism
1199 //purpose  : "AddPrism Doc BasisLabel Height Reverse(0/1)"
1200 //=======================================================================
1201 static Standard_Integer DNaming_AddPrism (Draw_Interpretor& theDI,
1202                                          Standard_Integer theNb, 
1203                                          const char** theArg)
1204 {
1205   if (theNb < 5 ) {
1206     cout<<"DNaming_AddPrism(): Wrong number of arguments"<<endl;
1207     return 1;
1208   }
1209 //
1210   Handle(TDocStd_Document) aDocument;   
1211   Standard_CString aDocS(theArg[1]);
1212   if (!DDocStd::GetDocument(aDocS, aDocument)) return 1;
1213  
1214   Handle(TDataStd_UAttribute) anObj = AddObject(aDocument);
1215   if(anObj.IsNull()) return 1;
1216   Standard_GUID funGUID;
1217   if(!GetFuncGUID("Prism",funGUID)) return 1;
1218   Handle(TFunction_Function) aFun = SetFunctionDS(anObj->Label(), funGUID);
1219   if(aFun.IsNull()) return 1;
1220   TDataStd_Name::Set(aFun->Label(), "Prism_Function");
1221   TDF_Reference::Set(anObj->Label(), aFun->Label().FindChild(FUNCTION_RESULT_LABEL)); //result is here 
1222
1223   // arguments
1224   Handle(TDataStd_UAttribute) aBasisObj;
1225   if (!DDocStd::Find(aDocument, theArg[2], GEOMOBJECT_GUID, aBasisObj)) return 1;
1226   DNaming::SetObjectArg(aFun, PRISM_BASIS, aBasisObj); 
1227   Standard_Real height = Draw::Atof(theArg[3]);
1228   DNaming::GetReal(aFun,PRISM_HEIGHT)->Set(height);
1229   Standard_Integer reverse = Draw::Atoi(theArg[4]);
1230   DNaming::GetInteger(aFun,PRISM_DIR)->Set(reverse);
1231   DDF::ReturnLabel(theDI, anObj->Label());
1232   return 0;
1233 }  
1234
1235 //=======================================================================
1236 //function : DNaming_PrismHeight
1237 //purpose  : "PrismHeight Doc PrismLabel NewHeight"
1238 //=======================================================================
1239 static Standard_Integer DNaming_PrismHeight (Draw_Interpretor& theDI,
1240                                             Standard_Integer theNb, 
1241                                             const char** theArg)
1242 {
1243   if (theNb == 4) {
1244     Handle(TDocStd_Document) aDoc;   
1245     Standard_CString aDocS(theArg[1]);
1246     if (!DDocStd::GetDocument(aDocS, aDoc)) return 1;
1247     TDF_Label objLabel;
1248     if (!DDF::FindLabel(aDoc->GetData(),theArg[2], objLabel)) return 1;
1249     
1250     Handle(TDataStd_UAttribute) anObj;
1251     if(!objLabel.FindAttribute(GEOMOBJECT_GUID, anObj)) return 1;       
1252     Standard_GUID funGUID;
1253     if(!GetFuncGUID("Prism",funGUID)) return 1;       
1254     
1255     Handle(TFunction_Function) aFun = GetFunction(objLabel,funGUID);
1256     if(!aFun.IsNull()) {
1257       Standard_Real value = Draw::Atof(theArg[3]);
1258       DNaming::GetReal(aFun, PRISM_HEIGHT)->Set(value);
1259       DDF::ReturnLabel(theDI, DNaming::GetReal(aFun,PRISM_HEIGHT)->Label());
1260       return 0;
1261     }
1262     
1263   }
1264   cout << "DNaming_PrismHeight : Error" << endl;
1265   return 1;  
1266 }
1267
1268
1269 //=======================================================================
1270 //function : DModel_AddRevol
1271 //purpose  : "AddRevol Doc BasisLabel  AxisLabel [Angle [Reverse(0/1)]] "
1272 //         : if Angle is presented - sectioned revolution, else - full
1273 //=======================================================================
1274 static Standard_Integer DNaming_AddRevol (Draw_Interpretor& theDI,
1275                                           Standard_Integer theNb, 
1276                                           const char** theArg)
1277 {
1278   if (theNb < 4 ) {
1279     cout<<"DNaming_AddRevol(): Wrong number of arguments"<<endl;
1280     return 1;
1281   }
1282
1283   Handle(TDocStd_Document) aDocument;   
1284   Standard_CString aDocS(theArg[1]);
1285   if (!DDocStd::GetDocument(aDocS, aDocument)) return 1;
1286
1287   Handle(TDataStd_UAttribute) aBasisObj;
1288   if (!DDocStd::Find(aDocument, theArg[2], GEOMOBJECT_GUID, aBasisObj)) return 1;
1289
1290   Handle(TDataStd_UAttribute) anAxObj;
1291   if (!DDocStd::Find(aDocument, theArg[3], GEOMOBJECT_GUID, anAxObj)) return 1;
1292
1293   Standard_Boolean aFull = Standard_True;
1294   Handle(TDataStd_UAttribute) anObj = AddObject(aDocument);
1295   if(anObj.IsNull()) return 1;
1296   if(theNb > 4 ) 
1297    aFull = Standard_False;
1298   Standard_GUID funGUID;
1299   if(aFull) {
1300     if(!GetFuncGUID("FulRevol",funGUID)) return 1;
1301   }
1302   else 
1303     if(!GetFuncGUID("SecRevol",funGUID)) return 1;
1304   Handle(TFunction_Function) aFun = SetFunctionDS(anObj->Label(), funGUID);
1305   if(aFun.IsNull()) return 1;
1306   if(aFull)
1307     TDataStd_Name::Set(aFun->Label(), "FulRevol_Function");
1308   else
1309     TDataStd_Name::Set(aFun->Label(), "SecRevol_Function");
1310   TDF_Reference::Set(anObj->Label(), aFun->Label().FindChild(FUNCTION_RESULT_LABEL)); //result is here 
1311   DNaming::SetObjectArg(aFun, REVOL_BASIS, aBasisObj); 
1312   DNaming::SetObjectArg(aFun, REVOL_AXIS, anAxObj); 
1313
1314   if(theNb > 4 ) {  
1315     Standard_Real angle = Draw::Atof(theArg[4]);
1316     Standard_Real aK = 2*M_PI/360;
1317     angle = angle * aK;
1318     DNaming::GetReal(aFun,REVOL_ANGLE)->Set(angle);
1319     if( theNb == 6) {
1320       Standard_Integer reverse = Draw::Atoi(theArg[5]);
1321       DNaming::GetInteger(aFun, REVOL_REV)->Set(reverse);
1322     }
1323   }
1324   DDF::ReturnLabel(theDI, anObj->Label());
1325   return 0;
1326 }
1327
1328 //=======================================================================
1329 //function : DNaming_RevolutionAngle
1330 //purpose  : "RevolutionAngle Doc RevolutionLabel NewAngle"
1331 //=======================================================================
1332 static Standard_Integer DNaming_RevolutionAngle (Draw_Interpretor& theDI,
1333                                             Standard_Integer theNb, 
1334                                             const char** theArg)
1335 {
1336   if (theNb == 4) {
1337     Handle(TDocStd_Document) aDoc;   
1338     Standard_CString aDocS(theArg[1]);
1339     if (!DDocStd::GetDocument(aDocS, aDoc)) return 1;
1340     TDF_Label objLabel;
1341     if (!DDF::FindLabel(aDoc->GetData(),theArg[2], objLabel)) return 1;
1342     
1343     Handle(TDataStd_UAttribute) anObj;
1344     if(!objLabel.FindAttribute(GEOMOBJECT_GUID, anObj)) return 1;       
1345     Standard_GUID funGUID;
1346     if(!GetFuncGUID("SecRevol",funGUID)) return 1;       
1347     
1348     Handle(TFunction_Function) aFun = GetFunction(objLabel,funGUID);
1349     if(!aFun.IsNull()) {
1350       Standard_Real value = Draw::Atof(theArg[3]);
1351       DNaming::GetReal(aFun, REVOL_ANGLE)->Set(value);
1352       DDF::ReturnLabel(theDI, DNaming::GetReal(aFun,REVOL_ANGLE)->Label());
1353       return 0;
1354     }    
1355   }
1356   cout << "DNaming_RevolutionAngle : Error" << endl;
1357   return 1;  
1358 }
1359
1360 //=======================================================================
1361 //function : DNaming_AddSphere
1362 //purpose  : "AddSphere Doc CenterLabel Radius "
1363 //=======================================================================
1364 static Standard_Integer DNaming_AddSphere (Draw_Interpretor& theDI,
1365                                           Standard_Integer theNb, 
1366                                           const char** theArg)
1367 {
1368   if (theNb != 4) {
1369     cout<<"DNaming_AddSphere(): Wrong number of arguments"<<endl;
1370     return 1;
1371   }
1372   Handle(TDocStd_Document) aDocument;   
1373   Standard_CString aDocS(theArg[1]);
1374   if (!DDocStd::GetDocument(aDocS, aDocument)) return 1;
1375
1376   Handle(TDataStd_UAttribute) anObj = AddObject(aDocument);
1377   if(anObj.IsNull()) return 1;
1378   Standard_GUID funGUID;
1379   if(!GetFuncGUID("Sph",funGUID)) return 1;
1380   Handle(TFunction_Function) aFun = SetFunctionDS(anObj->Label(), funGUID);
1381   if(aFun.IsNull()) return 1;
1382   TDataStd_Name::Set(aFun->Label(), "Sphere_Function");
1383   TDF_Reference::Set(anObj->Label(), aFun->Label().FindChild(FUNCTION_RESULT_LABEL)); //result is here 
1384  
1385   Handle(TDataStd_UAttribute) aCenterObj;
1386   if (!DDocStd::Find(aDocument, theArg[2], GEOMOBJECT_GUID, aCenterObj)) return 1;
1387   DNaming::SetObjectArg(aFun, SPHERE_CENTER, aCenterObj);
1388
1389   Standard_Real aRadius = Draw::Atof(theArg[3]);
1390   DNaming::GetReal(aFun,SPHERE_RADIUS)->Set(aRadius);
1391   
1392   DDF::ReturnLabel(theDI, anObj->Label());
1393   return 0;
1394 }
1395
1396 //=======================================================================
1397 //function : DNaming_SphereRadius
1398 //purpose  : "SphereRadius Doc SphLabel NewRadius"
1399 //=======================================================================
1400 static Standard_Integer DNaming_SphereRadius (Draw_Interpretor& theDI,
1401                                              Standard_Integer theNb, 
1402                                              const char** theArg)
1403 {
1404   if (theNb == 4) {
1405     Handle(TDocStd_Document) aDoc;   
1406     Standard_CString aDocS(theArg[1]);
1407     if (!DDocStd::GetDocument(aDocS, aDoc)) return 1;
1408     TDF_Label objLabel;
1409     if (!DDF::FindLabel(aDoc->GetData(),theArg[2], objLabel)) return 1;
1410
1411     Handle(TDataStd_UAttribute) anObj;
1412     if(!objLabel.FindAttribute(GEOMOBJECT_GUID, anObj)) return 1;       
1413     Standard_GUID funGUID;
1414     if(!GetFuncGUID("Sph",funGUID)) return 1;       
1415
1416     Handle(TFunction_Function) aFun = GetFunction(objLabel,funGUID);
1417     if(!aFun.IsNull()) {
1418       Standard_Real value = Draw::Atof(theArg[3]);
1419       DNaming::GetReal(aFun, SPHERE_RADIUS)->Set(value);
1420       DDF::ReturnLabel(theDI, DNaming::GetReal(aFun, SPHERE_RADIUS)->Label());
1421       return 0;
1422     }
1423   }
1424
1425   cout << "DNaming_SphRadius : Error" << endl;
1426   return 1;  
1427 }
1428 //=======================================================================
1429 //function : DNaming_AddPoint
1430 //purpose  : "AddPoint Doc x y z"
1431 //=======================================================================
1432 static Standard_Integer DNaming_AddPoint (Draw_Interpretor& theDI,
1433                                          Standard_Integer theNb, 
1434                                          const char** theArg)
1435 {
1436   if (theNb >= 4) {
1437
1438     Handle(TDocStd_Document) aDocument;
1439     Standard_CString aDocS(theArg[1]);
1440     if (!DDocStd::GetDocument(aDocS, aDocument)) return 1;
1441     Handle(TDataStd_UAttribute) anObj = AddObject(aDocument);
1442     if(anObj.IsNull()) return 1;
1443     Standard_GUID funGUID;
1444     if(!GetFuncGUID("PntXYZ",funGUID)) return 1;
1445
1446     Handle(TFunction_Function) aFun = SetFunctionDS(anObj->Label(), funGUID);
1447     if(aFun.IsNull()) return 1;
1448     TDataStd_Name::Set(aFun->Label(), "PntXYZ_Function");
1449     
1450     TDF_Reference::Set(anObj->Label(), aFun->Label().FindChild(FUNCTION_RESULT_LABEL)); //result is here 
1451     Standard_Real x,y,z;
1452     x = Draw::Atof(theArg[2]);
1453     y = Draw::Atof(theArg[3]);
1454     z = Draw::Atof(theArg[4]);
1455
1456     DNaming::GetReal(aFun,PNT_DX)->Set(x);
1457     DNaming::GetReal(aFun,PNT_DY)->Set(y);
1458     DNaming::GetReal(aFun,PNT_DZ)->Set(z);
1459     
1460     DDF::ReturnLabel(theDI, anObj->Label());
1461     return 0;
1462   }
1463   cout << "DNaming_AddPoint : Error" << endl;
1464   return 1;  
1465 }
1466
1467 //=======================================================================
1468 //function : DNaming_AddPointRlt
1469 //purpose  : "AddPointRlt Doc RefPnt dx dy dz"
1470 //=======================================================================
1471 static Standard_Integer DNaming_AddPointRlt (Draw_Interpretor& theDI,
1472                                          Standard_Integer theNb, 
1473                                          const char** theArg)
1474 {
1475   if (theNb >= 5) {
1476
1477     Handle(TDocStd_Document) aDocument;
1478     Standard_CString aDocS(theArg[1]);
1479     if (!DDocStd::GetDocument(aDocS, aDocument)) return 1;
1480     Handle(TDataStd_UAttribute) anObj = AddObject(aDocument);
1481     if(anObj.IsNull()) return 1;
1482     Standard_GUID funGUID;
1483     if(!GetFuncGUID("PntRLT",funGUID)) return 1;
1484
1485     Handle(TFunction_Function) aFun = SetFunctionDS(anObj->Label(), funGUID);
1486     if(aFun.IsNull()) return 1;
1487     TDataStd_Name::Set(aFun->Label(), "PntRLT_Function");
1488     
1489     TDF_Reference::Set(anObj->Label(), aFun->Label().FindChild(FUNCTION_RESULT_LABEL)); //result is here 
1490
1491     Handle(TDataStd_UAttribute) aRefPnt;
1492     if (!DDocStd::Find(aDocument, theArg[2], GEOMOBJECT_GUID, aRefPnt)) return 1;
1493
1494     Standard_Real dx,dy,dz;
1495     dx = Draw::Atof(theArg[3]);
1496     dy = Draw::Atof(theArg[4]);
1497     dz = Draw::Atof(theArg[5]);
1498
1499     DNaming::GetReal(aFun,PNT_DX)->Set(dx);
1500     DNaming::GetReal(aFun,PNT_DY)->Set(dy);
1501     DNaming::GetReal(aFun,PNT_DZ)->Set(dz);
1502
1503     DNaming::SetObjectArg(aFun, PNTRLT_REF, aRefPnt); 
1504
1505     DDF::ReturnLabel(theDI, anObj->Label());
1506     return 0;
1507   }
1508   cout << "DNaming_AddPoint : Error" << endl;
1509   return 1;  
1510 }
1511
1512 //=======================================================================
1513 //function : DNaming_PntOffset
1514 //purpose  : "PntOffset Doc PntLabel newDX newDY newDZ "
1515 //=======================================================================
1516
1517 static Standard_Integer DNaming_PntOffset (Draw_Interpretor& theDI,
1518                                            Standard_Integer theNb, 
1519                                            const char** theArg)
1520 {
1521   if (theNb == 6) {
1522     Handle(TDocStd_Document) aDoc;   
1523     Standard_CString aDocS(theArg[1]);
1524     if (!DDocStd::GetDocument(aDocS, aDoc)) return 1;
1525     TDF_Label objLabel;
1526     if (!DDF::FindLabel(aDoc->GetData(),theArg[2], objLabel)) return 1;
1527     
1528     Handle(TDataStd_UAttribute) anObj;
1529     if(!objLabel.FindAttribute(GEOMOBJECT_GUID, anObj)) return 1;       
1530     Standard_GUID funGUID;
1531
1532     if(!GetFuncGUID("PntXYZ",funGUID)) {
1533       if(!GetFuncGUID("PntRLT",funGUID))
1534         return 1;       
1535     }
1536     
1537     Handle(TFunction_Function) aFun = GetFunction(objLabel,funGUID);
1538     if(!aFun.IsNull()) {
1539       Standard_Real value(0.0);
1540       Standard_Boolean isDX = (strcmp(theArg[3],"skip"));
1541       if(isDX) {
1542         value = Draw::Atof(theArg[3]);
1543         DNaming::GetReal(aFun,PNT_DX)->Set(value);
1544       }
1545       Standard_Boolean isDY = (strcmp(theArg[4],"skip"));
1546       if(isDY) {
1547         value = Draw::Atof(theArg[4]);
1548         DNaming::GetReal(aFun,PNT_DY)->Set(value);
1549       }
1550       Standard_Boolean isDZ = (strcmp(theArg[5],"skip"));
1551       if(isDZ) {
1552         value = Draw::Atof(theArg[5]);
1553         DNaming::GetReal(aFun,PNT_DZ)->Set(value);
1554       }
1555       if(isDX || isDY || isDZ)
1556         DDF::ReturnLabel(theDI, objLabel);
1557       else
1558         cout <<"DNaming_PntOffset : Nothing changed" << endl; 
1559       return 0;
1560     }
1561   }
1562   cout << "DNaming_PntOffset : Error" << endl;
1563   return 1;  
1564 }
1565
1566 //=======================================================================
1567 //function : DNaming_AddLine3D
1568 //purpose  : "AddLine3D Doc CurveType(0|1) Pnt1Lab Pnt2Lab [Pnt3Lab [Pnt4Lab [...]]]"
1569 //         : Type = 0 - open, Type = 1 - closed
1570 //=======================================================================
1571 static Standard_Integer DNaming_Line3D (Draw_Interpretor& theDI,
1572                                           Standard_Integer theNb, 
1573                                           const char** theArg)
1574 {
1575   if (theNb < 5) {
1576     cout<<"DNaming_AddLine3D: Wrong number of arguments"<<endl;
1577     return 1;
1578   }
1579   Handle(TDocStd_Document) aDocument;   
1580   Standard_CString aDocS(theArg[1]);
1581   if (!DDocStd::GetDocument(aDocS, aDocument)) return 1;
1582
1583   Handle(TDataStd_UAttribute) anObj = AddObject(aDocument);
1584   if(anObj.IsNull()) return 1;
1585   Standard_GUID funGUID;
1586   if(!GetFuncGUID("Line3D",funGUID)) return 1;
1587   Handle(TFunction_Function) aFun = SetFunctionDS(anObj->Label(), funGUID);
1588   if(aFun.IsNull()) return 1;
1589   TDataStd_Name::Set(aFun->Label(), "Line3D_Function");
1590   TDF_Reference::Set(anObj->Label(), aFun->Label().FindChild(FUNCTION_RESULT_LABEL)); //result is here 
1591  
1592   Standard_Integer aType = Draw::Atoi(theArg[2]);
1593   DNaming::GetInteger(aFun,LINE3D_TYPE)->Set(aType);
1594   
1595 //LINE3D_PNTNB
1596   //set Pnts
1597   Standard_Integer aPos = LINE3D_TYPE;
1598   for(Standard_Integer i=3; i < theNb; i++) {
1599     Handle(TDataStd_UAttribute) aPntObj;
1600     if (!DDocStd::Find(aDocument, theArg[i], GEOMOBJECT_GUID, aPntObj)) return 1;
1601     aPos++;
1602     DNaming::SetObjectArg(aFun, aPos, aPntObj);
1603   }
1604
1605   DNaming::GetInteger(aFun,LINE3D_PNTNB)->Set(aPos - 1);//set number of points
1606
1607   DDF::ReturnLabel(theDI, anObj->Label());
1608   return 0;
1609 }
1610 // ***
1611 //==========================================================================================
1612 inline static void MapOfOrientedShapes(const TopoDS_Shape& S, TopTools_MapOfOrientedShape& M)
1613 {
1614   M.Add(S);
1615   TopoDS_Iterator It(S,Standard_True,Standard_True);
1616   while (It.More()) {
1617     MapOfOrientedShapes(It.Value(),M);
1618     It.Next();
1619   }
1620 }
1621 //==========================================================================================
1622 static void MapOfOrientedShapes(const TopoDS_Shape& S1, const TopoDS_Shape& S2, 
1623                                     TopTools_MapOfOrientedShape& M) {
1624
1625   TopoDS_Iterator it(S2,Standard_True,Standard_True);
1626   while (it.More()) {
1627     if(it.Value().ShapeType() == S1.ShapeType()) 
1628       M.Add(it.Value());
1629     it.Next();
1630   }
1631 }
1632
1633 //==========================================================================================
1634 static void MapOfShapes(const TopoDS_Shape& S1, const TopoDS_Shape& S2, 
1635                                     TopTools_MapOfShape& M) {
1636
1637   TopoDS_Iterator it(S2,Standard_True,Standard_True);
1638   while (it.More()) {
1639     if(it.Value().ShapeType() == S1.ShapeType()) 
1640       M.Add(it.Value());
1641     it.Next();
1642   }
1643 }
1644 //==========================================================================================
1645 inline static void CollectShapes(const TopoDS_Shape& S, TopTools_ListOfShape& List)
1646 {
1647   List.Append(S);
1648   TopoDS_Iterator It(S,Standard_True,Standard_True);
1649   while (It.More()) {
1650     CollectShapes(It.Value(),List);
1651     It.Next();
1652   }
1653 }
1654
1655 //==========================================================================================
1656 inline static void CollectMultShapes(const TopoDS_Shape& S, TopTools_ListOfShape& List)
1657 {  
1658   TopAbs_ShapeEnum aType = S.ShapeType();
1659   Standard_Integer aStop (TopAbs_SHAPE);
1660   for(Standard_Integer i = (Standard_Integer)aType +1; i < aStop; i++) {
1661     BRep_Builder aBuilder;
1662     TopoDS_Compound aCompound;
1663     aBuilder.MakeCompound(aCompound);
1664     TopExp_Explorer exp(S,(TopAbs_ShapeEnum)i);
1665     for (;exp.More();exp.Next()) 
1666       aBuilder.Add(aCompound, exp.Current());
1667     List.Append(aCompound);
1668   }
1669 }
1670 //==========================================================================================
1671 static Standard_Boolean MakeSelection (const Handle(TDataStd_UAttribute)& Obj, 
1672                                        const TopoDS_Shape& Selection, 
1673                                        const Handle(TDataStd_UAttribute)& ContextObj, 
1674                                        const Standard_Boolean Geometry, 
1675                                        const Standard_Boolean KeepOrientation)
1676 {
1677   if(!Obj.IsNull()) {
1678     Handle(TDataStd_TreeNode) aNode,RNode;
1679     Obj->Label().FindAttribute(TDataStd_TreeNode::GetDefaultTreeID(), aNode);
1680     if(aNode.IsNull())
1681       aNode = TDataStd_TreeNode::Set(Obj->Label());
1682     aNode->Remove();
1683     Handle(TDataStd_UAttribute) aContainer = ContextObj;
1684     if(aContainer->Label().FindAttribute(TDataStd_TreeNode::GetDefaultTreeID(), RNode))
1685       RNode->Append(aNode);
1686     TDataStd_Name::Set(Obj->Label(), "Auxiliary_Object");
1687     Standard_GUID funGUID;
1688     if(GetFuncGUID("Attach",funGUID)) {
1689       Handle(TFunction_Function) aFun = SetFunctionDS(Obj->Label(), funGUID);
1690       if(!aFun.IsNull()) {
1691         TDataStd_Name::Set(aFun->Label(), "ISelection");          
1692         TDF_Label aResultLabel =  aFun->Label().FindChild(FUNCTION_RESULT_LABEL, Standard_True); 
1693         TDF_Reference::Set(Obj->Label(), aResultLabel ); //result of the object
1694         aResultLabel.ForgetAllAttributes(Standard_True);
1695         Handle(TNaming_NamedShape) aNS = DNaming::GetObjectValue( ContextObj);  
1696         try{
1697           const TopoDS_Shape& aContext = aNS->Get();
1698           TNaming_Selector aSelector(aResultLabel);
1699           if(!aSelector.Select(Selection, aContext, Geometry, KeepOrientation))
1700             return Standard_False;
1701         }catch (...) {
1702           cout << "EXCEPTION: SELECTION_IMPOSSIBLE" <<endl;
1703         }
1704
1705         if(!aNS.IsNull()) {
1706
1707             //TCollection_AsciiString entry;
1708             //TDF_Tool::Entry(aNS->Label(), entry);
1709             //cout << "ContextNS Label = " << entry <<endl;
1710           Handle(TFunction_Function) aCntFun;
1711           if(aNS->Label().Father().FindAttribute(TFunction_Function::GetID(), aCntFun)) { //Fun:2 ==> result
1712               // First argument of Selection function refers to father function (of context object) 
1713               // to which selection is attached (because sel obj. itself already has refrence to result NS
1714             TDF_Reference::Set(aFun->Label().FindChild(FUNCTION_ARGUMENTS_LABEL).FindChild(ATTACH_ARG), 
1715                                aCntFun->Label()); //ref to function produced Context shape
1716             
1717           }
1718         }         
1719         return Standard_True;
1720       }
1721     }
1722   }
1723   return Standard_False;
1724 }
1725
1726 //==========================================================================================
1727 static Standard_Boolean MakeXSelection (const Handle(TDataStd_UAttribute)& Obj, 
1728                                        const TopoDS_Shape& Selection, 
1729                                        const Handle(TDataStd_UAttribute)& ContextObj, 
1730                                        const Standard_Boolean Geometry, 
1731                                        const Standard_Boolean KeepOrientation)
1732 {
1733   if(!Obj.IsNull()) {
1734 /*    Handle(TDataStd_TreeNode) aNode,RNode;
1735     Obj->Label().FindAttribute(TDataStd_TreeNode::GetDefaultTreeID(), aNode);
1736     if(aNode.IsNull())
1737       aNode = TDataStd_TreeNode::Set(Obj->Label());
1738     aNode->Remove();
1739     Handle(TDataStd_UAttribute) aContainer = ContextObj;
1740     if(aContainer->Label().FindAttribute(TDataStd_TreeNode::GetDefaultTreeID(), RNode))
1741       RNode->Append(aNode);
1742 */
1743     TDataStd_Name::Set(Obj->Label(), "Auxiliary_Object");
1744     Standard_GUID funGUID;
1745     if(GetFuncGUID("XAttach",funGUID)) {
1746       Handle(TFunction_Function) aFun = SetFunctionDS(Obj->Label(), funGUID);
1747       if(!aFun.IsNull()) {
1748         TDataStd_Name::Set(aFun->Label(), "XSelection");          
1749         TDF_Label aResultLabel =  aFun->Label().FindChild(FUNCTION_RESULT_LABEL, Standard_True); 
1750         TDF_Reference::Set(Obj->Label(), aResultLabel ); //result of the object
1751         aResultLabel.ForgetAllAttributes(Standard_True);
1752
1753         Handle(TNaming_NamedShape) aNS = DNaming::GetObjectValue( ContextObj);  
1754         try{
1755           const TopoDS_Shape& aContext = aNS->Get();
1756           TNaming_Selector aSelector(aResultLabel);
1757           if(!aSelector.Select(Selection, aContext, Geometry, KeepOrientation))
1758             return Standard_False;
1759         }catch (...) {
1760           cout << "EXCEPTION: SELECTION_IMPOSSIBLE" <<endl;
1761         }
1762
1763         if(!aNS.IsNull()) {
1764
1765             //TCollection_AsciiString entry;
1766             //TDF_Tool::Entry(aNS->Label(), entry);
1767             //cout << "ContextNS Label = " << entry <<endl;
1768           Handle(TFunction_Function) aCntFun;
1769           if(aNS->Label().Father().FindAttribute(TFunction_Function::GetID(), aCntFun)) { //Fun:2 ==> result
1770               // First argument of Selection function refers to father function (of context object) 
1771               // to which selection is attached (because sel obj. itself already has refrence to result NS
1772             TDF_Reference::Set(aFun->Label().FindChild(FUNCTION_ARGUMENTS_LABEL).FindChild(ATTACH_ARG), 
1773                                aCntFun->Label()); //ref to function produced Context shape
1774             
1775           }
1776         }         
1777         return Standard_True;
1778       }
1779     }
1780   }
1781   return Standard_False;
1782 }
1783 //=======================================================================
1784 inline static TCollection_ExtendedString compareShapes(const TopoDS_Shape& theShape1,
1785                                                        const TopoDS_Shape& theShape2,
1786                                                        const Standard_Boolean keepOrientation) 
1787 {
1788   TCollection_ExtendedString aResult("");
1789
1790   if((theShape1.ShapeType() != TopAbs_COMPOUND) &&
1791      theShape2.ShapeType() == TopAbs_COMPOUND) {
1792     if(keepOrientation) {
1793       TopTools_MapOfOrientedShape M;
1794       MapOfOrientedShapes(theShape1, theShape2, M);
1795       if(M.Contains(theShape1)) {
1796         aResult+=" Warning => Type migration to Compound";
1797         return aResult;
1798       }
1799     } else {
1800       TopTools_MapOfShape M;
1801       MapOfShapes(theShape1, theShape2, M);
1802       if(M.Contains(theShape1)) {
1803         aResult+=" Warning => Type migration to Compound";
1804         return aResult;
1805       }
1806     }
1807   }
1808   Standard_Boolean isComma=Standard_False;
1809   if(theShape1.ShapeType() != theShape2.ShapeType()) {
1810     aResult+=" Type was changed:";
1811     aResult+=" Initial type = "; aResult+= TCollection_ExtendedString(theShape1.ShapeType());
1812     aResult+="; Resulting type = "; aResult+= TCollection_ExtendedString(theShape2.ShapeType());
1813     isComma = Standard_True;
1814   } else if(theShape1.TShape() != theShape2.TShape()) {
1815     aResult+=" TShape was changed";
1816     isComma = Standard_True;
1817   } else if(theShape1.Location()!=theShape2.Location()) {
1818     aResult+=" Location was changed";
1819     isComma = Standard_True;
1820   }
1821
1822   if(keepOrientation) { 
1823     if(theShape1.Orientation()!=theShape2.Orientation() && theShape1.ShapeType() != TopAbs_VERTEX) {
1824       if(theShape2.ShapeType() != TopAbs_COMPOUND) {
1825         if(isComma)
1826           aResult+=",";
1827         aResult+=" Orientation was changed";
1828       }
1829       else {
1830         TopoDS_Iterator it(theShape2);
1831         for(;it.More(); it.Next()) {
1832           if(it.Value().IsSame(theShape1))
1833             if(theShape1.Orientation()!=it.Value().Orientation()) {
1834               if(isComma)
1835                 aResult+=",";
1836               aResult+=" Orientation was changed";
1837             }
1838         }
1839       }
1840     }
1841   }
1842   return aResult;
1843 }
1844
1845 //=======================================================================
1846 inline static TCollection_ExtendedString compareShapes(const TopoDS_Shape& theShape, 
1847                                                        //theShape - Compound of Multiple selection
1848                                                        const TopTools_MapOfOrientedShape& aMap)
1849 {
1850   TCollection_ExtendedString aResult("");
1851   if(theShape.ShapeType() != TopAbs_COMPOUND) {
1852     aResult+="the specified shape is not COMPOUND";
1853   } else {
1854     TopoDS_Iterator it(theShape);
1855     for(;it.More();it.Next()) {
1856       const TopoDS_Shape& newShape = it.Value();
1857       if(!aMap.Contains(newShape)) {
1858         aResult+=" Not in the context";
1859       }
1860     }
1861   }
1862   return aResult;
1863 }
1864 //=======================================================================
1865 //function : IntegerToShapeEnum
1866 //purpose  :
1867 //=======================================================================
1868
1869 static TCollection_AsciiString ShapeEnumToString (const TopAbs_ShapeEnum   type)
1870 {
1871   switch(type)
1872     {
1873     case  TopAbs_COMPOUND  : return TCollection_AsciiString("COMPOUND");
1874     case  TopAbs_COMPSOLID : return TCollection_AsciiString("COMPSOLID");
1875     case  TopAbs_SOLID     : return TCollection_AsciiString("SOLID");
1876     case  TopAbs_SHELL     : return TCollection_AsciiString("SHELL");
1877     case  TopAbs_FACE      : return TCollection_AsciiString("FACE");
1878     case  TopAbs_WIRE      : return TCollection_AsciiString("WIRE");
1879     case  TopAbs_EDGE      : return TCollection_AsciiString("EDGE");
1880     case  TopAbs_VERTEX    : return TCollection_AsciiString("VERTEX");
1881     case  TopAbs_SHAPE     : return TCollection_AsciiString("SHAPE");
1882     }
1883   return TCollection_AsciiString("SHAPE");
1884 }
1885 //=======================================================================
1886 //function : DNaming_TestSingle
1887 //purpose  : "TestSingleSelection Doc ObjectLabel [Orientation [Xselection [Geometry]]]"
1888 //         : returns DDF::ReturnLabel of a first Aux object
1889 //=======================================================================
1890 static Standard_Integer DNaming_TestSingle (Draw_Interpretor& theDI,
1891                                                     Standard_Integer theNb, 
1892                                                     const char** theArg)
1893 {
1894   if (theNb >= 3) {
1895     Handle(TDocStd_Document) aDoc;   
1896     Standard_CString aDocS(theArg[1]);
1897     if (!DDocStd::GetDocument(aDocS, aDoc)) return 1;
1898     TDF_Label ObjLabel;
1899     if (!DDF::FindLabel(aDoc->GetData(),theArg[2], ObjLabel)) return 1;
1900
1901     Handle(TDataStd_UAttribute) aCntObj;
1902     if(!ObjLabel.FindAttribute(GEOMOBJECT_GUID, aCntObj)) return 1;       
1903     Standard_Boolean Orientation(Standard_False);
1904     Standard_Boolean XSelection(Standard_False);
1905     Standard_Boolean Geometry(Standard_False);
1906     if(theNb == 4)
1907       Orientation = (Standard_Boolean)Draw::Atoi(theArg[3]);
1908     if(theNb == 5)
1909       XSelection = (Standard_Boolean)Draw::Atoi(theArg[4]);
1910     if (theNb == 6) 
1911       Geometry = (Standard_Boolean) Draw::Atoi(theArg[5]);
1912     Handle(TNaming_NamedShape) aNS = DNaming::GetObjectValue( aCntObj);
1913
1914     if(!aNS.IsNull() && !aNS->IsEmpty()) {
1915       const TopoDS_Shape&  aRootShape = aNS->Get();
1916       //TopTools_MapOfOrientedShape aMap0;
1917       //MapOfOrientedShapes(aRootShape, aMap0);
1918       TopTools_ListOfShape aList, aFailedList;
1919       CollectShapes(aRootShape, aList);
1920       if(aList.Extent())
1921         aList.RemoveFirst();
1922       Standard_Boolean isFirst(Standard_True);
1923       Handle(TDataStd_UAttribute) FirstAuxObj;
1924       TopTools_ListIteratorOfListOfShape it(aList);
1925       for(; it.More(); it.Next()) {
1926         const TopoDS_Shape& aCurShape = it.Value();
1927         if(aCurShape.IsNull()) continue;
1928         if(aCurShape.ShapeType() == TopAbs_EDGE) {
1929           if (BRep_Tool::Degenerated(TopoDS::Edge(aCurShape)))
1930             continue;
1931         }
1932         Handle(TDataStd_UAttribute) auxObj = AddObject (aDoc);
1933         if(isFirst) {
1934           FirstAuxObj = auxObj;
1935           isFirst = Standard_False;
1936           TCollection_AsciiString entry;
1937           TDF_Tool::Entry(FirstAuxObj->Label(), entry);
1938 #ifdef OCCT_DEBUG
1939           cout << "First Selection function at " << entry <<endl;
1940 #endif
1941         }
1942         Standard_Boolean isSelected (Standard_False);
1943         try {
1944           OCC_CATCH_SIGNALS
1945             {
1946               if(!XSelection)
1947                 isSelected = MakeSelection(auxObj, aCurShape, aCntObj, Geometry, Orientation);
1948               else
1949                 isSelected = MakeXSelection(auxObj, aCurShape, aCntObj, Geometry, Orientation);
1950             }
1951         }
1952         catch (Standard_Failure) {
1953           Handle(Standard_Failure) aFailure =  Standard_Failure::Caught();
1954           cout << "%%%INFO:Error: ::TestSingleSelection failed :";
1955           if (!aFailure.IsNull()) 
1956             cout << aFailure->GetMessageString() << endl;
1957           else
1958             cout << "%%%INFO:Error: ::TestSingleSelection : Standard_Failure with null caught failure"<< endl;
1959         }
1960         catch(...) {
1961           cout << "%%%INFO:Error: ::TestSingleSelection selection failed : unknown exception type";
1962         }
1963         TCollection_AsciiString entry;
1964         TDF_Tool::Entry(auxObj->Label(), entry);
1965         TCollection_ExtendedString aResult("");
1966         if(isSelected) {
1967           const Handle(TNaming_NamedShape)& aSelNS =  DNaming::GetObjectValue(auxObj);
1968           if(!aSelNS.IsNull() && !aSelNS->IsEmpty()) {
1969             const TopoDS_Shape& aSelectedShape = aSelNS->Get();
1970             aResult += compareShapes(aCurShape, aSelectedShape,Orientation);
1971             TDF_ChildIDIterator itn(auxObj->Label(),TNaming_Naming::GetID(), Standard_True);
1972             for(;itn.More();itn.Next()) {
1973               Handle(TNaming_Naming) aNaming = Handle(TNaming_Naming)::DownCast(itn.Value());
1974               if(!aNaming.IsNull()) {
1975                 const TNaming_Name& aName = aNaming->GetName();
1976                 if(aName.Type() == TNaming_UNKNOWN) {
1977                   aResult += " Selection at label = ";
1978                   aResult +=  entry;
1979                   aResult += " has UNKNOWN name type, shape type = ";
1980                   aResult += ShapeEnumToString(aCurShape.ShapeType());
1981                 }
1982                   
1983               }
1984             }
1985           }
1986         } else {
1987           aResult += " Selection at label = ";
1988           aResult +=  entry;
1989           aResult += " failed, shape type = ";
1990           aResult += ShapeEnumToString(aCurShape.ShapeType());
1991           aFailedList.Append(aCurShape);
1992         }
1993         if(aResult.Length()) {
1994           if(aResult.Search("Warning") == -1)
1995             cout << "Failed units: " << aResult << " at " << entry << endl;
1996           else 
1997             cout << aResult << " at " << entry << endl;
1998           TDataStd_Name::Set(auxObj->Label(), aResult);
1999         }
2000       } 
2001       if(aFailedList.Extent()) {
2002         cout << "Failed units are kept at: ";
2003         TopTools_ListIteratorOfListOfShape it1(aFailedList);
2004         for(; it1.More(); it1.Next()) {
2005           const TDF_Label& aLabel = TDF_TagSource::NewChild(aDoc->Main());
2006           
2007           TNaming_Builder B(aLabel);
2008           B.Generated(it1.Value());
2009           TCollection_AsciiString entry;
2010           TDF_Tool::Entry(aLabel, entry);
2011           cout << "\t" <<entry <<endl;
2012         }
2013       }
2014       if(!FirstAuxObj.IsNull())
2015         DDF::ReturnLabel(theDI, FirstAuxObj->Label());
2016       return 0;
2017     }
2018   }
2019
2020   cout << "DNaming_TestSingle : Error" << endl;
2021   return 1;  
2022 }
2023
2024 //=======================================================================
2025 //function : DNaming_Multiple
2026 //purpose  : "TestMultipleSelection Doc ObjectLabel [Orientation [Xselection [Geometry]]]"
2027 //         : returns DDF::ReturnLabel of a first Aux object
2028 //=======================================================================
2029 static Standard_Integer DNaming_Multiple (Draw_Interpretor& theDI,
2030                                                     Standard_Integer theNb, 
2031                                                     const char** theArg)
2032 {
2033   if (theNb >= 3) {
2034     Handle(TDocStd_Document) aDoc;   
2035     Standard_CString aDocS(theArg[1]);
2036     if (!DDocStd::GetDocument(aDocS, aDoc)) return 1;
2037     TDF_Label ObjLabel;
2038     if (!DDF::FindLabel(aDoc->GetData(),theArg[2], ObjLabel)) return 1;
2039
2040     Handle(TDataStd_UAttribute) aCntObj;
2041     if(!ObjLabel.FindAttribute(GEOMOBJECT_GUID, aCntObj)) return 1;       
2042     Standard_Boolean Orientation(Standard_False);
2043     Standard_Boolean XSelection(Standard_False);
2044     Standard_Boolean Geometry(Standard_False);
2045     if(theNb == 4)
2046       Orientation = (Standard_Boolean)Draw::Atoi(theArg[3]);
2047     if(theNb == 5)
2048       XSelection = (Standard_Boolean)Draw::Atoi(theArg[4]);
2049     if (theNb == 6) 
2050       Geometry = (Standard_Boolean) Draw::Atoi(theArg[5]);
2051     Handle(TNaming_NamedShape) aNS = DNaming::GetObjectValue( aCntObj);
2052
2053     if(!aNS.IsNull() && !aNS->IsEmpty()) {
2054       const TopoDS_Shape&  aRootShape = aNS->Get();
2055       TopTools_MapOfOrientedShape aMap0;
2056       MapOfOrientedShapes(aRootShape, aMap0);
2057       TopTools_ListOfShape aList, aFailedList;
2058       CollectMultShapes(aRootShape, aList);
2059
2060       Standard_Boolean isFirst(Standard_True);
2061       Handle(TDataStd_UAttribute) FirstAuxObj;
2062       TopTools_ListIteratorOfListOfShape it(aList);
2063       for(; it.More(); it.Next()) {
2064         const TopoDS_Shape& aCurShape = it.Value();
2065         if(aCurShape.IsNull()) continue;
2066         if(aCurShape.ShapeType() == TopAbs_EDGE) {
2067           if (BRep_Tool::Degenerated(TopoDS::Edge(aCurShape)))
2068             continue;
2069         }//
2070         Handle(TDataStd_UAttribute) auxObj = AddObject (aDoc);
2071         if(isFirst) {
2072           FirstAuxObj = auxObj;
2073           isFirst = Standard_False;
2074 #ifdef OCCT_DEBUG
2075           TCollection_AsciiString entry;
2076           TDF_Tool::Entry(FirstAuxObj->Label(), entry);
2077           cout << "First Selection function at " << entry <<endl;
2078 #endif
2079         }
2080         Standard_Boolean isSelected (Standard_False);
2081         try {
2082           OCC_CATCH_SIGNALS
2083             {
2084               if(!XSelection)
2085                 isSelected = MakeSelection(auxObj, aCurShape, aCntObj, Geometry, Orientation);
2086               else
2087                 isSelected = MakeXSelection(auxObj, aCurShape, aCntObj, Geometry, Orientation);
2088             }
2089         }
2090         catch (Standard_Failure) {
2091           Handle(Standard_Failure) aFailure =  Standard_Failure::Caught();
2092           cout << "%%%INFO:Error: ::TestSingleSelection failed :";
2093           if (!aFailure.IsNull()) 
2094             cout << aFailure->GetMessageString() << endl;
2095           else
2096             cout << "%%%INFO:Error: ::TestSingleSelection : Standard_Failure with null caught failure"<< endl;
2097         }
2098         catch(...) {
2099           cout << "%%%INFO:Error: ::TestSingleSelection selection failed : unknown exception type";
2100         }
2101         TCollection_AsciiString entry;
2102         TDF_Tool::Entry(auxObj->Label(), entry);
2103         TCollection_ExtendedString aResult("");
2104         if(isSelected) {
2105           const Handle(TNaming_NamedShape)& aSelNS =  DNaming::GetObjectValue(auxObj);
2106           if(!aSelNS.IsNull() && !aSelNS->IsEmpty()) {
2107             const TopoDS_Shape& aSelectedShape = aSelNS->Get();
2108             aResult += compareShapes(aSelectedShape, aMap0);
2109             TDF_ChildIDIterator itn(auxObj->Label(),TNaming_Naming::GetID(), Standard_True);
2110             for(;itn.More();itn.Next()) {
2111               Handle(TNaming_Naming) aNaming = Handle(TNaming_Naming)::DownCast(itn.Value());
2112               if(!aNaming.IsNull()) {
2113                 const TNaming_Name& aName = aNaming->GetName();
2114                 if(aName.Type() == TNaming_UNKNOWN) {
2115                   aResult += " Selection at label = ";
2116                   aResult +=  entry;
2117                   aResult += " has UNKNOWN name type, shape type = ";
2118                   aResult += ShapeEnumToString(aCurShape.ShapeType());
2119                 }
2120                   
2121               }
2122             }
2123           }
2124         } else {
2125           aResult += " Selection at label = ";
2126           aResult +=  entry;
2127           aResult += " failed, shape type = ";
2128           aResult += ShapeEnumToString(aCurShape.ShapeType());
2129           aFailedList.Append(aCurShape);
2130         }
2131         if(aResult.Length())
2132           cout << "Failed units: " << aResult << endl;
2133       }
2134  
2135       if(aFailedList.Extent()) {
2136         TopTools_ListIteratorOfListOfShape it1(aFailedList);
2137         for(; it1.More(); it1.Next()) {
2138           const TDF_Label& aLabel = TDF_TagSource::NewChild(aDoc->Main());
2139           
2140           TNaming_Builder B(aLabel);
2141           B.Generated(it1.Value());
2142         }
2143       }
2144       if(!FirstAuxObj.IsNull())
2145         DDF::ReturnLabel(theDI, FirstAuxObj->Label());
2146       return 0;
2147     }
2148   }
2149
2150   cout << "DNaming_TestMultiple : Error" << endl;
2151   return 1;  
2152 }
2153
2154 //=======================================================================
2155 //function : ModelingCommands
2156 //purpose  : 
2157 //=======================================================================
2158
2159 void DNaming::ModelingCommands (Draw_Interpretor& theCommands)
2160 {  
2161
2162   static Standard_Boolean done = Standard_False;
2163   if (done) return;
2164   done = Standard_True;
2165   const char* g2 = "Naming modeling commands" ;
2166
2167   theCommands.Add ("AddObject", 
2168                    "AddObject D",
2169                    __FILE__, DNaming_AddObject, g2);
2170
2171   theCommands.Add ("AddFunction", 
2172                    "AddFunction D ObjEntry FunNane[Box|Sph|Cyl|Cut|Fuse|Prism|Revol|PMove|Fillet|Attach|XAttach]",
2173                    __FILE__, DNaming_AddFunction, g2);
2174
2175   theCommands.Add ("AddBox", "AddBox Doc dx dy dz",      __FILE__, DNaming_AddBox, g2);
2176   
2177   theCommands.Add ("BoxDX",  "BoxDX Doc BoxLabel NewDX", __FILE__, DNaming_BoxDX, g2);  
2178   
2179   theCommands.Add ("BoxDY",  "BoxDY Doc BoxLabel NewDY", __FILE__, DNaming_BoxDY, g2);  
2180   
2181   theCommands.Add ("BoxDZ",  "BoxDZ Doc BoxLabel NewDZ", __FILE__, DNaming_BoxDZ, g2);
2182   
2183   theCommands.Add ("ComputeFun", "ComputeFun Doc FunLabel", __FILE__, DNaming_ComputeFun, g2);
2184   
2185   theCommands.Add ("InitLogBook", "InitLogBook Doc",        __FILE__, DNaming_InitLogBook, g2);
2186
2187   theCommands.Add ("AddDriver", 
2188                    "AddDriver Doc Name [Box|Sph|Cyl|Cut|Fuse|Prism|Revol|PTxyz|PTALine|PRLine|PMirr|Fillet|Attach|XAttach]",
2189                    __FILE__, DNaming_AddDriver, g2);
2190
2191   theCommands.Add ("AttachShape", 
2192                    "AttachShape Doc Shape Context [Container [KeepOrientation [Geometry]]]",
2193                    __FILE__, DNaming_AttachShape, g2);
2194
2195   theCommands.Add ("XAttachShape", 
2196                    "XAttachShape Doc Shape Context [KeepOrientation [Geometry]]",
2197                    __FILE__, DNaming_XAttachShape, g2);  
2198
2199   theCommands.Add ("AddCyl", "AddCyl Doc Radius Height Axis", __FILE__, DNaming_AddCylinder, g2);
2200
2201   theCommands.Add ("CylRad", "CylRad Doc CylLabel NewRad",    __FILE__, DNaming_CylRad, g2);
2202
2203
2204   theCommands.Add ("AddFuse", "AddFuse Doc Object Tool",     __FILE__, DNaming_AddFuse, g2);  
2205
2206   theCommands.Add ("AddCut",   "AddCut Doc Object Tool",     __FILE__, DNaming_AddCut, g2);  
2207
2208   theCommands.Add ("AddCommon", "AddCommon Doc Object Tool",  __FILE__, DNaming_AddCommon, g2);  
2209
2210   theCommands.Add ("AddSection", "AddSection Doc Object Tool",  __FILE__, DNaming_AddSection, g2);  
2211
2212   theCommands.Add ("AddFillet", 
2213                    "AddFillet Doc Object Radius Path [SurfaceType(0-Rational;1-QuasiAngular;2-Polynomial)]",
2214                    __FILE__, DNaming_AddFillet, g2);
2215
2216   theCommands.Add ("PTranslateDXYZ", "PTranslateDXYZ Doc ShapeEntry dx dy dz",
2217                    __FILE__, DNaming_PTranslateDXYZ, g2);
2218
2219   theCommands.Add ("PTranslateAlongLine", "PTranslateAlongLine Doc ShapeEntry  Line off",
2220                    __FILE__, DNaming_PTranslateLine, g2);
2221
2222   theCommands.Add ("PRotateRoundLine", "PRotateRoundLine Doc ShapeEntry Line Angle",
2223                    __FILE__, DNaming_PRotateLine, g2);
2224
2225   theCommands.Add ("PMirror", "PMirror Doc ShapeEntry PlaneObj",
2226                    __FILE__, DNaming_PMirrorObject, g2);
2227
2228
2229   theCommands.Add ("AddPrism", "AddPrism Doc BasisLabel Height Reverse(0/1) ",
2230                    __FILE__, DNaming_AddPrism, g2);
2231
2232   theCommands.Add ("PrismHeight", "PrismHeight Doc PrismLabel NewHeight",
2233                    __FILE__, DNaming_PrismHeight, g2);
2234
2235   theCommands.Add ("AddRevol", "AddRevol Doc BasisLabel  AxisLabel [Angle [Reverse(0/1)]] ",
2236                    __FILE__, DNaming_AddRevol, g2);
2237
2238   theCommands.Add ("RevolutionAngle", "RevolutionAngle Doc RevolutionLabel NewAngle",
2239                    __FILE__, DNaming_RevolutionAngle, g2);
2240
2241   theCommands.Add ("AddSphere", "AddSphere Doc CenterLabel Radius ",
2242                    __FILE__, DNaming_AddSphere, g2);
2243
2244   theCommands.Add ("SphereRadius", "SphereRadius Doc SphereLabel NewRadius",
2245                    __FILE__, DNaming_SphereRadius, g2);
2246   
2247   theCommands.Add ("TestSingleSelection", 
2248                     "TestSingleSelection Doc ObjectLabel [Orientation [Xselection [Geometry]]]",
2249                    __FILE__, DNaming_TestSingle, g2);
2250
2251   theCommands.Add ("SolveFlatFrom", "SolveFlatFrom Doc FistAuxObjLabel",
2252                    __FILE__, DNaming_SolveFlatFrom, g2);
2253
2254   theCommands.Add ("CheckLogBook", "CheckLogBook Doc",  __FILE__, DNaming_CheckLogBook, g2);
2255
2256   theCommands.Add ("TestMultipleSelection", 
2257                    "TestMultipleSelection Doc ObjectLabel [Orientation [Xselection [Geometry]]]",
2258                    __FILE__, DNaming_Multiple, g2);
2259
2260   theCommands.Add ("AddPoint", "AddPoint Doc x y z", __FILE__, DNaming_AddPoint, g2);
2261
2262   theCommands.Add ("AddPointRlt", "AddPointRlt Doc RefPntObj dx dy dz",
2263                    __FILE__, DNaming_AddPointRlt, g2);
2264
2265   theCommands.Add ("PntOffset", "PntOffset Doc PntLabel newDX|skip newDY|skip newDZ|skip",
2266                    __FILE__, DNaming_PntOffset, g2);
2267
2268   theCommands.Add ("AddLine3D", "AddLine3D Doc CurveType(0|1) Pnt1 Pnt2 [Pnt3 [Pnt4 [...]]]",
2269                    __FILE__, DNaming_Line3D, g2);
2270 }
2271
2272