6e97d2ea9d0605d5afcfd191848302e5b890aa6c
[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   //  modified by NIZHNY-EAP Tue Aug 29 11:17:01 2000 ___END___
106   Handle(IGESData_IGESEntity) ent; 
107   BRepToIGES_BREntity   B0;  B0.SetTransferProcess(theTP); B0.SetModel(themod);
108   BRepToIGESBRep_Entity B1;  B1.SetTransferProcess(theTP); B1.SetModel(themod);
109   if (thecr) ent = B1.TransferShape(Shape);
110   else       ent = B0.TransferShape(Shape);
111 //  modified by NIZHNY-EAP Tue Aug 29 11:37:18 2000 ___BEGIN___
112   XSAlgo::AlgoContainer()->MergeTransferInfo(theTP, info);
113 //  modified by NIZHNY-EAP Tue Aug 29 11:37:25 2000 ___END___
114   
115   //22.10.98 gka BUC60080
116
117   Standard_Integer oldnb = themod->NbEntities();
118   Standard_Boolean aent = AddEntity (ent);
119   Standard_Integer newnb = themod->NbEntities();
120
121   Standard_Real oldtol = themod->GlobalSection().Resolution(), newtol;
122   
123   Standard_Integer tolmod = Interface_Static::IVal("write.precision.mode");
124   if (tolmod == 2)
125     newtol = Interface_Static::RVal("write.precision.val");
126   else {
127     ShapeAnalysis_ShapeTolerance stu; 
128     Standard_Real Tolv = stu.Tolerance (Shape, tolmod, TopAbs_VERTEX);
129     Standard_Real Tole = stu.Tolerance (Shape, tolmod, TopAbs_EDGE); 
130
131     if (tolmod == 0 ) {   //Average
132       Standard_Real Tol1 = (Tolv + Tole) / 2;
133       newtol = (oldtol * oldnb + Tol1 * (newnb - oldnb)) / newnb;
134     }
135     else if (tolmod < 0) {//Least
136       newtol = Min (Tolv, Tole);
137       if (oldnb > 0) newtol = Min (oldtol, newtol);
138     }
139     else {                //Greatest
140       newtol = Max (Tolv, Tole);
141       if (oldnb > 0) newtol = Max (oldtol, newtol);
142     }
143   }
144   
145   IGESData_GlobalSection gs = themod->GlobalSection();
146   gs.SetResolution (newtol / gs.UnitValue());//rln 28.12.98 CCI60005
147
148   //#34 22.10.98 rln BUC60081
149   Bnd_Box box;
150   BRepBndLib::Add (Shape, box);
151   Standard_Real aXmin, aYmin, aZmin, aXmax, aYmax, aZmax;
152   box.Get (aXmin, aYmin, aZmin, aXmax, aYmax, aZmax);
153   gs.MaxMaxCoords (gp_XYZ (aXmax / gs.UnitValue(),
154                            aYmax / gs.UnitValue(),
155                            aZmax / gs.UnitValue()));
156   gs.MaxMaxCoords (gp_XYZ (aXmin / gs.UnitValue(),
157                            aYmin / gs.UnitValue(),
158                            aZmin / gs.UnitValue()));
159
160   themod->SetGlobalSection(gs);
161
162   return aent;
163 }
164
165 Standard_Boolean IGESControl_Writer::AddGeom (const Handle(Standard_Transient)& geom)
166 {
167   if (geom.IsNull() || !geom->IsKind (STANDARD_TYPE (Geom_Geometry)))
168     return Standard_False;
169   DeclareAndCast(Geom_Curve,Curve,geom);
170   DeclareAndCast(Geom_Surface,Surf,geom);
171   Handle(IGESData_IGESEntity) ent; 
172
173 //  On reconnait : Curve et Surface de Geom
174 //   quid de Point; Geom2d ?
175
176 //  GeomToIGES_GeomPoint GP;
177   GeomToIGES_GeomCurve GC;    GC.SetModel(themod);
178   GeomToIGES_GeomSurface GS;  GS.SetModel(themod);
179
180   //#34 22.10.98 rln BUC60081
181   IGESData_GlobalSection gs = themod->GlobalSection();
182   Bnd_Box box;
183
184   if (!Curve.IsNull()) {
185     ent = GC.TransferCurve(Curve,Curve->FirstParameter(),Curve->LastParameter());
186     BndLib_Add3dCurve::Add (GeomAdaptor_Curve (Curve), 0, box); }
187   else if (!Surf.IsNull()) {
188     Standard_Real U1,U2,V1,V2;
189     Surf->Bounds(U1,U2,V1,V2);
190     ent = GS.TransferSurface(Surf,U1,U2,V1,V2);
191     BndLib_AddSurface::Add (GeomAdaptor_Surface (Surf), 0, box);
192   }
193
194   Standard_Real aXmin, aYmin, aZmin, aXmax, aYmax, aZmax;
195   box.Get (aXmin, aYmin, aZmin, aXmax, aYmax, aZmax);
196   gs.MaxMaxCoords (gp_XYZ (aXmax / gs.UnitValue(),
197                            aYmax / gs.UnitValue(),
198                            aZmax / gs.UnitValue()));
199   gs.MaxMaxCoords (gp_XYZ (aXmin / gs.UnitValue(),
200                            aYmin / gs.UnitValue(),
201                            aZmin / gs.UnitValue()));
202   themod->SetGlobalSection(gs);
203   return AddEntity (ent);
204 }
205
206 Standard_Boolean IGESControl_Writer::AddEntity (const Handle(IGESData_IGESEntity)& ent)
207 {
208   if (ent.IsNull()) return Standard_False;
209   themod->AddWithRefs(ent,IGESSelect_WorkLibrary::DefineProtocol());
210   thest = Standard_False;
211   return Standard_True;
212 }
213
214 void IGESControl_Writer::ComputeModel ()
215 {
216   if (thest) return;
217   thedit.ComputeStatus();
218   thedit.AutoCorrectModel();
219   thest = Standard_True;
220 }
221
222 Standard_Boolean IGESControl_Writer::Write
223   (Standard_OStream& S, const Standard_Boolean fnes)
224 {
225   if (!S) return Standard_False;
226   ComputeModel();
227 #ifdef DEBUG
228   cout<<" IGES Write : "<<themod->NbEntities()<<" ent.s"<< flush;
229 #endif
230   IGESData_IGESWriter IW (themod);
231 //  ne pas oublier le mode fnes ... a transmettre a IW
232   IW.SendModel (IGESSelect_WorkLibrary::DefineProtocol());
233 #ifdef DEBUG
234   cout<<" ...  ecriture  ..."<<flush;
235 #endif
236   if (fnes) IW.WriteMode() = 10;
237   Standard_Boolean status = IW.Print(S);
238 #ifdef DEBUG
239   cout<<" ...  fichier ecrit  ..."<<endl;
240 #endif
241   return status;
242 }
243
244 Standard_Boolean IGESControl_Writer::Write
245   (const Standard_CString file, const Standard_Boolean fnes)
246 {
247   ofstream fout(file,ios::out);
248   if (!fout) return Standard_False;
249 #ifdef DEBUG
250   cout<<" Ecriture fichier ("<< (fnes ? "fnes" : "IGES") <<"): "<<file<<endl;
251 #endif
252   Standard_Boolean res = Write (fout,fnes);
253
254   errno = 0;
255   fout.close();
256   res = fout.good() && res && !errno;
257
258   return res;
259 }
260
261 void IGESControl_Writer::PrintStatsTransfer
262   (const Standard_Integer what, const Standard_Integer mode) const
263 {
264   XSControl_TransferWriter::PrintStatsProcess (theTP,what,mode);
265 }