0022746: Progress indicator in ShapeHealing
[occt.git] / src / IGESControl / IGESControl_Writer.cxx
1 //cky 16.01.99 Remove couts.
2 //rln 28.12.98 CCI60005
3
4 #include <Standard_Stream.hxx>
5
6 #include <IGESControl_Writer.ixx>
7 #include <IGESControl_Controller.hxx>
8 #include <IGESSelect_WorkLibrary.hxx>
9 #include <BRepToIGES_BREntity.hxx>
10 #include <BRepToIGESBRep_Entity.hxx>
11 #include <Geom_Curve.hxx>
12 #include <Geom_Surface.hxx>
13 #include <GeomToIGES_GeomCurve.hxx>
14 #include <GeomToIGES_GeomSurface.hxx>
15 #include <IGESData_IGESWriter.hxx>
16 #include <XSControl_TransferWriter.hxx>
17
18 #include <Interface_Macros.hxx>
19 #include <Interface_Static.hxx>
20 #include <ShapeAnalysis_ShapeTolerance.hxx>
21
22 #include <gp_XYZ.hxx>
23 #include <Bnd_Box.hxx>
24 #include <BRepBndLib.hxx>
25 #include <GeomAdaptor_Curve.hxx>
26 #include <GeomAdaptor_Surface.hxx>
27 #include <BndLib_Add3dCurve.hxx>
28 #include <BndLib_AddSurface.hxx>
29 #include <XSAlgo.hxx>
30 #include <XSAlgo_AlgoContainer.hxx>
31 #include <TopExp_Explorer.hxx>
32 #include <Message_ProgressIndicator.hxx>
33 #include <errno.h>
34
35 IGESControl_Writer::IGESControl_Writer ()
36     :  theTP (new Transfer_FinderProcess(10000)) ,
37        thedit (IGESSelect_WorkLibrary::DefineProtocol()) ,
38        thecr (0) , thest (Standard_False)
39 {
40 //  faudrait aussi (?) prendre les parametres par defaut ... ?
41   IGESControl_Controller::Init();
42   thedit.SetUnitName(Interface_Static::CVal ("write.iges.unit"));
43   thedit.ApplyUnit(); 
44   themod = thedit.Model();
45 }
46
47 IGESControl_Writer::IGESControl_Writer
48   (const Standard_CString unit, const Standard_Integer modecr)
49     :  theTP (new Transfer_FinderProcess(10000)) ,
50        thedit (IGESSelect_WorkLibrary::DefineProtocol()) ,
51        thecr (modecr) , thest (Standard_False)
52 {
53 //  faudrait aussi (?) prendre les parametres par defaut ... ?
54   IGESControl_Controller::Init();
55   thedit.SetUnitName(unit);
56   thedit.ApplyUnit();
57   themod = thedit.Model();
58 }
59
60 IGESControl_Writer::IGESControl_Writer
61   (const Handle(IGESData_IGESModel)& model, const Standard_Integer modecr)
62     :  theTP (new Transfer_FinderProcess(10000)) ,
63        themod (model) , 
64        thedit (model,IGESSelect_WorkLibrary::DefineProtocol()) ,
65        thecr (modecr) , thest (Standard_False)     {  }
66
67 Handle(IGESData_IGESModel) IGESControl_Writer::Model () const
68 {
69   return themod;
70 }
71
72 Handle(Transfer_FinderProcess) IGESControl_Writer::TransferProcess () const
73 {
74   return theTP;
75 }
76
77 void IGESControl_Writer::SetTransferProcess
78   (const Handle(Transfer_FinderProcess)& TP)
79 {
80   theTP = TP;
81 }
82
83 Standard_Boolean IGESControl_Writer::AddShape (const TopoDS_Shape& theShape)
84 {
85   if (theShape.IsNull()) return Standard_False;
86   
87   // for progress indication
88   Handle(Message_ProgressIndicator) progress = theTP->GetProgress();
89   if ( ! progress.IsNull() ) { 
90     Standard_Integer nbfaces=0;
91     for( TopExp_Explorer exp(theShape,TopAbs_FACE); exp.More(); exp.Next() )
92       nbfaces++;
93     progress->SetScale ( "Faces", 0, nbfaces, 1 );
94   }
95   
96   XSAlgo::AlgoContainer()->PrepareForTransfer();
97   
98   //  modified by NIZHNY-EAP Tue Aug 29 11:16:54 2000 ___BEGIN___
99   Handle(Standard_Transient) info;
100   Standard_Real Tol = Interface_Static::RVal("write.precision.val");
101   Standard_Real maxTol = Interface_Static::RVal("read.maxprecision.val");
102   TopoDS_Shape Shape = XSAlgo::AlgoContainer()->ProcessShape( theShape, Tol, maxTol, 
103                                                               "write.iges.resource.name", 
104                                                               "write.iges.sequence", info,
105                                                               progress );
106   //  modified by NIZHNY-EAP Tue Aug 29 11:17:01 2000 ___END___
107   Handle(IGESData_IGESEntity) ent; 
108   BRepToIGES_BREntity   B0;  B0.SetTransferProcess(theTP); B0.SetModel(themod);
109   BRepToIGESBRep_Entity B1;  B1.SetTransferProcess(theTP); B1.SetModel(themod);
110   if (thecr) ent = B1.TransferShape(Shape);
111   else       ent = B0.TransferShape(Shape);
112 //  modified by NIZHNY-EAP Tue Aug 29 11:37:18 2000 ___BEGIN___
113   XSAlgo::AlgoContainer()->MergeTransferInfo(theTP, info);
114 //  modified by NIZHNY-EAP Tue Aug 29 11:37:25 2000 ___END___
115   
116   //22.10.98 gka BUC60080
117
118   Standard_Integer oldnb = themod->NbEntities();
119   Standard_Boolean aent = AddEntity (ent);
120   Standard_Integer newnb = themod->NbEntities();
121
122   Standard_Real oldtol = themod->GlobalSection().Resolution(), newtol;
123   
124   Standard_Integer tolmod = Interface_Static::IVal("write.precision.mode");
125   if (tolmod == 2)
126     newtol = Interface_Static::RVal("write.precision.val");
127   else {
128     ShapeAnalysis_ShapeTolerance stu; 
129     Standard_Real Tolv = stu.Tolerance (Shape, tolmod, TopAbs_VERTEX);
130     Standard_Real Tole = stu.Tolerance (Shape, tolmod, TopAbs_EDGE); 
131
132     if (tolmod == 0 ) {   //Average
133       Standard_Real Tol1 = (Tolv + Tole) / 2;
134       newtol = (oldtol * oldnb + Tol1 * (newnb - oldnb)) / newnb;
135     }
136     else if (tolmod < 0) {//Least
137       newtol = Min (Tolv, Tole);
138       if (oldnb > 0) newtol = Min (oldtol, newtol);
139     }
140     else {                //Greatest
141       newtol = Max (Tolv, Tole);
142       if (oldnb > 0) newtol = Max (oldtol, newtol);
143     }
144   }
145   
146   IGESData_GlobalSection gs = themod->GlobalSection();
147   gs.SetResolution (newtol / gs.UnitValue());//rln 28.12.98 CCI60005
148
149   //#34 22.10.98 rln BUC60081
150   Bnd_Box box;
151   BRepBndLib::Add (Shape, box);
152   Standard_Real aXmin, aYmin, aZmin, aXmax, aYmax, aZmax;
153   box.Get (aXmin, aYmin, aZmin, aXmax, aYmax, aZmax);
154   gs.MaxMaxCoords (gp_XYZ (aXmax / gs.UnitValue(),
155                            aYmax / gs.UnitValue(),
156                            aZmax / gs.UnitValue()));
157   gs.MaxMaxCoords (gp_XYZ (aXmin / gs.UnitValue(),
158                            aYmin / gs.UnitValue(),
159                            aZmin / gs.UnitValue()));
160
161   themod->SetGlobalSection(gs);
162
163   return aent;
164 }
165
166 Standard_Boolean IGESControl_Writer::AddGeom (const Handle(Standard_Transient)& geom)
167 {
168   if (geom.IsNull() || !geom->IsKind (STANDARD_TYPE (Geom_Geometry)))
169     return Standard_False;
170   DeclareAndCast(Geom_Curve,Curve,geom);
171   DeclareAndCast(Geom_Surface,Surf,geom);
172   Handle(IGESData_IGESEntity) ent; 
173
174 //  On reconnait : Curve et Surface de Geom
175 //   quid de Point; Geom2d ?
176
177 //  GeomToIGES_GeomPoint GP;
178   GeomToIGES_GeomCurve GC;    GC.SetModel(themod);
179   GeomToIGES_GeomSurface GS;  GS.SetModel(themod);
180
181   //#34 22.10.98 rln BUC60081
182   IGESData_GlobalSection gs = themod->GlobalSection();
183   Bnd_Box box;
184
185   if (!Curve.IsNull()) {
186     ent = GC.TransferCurve(Curve,Curve->FirstParameter(),Curve->LastParameter());
187     BndLib_Add3dCurve::Add (GeomAdaptor_Curve (Curve), 0, box); }
188   else if (!Surf.IsNull()) {
189     Standard_Real U1,U2,V1,V2;
190     Surf->Bounds(U1,U2,V1,V2);
191     ent = GS.TransferSurface(Surf,U1,U2,V1,V2);
192     BndLib_AddSurface::Add (GeomAdaptor_Surface (Surf), 0, box);
193   }
194
195   Standard_Real aXmin, aYmin, aZmin, aXmax, aYmax, aZmax;
196   box.Get (aXmin, aYmin, aZmin, aXmax, aYmax, aZmax);
197   gs.MaxMaxCoords (gp_XYZ (aXmax / gs.UnitValue(),
198                            aYmax / gs.UnitValue(),
199                            aZmax / gs.UnitValue()));
200   gs.MaxMaxCoords (gp_XYZ (aXmin / gs.UnitValue(),
201                            aYmin / gs.UnitValue(),
202                            aZmin / gs.UnitValue()));
203   themod->SetGlobalSection(gs);
204   return AddEntity (ent);
205 }
206
207 Standard_Boolean IGESControl_Writer::AddEntity (const Handle(IGESData_IGESEntity)& ent)
208 {
209   if (ent.IsNull()) return Standard_False;
210   themod->AddWithRefs(ent,IGESSelect_WorkLibrary::DefineProtocol());
211   thest = Standard_False;
212   return Standard_True;
213 }
214
215 void IGESControl_Writer::ComputeModel ()
216 {
217   if (thest) return;
218   thedit.ComputeStatus();
219   thedit.AutoCorrectModel();
220   thest = Standard_True;
221 }
222
223 Standard_Boolean IGESControl_Writer::Write
224   (Standard_OStream& S, const Standard_Boolean fnes)
225 {
226   if (!S) return Standard_False;
227   ComputeModel();
228 #ifdef DEBUG
229   cout<<" IGES Write : "<<themod->NbEntities()<<" ent.s"<< flush;
230 #endif
231   IGESData_IGESWriter IW (themod);
232 //  ne pas oublier le mode fnes ... a transmettre a IW
233   IW.SendModel (IGESSelect_WorkLibrary::DefineProtocol());
234 #ifdef DEBUG
235   cout<<" ...  ecriture  ..."<<flush;
236 #endif
237   if (fnes) IW.WriteMode() = 10;
238   Standard_Boolean status = IW.Print(S);
239 #ifdef DEBUG
240   cout<<" ...  fichier ecrit  ..."<<endl;
241 #endif
242   return status;
243 }
244
245 Standard_Boolean IGESControl_Writer::Write
246   (const Standard_CString file, const Standard_Boolean fnes)
247 {
248   ofstream fout(file,ios::out);
249   if (!fout) return Standard_False;
250 #ifdef DEBUG
251   cout<<" Ecriture fichier ("<< (fnes ? "fnes" : "IGES") <<"): "<<file<<endl;
252 #endif
253   Standard_Boolean res = Write (fout,fnes);
254
255   errno = 0;
256   fout.close();
257   res = fout.good() && res && !errno;
258
259   return res;
260 }
261
262 void IGESControl_Writer::PrintStatsTransfer
263   (const Standard_Integer what, const Standard_Integer mode) const
264 {
265   XSControl_TransferWriter::PrintStatsProcess (theTP,what,mode);
266 }