0011758: TCollection strings are not memory safe as reported by Purify
[occt.git] / src / BRepTest / BRepTest_TopologyCommands.cxx
1 // Created on: 1993-07-22
2 // Created by: Remi LEQUETTE
3 // Copyright (c) 1993-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
21
22 #ifdef HAVE_CONFIG_H
23 # include <config.h>
24 #endif
25 #include <BRepTest.hxx>
26 #include <DBRep.hxx>
27 #include <Draw_Interpretor.hxx>
28 #include <DrawTrSurf.hxx>
29 #include <Geom_Plane.hxx>
30 #include <Draw_Appli.hxx>
31 #include <BRep_Builder.hxx>
32
33 #include <BRepAlgo_Fuse.hxx>
34 #include <BRepAlgo_Common.hxx>
35 #include <BRepAlgo_Cut.hxx>
36 #include <BRepAlgo_Section.hxx>
37
38 #include <BRepFilletAPI_MakeFillet.hxx>
39 #include <BRepBuilderAPI_MakeVertex.hxx>
40 #include <BRepPrimAPI_MakeHalfSpace.hxx>
41 #include <BRepAlgo_FaceRestrictor.hxx>
42 #include <BRepExtrema_ExtPF.hxx>
43 #include <BRepLProp_SLProps.hxx>
44 #include <TopTools_ListIteratorOfListOfShape.hxx>
45 #include <TopoDS.hxx>
46 #include <TopoDS_Edge.hxx>
47 #include <TopoDS_Compound.hxx>
48 #include <TopoDS_Wire.hxx>
49 #include <TopExp_Explorer.hxx>
50 #include <TopOpeBRepBuild_HBuilder.hxx>
51 #include <TopOpeBRepDS_HDataStructure.hxx>
52 #include <gp.hxx>
53 #include <gp_Pln.hxx>
54 #include <TopTools_IndexedMapOfShape.hxx>
55 #include <TopExp.hxx>
56
57 #ifdef HAVE_STRINGS_H
58 # include <strings.h>
59 #endif
60
61 //=======================================================================
62 // topop
63 //=======================================================================
64
65 static Standard_Integer topop(Draw_Interpretor& , Standard_Integer n, const char** a)
66 {
67   if (n < 4) return 1;
68
69   TopoDS_Shape s1 = DBRep::Get(a[2]);
70   TopoDS_Shape s2 = DBRep::Get(a[3]);
71
72   if (s1.IsNull() || s2.IsNull()) return 1;
73
74   TopoDS_Shape res;
75
76   if (*a[0] == 'f')
77     res = BRepAlgo_Fuse(s1,s2);
78   else if (*(a[0]+1) == 'o')
79     res = BRepAlgo_Common(s1,s2);
80   else 
81     res = BRepAlgo_Cut(s1,s2);
82
83   DBRep::Set(a[1],res);
84
85   return 0;
86 }
87
88
89 //=======================================================================
90 // section
91 //=======================================================================
92
93 static Standard_Integer section(Draw_Interpretor& , Standard_Integer n, const char** a)
94 {
95
96   if (n < 4) return 1;
97
98   TopoDS_Shape s1 = DBRep::Get(a[2]);
99   TopoDS_Shape s2 = DBRep::Get(a[3]);
100   
101   if (s1.IsNull() || s2.IsNull())
102     return 1;
103
104   BRepAlgo_Section Sec(s1, s2, Standard_False);
105   TopoDS_Shape res;
106
107   for (int i=4; i < n; i++) {
108     if (!strcasecmp(a[i], "-2d"))
109     {
110       Sec.ComputePCurveOn1(Standard_True);
111       Sec.ComputePCurveOn2(Standard_True);
112     } 
113     else if (!strcasecmp(a[i], "-2d1")) 
114     {
115       Sec.ComputePCurveOn1(Standard_True);
116       Sec.ComputePCurveOn2(Standard_False);
117     } 
118     else if (!strcasecmp(a[i], "-2d2")) 
119     {
120       Sec.ComputePCurveOn1(Standard_False);
121       Sec.ComputePCurveOn2(Standard_True);
122     } 
123     else if (!strcasecmp(a[i], "-no2d"))
124     {
125       Sec.ComputePCurveOn1(Standard_False);
126       Sec.ComputePCurveOn2(Standard_False);
127     } 
128     else if (!strcasecmp(a[i], "-a")) 
129       Sec.Approximation(Standard_True);
130     else if (strcasecmp(a[i], "-p"))
131     {
132       cout << "Unknown option: " << a[i] << endl;
133       return 1;
134     }
135   }
136
137   res = Sec.Shape();
138   
139   DBRep::Set(a[1],res);
140   
141   return 0;
142 }
143
144 //=======================================================================
145 // psection
146 //=======================================================================
147
148 static Standard_Integer psection(Draw_Interpretor& , Standard_Integer n, const char** a)
149 {
150   if (n < 4) return 1;
151
152   TopoDS_Shape s = DBRep::Get(a[2]);
153   if (s.IsNull()) return 1;
154
155   Handle(Geom_Surface) ps = DrawTrSurf::GetSurface(a[3]);
156   if (ps.IsNull()) return 1;
157
158   Handle(Geom_Plane) pg = Handle(Geom_Plane)::DownCast(ps);
159   if (pg.IsNull()) return 1;
160
161   const gp_Pln& p = pg->Pln();
162
163   TopoDS_Shape res = BRepAlgo_Section(s,p);
164
165   DBRep::Set(a[1],res);
166
167   return 0;
168 }
169
170 static Standard_Integer halfspace(Draw_Interpretor& di,
171                                   Standard_Integer n, const char** a)
172 {
173   if (n < 6) return 1;
174
175   // Le point indiquant le cote "matiere".
176   gp_Pnt RefPnt = gp_Pnt(Draw::Atof(a[3]),Draw::Atof(a[4]),Draw::Atof(a[5]));
177
178   TopoDS_Shape Face = DBRep::Get(a[2],TopAbs_FACE);
179   if ( Face.IsNull()) {
180     TopoDS_Shape Shell  = DBRep::Get(a[2],TopAbs_SHELL);
181     if (Shell.IsNull()) {
182       //cout << a[2] << " must be a face or a shell" << endl;
183       di << a[2] << " must be a face or a shell" << "\n";
184       return 1;
185     }
186     else {
187       BRepPrimAPI_MakeHalfSpace Half(TopoDS::Shell(Shell),RefPnt);
188       if ( Half.IsDone()) {
189         DBRep::Set(a[1],Half.Solid());
190       }
191       else {
192         //cout << " HalfSpace NotDone" << endl;
193         di << " HalfSpace NotDone" << "\n";
194         return 1;
195       }
196     }
197   }
198   else {
199     BRepPrimAPI_MakeHalfSpace Half(TopoDS::Face(Face),RefPnt);
200     if ( Half.IsDone()) {
201       DBRep::Set(a[1],Half.Solid());
202     }
203     else {
204       //cout << " HalfSpace NotDone" << endl;
205       di << " HalfSpace NotDone" << "\n";
206       return 1;
207     }
208   }
209   return 0;
210 }
211
212 //=======================================================================
213 //function : buildfaces
214 //purpose  : 
215 //=======================================================================
216
217 static Standard_Integer buildfaces(Draw_Interpretor& , Standard_Integer narg, const char** a)
218 {
219   if (narg < 4) return 1;
220   
221   TopoDS_Shape InputShape(DBRep::Get( a[2] ,TopAbs_FACE));
222   TopoDS_Face F = TopoDS::Face(InputShape);
223 //  TopoDS_Face F = TopoDS::Face(DBRep::Get(a[2],TopAbs_FACE));
224   BRepAlgo_FaceRestrictor FR;
225   FR.Init(F);
226   
227   for (Standard_Integer i = 3 ; i < narg ; i++) {
228     TopoDS_Shape InputWire(DBRep::Get(a[i],TopAbs_WIRE));
229     TopoDS_Wire W = TopoDS::Wire(InputWire);
230 //    TopoDS_Wire W = TopoDS::Wire(DBRep::Get(a[i],TopAbs_WIRE));
231     FR.Add(W);
232   }
233   FR.Perform();
234   if (!FR.IsDone()) return 1;
235   
236   TopoDS_Compound Res;
237   BRep_Builder BB;
238   BB.MakeCompound(Res);
239
240   for (; FR.More(); FR.Next()) {
241     TopoDS_Face FF = FR.Current();
242     BB.Add(Res,FF);
243     DBRep::Set(a[1],Res);
244   }
245   return 0;
246 }
247
248 //=======================================================================
249 //function : TopologyCommands
250 //purpose  : 
251 //=======================================================================
252
253 void  BRepTest::TopologyCommands(Draw_Interpretor& theCommands)
254 {
255   static Standard_Boolean done = Standard_False;
256   if (done) return;
257   done = Standard_True;
258
259   DBRep::BasicCommands(theCommands);
260
261   const char* g = "TOPOLOGY Topological operation commands";
262   
263   theCommands.Add("fuse","fuse result s1 s2",__FILE__,topop,g);
264   theCommands.Add("common","common result s1 s2",__FILE__,topop,g);
265   theCommands.Add("cut","cut result part tool",__FILE__,topop,g);
266   theCommands.Add("section","section result s1 s2 [-no2d/-2d/-2d1/-2d2] [-p/-a]",__FILE__,section,g);
267   theCommands.Add("psection","psection result s plane",__FILE__,psection,g);
268   theCommands.Add("halfspace","halfspace result face/shell x y z",__FILE__,halfspace,g);
269   theCommands.Add("buildfaces","buildfaces result faceReference wire1 wire2 ...",__FILE__,buildfaces,g);
270 }