Salome HOME
22752: [EDF] Provide explicit feedback on what has been done by Shape Processing...
[modules/geom.git] / src / ShHealOper / ShHealOper_ShapeProcess.cxx
1 // Copyright (C) 2007-2014  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 // File:      ShHealOper_ShapeProcess.cxx
24 // Created:   14.04.04 10:58:04
25 // Author:    Galina KULIKOVA
26 //
27 #include <ShHealOper_ShapeProcess.hxx>
28 #include <ShapeProcessAPI_ApplySequence.hxx>
29 #include <ShapeProcess_Context.hxx>
30 #include <TCollection_AsciiString.hxx>
31 #include <Resource_Manager.hxx>
32 #include <TopoDS_Shape.hxx>
33 #include <TopAbs_ShapeEnum.hxx>
34 #include <TopTools_DataMapOfShapeShape.hxx>
35 #include <ShapeProcess_ShapeContext.hxx>
36 #include <ShapeAnalysis_ShapeTolerance.hxx>
37 #include <Precision.hxx>
38 #include <BRep_Builder.hxx>
39 #include <TopoDS_Iterator.hxx>
40 #include <TColStd_IndexedDataMapOfTransientTransient.hxx>
41 #include <TNaming_CopyShape.hxx>
42 #include <TopTools_DataMapIteratorOfDataMapOfShapeShape.hxx>
43 #include <ShapeExtend_MsgRegistrator.hxx>
44 #include <ShapeExtend_DataMapOfShapeListOfMsg.hxx>
45 #include <ShapeExtend_DataMapIteratorOfDataMapOfShapeListOfMsg.hxx>
46 #include <Message_ListOfMsg.hxx>
47 #include <Message_ListIteratorOfListOfMsg.hxx>
48 #include <Message_Msg.hxx>
49
50 //=======================================================================
51 //function : ShHealOper_ShapeProcess()
52 //purpose  : Constructor
53 //=======================================================================
54
55 ShHealOper_ShapeProcess::ShHealOper_ShapeProcess (  ) :
56        myOperations(TCollection_AsciiString("ShHealing").ToCString(),
57                     TCollection_AsciiString("ShapeProcess").ToCString())
58 {
59    //myResource = new Resource_Manager("ShHealing");
60    myPrefix = "ShapeProcess";
61    mySaveHistoryMode = Standard_False;
62    myLevel = TopAbs_EDGE;
63    myDone = Standard_False;
64    myOperations.Context()->SetDetalisation ( TopAbs_EDGE );
65    myOperations.Context()->SetTraceLevel( 3 );
66 }
67
68 //=======================================================================
69 //function : ShHealOper_ShapeProcess
70 //purpose  : 
71 //=======================================================================
72
73 ShHealOper_ShapeProcess::ShHealOper_ShapeProcess (const TCollection_AsciiString& theNameResource,
74                                                   const TCollection_AsciiString& thePrefix ) :
75   myOperations(theNameResource.ToCString(),thePrefix.ToCString())
76 {
77   //myResource = new Resource_Manager(theNameResource);
78   myPrefix = thePrefix;
79   mySaveHistoryMode = Standard_False;
80   myLevel = TopAbs_EDGE;
81   myDone = Standard_False;
82 }
83
84 //=======================================================================
85 //function : Perform
86 //purpose  :
87 //=======================================================================
88 void ShHealOper_ShapeProcess::Perform(const TopoDS_Shape& theOldShape,
89                                       TopoDS_Shape&       theNewShape)
90 {
91   myMapModifications.Clear();
92   //ShapeProcessAPI_ApplySequence aOperations(myResource,myPrefix.ToCString());
93   //myDone = Standard_False;
94   myOperations.ClearMap();
95   ShapeAnalysis_ShapeTolerance aSatol;
96   Standard_Real ainitTol = aSatol.Tolerance(theOldShape,0);
97
98   // PAL6487: san -- preserve the original shape from being modified
99   TopoDS_Shape anOldShape;
100   TColStd_IndexedDataMapOfTransientTransient aMap;
101   TNaming_CopyShape::CopyTool(theOldShape, aMap, anOldShape);
102   // PAL6487: san -- preserve the original shape from being modified
103
104   theNewShape = myOperations.PrepareShape(anOldShape,mySaveHistoryMode,myLevel);
105   if(mySaveHistoryMode)
106     myMapModifications = myOperations.Map();
107   myDone = !anOldShape.IsSame(theNewShape);
108   if(!myDone) {
109     Standard_Real aendTol =aSatol.Tolerance(theNewShape,0);
110     myDone = (fabs(ainitTol - aendTol) > Precision::Confusion());
111     if ( myDone ) {
112       if ( ainitTol > aendTol )
113         myStatistics.AddModif( "Tolerance fixed (decreased)" );
114       else
115         myStatistics.AddModif( "Tolerance fixed (increased)" );
116     }
117   }
118
119   // fill myStatistics with messages
120   Handle(ShapeExtend_MsgRegistrator) msg = myOperations.Context()->Messages();
121   const ShapeExtend_DataMapOfShapeListOfMsg& shape2msg = msg->MapShape();
122   ShapeExtend_DataMapIteratorOfDataMapOfShapeListOfMsg s2msg( shape2msg );
123   for ( ; s2msg.More(); s2msg.Next() )
124   {
125     const Message_ListOfMsg & msgList = s2msg.Value();
126     Message_ListIteratorOfListOfMsg mIt( msgList );
127     for ( ; mIt.More(); mIt.Next() )
128     {
129       Message_Msg& m = mIt.Value();
130       TCollection_AsciiString txt = m.Get();
131       myStatistics.AddModif( txt.ToCString() );
132     }
133   }
134
135 //   for (TopTools_DataMapIteratorOfDataMapOfShapeShape It (myOperations.Context()->Map()); It.More(); It.Next()) {
136 //     TopoDS_Shape keyshape = It.Key(), valueshape = It.Value();
137 // /*    if (keyshape.ShapeType() == TopAbs_SHELL) {
138 //       if (valueshape.IsNull()) SN++;
139 //       else SS++;
140 //     }
141 //     else if (keyshape.ShapeType() == TopAbs_FACE)*/ {
142 //       if (valueshape.IsNull()) cout << "Removed" << endl;
143 //       else {
144 //         TopAbs::Print( keyshape.ShapeType(), cout ) << " -> ";
145 //         TopAbs::Print( valueshape.ShapeType(), cout ) << " IsSame()=" << keyshape.IsSame(valueshape) << endl;
146 //       }
147 //     }
148 //   }
149   
150 }
151 //=======================================================================
152 //function : SetOperators
153 //purpose  :
154 //=======================================================================
155
156 void ShHealOper_ShapeProcess::SetOperators(const TColStd_SequenceOfAsciiString& theSeqOperators)
157 {
158   
159   TCollection_AsciiString anameParam(myPrefix);
160   anameParam += ".";
161   anameParam += "exec.op";
162   TCollection_AsciiString aseq;
163   Standard_Integer i =1;
164   for( ; i <= theSeqOperators.Length(); i++) {
165     aseq += theSeqOperators.Value(i);
166     aseq += " ";
167   }
168     
169   myOperations.Context()->ResourceManager()->SetResource(anameParam.ToCString(),aseq.ToCString());
170 }
171 //=======================================================================
172 //function : SetParameter
173 //purpose  : 
174 //=======================================================================
175
176 void ShHealOper_ShapeProcess::SetParameter(const TCollection_AsciiString& theNameParam,
177                                            const TCollection_AsciiString& theVal)
178 {
179   TCollection_AsciiString anameParam(myPrefix);
180   anameParam += ".";
181   anameParam+= theNameParam;
182   if(theVal.IsIntegerValue())
183     myOperations.Context()->ResourceManager()->
184       SetResource(anameParam.ToCString(),theVal.IntegerValue());
185   else if(theVal.IsRealValue())
186     myOperations.Context()->ResourceManager()->
187       SetResource(anameParam.ToCString(),theVal.RealValue());
188   else
189     myOperations.Context()->ResourceManager()->
190       SetResource(anameParam.ToCString(),theVal.ToCString());
191
192 }
193 //=======================================================================
194 //function : GetOperators
195 //purpose  : 
196 //=======================================================================
197
198 Standard_Boolean ShHealOper_ShapeProcess::GetOperators(TColStd_SequenceOfAsciiString& theSeqOperators)
199 {
200   TCollection_AsciiString nameSeq(myPrefix);
201   nameSeq +=".exec.op";
202   if(!myOperations.Context()->ResourceManager()->Find(nameSeq.ToCString()))
203     return Standard_False;
204   
205   TCollection_AsciiString seqoper = myOperations.Context()->ResourceManager()->Value(nameSeq.ToCString());
206   Standard_Integer i;
207   for ( i=1; ; i++ ) {
208     TCollection_AsciiString anOper = seqoper.Token ( " \t,;", i );
209     if ( anOper.Length() <=0 ) break;
210     theSeqOperators.Append(anOper);
211   }
212   return (theSeqOperators.Length());
213 }
214 //=======================================================================
215 //function : GetParameter
216 //purpose  : 
217 //=======================================================================
218
219 Standard_Boolean ShHealOper_ShapeProcess::GetParameter(const TCollection_AsciiString& theNameParam,
220                                                        TCollection_AsciiString& theVal)
221 {
222   TCollection_AsciiString namePar(myPrefix);
223   namePar += ".";
224   namePar += theNameParam;
225   if(!myOperations.Context()->ResourceManager()->Find(namePar.ToCString()))
226     return Standard_False;
227
228   theVal = myOperations.Context()->ResourceManager()->Value(namePar.ToCString());
229   return Standard_True;
230 }