0026786: Segmentation violation exception raised if a shape to be fixed is null
[occt.git] / src / ShapeProcessAPI / ShapeProcessAPI_ApplySequence.cxx
1 // Created on: 1999-06-22
2 // Created by: data exchange team
3 // Copyright (c) 1999-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
18 #include <Message_Messenger.hxx>
19 #include <Message_Msg.hxx>
20 #include <Resource_Manager.hxx>
21 #include <ShapeProcess.hxx>
22 #include <ShapeProcess_OperLibrary.hxx>
23 #include <ShapeProcess_ShapeContext.hxx>
24 #include <ShapeProcessAPI_ApplySequence.hxx>
25 #include <TCollection_AsciiString.hxx>
26 #include <TopoDS_Iterator.hxx>
27 #include <TopoDS_Shape.hxx>
28 #include <TopTools_DataMapIteratorOfDataMapOfShapeShape.hxx>
29
30 //=======================================================================
31 //function : ShapeProcessAPI_ApplySequence
32 //purpose  : 
33 //=======================================================================
34 ShapeProcessAPI_ApplySequence::ShapeProcessAPI_ApplySequence(const Standard_CString rscName, 
35                                                              const Standard_CString seqName)
36 {
37
38   myContext = new ShapeProcess_ShapeContext(rscName);
39   myContext->SetDetalisation ( TopAbs_FACE );
40   TCollection_AsciiString str ( seqName );
41
42   // initialize operators
43   ShapeProcess_OperLibrary::Init ();
44   
45   mySeq = str;
46 }
47
48 //=======================================================================
49 //function : Context
50 //purpose  : 
51 //=======================================================================
52
53 Handle(ShapeProcess_ShapeContext)& ShapeProcessAPI_ApplySequence::Context()
54 {
55   return myContext;
56 }
57
58 //=======================================================================
59 //function : PrepareShape
60 //purpose  : 
61 //=======================================================================
62
63 TopoDS_Shape ShapeProcessAPI_ApplySequence::PrepareShape(const TopoDS_Shape& shape,
64                                                          const Standard_Boolean /*fillmap*/,
65                                                          const TopAbs_ShapeEnum /*until*/)
66 {
67   if (shape.IsNull())
68     return shape;
69   Handle(Resource_Manager) rsc = myContext->ResourceManager();
70   myContext->Init(shape);
71   
72   TCollection_AsciiString str(mySeq);
73   str += ".exec.op";
74   if ( rsc->Find ( str.ToCString() ) ) {
75     ShapeProcess::Perform ( myContext, mySeq.ToCString() );
76   }
77   
78   return myContext->Result();
79 }
80
81 //=======================================================================
82 //function : ClearMap
83 //purpose  : 
84 //=======================================================================
85
86  void ShapeProcessAPI_ApplySequence::ClearMap() 
87 {
88   //myContext->Map().Clear();
89 }
90
91 //=======================================================================
92 //function : Map
93 //purpose  : 
94 //=======================================================================
95
96 const TopTools_DataMapOfShapeShape& ShapeProcessAPI_ApplySequence::Map() const
97 {
98   return myContext->Map();
99 }
100
101 //=======================================================================
102 //function : PrintPreparationResult
103 //purpose  : 
104 //=======================================================================
105
106 void ShapeProcessAPI_ApplySequence::PrintPreparationResult () const
107 {
108   Standard_Integer SS = 0, SN = 0, FF = 0, FS = 0, FN = 0;
109   for (TopTools_DataMapIteratorOfDataMapOfShapeShape It (myContext->Map()); It.More(); It.Next()) {
110     TopoDS_Shape keyshape = It.Key(), valueshape = It.Value();
111     if (keyshape.ShapeType() == TopAbs_SHELL) {
112       if (valueshape.IsNull()) SN++;
113       else SS++;
114     }
115     else if (keyshape.ShapeType() == TopAbs_FACE) {
116       if (valueshape.IsNull()) FN++;
117       else if (valueshape.ShapeType() == TopAbs_SHELL) FS++;
118       else FF++;
119     }
120   }
121   
122   Handle(Message_Messenger) aMessenger = myContext->Messenger();
123
124   // mapping
125   Message_Msg EPMSG100 ("PrResult.Print.MSG100"); //Mapping:
126   aMessenger->Send (EPMSG100, Message_Info, Standard_True);
127   Message_Msg TPMSG50 ("PrResult.Print.MSG50"); //  Shells:
128   aMessenger->Send (TPMSG50, Message_Info, Standard_True);
129   Message_Msg EPMSG110 ("PrResult.Print.MSG110"); //    Result is Shell                 : %d
130   EPMSG110.Arg (SS);
131   aMessenger->Send (EPMSG110, Message_Info, Standard_True);
132   Message_Msg EPMSG150 ("PrResult.Print.MSG150"); //    No Result                       : %d
133   EPMSG150.Arg (SN);
134   aMessenger->Send (EPMSG150, Message_Info, Standard_True);
135   
136   TCollection_AsciiString tmp110 (EPMSG110.Original()), tmp150  (EPMSG150.Original());
137   EPMSG110.Set (tmp110.ToCString());
138   EPMSG150.Set (tmp150.ToCString());
139
140   Message_Msg TPMSG55 ("PrResult.Print.MSG55"); //  Faces:
141   aMessenger->Send (TPMSG55, Message_Info, Standard_True);
142   Message_Msg EPMSG115 ("PrResult.Print.MSG115"); //    Result is Face                  : %d
143   EPMSG115.Arg (FF);
144   aMessenger->Send (EPMSG115, Message_Info, Standard_True);
145   EPMSG110.Arg (FS);
146   aMessenger->Send (EPMSG110, Message_Info, Standard_True);
147   EPMSG150.Arg (FN);
148   aMessenger->Send (EPMSG150, Message_Info, Standard_True);
149   
150   // preparation ratio
151   Standard_Real SPR = 1, FPR = 1;
152   Standard_Integer STotalR = SS, FTotalR  = FF + FS;
153   Standard_Integer NbS = STotalR + SN, NbF = FTotalR + FN;
154   if (NbS > 0) SPR = 1. * (NbS - SN) / NbS;
155   if (NbF > 0) FPR = 1. * (NbF - FN) / NbF;
156   Message_Msg PMSG200 ("PrResult.Print.MSG200"); //Preparation ratio:
157   aMessenger->Send (PMSG200, Message_Info, Standard_True);
158   Message_Msg PMSG205 ("PrResult.Print.MSG205"); //  Shells: %d per cent
159   PMSG205.Arg ((Standard_Integer) (100 * SPR));
160   aMessenger->Send (PMSG205, Message_Info, Standard_True);
161   Message_Msg PMSG210 ("PrResult.Print.MSG210"); //  Faces : %d per cent
162   PMSG210.Arg ((Standard_Integer) (100 * FPR));
163   aMessenger->Send (PMSG210, Message_Info, Standard_True);
164 }
165