1 // Copyright (c) 1999-2014 OPEN CASCADE SAS
3 // This file is part of Open CASCADE Technology software library.
5 // This library is free software; you can redistribute it and/or modify it under
6 // the terms of the GNU Lesser General Public License version 2.1 as published
7 // by the Free Software Foundation, with special exception defined in the file
8 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
9 // distribution for complete text of the license and disclaimer of any warranty.
11 // Alternatively, this file may be used under the terms of Open CASCADE
12 // commercial license or contractual agreement.
14 //cky 16.01.99 Remove couts.
15 //rln 28.12.98 CCI60005
17 #include <Standard_Stream.hxx>
19 #include <IGESControl_Writer.ixx>
20 #include <IGESControl_Controller.hxx>
21 #include <IGESSelect_WorkLibrary.hxx>
22 #include <BRepToIGES_BREntity.hxx>
23 #include <BRepToIGESBRep_Entity.hxx>
24 #include <Geom_Curve.hxx>
25 #include <Geom_Surface.hxx>
26 #include <GeomToIGES_GeomCurve.hxx>
27 #include <GeomToIGES_GeomSurface.hxx>
28 #include <IGESData_IGESWriter.hxx>
29 #include <XSControl_TransferWriter.hxx>
31 #include <Interface_Macros.hxx>
32 #include <Interface_Static.hxx>
33 #include <ShapeAnalysis_ShapeTolerance.hxx>
36 #include <Bnd_Box.hxx>
37 #include <BRepBndLib.hxx>
38 #include <GeomAdaptor_Curve.hxx>
39 #include <GeomAdaptor_Surface.hxx>
40 #include <BndLib_Add3dCurve.hxx>
41 #include <BndLib_AddSurface.hxx>
43 #include <XSAlgo_AlgoContainer.hxx>
44 #include <TopExp_Explorer.hxx>
45 #include <Message_ProgressIndicator.hxx>
47 #include <OSD_OpenFile.hxx>
48 #include <Geom_Geometry.hxx>
50 IGESControl_Writer::IGESControl_Writer ()
51 : theTP (new Transfer_FinderProcess(10000)) ,
52 thest (Standard_False)
54 // faudrait aussi (?) prendre les parametres par defaut ... ?
55 IGESControl_Controller::Init();
56 thedit.Init(IGESSelect_WorkLibrary::DefineProtocol());
57 thedit.SetUnitName(Interface_Static::CVal ("write.iges.unit"));
59 thecr = Interface_Static::IVal ("write.iges.brep.mode");
60 themod = thedit.Model();
63 IGESControl_Writer::IGESControl_Writer
64 (const Standard_CString unit, const Standard_Integer modecr)
65 : theTP (new Transfer_FinderProcess(10000)) ,
66 thecr (modecr) , thest (Standard_False)
68 // faudrait aussi (?) prendre les parametres par defaut ... ?
69 IGESControl_Controller::Init();
70 thedit.Init(IGESSelect_WorkLibrary::DefineProtocol());
71 thedit.SetUnitName(unit);
73 themod = thedit.Model();
76 IGESControl_Writer::IGESControl_Writer
77 (const Handle(IGESData_IGESModel)& model, const Standard_Integer modecr)
78 : theTP (new Transfer_FinderProcess(10000)) ,
80 thedit (model,IGESSelect_WorkLibrary::DefineProtocol()) ,
81 thecr (modecr) , thest (Standard_False) { }
83 Handle(IGESData_IGESModel) IGESControl_Writer::Model () const
88 Handle(Transfer_FinderProcess) IGESControl_Writer::TransferProcess () const
93 void IGESControl_Writer::SetTransferProcess
94 (const Handle(Transfer_FinderProcess)& TP)
99 Standard_Boolean IGESControl_Writer::AddShape (const TopoDS_Shape& theShape)
101 if (theShape.IsNull()) return Standard_False;
103 // for progress indication
104 Handle(Message_ProgressIndicator) progress = theTP->GetProgress();
105 if ( ! progress.IsNull() ) {
106 Standard_Integer nbfaces=0;
107 for( TopExp_Explorer exp(theShape,TopAbs_FACE); exp.More(); exp.Next() )
109 progress->SetScale ( "Faces", 0, nbfaces, 1 );
112 XSAlgo::AlgoContainer()->PrepareForTransfer();
114 // modified by NIZHNY-EAP Tue Aug 29 11:16:54 2000 ___BEGIN___
115 Handle(Standard_Transient) info;
116 Standard_Real Tol = Interface_Static::RVal("write.precision.val");
117 Standard_Real maxTol = Interface_Static::RVal("read.maxprecision.val");
118 TopoDS_Shape Shape = XSAlgo::AlgoContainer()->ProcessShape( theShape, Tol, maxTol,
119 "write.iges.resource.name",
120 "write.iges.sequence", info,
122 // modified by NIZHNY-EAP Tue Aug 29 11:17:01 2000 ___END___
123 Handle(IGESData_IGESEntity) ent;
124 BRepToIGES_BREntity B0; B0.SetTransferProcess(theTP); B0.SetModel(themod);
125 BRepToIGESBRep_Entity B1; B1.SetTransferProcess(theTP); B1.SetModel(themod);
126 if (thecr) ent = B1.TransferShape(Shape);
127 else ent = B0.TransferShape(Shape);
130 return Standard_False;
131 // modified by NIZHNY-EAP Tue Aug 29 11:37:18 2000 ___BEGIN___
132 XSAlgo::AlgoContainer()->MergeTransferInfo(theTP, info);
133 // modified by NIZHNY-EAP Tue Aug 29 11:37:25 2000 ___END___
135 //22.10.98 gka BUC60080
137 Standard_Integer oldnb = themod->NbEntities();
138 Standard_Boolean aent = AddEntity (ent);
139 Standard_Integer newnb = themod->NbEntities();
141 Standard_Real oldtol = themod->GlobalSection().Resolution(), newtol;
143 Standard_Integer tolmod = Interface_Static::IVal("write.precision.mode");
145 newtol = Interface_Static::RVal("write.precision.val");
147 ShapeAnalysis_ShapeTolerance stu;
148 Standard_Real Tolv = stu.Tolerance (Shape, tolmod, TopAbs_VERTEX);
149 Standard_Real Tole = stu.Tolerance (Shape, tolmod, TopAbs_EDGE);
151 if (tolmod == 0 ) { //Average
152 Standard_Real Tol1 = (Tolv + Tole) / 2;
153 newtol = (oldtol * oldnb + Tol1 * (newnb - oldnb)) / newnb;
155 else if (tolmod < 0) {//Least
156 newtol = Min (Tolv, Tole);
157 if (oldnb > 0) newtol = Min (oldtol, newtol);
160 newtol = Max (Tolv, Tole);
161 if (oldnb > 0) newtol = Max (oldtol, newtol);
165 IGESData_GlobalSection gs = themod->GlobalSection();
166 gs.SetResolution (newtol / gs.UnitValue());//rln 28.12.98 CCI60005
168 //#34 22.10.98 rln BUC60081
170 BRepBndLib::Add (Shape, box);
171 if (!(box.IsVoid() || box.IsOpenXmax() || box.IsOpenYmax() || box.IsOpenZmax() || box.IsOpenXmin() || box.IsOpenYmin() || box.IsOpenZmin())){
172 Standard_Real aXmin, aYmin, aZmin, aXmax, aYmax, aZmax;
173 box.Get (aXmin, aYmin, aZmin, aXmax, aYmax, aZmax);
174 gs.MaxMaxCoords (gp_XYZ (aXmax / gs.UnitValue(),
175 aYmax / gs.UnitValue(),
176 aZmax / gs.UnitValue()));
177 gs.MaxMaxCoords (gp_XYZ (aXmin / gs.UnitValue(),
178 aYmin / gs.UnitValue(),
179 aZmin / gs.UnitValue()));
182 themod->SetGlobalSection(gs);
187 Standard_Boolean IGESControl_Writer::AddGeom (const Handle(Standard_Transient)& geom)
189 if (geom.IsNull() || !geom->IsKind (STANDARD_TYPE (Geom_Geometry)))
190 return Standard_False;
191 DeclareAndCast(Geom_Curve,Curve,geom);
192 DeclareAndCast(Geom_Surface,Surf,geom);
193 Handle(IGESData_IGESEntity) ent;
195 // On reconnait : Curve et Surface de Geom
196 // quid de Point; Geom2d ?
198 // GeomToIGES_GeomPoint GP;
199 GeomToIGES_GeomCurve GC; GC.SetModel(themod);
200 GeomToIGES_GeomSurface GS; GS.SetModel(themod);
202 //#34 22.10.98 rln BUC60081
203 IGESData_GlobalSection gs = themod->GlobalSection();
206 if (!Curve.IsNull()) {
207 ent = GC.TransferCurve(Curve,Curve->FirstParameter(),Curve->LastParameter());
208 BndLib_Add3dCurve::Add (GeomAdaptor_Curve (Curve), 0, box); }
209 else if (!Surf.IsNull()) {
210 Standard_Real U1,U2,V1,V2;
211 Surf->Bounds(U1,U2,V1,V2);
212 ent = GS.TransferSurface(Surf,U1,U2,V1,V2);
213 BndLib_AddSurface::Add (GeomAdaptor_Surface (Surf), 0, box);
216 Standard_Real aXmin, aYmin, aZmin, aXmax, aYmax, aZmax;
217 box.Get (aXmin, aYmin, aZmin, aXmax, aYmax, aZmax);
218 gs.MaxMaxCoords (gp_XYZ (aXmax / gs.UnitValue(),
219 aYmax / gs.UnitValue(),
220 aZmax / gs.UnitValue()));
221 gs.MaxMaxCoords (gp_XYZ (aXmin / gs.UnitValue(),
222 aYmin / gs.UnitValue(),
223 aZmin / gs.UnitValue()));
224 themod->SetGlobalSection(gs);
225 return AddEntity (ent);
228 Standard_Boolean IGESControl_Writer::AddEntity (const Handle(IGESData_IGESEntity)& ent)
230 if (ent.IsNull()) return Standard_False;
231 themod->AddWithRefs(ent,IGESSelect_WorkLibrary::DefineProtocol());
232 thest = Standard_False;
233 return Standard_True;
236 void IGESControl_Writer::ComputeModel ()
239 thedit.ComputeStatus();
240 thedit.AutoCorrectModel();
241 thest = Standard_True;
244 Standard_Boolean IGESControl_Writer::Write
245 (Standard_OStream& S, const Standard_Boolean fnes)
247 if (!S) return Standard_False;
249 Standard_Integer nbEnt = themod->NbEntities();
251 cout<<" IGES Write : "<<nbEnt<<" ent.s"<< flush;
254 return Standard_False;
255 IGESData_IGESWriter IW (themod);
256 // ne pas oublier le mode fnes ... a transmettre a IW
257 IW.SendModel (IGESSelect_WorkLibrary::DefineProtocol());
259 cout<<" ... ecriture ..."<<flush;
261 if (fnes) IW.WriteMode() = 10;
262 Standard_Boolean status = IW.Print(S);
264 cout<<" ... fichier ecrit ..."<<endl;
269 Standard_Boolean IGESControl_Writer::Write
270 (const Standard_CString file, const Standard_Boolean fnes)
273 OSD_OpenStream(fout,file,ios::out);
274 if (!fout) return Standard_False;
276 cout<<" Ecriture fichier ("<< (fnes ? "fnes" : "IGES") <<"): "<<file<<endl;
278 Standard_Boolean res = Write (fout,fnes);
282 res = fout.good() && res && !errno;
287 void IGESControl_Writer::PrintStatsTransfer
288 (const Standard_Integer what, const Standard_Integer mode) const
290 XSControl_TransferWriter::PrintStatsProcess (theTP,what,mode);