1 // Created on: 2000-08-22
2 // Created by: Andrey BETENEV
3 // Copyright (c) 2000-2014 OPEN CASCADE SAS
5 // This file is part of Open CASCADE Technology software library.
7 // This library is free software; you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License version 2.1 as published
9 // by the Free Software Foundation, with special exception defined in the file
10 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11 // distribution for complete text of the license and disclaimer of any warranty.
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
16 #include <ShapeProcess_ShapeContext.ixx>
18 #include <TCollection_AsciiString.hxx>
19 #include <TopoDS_Iterator.hxx>
20 #include <TopTools_DataMapIteratorOfDataMapOfShapeShape.hxx>
21 #include <BRep_Builder.hxx>
23 #include <Message_Msg.hxx>
24 #include <Message_Messenger.hxx>
25 #include <Message_ListOfMsg.hxx>
26 #include <Message_ListIteratorOfListOfMsg.hxx>
27 #include <ShapeExtend_MsgRegistrator.hxx>
28 #include <ShapeExtend_DataMapOfShapeListOfMsg.hxx>
30 //=======================================================================
31 //function : ShapeProcess_ShapeContext
33 //=======================================================================
35 ShapeProcess_ShapeContext::ShapeProcess_ShapeContext (const Standard_CString file,
36 const Standard_CString seq)
37 : ShapeProcess_Context ( file, seq ), myUntil(TopAbs_FACE)
41 //=======================================================================
42 //function : ShapeProcess_ShapeContext
44 //=======================================================================
46 ShapeProcess_ShapeContext::ShapeProcess_ShapeContext (const TopoDS_Shape &S,
47 const Standard_CString file,
48 const Standard_CString seq)
49 : ShapeProcess_Context ( file, seq ), myUntil(TopAbs_FACE)
54 //=======================================================================
57 //=======================================================================
59 void ShapeProcess_ShapeContext::Init (const TopoDS_Shape &S)
62 myMsg = new ShapeExtend_MsgRegistrator;
64 myResult = myShape;//.Nullify();
67 //=======================================================================
70 //=======================================================================
72 const TopoDS_Shape &ShapeProcess_ShapeContext::Shape () const
77 //=======================================================================
80 //=======================================================================
82 const TopoDS_Shape &ShapeProcess_ShapeContext::Result () const
87 //=======================================================================
90 //=======================================================================
92 const TopTools_DataMapOfShapeShape &ShapeProcess_ShapeContext::Map () const
97 //=======================================================================
100 //=======================================================================
102 Handle(ShapeExtend_MsgRegistrator) &ShapeProcess_ShapeContext::Messages ()
107 //=======================================================================
108 //function : Messages
110 //=======================================================================
112 const Handle(ShapeExtend_MsgRegistrator) &ShapeProcess_ShapeContext::Messages () const
117 //=======================================================================
118 //function : SetDetalisation
120 //=======================================================================
122 void ShapeProcess_ShapeContext::SetDetalisation (const TopAbs_ShapeEnum level)
127 //=======================================================================
128 //function : GetDetalisation
130 //=======================================================================
132 TopAbs_ShapeEnum ShapeProcess_ShapeContext::GetDetalisation () const
137 //=======================================================================
138 //function : SetResult
140 //=======================================================================
142 void ShapeProcess_ShapeContext::SetResult (const TopoDS_Shape &res)
145 myMap.Bind ( myShape, myResult );
148 //=======================================================================
149 //function : RecordModification
151 //=======================================================================
155 static void DumpMap (const TopTools_DataMapOfShapeShape &map)
157 cout << "----" << endl;
158 cout << "Map:" << endl;
159 for (TopTools_DataMapIteratorOfDataMapOfShapeShape It (map); It.More(); It.Next()) {
160 TopoDS_Shape S0 = It.Key(), S = It.Value();
161 cout << S0.TShape()->DynamicType()->Name() << "\t" << *(void**)&S0.TShape() <<
162 " \t-> " << S.TShape()->DynamicType()->Name() << "\t" << *(void**)&S.TShape() << endl;
164 cout << "----" << endl;
169 static void RecModif (const TopoDS_Shape &S,
170 const TopTools_DataMapOfShapeShape &repl,
171 const Handle(ShapeExtend_MsgRegistrator) &msg,
172 TopTools_DataMapOfShapeShape &map,
173 Handle(ShapeExtend_MsgRegistrator) &myMsg,
174 const TopAbs_ShapeEnum until)
177 //gka -modification to keep history for shape with location (OCC21617)
178 TopLoc_Location aShLoc = S.Location();
179 TopLoc_Location aNullLoc;
180 r.Location(aNullLoc);
182 if ( map.IsBound ( r ) )
184 if ( ! r.IsNull() ) {
185 TopoDS_Shape res = r;
187 if ( repl.IsBound ( r.Located(aShLoc) ) ) {
188 res = repl.Find ( r.Located(aShLoc) );
189 // it is supposed that map is created for r having FORWARD orientation
190 // hence, if it is reversed, result should be reversed too
191 // INTERNAL or EXTERNAL orientations are not allowed
192 if ( r.Orientation() != TopAbs_FORWARD )
196 // Treat special case: if S was split, r will be a compound of
197 // resulting shapes, each to be checked separately
198 // It is supposed that repl does not contain such splitting
199 else if ( r.ShapeType() < S.ShapeType() ) {
200 TopoDS_Shape result = r.EmptyCopied();
201 result.Orientation(TopAbs_FORWARD); // protect against INTERNAL or EXTERNAL shapes
202 Standard_Boolean modif = Standard_False;
204 for ( TopoDS_Iterator it(r,Standard_False); it.More(); it.Next() ) {
205 TopoDS_Shape sh = it.Value();
206 if ( repl.IsBound(sh) ) {
207 TopoDS_Shape newsh = repl.Find(sh);
208 if ( ! newsh.IsNull() ) B.Add ( result, newsh );
209 modif = Standard_True;
211 else B.Add ( result, sh );
215 result.Closed (BRep_Tool::IsClosed (result));
220 if ( res != r ) map.Bind ( S.Located(aNullLoc), res );
223 // update messages (messages must be taken from each level in the substitution map)
224 if ( ! r.IsNull() && ! myMsg.IsNull() &&
225 ! msg.IsNull() && msg->MapShape().Extent() >0 )
227 const ShapeExtend_DataMapOfShapeListOfMsg& msgmap = msg->MapShape();
228 if ( msgmap.IsBound( r )) {
229 const Message_ListOfMsg &msglist = msgmap.Find (r);
230 for (Message_ListIteratorOfListOfMsg iter (msglist); iter.More(); iter.Next())
231 myMsg->Send ( S, iter.Value(), Message_Warning );
233 else if ( msgmap.IsBound( S )) {
234 const Message_ListOfMsg &msglist = msgmap.Find (S);
235 for (Message_ListIteratorOfListOfMsg iter (msglist); iter.More(); iter.Next())
236 myMsg->Send ( S, iter.Value(), Message_Warning );
240 if ( until == TopAbs_SHAPE || S.ShapeType() >= until ) return;
242 for ( TopoDS_Iterator it(S); it.More(); it.Next() ) {
243 RecModif ( it.Value(), repl, msg, map, myMsg, until );
247 void ShapeProcess_ShapeContext::RecordModification (const TopTools_DataMapOfShapeShape &repl,
248 const Handle(ShapeExtend_MsgRegistrator)& msg)
250 if ( repl.Extent() <=0 ) return;
251 RecModif ( myShape, repl, msg, myMap, myMsg, myUntil );
252 if ( myMap.IsBound(myShape) ) myResult = myMap.Find ( myShape );
254 // cout << "Modifier: " << endl; DumpMap (myMap);
258 //=======================================================================
259 //function : RecordModification
261 //=======================================================================
263 static void RecModif (const TopoDS_Shape &S,
264 const Handle(ShapeBuild_ReShape) &repl,
265 const Handle(ShapeExtend_MsgRegistrator) &msg,
266 TopTools_DataMapOfShapeShape &map,
267 Handle(ShapeExtend_MsgRegistrator) &myMsg,
268 const TopAbs_ShapeEnum until)
272 //gka -modification to keep history for shape with location (OCC21617)
273 TopLoc_Location aNullLoc;
274 TopoDS_Shape aS = S.Located(aNullLoc);
277 if ( map.IsBound ( r ) )
279 if ( ! r.IsNull() ) {
281 if ( repl->Status (r, res, Standard_True ) && res != r )
282 map.Bind ( aS, res );
284 // Treat special case: if S was split, r will be a compound of
285 // resulting shapes, recursive procedure should be applied
286 else if ( r.ShapeType() < S.ShapeType() ) {
287 res = repl->Apply ( r, (TopAbs_ShapeEnum)((Standard_Integer)S.ShapeType()+1) );
288 if ( res != r ) map.Bind ( aS, res );
292 // update messages (messages must be taken from each level in the substitution map)
293 if ( ! r.IsNull() && ! myMsg.IsNull() &&
294 ! msg.IsNull() && msg->MapShape().Extent() >0 ) {
295 TopoDS_Shape cur, next = r;
296 const ShapeExtend_DataMapOfShapeListOfMsg& msgmap = msg->MapShape();
297 if ( msgmap.IsBound( S ))
301 if (msgmap.IsBound (cur)) {
302 const Message_ListOfMsg &msglist = msgmap.Find (cur);
303 for (Message_ListIteratorOfListOfMsg iter (msglist); iter.More(); iter.Next()) {
304 myMsg->Send ( S, iter.Value(), Message_Warning );
307 next = repl->Value (cur);
308 } while ( ! next.IsNull() && cur != next);
311 if ( until == TopAbs_SHAPE || S.ShapeType() >= until ) return;
313 for ( TopoDS_Iterator it(S,Standard_False/*,Standard_False*/); it.More(); it.Next() ) {
314 RecModif ( it.Value(), repl, msg, map, myMsg, until );
318 void ShapeProcess_ShapeContext::RecordModification (const Handle(ShapeBuild_ReShape) &repl,
319 const Handle(ShapeExtend_MsgRegistrator) &msg)
322 RecModif ( myShape, repl, msg, myMap, myMsg, myUntil );
323 if ( myMap.IsBound(myShape) )
325 myResult = myMap.Find ( myShape );
326 myResult.Location(myShape.Location());
329 // cout << "ReShape: " << endl; DumpMap (myMap);
333 //=======================================================================
334 //function : RecordModification
336 //=======================================================================
338 void ShapeProcess_ShapeContext::RecordModification (const Handle(ShapeBuild_ReShape) &repl)
340 Handle(ShapeExtend_MsgRegistrator) msg;
341 RecordModification ( repl, msg );
344 //=======================================================================
345 //function : AddMessage
347 //=======================================================================
349 void ShapeProcess_ShapeContext::AddMessage (const TopoDS_Shape &S,
350 const Message_Msg &msg,
351 const Message_Gravity grv)
353 if ( ! myMsg.IsNull() ) myMsg->Send ( S, msg, grv );
356 //=======================================================================
357 //function : RecordModification
359 //=======================================================================
361 static void ExplodeModifier (const TopoDS_Shape &S,
362 const BRepTools_Modifier &repl,
363 TopTools_DataMapOfShapeShape &map,
364 const TopAbs_ShapeEnum until)
366 TopoDS_Shape res = repl.ModifiedShape ( S );
372 if ( until == TopAbs_SHAPE || S.ShapeType() >= until ) return;
373 for ( TopoDS_Iterator it(S); it.More(); it.Next() ) {
374 ExplodeModifier ( it.Value(), repl, map, until );
378 void ShapeProcess_ShapeContext::RecordModification (const TopoDS_Shape &S,
379 const BRepTools_Modifier &repl,
380 const Handle(ShapeExtend_MsgRegistrator)& msg)
382 TopTools_DataMapOfShapeShape map;
383 ExplodeModifier ( S, repl, map, myUntil );
384 RecordModification ( map, msg );
387 //=======================================================================
388 //function : GetContinuity
390 //=======================================================================
392 Standard_Boolean ShapeProcess_ShapeContext::GetContinuity (const Standard_CString param,
393 GeomAbs_Shape &cont) const
395 TCollection_AsciiString str;
396 if ( ! GetString ( param, str ) ) return Standard_False;
402 if ( str.IsEqual ( "C0" ) ) cont = GeomAbs_C0;
403 else if ( str.IsEqual ( "G1" ) ) cont = GeomAbs_G1;
404 else if ( str.IsEqual ( "C1" ) ) cont = GeomAbs_C1;
405 else if ( str.IsEqual ( "G2" ) ) cont = GeomAbs_G2;
406 else if ( str.IsEqual ( "C2" ) ) cont = GeomAbs_C2;
407 else if ( str.IsEqual ( "C3" ) ) cont = GeomAbs_C3;
408 else if ( str.IsEqual ( "CN" ) ) cont = GeomAbs_CN;
409 else return Standard_False;
410 return Standard_True;
413 //=======================================================================
414 //function : ContinuityVal
416 //=======================================================================
418 GeomAbs_Shape ShapeProcess_ShapeContext::ContinuityVal (const Standard_CString param,
419 const GeomAbs_Shape def) const
422 return GetContinuity ( param, val ) ? val : def;
425 //=======================================================================
426 //function : PrintStatistics
428 //=======================================================================
430 void ShapeProcess_ShapeContext::PrintStatistics () const
432 Standard_Integer SS = 0, SN = 0, FF = 0, FS = 0, FN = 0;
433 for (TopTools_DataMapIteratorOfDataMapOfShapeShape It (myMap); It.More(); It.Next()) {
434 TopoDS_Shape keyshape = It.Key(), valueshape = It.Value();
435 if (keyshape.ShapeType() == TopAbs_SHELL)
436 if (valueshape.IsNull()) SN++;
438 else if (keyshape.ShapeType() == TopAbs_FACE)
440 if (valueshape.IsNull()) FN++;
441 else if (valueshape.ShapeType() == TopAbs_SHELL) FS++;
447 Message_Msg EPMSG100 ("PrResult.Print.MSG100"); //Mapping:
448 Messenger()->Send (EPMSG100, Message_Info);
449 Message_Msg TPMSG50 ("PrResult.Print.MSG50"); // Shells:
450 Messenger()->Send (TPMSG50, Message_Info);
451 Message_Msg EPMSG110 ("PrResult.Print.MSG110"); // Result is Shell : %d
453 Messenger()->Send (EPMSG110, Message_Info);
454 Message_Msg EPMSG150 ("PrResult.Print.MSG150"); // No Result : %d
456 Messenger()->Send (EPMSG150.Get(), Message_Info);
458 TCollection_AsciiString tmp110 (EPMSG110.Original()), tmp150 (EPMSG150.Original());
459 EPMSG110.Set (tmp110.ToCString());
460 EPMSG150.Set (tmp150.ToCString());
462 Message_Msg TPMSG55 ("PrResult.Print.MSG55"); // Faces:
463 Messenger()->Send (TPMSG55, Message_Info);
464 Message_Msg EPMSG115 ("PrResult.Print.MSG115"); // Result is Face : %d
466 Messenger()->Send (EPMSG115, Message_Info);
468 Messenger()->Send (EPMSG110, Message_Info);
470 Messenger()->Send (EPMSG150, Message_Info);
473 Standard_Real SPR = 1, FPR = 1;
474 Standard_Integer STotalR = SS, FTotalR = FF + FS;
475 Standard_Integer NbS = STotalR + SN, NbF = FTotalR + FN;
476 if (NbS > 0) SPR = 1. * (NbS - SN) / NbS;
477 if (NbF > 0) FPR = 1. * (NbF - FN) / NbF;
478 Message_Msg PMSG200 ("PrResult.Print.MSG200"); //Preparation ratio:
479 Messenger()->Send (PMSG200, Message_Info);
480 Message_Msg PMSG205 ("PrResult.Print.MSG205"); // Shells: %d per cent
481 PMSG205.Arg ((Standard_Integer) (100. * SPR));
482 Messenger()->Send (PMSG205, Message_Info);
483 Message_Msg PMSG210 ("PrResult.Print.MSG210"); // Faces : %d per cent
484 PMSG210.Arg ((Standard_Integer) (100. * FPR));
485 Messenger()->Send (PMSG210, Message_Info);