summaryrefslogtreecommitdiff
path: root/src/BRepTest/BRepTest_TopologyCommands.cxx
blob: 419cdbd7aa2327f8502588c634671bd380efa223 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
// File:	DBRep_6.cxx
// Created:	Thu Jul 22 16:40:43 1993
// Author:	Remi LEQUETTE
//		<rle@nonox>

#ifdef HAVE_CONFIG_H
# include <oce-config.h>
#endif
#include <BRepTest.hxx>
#include <DBRep.hxx>
#include <Draw_Interpretor.hxx>
#include <DrawTrSurf.hxx>
#include <Geom_Plane.hxx>
#include <Draw_Appli.hxx>
#include <BRep_Builder.hxx>

#include <BRepAlgo_Fuse.hxx>
#include <BRepAlgo_Common.hxx>
#include <BRepAlgo_Cut.hxx>
#include <BRepAlgo_Section.hxx>

#include <BRepFilletAPI_MakeFillet.hxx>
#include <BRepBuilderAPI_MakeVertex.hxx>
#include <BRepPrimAPI_MakeHalfSpace.hxx>
#include <BRepAlgo_FaceRestrictor.hxx>
#include <BRepExtrema_ExtPF.hxx>
#include <BRepLProp_SLProps.hxx>
#include <TopTools_ListIteratorOfListOfShape.hxx>
#include <TopoDS.hxx>
#include <TopoDS_Edge.hxx>
#include <TopoDS_Compound.hxx>
#include <TopoDS_Wire.hxx>
#include <TopExp_Explorer.hxx>
#include <TopOpeBRepBuild_HBuilder.hxx>
#include <TopOpeBRepDS_HDataStructure.hxx>
#include <gp.hxx>
#include <gp_Pln.hxx>
#include <TopTools_IndexedMapOfShape.hxx>
#include <TopExp.hxx>

#ifdef HAVE_STRINGS_H
# include <strings.h>
#endif

//=======================================================================
// topop
//=======================================================================

static Standard_Integer topop(Draw_Interpretor& , Standard_Integer n, const char** a)
{
  if (n < 4) return 1;

  TopoDS_Shape s1 = DBRep::Get(a[2]);
  TopoDS_Shape s2 = DBRep::Get(a[3]);

  if (s1.IsNull() || s2.IsNull()) return 1;

  TopoDS_Shape res;

  if (*a[0] == 'f')
    res = BRepAlgo_Fuse(s1,s2);
  else if (*(a[0]+1) == 'o')
    res = BRepAlgo_Common(s1,s2);
  else 
    res = BRepAlgo_Cut(s1,s2);

  DBRep::Set(a[1],res);

  return 0;
}


//=======================================================================
// section
//=======================================================================

static Standard_Integer section(Draw_Interpretor& , Standard_Integer n, const char** a)
{

  if (n < 4) return 1;

  TopoDS_Shape s1 = DBRep::Get(a[2]);
  TopoDS_Shape s2 = DBRep::Get(a[3]);
  
  if (s1.IsNull() || s2.IsNull())
    return 1;

  BRepAlgo_Section Sec(s1, s2, Standard_False);
  TopoDS_Shape res;

  if (n > 4) {
#ifdef WNT
    if (!strcasecmp(a[4],"-2d") || !strcasecmp(a[4], "-no2d")) {
#else 
    if (!strncasecmp(a[4],"-2d", 3) || !strcasecmp(a[4], "-no2d")) {
#endif
      if (!strcasecmp(a[4], "-2d")) {
	Sec.ComputePCurveOn1(Standard_True);
	Sec.ComputePCurveOn2(Standard_True);
      } else
	if (!strcasecmp(a[4], "-2d1")) 
	  Sec.ComputePCurveOn1(Standard_True);
	else
	  if (!strcasecmp(a[4], "-2d2")) 
	    Sec.ComputePCurveOn2(Standard_True);
	  else
	    if (strcasecmp(a[4], "-no2d"))
	      return 1;
      if(n > 5) {
	if (!strcasecmp(a[5], "-a")) 
	  Sec.Approximation(TopOpeBRepTool_APPROX);
	else 
	  if (strcasecmp(a[5], "-p"))
	    return 1;
      }
    } else {// fin a[4],"-2d"
      if (!strcasecmp(a[4], "-a") || !strcasecmp(a[4], "-p")) {
	if (!strcasecmp(a[4], "-a")) 
	  Sec.Approximation(TopOpeBRepTool_APPROX);
	if(n > 5) {
#ifdef WNT
	  if (!strcasecmp(a[5],"-2d") || !strcasecmp(a[5], "-no2d")) {
#else 
	  if (!strncasecmp(a[5],"-2d", 3) || !strcasecmp(a[5], "-no2d")) {
#endif
	    if (!strcasecmp(a[5], "-2d")) {
	      Sec.ComputePCurveOn1(Standard_True);
	      Sec.ComputePCurveOn2(Standard_True);
	    } else
	      if (!strcasecmp(a[5], "-2d1")) 
		Sec.ComputePCurveOn1(Standard_True);
	      else
		if (!strcasecmp(a[5], "-2d2")) 
		  Sec.ComputePCurveOn2(Standard_True);
		else
		  if (strcasecmp(a[5], "-no2d"))
		    return 1;
	  }
	}
      } else // fin a[4],"-a"
	return 1;
    }
  }// fin n > 4

  res = Sec.Shape();
  
  DBRep::Set(a[1],res);
  
  return 0;
}

//=======================================================================
// psection
//=======================================================================

static Standard_Integer psection(Draw_Interpretor& , Standard_Integer n, const char** a)
{
  if (n < 4) return 1;

  TopoDS_Shape s = DBRep::Get(a[2]);
  if (s.IsNull()) return 1;

  Handle(Geom_Surface) ps = DrawTrSurf::GetSurface(a[3]);
  if (ps.IsNull()) return 1;

  Handle(Geom_Plane) pg = Handle(Geom_Plane)::DownCast(ps);
  if (pg.IsNull()) return 1;

  const gp_Pln& p = pg->Pln();

  TopoDS_Shape res = BRepAlgo_Section(s,p);

  DBRep::Set(a[1],res);

  return 0;
}

static Standard_Integer halfspace(Draw_Interpretor& di,
				  Standard_Integer n, const char** a)
{
  if (n < 6) return 1;

  // Le point indiquant le cote "matiere".
  gp_Pnt RefPnt = gp_Pnt(atof(a[3]),atof(a[4]),atof(a[5]));

  TopoDS_Shape Face = DBRep::Get(a[2],TopAbs_FACE);
  if ( Face.IsNull()) {
    TopoDS_Shape Shell  = DBRep::Get(a[2],TopAbs_SHELL);
    if (Shell.IsNull()) {
      //cout << a[2] << " must be a face or a shell" << endl;
      di << a[2] << " must be a face or a shell" << "\n";
      return 1;
    }
    else {
      BRepPrimAPI_MakeHalfSpace Half(TopoDS::Shell(Shell),RefPnt);
      if ( Half.IsDone()) {
	DBRep::Set(a[1],Half.Solid());
      }
      else {
	//cout << " HalfSpace NotDone" << endl;
	di << " HalfSpace NotDone" << "\n";
	return 1;
      }
    }
  }
  else {
    BRepPrimAPI_MakeHalfSpace Half(TopoDS::Face(Face),RefPnt);
    if ( Half.IsDone()) {
      DBRep::Set(a[1],Half.Solid());
    }
    else {
      //cout << " HalfSpace NotDone" << endl;
      di << " HalfSpace NotDone" << "\n";
      return 1;
    }
  }
  return 0;
}

//=======================================================================
//function : buildfaces
//purpose  : 
//=======================================================================

static Standard_Integer buildfaces(Draw_Interpretor& , Standard_Integer narg, const char** a)
{
  if (narg < 4) return 1;
  
  TopoDS_Shape InputShape(DBRep::Get( a[2] ,TopAbs_FACE));
  TopoDS_Face F = TopoDS::Face(InputShape);
//  TopoDS_Face F = TopoDS::Face(DBRep::Get(a[2],TopAbs_FACE));
  BRepAlgo_FaceRestrictor FR;
  FR.Init(F);
  
  for (Standard_Integer i = 3 ; i < narg ; i++) {
    TopoDS_Shape InputWire(DBRep::Get(a[i],TopAbs_WIRE));
    TopoDS_Wire W = TopoDS::Wire(InputWire);
//    TopoDS_Wire W = TopoDS::Wire(DBRep::Get(a[i],TopAbs_WIRE));
    FR.Add(W);
  }
  FR.Perform();
  if (!FR.IsDone()) return 1;
  
  TopoDS_Compound Res;
  BRep_Builder BB;
  BB.MakeCompound(Res);

  for (; FR.More(); FR.Next()) {
    TopoDS_Face FF = FR.Current();
    BB.Add(Res,FF);
    DBRep::Set(a[1],Res);
  }
  return 0;
}

//=======================================================================
//function : TopologyCommands
//purpose  : 
//=======================================================================

void  BRepTest::TopologyCommands(Draw_Interpretor& theCommands)
{
  static Standard_Boolean done = Standard_False;
  if (done) return;
  done = Standard_True;

  DBRep::BasicCommands(theCommands);

  const char* g = "TOPOLOGY Topological operation commands";
  
  theCommands.Add("fuse","fuse result s1 s2",__FILE__,topop,g);
  theCommands.Add("common","common result s1 s2",__FILE__,topop,g);
  theCommands.Add("cut","cut result part tool",__FILE__,topop,g);
  theCommands.Add("section","section result s1 s2 [-no2d/-2d/-2d1/-2d2] [-p/-a]",__FILE__,section,g);
  theCommands.Add("psection","psection result s plane",__FILE__,psection,g);
  theCommands.Add("halfspace","halfspace result face/shell x y z",__FILE__,halfspace,g);
  theCommands.Add("buildfaces","buildfaces result faceReference wire1 wire2 ...",__FILE__,buildfaces,g);
}