summaryrefslogtreecommitdiff
path: root/src/ShapeProcess/ShapeProcess_ShapeContext.cxx
blob: 3fbd7a2a46d6f89c88d071df24647285c6906d57 (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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
// File:	ShapeProcess_ShapeContext.cxx
// Created:	Tue Aug 22 12:56:45 2000
// Author:	Andrey BETENEV
//		<abv@nnov.matra-dtv.fr>

#include <ShapeProcess_ShapeContext.ixx>

#include <TCollection_AsciiString.hxx>
#include <TopoDS_Iterator.hxx>
#include <TopTools_DataMapIteratorOfDataMapOfShapeShape.hxx>
#include <BRep_Builder.hxx>

#include <Message_Msg.hxx>
#include <Message_Messenger.hxx>
#include <Message_ListOfMsg.hxx>
#include <Message_ListIteratorOfListOfMsg.hxx>
#include <ShapeExtend_MsgRegistrator.hxx>
#include <ShapeExtend_DataMapOfShapeListOfMsg.hxx>

//=======================================================================
//function : ShapeProcess_ShapeContext
//purpose  : 
//=======================================================================

ShapeProcess_ShapeContext::ShapeProcess_ShapeContext (const Standard_CString file,
                                                      const Standard_CString seq) 
     : ShapeProcess_Context ( file, seq ), myUntil(TopAbs_FACE)
{
}

//=======================================================================
//function : ShapeProcess_ShapeContext
//purpose  : 
//=======================================================================

ShapeProcess_ShapeContext::ShapeProcess_ShapeContext (const TopoDS_Shape &S,
                                                      const Standard_CString file,
                                                      const Standard_CString seq) 
     : ShapeProcess_Context ( file, seq ), myUntil(TopAbs_FACE)
{
  Init ( S );
}

//=======================================================================
//function : Init
//purpose  : 
//=======================================================================

void ShapeProcess_ShapeContext::Init (const TopoDS_Shape &S)
{
  myMap.Clear();
  myMsg = new ShapeExtend_MsgRegistrator;
  myShape = S;
  myResult = myShape;//.Nullify();
}

//=======================================================================
//function : Shape
//purpose  : 
//=======================================================================

const TopoDS_Shape &ShapeProcess_ShapeContext::Shape () const
{
  return myShape;
}

//=======================================================================
//function : Result
//purpose  : 
//=======================================================================

const TopoDS_Shape &ShapeProcess_ShapeContext::Result () const
{
  return myResult;
}

//=======================================================================
//function : Map
//purpose  : 
//=======================================================================

const TopTools_DataMapOfShapeShape &ShapeProcess_ShapeContext::Map () const
{
  return myMap;
}

//=======================================================================
//function : Messages
//purpose  : 
//=======================================================================

Handle(ShapeExtend_MsgRegistrator) &ShapeProcess_ShapeContext::Messages () 
{
  return myMsg;
}

//=======================================================================
//function : Messages
//purpose  : 
//=======================================================================

const Handle(ShapeExtend_MsgRegistrator) &ShapeProcess_ShapeContext::Messages () const
{
  return myMsg;
}

//=======================================================================
//function : SetDetalisation
//purpose  : 
//=======================================================================

void ShapeProcess_ShapeContext::SetDetalisation (const TopAbs_ShapeEnum level)
{
  myUntil = level;
}

//=======================================================================
//function : GetDetalisation
//purpose  : 
//=======================================================================

TopAbs_ShapeEnum ShapeProcess_ShapeContext::GetDetalisation () const
{
  return myUntil;
}
       
//=======================================================================
//function : SetResult
//purpose  : 
//=======================================================================

void ShapeProcess_ShapeContext::SetResult (const TopoDS_Shape &res) 
{
  myResult = res;
  myMap.Bind ( myShape, myResult );
}

//=======================================================================
//function : RecordModification
//purpose  : 
//=======================================================================

/*
#ifdef DEB
static void DumpMap (const TopTools_DataMapOfShapeShape &map)
{
  cout << "----" << endl;
  cout << "Map:" << endl;
  for (TopTools_DataMapIteratorOfDataMapOfShapeShape It (map); It.More(); It.Next()) {
    TopoDS_Shape S0 = It.Key(), S = It.Value();
    cout << S0.TShape()->DynamicType()->Name() << "\t" << *(void**)&S0.TShape() << 
      "     \t->    " << S.TShape()->DynamicType()->Name() << "\t" << *(void**)&S.TShape() << endl;
  }
  cout << "----" << endl;
}	     
#endif
*/

static void RecModif (const TopoDS_Shape &S, 
		      const TopTools_DataMapOfShapeShape &repl,
		      TopTools_DataMapOfShapeShape &map,
		      const TopAbs_ShapeEnum until)
{
  TopoDS_Shape r = S;
  //gka  -modification to keep history for shape with location (OCC21617)
  TopLoc_Location aShLoc = S.Location();
  TopLoc_Location aNullLoc;
  r.Location(aNullLoc);

  if ( map.IsBound ( r ) ) 
    r = map.Find ( r );
  if ( ! r.IsNull() ) {
    TopoDS_Shape res = r;
    
    if ( repl.IsBound ( r.Located(aShLoc) ) ) {
      res = repl.Find (  r.Located(aShLoc) );
      // it is supposed that map is created for r having FORWARD orientation
      // hence, if it is reversed, result should be reversed too
      // INTERNAL or EXTERNAL orientations are not allowed
      if ( r.Orientation() != TopAbs_FORWARD ) 
	res.Reverse();
     
    }
    // Treat special case: if S was split, r will be a compound of
    // resulting shapes, each to be checked separately
    // It is supposed that repl does not contain such splitting
    else if ( r.ShapeType() < S.ShapeType() ) {
      TopoDS_Shape result = r.EmptyCopied();
      result.Orientation(TopAbs_FORWARD); // protect against INTERNAL or EXTERNAL shapes
      Standard_Boolean modif = Standard_False;
      BRep_Builder B;
      for ( TopoDS_Iterator it(r,Standard_False); it.More(); it.Next() ) {
	TopoDS_Shape sh = it.Value();
	if ( repl.IsBound(sh) ) {
	  TopoDS_Shape newsh = repl.Find(sh);
	  if ( ! newsh.IsNull() ) B.Add ( result, newsh );
	  modif = Standard_True;
	}
	else B.Add ( result, sh );
      }
      if ( modif ) res = result;
    }
    
    if ( res != r ) map.Bind ( S.Located(aNullLoc), res );
  }
  
  if ( until == TopAbs_SHAPE || S.ShapeType() >= until ) return;

  for ( TopoDS_Iterator it(S); it.More(); it.Next() ) {
    RecModif ( it.Value(), repl, map, until );
  }
}

void ShapeProcess_ShapeContext::RecordModification (const TopTools_DataMapOfShapeShape &repl)
{
  if ( repl.Extent() <=0 ) return;
  RecModif ( myShape, repl, myMap, myUntil );
  if ( myMap.IsBound(myShape) ) myResult = myMap.Find ( myShape );
#ifdef DEB
//  cout << "Modifier: " << endl; DumpMap (myMap);
#endif
}

//=======================================================================
//function : RecordModification
//purpose  : 
//=======================================================================

static void RecModif (const TopoDS_Shape &S, 
		      const Handle(ShapeBuild_ReShape) &repl,
		      const Handle(ShapeExtend_MsgRegistrator) &msg,
		      TopTools_DataMapOfShapeShape &map,
		      Handle(ShapeExtend_MsgRegistrator) &myMsg,
		      const TopAbs_ShapeEnum until)
{
  if(S.IsNull())
    return;
  //gka  -modification to keep history for shape with location (OCC21617)
  TopLoc_Location aNullLoc;
  TopoDS_Shape aS = S.Located(aNullLoc);
  TopoDS_Shape r = aS;
 
  if ( map.IsBound ( r ) ) 
    r = map.Find ( r );
  if ( ! r.IsNull() ) {
    TopoDS_Shape res;
    if ( repl->Status (r, res, Standard_True ) && res != r ) 
      map.Bind ( aS, res );
    
    // Treat special case: if S was split, r will be a compound of
    // resulting shapes, recursive procedure should be applied
    else if ( r.ShapeType() < S.ShapeType() ) {
      res = repl->Apply ( r, (TopAbs_ShapeEnum)((Standard_Integer)S.ShapeType()+1) );
      if ( res != r ) map.Bind ( aS, res );
    }
  }

  // update messages (messages must be taken from each level in the substitution map)
  if ( ! r.IsNull() && ! myMsg.IsNull() && 
       ! msg.IsNull() && msg->MapShape().Extent() >0 ) {
    TopoDS_Shape cur, next = r;
    const ShapeExtend_DataMapOfShapeListOfMsg& msgmap = msg->MapShape();
    do {
      cur = next;
      if (msgmap.IsBound (cur)) {
	const Message_ListOfMsg &msglist = msgmap.Find (cur);
	for (Message_ListIteratorOfListOfMsg iter (msglist); iter.More(); iter.Next()) {
	  myMsg->Send ( S, iter.Value(), Message_Warning );
	}
      }
      next = repl->Value (cur);
    } while ( ! next.IsNull() && cur != next);
  }

  if ( until == TopAbs_SHAPE || S.ShapeType() >= until ) return;
  
  for ( TopoDS_Iterator it(S,Standard_False,Standard_False); it.More(); it.Next() ) {
    RecModif ( it.Value(), repl, msg, map, myMsg, until );
  }
}

void ShapeProcess_ShapeContext::RecordModification (const Handle(ShapeBuild_ReShape) &repl,
                                                    const Handle(ShapeExtend_MsgRegistrator) &msg)
{
  
  RecModif ( myShape, repl, msg, myMap, myMsg, myUntil );
  if ( myMap.IsBound(myShape) ) 
  {
    myResult = myMap.Find ( myShape );
    myResult.Location(myShape.Location());
  }
#ifdef DEB
//  cout << "ReShape: " << endl; DumpMap (myMap);
#endif
}

//=======================================================================
//function : RecordModification
//purpose  : 
//=======================================================================

void ShapeProcess_ShapeContext::RecordModification (const Handle(ShapeBuild_ReShape) &repl)
{
  Handle(ShapeExtend_MsgRegistrator) msg;
  RecordModification ( repl, msg );
}

//=======================================================================
//function : AddMessage
//purpose  : 
//=======================================================================

void ShapeProcess_ShapeContext::AddMessage (const TopoDS_Shape &S,
                                            const Message_Msg &msg,
                                            const Message_Gravity grv) 
{
  if ( ! myMsg.IsNull() ) myMsg->Send ( S, msg, grv );
}

//=======================================================================
//function : RecordModification
//purpose  : 
//=======================================================================

static void ExplodeModifier (const TopoDS_Shape &S, 
			     const BRepTools_Modifier &repl,
			     TopTools_DataMapOfShapeShape &map,
			     const TopAbs_ShapeEnum until)
{
  TopoDS_Shape res = repl.ModifiedShape ( S );
  
  if ( res != S ) 
  {
   map.Bind ( S, res );
 }
  if ( until == TopAbs_SHAPE || S.ShapeType() >= until ) return;
  for ( TopoDS_Iterator it(S); it.More(); it.Next() ) {
    ExplodeModifier ( it.Value(), repl, map, until );
  }
}

void ShapeProcess_ShapeContext::RecordModification (const TopoDS_Shape &S, 
                                                    const BRepTools_Modifier &repl)
{
  TopTools_DataMapOfShapeShape map;
  ExplodeModifier ( S, repl, map, myUntil );
  RecordModification ( map );
}

//=======================================================================
//function : GetContinuity
//purpose  : 
//=======================================================================

Standard_Boolean ShapeProcess_ShapeContext::GetContinuity (const Standard_CString param,
                                                           GeomAbs_Shape &cont) const
{
  TCollection_AsciiString str;
  if ( ! GetString ( param, str ) ) return Standard_False;
  
  str.LeftAdjust();
  str.RightAdjust();
  str.UpperCase();
  
  if      ( str.IsEqual ( "C0" ) ) cont = GeomAbs_C0;
  else if ( str.IsEqual ( "G1" ) ) cont = GeomAbs_G1;
  else if ( str.IsEqual ( "C1" ) ) cont = GeomAbs_C1;
  else if ( str.IsEqual ( "G2" ) ) cont = GeomAbs_G2;
  else if ( str.IsEqual ( "C2" ) ) cont = GeomAbs_C2;
  else if ( str.IsEqual ( "C3" ) ) cont = GeomAbs_C3;
  else if ( str.IsEqual ( "CN" ) ) cont = GeomAbs_CN;
  else return Standard_False;
  return Standard_True;
}

//=======================================================================
//function : ContinuityVal
//purpose  : 
//=======================================================================

GeomAbs_Shape ShapeProcess_ShapeContext::ContinuityVal (const Standard_CString param,
                                                        const GeomAbs_Shape def) const
{
  GeomAbs_Shape val;
  return GetContinuity ( param, val ) ? val : def;
}

//=======================================================================
//function : PrintStatistics
//purpose  : 
//=======================================================================

void ShapeProcess_ShapeContext::PrintStatistics () const
{
  Standard_Integer SS = 0, SN = 0, FF = 0, FS = 0, FN = 0;
  for (TopTools_DataMapIteratorOfDataMapOfShapeShape It (myMap); It.More(); It.Next()) {
    TopoDS_Shape keyshape = It.Key(), valueshape = It.Value();
    if (keyshape.ShapeType() == TopAbs_SHELL)
      if (valueshape.IsNull()) SN++;
      else SS++;
    else if (keyshape.ShapeType() == TopAbs_FACE) {
      if (valueshape.IsNull()) FN++;
      else if (valueshape.ShapeType() == TopAbs_SHELL) FS++;
      else FF++;
    }
  }
  
  // mapping
  Message_Msg EPMSG100 ("PrResult.Print.MSG100"); //Mapping:
  Messenger()->Send (EPMSG100, Message_Info);
  Message_Msg TPMSG50 ("PrResult.Print.MSG50"); //  Shells:
  Messenger()->Send (TPMSG50, Message_Info);
  Message_Msg EPMSG110 ("PrResult.Print.MSG110"); //    Result is Shell                 : %d
  EPMSG110.Arg (SS);
  Messenger()->Send (EPMSG110, Message_Info);
  Message_Msg EPMSG150 ("PrResult.Print.MSG150"); //    No Result                       : %d
  EPMSG150.Arg (SN);
  Messenger()->Send (EPMSG150.Get(), Message_Info);
  
  TCollection_AsciiString tmp110 (EPMSG110.Original()), tmp150  (EPMSG150.Original());
  EPMSG110.Set (tmp110.ToCString());
  EPMSG150.Set (tmp150.ToCString());

  Message_Msg TPMSG55 ("PrResult.Print.MSG55"); //  Faces:
  Messenger()->Send (TPMSG55, Message_Info);
  Message_Msg EPMSG115 ("PrResult.Print.MSG115"); //    Result is Face                  : %d
  EPMSG115.Arg (FF);
  Messenger()->Send (EPMSG115, Message_Info);
  EPMSG110.Arg (FS);
  Messenger()->Send (EPMSG110, Message_Info);
  EPMSG150.Arg (FN);
  Messenger()->Send (EPMSG150, Message_Info);
  
  // preparation ratio
  Standard_Real SPR = 1, FPR = 1;
  Standard_Integer STotalR = SS, FTotalR  = FF + FS;
  Standard_Integer NbS = STotalR + SN, NbF = FTotalR + FN;
  if (NbS > 0) SPR = 1. * (NbS - SN) / NbS;
  if (NbF > 0) FPR = 1. * (NbF - FN) / NbF;
  Message_Msg PMSG200 ("PrResult.Print.MSG200"); //Preparation ratio:
  Messenger()->Send (PMSG200, Message_Info);
  Message_Msg PMSG205 ("PrResult.Print.MSG205"); //  Shells: %d per cent
  PMSG205.Arg ((Standard_Integer) (100. * SPR));
  Messenger()->Send (PMSG205, Message_Info);
  Message_Msg PMSG210 ("PrResult.Print.MSG210"); //  Faces : %d per cent
  PMSG210.Arg ((Standard_Integer) (100. * FPR));
  Messenger()->Send (PMSG210, Message_Info);
}