0031642: Visualization - crash in Graphic3d_Structure::SetVisual() on redisplaying...
[occt.git] / src / DNaming / DNaming_ToolsCommands.cxx
1 // Created on: 1999-06-24
2 // Created by: Sergey ZARITCHNY
3 // Copyright (c) 1999-1999 Matra Datavision
4 // Copyright (c) 1999-2014 OPEN CASCADE SAS
5 //
6 // This file is part of Open CASCADE Technology software library.
7 //
8 // This library is free software; you can redistribute it and/or modify it under
9 // the terms of the GNU Lesser General Public License version 2.1 as published
10 // by the Free Software Foundation, with special exception defined in the file
11 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
12 // distribution for complete text of the license and disclaimer of any warranty.
13 //
14 // Alternatively, this file may be used under the terms of Open CASCADE
15 // commercial license or contractual agreement.
16
17 #include <Draw_Interpretor.hxx>
18 #include <Draw.hxx>
19 #include <DBRep.hxx>
20 #include <DNaming.hxx>
21 #include <BRepTools.hxx>
22 #include <TopoDS_Face.hxx>
23 #include <TopLoc_Location.hxx>
24 #include <BRep_Builder.hxx>
25 #include <gp_Pnt.hxx>
26 #include <TopExp_Explorer.hxx>
27 #include <TCollection_AsciiString.hxx>
28 #include <TopAbs.hxx>
29 #include <TNaming_CopyShape.hxx>
30 #include <TNaming_Translator.hxx>
31 #include <TopTools_DataMapIteratorOfDataMapOfShapeShape.hxx>
32 #include <DNaming_DataMapOfShapeOfName.hxx>
33 #include <DNaming_DataMapIteratorOfDataMapOfShapeOfName.hxx>
34 #include <TopTools_MapOfShape.hxx>
35 #include <TopTools_MapIteratorOfMapOfShape.hxx>
36 //=======================================================================
37 //function : DNaming_CheckHasSame 
38 //purpose  : CheckIsSame  Shape1 Shape2 
39 //           - for test ShapeCopy mechanism
40 //=======================================================================
41
42 static Standard_Integer DNaming_CheckHasSame (Draw_Interpretor& di,
43                                               Standard_Integer nb, 
44                                               const char** arg)
45 {
46   if(nb < 4) return 1;
47   TopoDS_Shape S1 = DBRep::Get(arg[1]);
48   if ( S1.IsNull() ) {
49     BRep_Builder aBuilder;
50     BRepTools::Read( S1, arg[1], aBuilder);
51     }
52   
53   TopoDS_Shape S2 = DBRep::Get(arg[2]);
54   if ( S2.IsNull() ) {
55     BRep_Builder aBuilder;
56     BRepTools::Read( S2, arg[2], aBuilder);
57     }
58   char M[8];
59   strcpy(M, arg[3]);
60   strtok(M, " \t");
61   TopAbs_ShapeEnum mod = TopAbs_FACE;
62   if(M[0] == 'F' || M[0] == 'f')
63     mod = TopAbs_FACE;
64   else if(M[0] == 'E' || M[0] == 'e')
65     mod = TopAbs_EDGE;
66   else if(M[0] == 'V' || M[0] == 'v')
67     mod = TopAbs_VERTEX;
68   else 
69     return 1;
70
71   TopExp_Explorer Exp1, Exp2;
72
73   TopTools_MapOfShape M1, M2;
74   for(Exp1.Init(S1, mod);Exp1.More();Exp1.Next()) {
75     M1.Add(Exp1.Current());
76   }
77   for(Exp2.Init(S2, mod);Exp2.More();Exp2.Next()) {
78     M2.Add(Exp2.Current());
79   }
80
81   TopTools_MapIteratorOfMapOfShape itr1(M1);
82   TopTools_MapIteratorOfMapOfShape itr2;
83   for(;itr1.More();itr1.Next()) {
84     const TopoDS_Shape& s1 = itr1.Key();
85     
86     for(itr2.Initialize(M2);itr2.More();itr2.Next()) {
87       const TopoDS_Shape& s2 = itr2.Key();
88       if(s1.IsSame(s2))
89         di << "Shapes " << arg[1]<< " and "<< arg[2]<< " have SAME subshapes\n";
90     }
91   }
92
93   return 0;
94 }           
95 //=======================================================================
96 //function : DNaming_TCopyShape
97 //purpose  : CopyShape  Shape1 [Shape2 ...] 
98 //           - for test ShapeCopy mechanism
99 //=======================================================================
100
101 static Standard_Integer DNaming_TCopyShape (Draw_Interpretor& di,
102                                               Standard_Integer nb, 
103                                               const char** arg)
104 {
105   TNaming_Translator TR;
106   if(nb < 2) return (1);
107
108   DNaming_DataMapOfShapeOfName aDMapOfShapeOfName;
109   for(Standard_Integer i= 1;i < nb; i++) {
110     TopoDS_Shape S = DBRep::Get(arg[i]);
111     TCollection_AsciiString name(arg[i]);
112     name.AssignCat("_c");
113     if ( S.IsNull() ) {
114       BRep_Builder aBuilder;
115       BRepTools::Read( S, arg[i], aBuilder);
116     }
117     
118 // Add to Map                
119     if(S.IsNull()) return(1);
120     else {
121       aDMapOfShapeOfName.Bind(S, name);
122       TR.Add(S);
123     }
124   } // for ...
125
126 // PERFORM 
127   TR.Perform();
128
129   if(TR.IsDone()){
130     di << "DNaming_CopyShape:: Copy is Done \n";
131
132     DNaming_DataMapIteratorOfDataMapOfShapeOfName itrn(aDMapOfShapeOfName);
133     for(;itrn.More();itrn.Next()) {
134       TCollection_AsciiString name = itrn.Value();
135       const TopoDS_Shape Result = TR.Copied(itrn.Key());
136       DBRep::Set(name.ToCString(), Result);
137       di.AppendElement(name.ToCString());
138     }
139     return 0;
140   }
141   di << "DNaming_CopyShape : Error\n";
142   return 1;
143 }
144
145 //=======================================================================
146 //function : DNaming_TCopyTool
147 //purpose  : CopyTool  Shape1 [Shape2 ...] 
148 //           - for test TNaming_CopyShape::CopyTool mechanism
149 //=======================================================================
150
151 static Standard_Integer DNaming_TCopyTool (Draw_Interpretor& di,
152                                            Standard_Integer nb, 
153                                            const char** arg)
154 {
155   if (nb < 2) {
156     di << "Usage: CopyTool Shape1 [Shape2] ...\n";
157     return 1;
158   }
159
160   Standard_Integer                           i;
161   TCollection_AsciiString                    aCopyNames;
162   BRep_Builder                               aBuilder;
163   TColStd_IndexedDataMapOfTransientTransient aMap;
164   TopoDS_Shape                               aResult;
165
166   for (i = 1; i < nb; i++) {
167     TopoDS_Shape aShape = DBRep::Get(arg[i]);
168
169     if (aShape.IsNull()) {
170       BRepTools::Read(aShape, arg[i], aBuilder);
171     }
172
173     if (aShape.IsNull()) {
174       di << arg[i] << " is neither a shape nor a BREP file. Skip it.\n";
175       continue;
176     }
177
178     // Perform copying.
179     TNaming_CopyShape::CopyTool(aShape, aMap, aResult);
180
181     // Draw result.
182     TCollection_AsciiString aName(arg[i]);
183
184     aName.AssignCat("_c");
185     DBRep::Set(aName.ToCString(), aResult);
186
187     // Compose all names of copies.
188     if (!aCopyNames.IsEmpty()) {
189       aCopyNames.AssignCat(" ");
190     }
191
192     aCopyNames.AssignCat(aName);
193   }
194
195   di << aCopyNames.ToCString() << "\n";
196
197   return 0;
198 }
199
200 //=======================================================================
201 //function : ToolsCommands
202 //purpose  : 
203 //=======================================================================
204
205 void DNaming::ToolsCommands (Draw_Interpretor& theCommands)
206 {  
207
208   static Standard_Boolean done = Standard_False;
209   if (done) return;
210   done = Standard_True;
211   const char* g = "Naming data commands " ;
212
213   theCommands.Add ("CopyShape", 
214                    "CopyShape (Shape1 [Shape2] ...)",
215                    __FILE__, DNaming_TCopyShape, g); 
216
217   theCommands.Add ("CopyTool", 
218                    "CopyTool Shape1 [Shape2] ...",
219                    __FILE__, DNaming_TCopyTool, g); 
220
221   theCommands.Add ("CheckSame", 
222                    "CheckSame (Shape1 Shape2 ExploMode[F|E|V])",
223                    __FILE__, DNaming_CheckHasSame, g); 
224  
225 }
226
227