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>
48 IGESControl_Writer::IGESControl_Writer ()
49 : theTP (new Transfer_FinderProcess(10000)) ,
50 thedit (IGESSelect_WorkLibrary::DefineProtocol()) ,
51 thest (Standard_False)
53 // faudrait aussi (?) prendre les parametres par defaut ... ?
54 IGESControl_Controller::Init();
55 thedit.SetUnitName(Interface_Static::CVal ("write.iges.unit"));
57 thecr = Interface_Static::IVal ("write.iges.brep.mode");
58 themod = thedit.Model();
61 IGESControl_Writer::IGESControl_Writer
62 (const Standard_CString unit, const Standard_Integer modecr)
63 : theTP (new Transfer_FinderProcess(10000)) ,
64 thedit (IGESSelect_WorkLibrary::DefineProtocol()) ,
65 thecr (modecr) , thest (Standard_False)
67 // faudrait aussi (?) prendre les parametres par defaut ... ?
68 IGESControl_Controller::Init();
69 thedit.SetUnitName(unit);
71 themod = thedit.Model();
74 IGESControl_Writer::IGESControl_Writer
75 (const Handle(IGESData_IGESModel)& model, const Standard_Integer modecr)
76 : theTP (new Transfer_FinderProcess(10000)) ,
78 thedit (model,IGESSelect_WorkLibrary::DefineProtocol()) ,
79 thecr (modecr) , thest (Standard_False) { }
81 Handle(IGESData_IGESModel) IGESControl_Writer::Model () const
86 Handle(Transfer_FinderProcess) IGESControl_Writer::TransferProcess () const
91 void IGESControl_Writer::SetTransferProcess
92 (const Handle(Transfer_FinderProcess)& TP)
97 Standard_Boolean IGESControl_Writer::AddShape (const TopoDS_Shape& theShape)
99 if (theShape.IsNull()) return Standard_False;
101 // for progress indication
102 Handle(Message_ProgressIndicator) progress = theTP->GetProgress();
103 if ( ! progress.IsNull() ) {
104 Standard_Integer nbfaces=0;
105 for( TopExp_Explorer exp(theShape,TopAbs_FACE); exp.More(); exp.Next() )
107 progress->SetScale ( "Faces", 0, nbfaces, 1 );
110 XSAlgo::AlgoContainer()->PrepareForTransfer();
112 // modified by NIZHNY-EAP Tue Aug 29 11:16:54 2000 ___BEGIN___
113 Handle(Standard_Transient) info;
114 Standard_Real Tol = Interface_Static::RVal("write.precision.val");
115 Standard_Real maxTol = Interface_Static::RVal("read.maxprecision.val");
116 TopoDS_Shape Shape = XSAlgo::AlgoContainer()->ProcessShape( theShape, Tol, maxTol,
117 "write.iges.resource.name",
118 "write.iges.sequence", info,
120 // modified by NIZHNY-EAP Tue Aug 29 11:17:01 2000 ___END___
121 Handle(IGESData_IGESEntity) ent;
122 BRepToIGES_BREntity B0; B0.SetTransferProcess(theTP); B0.SetModel(themod);
123 BRepToIGESBRep_Entity B1; B1.SetTransferProcess(theTP); B1.SetModel(themod);
124 if (thecr) ent = B1.TransferShape(Shape);
125 else ent = B0.TransferShape(Shape);
128 return Standard_False;
129 // modified by NIZHNY-EAP Tue Aug 29 11:37:18 2000 ___BEGIN___
130 XSAlgo::AlgoContainer()->MergeTransferInfo(theTP, info);
131 // modified by NIZHNY-EAP Tue Aug 29 11:37:25 2000 ___END___
133 //22.10.98 gka BUC60080
135 Standard_Integer oldnb = themod->NbEntities();
136 Standard_Boolean aent = AddEntity (ent);
137 Standard_Integer newnb = themod->NbEntities();
139 Standard_Real oldtol = themod->GlobalSection().Resolution(), newtol;
141 Standard_Integer tolmod = Interface_Static::IVal("write.precision.mode");
143 newtol = Interface_Static::RVal("write.precision.val");
145 ShapeAnalysis_ShapeTolerance stu;
146 Standard_Real Tolv = stu.Tolerance (Shape, tolmod, TopAbs_VERTEX);
147 Standard_Real Tole = stu.Tolerance (Shape, tolmod, TopAbs_EDGE);
149 if (tolmod == 0 ) { //Average
150 Standard_Real Tol1 = (Tolv + Tole) / 2;
151 newtol = (oldtol * oldnb + Tol1 * (newnb - oldnb)) / newnb;
153 else if (tolmod < 0) {//Least
154 newtol = Min (Tolv, Tole);
155 if (oldnb > 0) newtol = Min (oldtol, newtol);
158 newtol = Max (Tolv, Tole);
159 if (oldnb > 0) newtol = Max (oldtol, newtol);
163 IGESData_GlobalSection gs = themod->GlobalSection();
164 gs.SetResolution (newtol / gs.UnitValue());//rln 28.12.98 CCI60005
166 //#34 22.10.98 rln BUC60081
168 BRepBndLib::Add (Shape, box);
169 if (!(box.IsVoid() || box.IsOpenXmax() || box.IsOpenYmax() || box.IsOpenZmax() || box.IsOpenXmin() || box.IsOpenYmin() || box.IsOpenZmin())){
170 Standard_Real aXmin, aYmin, aZmin, aXmax, aYmax, aZmax;
171 box.Get (aXmin, aYmin, aZmin, aXmax, aYmax, aZmax);
172 gs.MaxMaxCoords (gp_XYZ (aXmax / gs.UnitValue(),
173 aYmax / gs.UnitValue(),
174 aZmax / gs.UnitValue()));
175 gs.MaxMaxCoords (gp_XYZ (aXmin / gs.UnitValue(),
176 aYmin / gs.UnitValue(),
177 aZmin / gs.UnitValue()));
180 themod->SetGlobalSection(gs);
185 Standard_Boolean IGESControl_Writer::AddGeom (const Handle(Standard_Transient)& geom)
187 if (geom.IsNull() || !geom->IsKind (STANDARD_TYPE (Geom_Geometry)))
188 return Standard_False;
189 DeclareAndCast(Geom_Curve,Curve,geom);
190 DeclareAndCast(Geom_Surface,Surf,geom);
191 Handle(IGESData_IGESEntity) ent;
193 // On reconnait : Curve et Surface de Geom
194 // quid de Point; Geom2d ?
196 // GeomToIGES_GeomPoint GP;
197 GeomToIGES_GeomCurve GC; GC.SetModel(themod);
198 GeomToIGES_GeomSurface GS; GS.SetModel(themod);
200 //#34 22.10.98 rln BUC60081
201 IGESData_GlobalSection gs = themod->GlobalSection();
204 if (!Curve.IsNull()) {
205 ent = GC.TransferCurve(Curve,Curve->FirstParameter(),Curve->LastParameter());
206 BndLib_Add3dCurve::Add (GeomAdaptor_Curve (Curve), 0, box); }
207 else if (!Surf.IsNull()) {
208 Standard_Real U1,U2,V1,V2;
209 Surf->Bounds(U1,U2,V1,V2);
210 ent = GS.TransferSurface(Surf,U1,U2,V1,V2);
211 BndLib_AddSurface::Add (GeomAdaptor_Surface (Surf), 0, box);
214 Standard_Real aXmin, aYmin, aZmin, aXmax, aYmax, aZmax;
215 box.Get (aXmin, aYmin, aZmin, aXmax, aYmax, aZmax);
216 gs.MaxMaxCoords (gp_XYZ (aXmax / gs.UnitValue(),
217 aYmax / gs.UnitValue(),
218 aZmax / gs.UnitValue()));
219 gs.MaxMaxCoords (gp_XYZ (aXmin / gs.UnitValue(),
220 aYmin / gs.UnitValue(),
221 aZmin / gs.UnitValue()));
222 themod->SetGlobalSection(gs);
223 return AddEntity (ent);
226 Standard_Boolean IGESControl_Writer::AddEntity (const Handle(IGESData_IGESEntity)& ent)
228 if (ent.IsNull()) return Standard_False;
229 themod->AddWithRefs(ent,IGESSelect_WorkLibrary::DefineProtocol());
230 thest = Standard_False;
231 return Standard_True;
234 void IGESControl_Writer::ComputeModel ()
237 thedit.ComputeStatus();
238 thedit.AutoCorrectModel();
239 thest = Standard_True;
242 Standard_Boolean IGESControl_Writer::Write
243 (Standard_OStream& S, const Standard_Boolean fnes)
245 if (!S) return Standard_False;
247 Standard_Integer nbEnt = themod->NbEntities();
248 #ifdef IGESCONTROL_DEB
249 cout<<" IGES Write : "<<nbEnt<<" ent.s"<< flush;
252 return Standard_False;
253 IGESData_IGESWriter IW (themod);
254 // ne pas oublier le mode fnes ... a transmettre a IW
255 IW.SendModel (IGESSelect_WorkLibrary::DefineProtocol());
256 #ifdef IGESCONTROL_DEB
257 cout<<" ... ecriture ..."<<flush;
259 if (fnes) IW.WriteMode() = 10;
260 Standard_Boolean status = IW.Print(S);
261 #ifdef IGESCONTROL_DEB
262 cout<<" ... fichier ecrit ..."<<endl;
267 Standard_Boolean IGESControl_Writer::Write
268 (const Standard_CString file, const Standard_Boolean fnes)
270 ofstream fout(file,ios::out);
271 if (!fout) return Standard_False;
272 #ifdef IGESCONTROL_DEB
273 cout<<" Ecriture fichier ("<< (fnes ? "fnes" : "IGES") <<"): "<<file<<endl;
275 Standard_Boolean res = Write (fout,fnes);
279 res = fout.good() && res && !errno;
284 void IGESControl_Writer::PrintStatsTransfer
285 (const Standard_Integer what, const Standard_Integer mode) const
287 XSControl_TransferWriter::PrintStatsProcess (theTP,what,mode);