0024263: TNaming_CopyShape::CopyTool failure
[occt.git] / src / DNaming / DNaming_ToolsCommands.cxx
CommitLineData
b311480e 1// Created on: 1999-06-24
2// Created by: Sergey ZARITCHNY
3// Copyright (c) 1999-1999 Matra Datavision
4// Copyright (c) 1999-2012 OPEN CASCADE SAS
5//
6// The content of this file is subject to the Open CASCADE Technology Public
7// License Version 6.5 (the "License"). You may not use the content of this file
8// except in compliance with the License. Please obtain a copy of the License
9// at http://www.opencascade.org and read it completely before using this file.
10//
11// The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
12// main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
13//
14// The Original Code and all software distributed under the License is
15// distributed on an "AS IS" basis, without warranty of any kind, and the
16// Initial Developer hereby disclaims all such warranties, including without
17// limitation, any warranties of merchantability, fitness for a particular
18// purpose or non-infringement. Please see the License for the specific terms
19// and conditions governing the rights and limitations under the License.
20
7fd59977 21
22#include <Draw_Interpretor.hxx>
23#include <Draw.hxx>
24#include <DBRep.hxx>
25#include <DNaming.hxx>
26#include <BRepTools.hxx>
27#include <TopoDS_Face.hxx>
28#include <TopLoc_Location.hxx>
29#include <BRep_Builder.hxx>
30#include <gp_Pnt.hxx>
31#include <TopExp_Explorer.hxx>
32#include <TCollection_AsciiString.hxx>
33#include <TopAbs.hxx>
659b232a 34#include <TNaming_CopyShape.hxx>
7fd59977 35#include <TNaming_Translator.hxx>
36#include <TopTools_DataMapIteratorOfDataMapOfShapeShape.hxx>
37#include <DNaming_DataMapOfShapeOfName.hxx>
38#include <DNaming_DataMapIteratorOfDataMapOfShapeOfName.hxx>
39#include <TopTools_MapOfShape.hxx>
40#include <TopTools_MapIteratorOfMapOfShape.hxx>
41//=======================================================================
42//function : DNaming_CheckHasSame
43//purpose : CheckIsSame Shape1 Shape2
44// - for test ShapeCopy mechanism
45//=======================================================================
46
47static Standard_Integer DNaming_CheckHasSame (Draw_Interpretor& di,
48 Standard_Integer nb,
49 const char** arg)
50{
51 if(nb < 4) return 1;
52 TopoDS_Shape S1 = DBRep::Get(arg[1]);
53 if ( S1.IsNull() ) {
54 BRep_Builder aBuilder;
55 BRepTools::Read( S1, arg[1], aBuilder);
56 }
57
58 TopoDS_Shape S2 = DBRep::Get(arg[2]);
59 if ( S2.IsNull() ) {
60 BRep_Builder aBuilder;
61 BRepTools::Read( S2, arg[2], aBuilder);
62 }
63 char M[8];
64 strcpy(M, arg[3]);
65 strtok(M, " \t");
66 TopAbs_ShapeEnum mod = TopAbs_FACE;
67 if(M[0] == 'F' || M[0] == 'f')
68 mod = TopAbs_FACE;
69 else if(M[0] == 'E' || M[0] == 'e')
70 mod = TopAbs_EDGE;
71 else if(M[0] == 'V' || M[0] == 'v')
72 mod = TopAbs_VERTEX;
73 else
74 return 1;
75
76 TopExp_Explorer Exp1, Exp2;
77
78 TopTools_MapOfShape M1, M2;
79 for(Exp1.Init(S1, mod);Exp1.More();Exp1.Next()) {
80 M1.Add(Exp1.Current());
81 }
82 for(Exp2.Init(S2, mod);Exp2.More();Exp2.Next()) {
83 M2.Add(Exp2.Current());
84 }
85
86 TopTools_MapIteratorOfMapOfShape itr1(M1);
87 TopTools_MapIteratorOfMapOfShape itr2;
88 for(;itr1.More();itr1.Next()) {
89 const TopoDS_Shape& s1 = itr1.Key();
90
91 for(itr2.Initialize(M2);itr2.More();itr2.Next()) {
92 const TopoDS_Shape& s2 = itr2.Key();
93 if(s1.IsSame(s2))
94 di << "Shapes " << arg[1]<< " and "<< arg[2]<< " have SAME subshapes" <<"\n";
95 }
96 }
97
98 return 0;
99}
100//=======================================================================
101//function : DNaming_TCopyShape
102//purpose : CopyShape Shape1 [Shape2 ...]
103// - for test ShapeCopy mechanism
104//=======================================================================
105
106static Standard_Integer DNaming_TCopyShape (Draw_Interpretor& di,
107 Standard_Integer nb,
108 const char** arg)
109{
110 TNaming_Translator TR;
111 if(nb < 2) return (1);
112
113 DNaming_DataMapOfShapeOfName aDMapOfShapeOfName;
114 for(Standard_Integer i= 1;i < nb; i++) {
115 TopoDS_Shape S = DBRep::Get(arg[i]);
116 TCollection_AsciiString name(arg[i]);
117 name.AssignCat("_c");
118 if ( S.IsNull() ) {
119 BRep_Builder aBuilder;
120 BRepTools::Read( S, arg[i], aBuilder);
121 }
122
123// Add to Map
124 if(S.IsNull()) return(1);
125 else {
126 aDMapOfShapeOfName.Bind(S, name);
127 TR.Add(S);
128 }
129 } // for ...
130
131// PERFORM
132 TR.Perform();
133
134 if(TR.IsDone()){
135 di << "DNaming_CopyShape:: Copy is Done " << "\n";
136
137 DNaming_DataMapIteratorOfDataMapOfShapeOfName itrn(aDMapOfShapeOfName);
138 for(;itrn.More();itrn.Next()) {
139 TCollection_AsciiString name = itrn.Value();
140 const TopoDS_Shape& Result = TR.Copied(itrn.Key());
141 DBRep::Set(name.ToCString(), Result);
142 di.AppendElement(name.ToCString());
143 }
144 return 0;
145 }
146 di << "DNaming_CopyShape : Error" << "\n";
147 return 1;
148}
149
150//=======================================================================
659b232a 151//function : DNaming_TCopyTool
152//purpose : CopyTool Shape1 [Shape2 ...]
153// - for test TNaming_CopyShape::CopyTool mechanism
154//=======================================================================
155
156static Standard_Integer DNaming_TCopyTool (Draw_Interpretor& di,
157 Standard_Integer nb,
158 const char** arg)
159{
160 if (nb < 2) {
161 di << "Usage: CopyTool Shape1 [Shape2] ..." << "\n";
162 return 1;
163 }
164
165 Standard_Integer i;
166 TCollection_AsciiString aCopyNames;
167 BRep_Builder aBuilder;
168 TColStd_IndexedDataMapOfTransientTransient aMap;
169 TopoDS_Shape aResult;
170
171 for (i = 1; i < nb; i++) {
172 TopoDS_Shape aShape = DBRep::Get(arg[i]);
173
174 if (aShape.IsNull()) {
175 BRepTools::Read(aShape, arg[i], aBuilder);
176 }
177
178 if (aShape.IsNull()) {
179 di << arg[i] << " is neither a shape nor a BREP file. Skip it." << "\n";
180 continue;
181 }
182
183 // Perform copying.
184 TNaming_CopyShape::CopyTool(aShape, aMap, aResult);
185
186 // Draw result.
187 TCollection_AsciiString aName(arg[i]);
188
189 aName.AssignCat("_c");
190 DBRep::Set(aName.ToCString(), aResult);
191
192 // Compose all names of copies.
193 if (!aCopyNames.IsEmpty()) {
194 aCopyNames.AssignCat(" ");
195 }
196
197 aCopyNames.AssignCat(aName);
198 }
199
200 di << aCopyNames.ToCString() << "\n";
201
202 return 0;
203}
204
205//=======================================================================
7fd59977 206//function : ToolsCommands
207//purpose :
208//=======================================================================
209
210void DNaming::ToolsCommands (Draw_Interpretor& theCommands)
211{
212
213 static Standard_Boolean done = Standard_False;
214 if (done) return;
215 done = Standard_True;
216 const char* g = "Naming data commands " ;
7fd59977 217
218 theCommands.Add ("CopyShape",
219 "CopyShape (Shape1 [Shape2] ...)",
220 __FILE__, DNaming_TCopyShape, g);
221
659b232a 222 theCommands.Add ("CopyTool",
223 "CopyTool Shape1 [Shape2] ...",
224 __FILE__, DNaming_TCopyTool, g);
225
7fd59977 226 theCommands.Add ("CheckSame",
227 "CheckSame (Shape1 Shape2 ExploMode[F|E|V])",
228 __FILE__, DNaming_CheckHasSame, g);
229
230}
231
232