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