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 <Bnd_Box.hxx>
18 #include <BndLib_Add3dCurve.hxx>
19 #include <BndLib_AddSurface.hxx>
20 #include <BRepBndLib.hxx>
21 #include <BRepToIGES_BREntity.hxx>
22 #include <BRepToIGESBRep_Entity.hxx>
23 #include <Geom_Curve.hxx>
24 #include <Geom_Geometry.hxx>
25 #include <Geom_Surface.hxx>
26 #include <GeomAdaptor_Curve.hxx>
27 #include <GeomAdaptor_Surface.hxx>
28 #include <GeomToIGES_GeomCurve.hxx>
29 #include <GeomToIGES_GeomSurface.hxx>
31 #include <IGESControl_Controller.hxx>
32 #include <IGESControl_Writer.hxx>
33 #include <IGESData_IGESEntity.hxx>
34 #include <IGESData_IGESModel.hxx>
35 #include <IGESData_IGESWriter.hxx>
36 #include <IGESData_Protocol.hxx>
37 #include <IGESSelect_WorkLibrary.hxx>
38 #include <Interface_Macros.hxx>
39 #include <Interface_Static.hxx>
40 #include <Message_ProgressIndicator.hxx>
41 #include <OSD_OpenFile.hxx>
42 #include <ShapeAnalysis_ShapeTolerance.hxx>
43 #include <Standard_Stream.hxx>
44 #include <Standard_Transient.hxx>
45 #include <TopExp_Explorer.hxx>
46 #include <TopoDS_Shape.hxx>
47 #include <Transfer_FinderProcess.hxx>
49 #include <XSAlgo_AlgoContainer.hxx>
50 #include <XSControl_TransferWriter.hxx>
53 IGESControl_Writer::IGESControl_Writer ()
54 : theTP (new Transfer_FinderProcess(10000)) ,
55 thest (Standard_False)
57 // faudrait aussi (?) prendre les parametres par defaut ... ?
58 IGESControl_Controller::Init();
59 thedit.Init(IGESSelect_WorkLibrary::DefineProtocol());
60 thedit.SetUnitName(Interface_Static::CVal ("write.iges.unit"));
62 thecr = Interface_Static::IVal ("write.iges.brep.mode");
63 themod = thedit.Model();
66 IGESControl_Writer::IGESControl_Writer
67 (const Standard_CString unit, const Standard_Integer modecr)
68 : theTP (new Transfer_FinderProcess(10000)) ,
69 thecr (modecr) , thest (Standard_False)
71 // faudrait aussi (?) prendre les parametres par defaut ... ?
72 IGESControl_Controller::Init();
73 thedit.Init(IGESSelect_WorkLibrary::DefineProtocol());
74 thedit.SetUnitName(unit);
76 themod = thedit.Model();
79 IGESControl_Writer::IGESControl_Writer
80 (const Handle(IGESData_IGESModel)& model, const Standard_Integer modecr)
81 : theTP (new Transfer_FinderProcess(10000)) ,
83 thedit (model,IGESSelect_WorkLibrary::DefineProtocol()) ,
84 thecr (modecr) , thest (Standard_False) { }
86 Handle(IGESData_IGESModel) IGESControl_Writer::Model () const
91 Handle(Transfer_FinderProcess) IGESControl_Writer::TransferProcess () const
96 void IGESControl_Writer::SetTransferProcess
97 (const Handle(Transfer_FinderProcess)& TP)
102 Standard_Boolean IGESControl_Writer::AddShape (const TopoDS_Shape& theShape)
104 if (theShape.IsNull()) return Standard_False;
106 // for progress indication
107 Handle(Message_ProgressIndicator) progress = theTP->GetProgress();
108 if ( ! progress.IsNull() ) {
109 Standard_Integer nbfaces=0;
110 for( TopExp_Explorer exp(theShape,TopAbs_FACE); exp.More(); exp.Next() )
112 progress->SetScale ( "Faces", 0, nbfaces, 1 );
115 XSAlgo::AlgoContainer()->PrepareForTransfer();
117 // modified by NIZHNY-EAP Tue Aug 29 11:16:54 2000 ___BEGIN___
118 Handle(Standard_Transient) info;
119 Standard_Real Tol = Interface_Static::RVal("write.precision.val");
120 Standard_Real maxTol = Interface_Static::RVal("read.maxprecision.val");
121 TopoDS_Shape Shape = XSAlgo::AlgoContainer()->ProcessShape( theShape, Tol, maxTol,
122 "write.iges.resource.name",
123 "write.iges.sequence", info,
125 // modified by NIZHNY-EAP Tue Aug 29 11:17:01 2000 ___END___
126 Handle(IGESData_IGESEntity) ent;
127 BRepToIGES_BREntity B0; B0.SetTransferProcess(theTP); B0.SetModel(themod);
128 BRepToIGESBRep_Entity B1; B1.SetTransferProcess(theTP); B1.SetModel(themod);
129 if (thecr) ent = B1.TransferShape(Shape);
130 else ent = B0.TransferShape(Shape);
133 return Standard_False;
134 // modified by NIZHNY-EAP Tue Aug 29 11:37:18 2000 ___BEGIN___
135 XSAlgo::AlgoContainer()->MergeTransferInfo(theTP, info);
136 // modified by NIZHNY-EAP Tue Aug 29 11:37:25 2000 ___END___
138 //22.10.98 gka BUC60080
140 Standard_Integer oldnb = themod->NbEntities();
141 Standard_Boolean aent = AddEntity (ent);
142 Standard_Integer newnb = themod->NbEntities();
144 Standard_Real oldtol = themod->GlobalSection().Resolution(), newtol;
146 Standard_Integer tolmod = Interface_Static::IVal("write.precision.mode");
148 newtol = Interface_Static::RVal("write.precision.val");
150 ShapeAnalysis_ShapeTolerance stu;
151 Standard_Real Tolv = stu.Tolerance (Shape, tolmod, TopAbs_VERTEX);
152 Standard_Real Tole = stu.Tolerance (Shape, tolmod, TopAbs_EDGE);
154 if (tolmod == 0 ) { //Average
155 Standard_Real Tol1 = (Tolv + Tole) / 2;
156 newtol = (oldtol * oldnb + Tol1 * (newnb - oldnb)) / newnb;
158 else if (tolmod < 0) {//Least
159 newtol = Min (Tolv, Tole);
160 if (oldnb > 0) newtol = Min (oldtol, newtol);
163 newtol = Max (Tolv, Tole);
164 if (oldnb > 0) newtol = Max (oldtol, newtol);
168 IGESData_GlobalSection gs = themod->GlobalSection();
169 gs.SetResolution (newtol / gs.UnitValue());//rln 28.12.98 CCI60005
171 //#34 22.10.98 rln BUC60081
173 BRepBndLib::Add (Shape, box);
174 if (!(box.IsVoid() || box.IsOpenXmax() || box.IsOpenYmax() || box.IsOpenZmax() || box.IsOpenXmin() || box.IsOpenYmin() || box.IsOpenZmin())){
175 Standard_Real aXmin, aYmin, aZmin, aXmax, aYmax, aZmax;
176 box.Get (aXmin, aYmin, aZmin, aXmax, aYmax, aZmax);
177 gs.MaxMaxCoords (gp_XYZ (aXmax / gs.UnitValue(),
178 aYmax / gs.UnitValue(),
179 aZmax / gs.UnitValue()));
180 gs.MaxMaxCoords (gp_XYZ (aXmin / gs.UnitValue(),
181 aYmin / gs.UnitValue(),
182 aZmin / gs.UnitValue()));
185 themod->SetGlobalSection(gs);
190 Standard_Boolean IGESControl_Writer::AddGeom (const Handle(Standard_Transient)& geom)
192 if (geom.IsNull() || !geom->IsKind (STANDARD_TYPE (Geom_Geometry)))
193 return Standard_False;
194 DeclareAndCast(Geom_Curve,Curve,geom);
195 DeclareAndCast(Geom_Surface,Surf,geom);
196 Handle(IGESData_IGESEntity) ent;
198 // On reconnait : Curve et Surface de Geom
199 // quid de Point; Geom2d ?
201 // GeomToIGES_GeomPoint GP;
202 GeomToIGES_GeomCurve GC; GC.SetModel(themod);
203 GeomToIGES_GeomSurface GS; GS.SetModel(themod);
205 //#34 22.10.98 rln BUC60081
206 IGESData_GlobalSection gs = themod->GlobalSection();
209 if (!Curve.IsNull()) {
210 ent = GC.TransferCurve(Curve,Curve->FirstParameter(),Curve->LastParameter());
211 BndLib_Add3dCurve::Add (GeomAdaptor_Curve (Curve), 0, box); }
212 else if (!Surf.IsNull()) {
213 Standard_Real U1,U2,V1,V2;
214 Surf->Bounds(U1,U2,V1,V2);
215 ent = GS.TransferSurface(Surf,U1,U2,V1,V2);
216 BndLib_AddSurface::Add (GeomAdaptor_Surface (Surf), 0, box);
219 Standard_Real aXmin, aYmin, aZmin, aXmax, aYmax, aZmax;
220 box.Get (aXmin, aYmin, aZmin, aXmax, aYmax, aZmax);
221 gs.MaxMaxCoords (gp_XYZ (aXmax / gs.UnitValue(),
222 aYmax / gs.UnitValue(),
223 aZmax / gs.UnitValue()));
224 gs.MaxMaxCoords (gp_XYZ (aXmin / gs.UnitValue(),
225 aYmin / gs.UnitValue(),
226 aZmin / gs.UnitValue()));
227 themod->SetGlobalSection(gs);
228 return AddEntity (ent);
231 Standard_Boolean IGESControl_Writer::AddEntity (const Handle(IGESData_IGESEntity)& ent)
233 if (ent.IsNull()) return Standard_False;
234 themod->AddWithRefs(ent,IGESSelect_WorkLibrary::DefineProtocol());
235 thest = Standard_False;
236 return Standard_True;
239 void IGESControl_Writer::ComputeModel ()
242 thedit.ComputeStatus();
243 thedit.AutoCorrectModel();
244 thest = Standard_True;
247 Standard_Boolean IGESControl_Writer::Write
248 (Standard_OStream& S, const Standard_Boolean fnes)
250 if (!S) return Standard_False;
252 Standard_Integer nbEnt = themod->NbEntities();
254 cout<<" IGES Write : "<<nbEnt<<" ent.s"<< flush;
257 return Standard_False;
258 IGESData_IGESWriter IW (themod);
259 // ne pas oublier le mode fnes ... a transmettre a IW
260 IW.SendModel (IGESSelect_WorkLibrary::DefineProtocol());
262 cout<<" ... ecriture ..."<<flush;
264 if (fnes) IW.WriteMode() = 10;
265 Standard_Boolean status = IW.Print(S);
267 cout<<" ... fichier ecrit ..."<<endl;
272 Standard_Boolean IGESControl_Writer::Write
273 (const Standard_CString file, const Standard_Boolean fnes)
276 OSD_OpenStream(fout,file,ios::out);
277 if (!fout) return Standard_False;
279 cout<<" Ecriture fichier ("<< (fnes ? "fnes" : "IGES") <<"): "<<file<<endl;
281 Standard_Boolean res = Write (fout,fnes);
285 res = fout.good() && res && !errno;
290 void IGESControl_Writer::PrintStatsTransfer
291 (const Standard_Integer what, const Standard_Integer mode) const
293 XSControl_TransferWriter::PrintStatsProcess (theTP,what,mode);