1 // Copyright (c) 1999-2012 OPEN CASCADE SAS
3 // The content of this file is subject to the Open CASCADE Technology Public
4 // License Version 6.5 (the "License"). You may not use the content of this file
5 // except in compliance with the License. Please obtain a copy of the License
6 // at http://www.opencascade.org and read it completely before using this file.
8 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
9 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
11 // The Original Code and all software distributed under the License is
12 // distributed on an "AS IS" basis, without warranty of any kind, and the
13 // Initial Developer hereby disclaims all such warranties, including without
14 // limitation, any warranties of merchantability, fitness for a particular
15 // purpose or non-infringement. Please see the License for the specific terms
16 // and conditions governing the rights and limitations under the License.
18 //cky 16.01.99 Remove couts.
19 //rln 28.12.98 CCI60005
21 #include <Standard_Stream.hxx>
23 #include <IGESControl_Writer.ixx>
24 #include <IGESControl_Controller.hxx>
25 #include <IGESSelect_WorkLibrary.hxx>
26 #include <BRepToIGES_BREntity.hxx>
27 #include <BRepToIGESBRep_Entity.hxx>
28 #include <Geom_Curve.hxx>
29 #include <Geom_Surface.hxx>
30 #include <GeomToIGES_GeomCurve.hxx>
31 #include <GeomToIGES_GeomSurface.hxx>
32 #include <IGESData_IGESWriter.hxx>
33 #include <XSControl_TransferWriter.hxx>
35 #include <Interface_Macros.hxx>
36 #include <Interface_Static.hxx>
37 #include <ShapeAnalysis_ShapeTolerance.hxx>
40 #include <Bnd_Box.hxx>
41 #include <BRepBndLib.hxx>
42 #include <GeomAdaptor_Curve.hxx>
43 #include <GeomAdaptor_Surface.hxx>
44 #include <BndLib_Add3dCurve.hxx>
45 #include <BndLib_AddSurface.hxx>
47 #include <XSAlgo_AlgoContainer.hxx>
48 #include <TopExp_Explorer.hxx>
49 #include <Message_ProgressIndicator.hxx>
52 IGESControl_Writer::IGESControl_Writer ()
53 : theTP (new Transfer_FinderProcess(10000)) ,
54 thedit (IGESSelect_WorkLibrary::DefineProtocol()) ,
55 thest (Standard_False)
57 // faudrait aussi (?) prendre les parametres par defaut ... ?
58 IGESControl_Controller::Init();
59 thedit.SetUnitName(Interface_Static::CVal ("write.iges.unit"));
61 thecr = Interface_Static::IVal ("write.iges.brep.mode");
62 themod = thedit.Model();
65 IGESControl_Writer::IGESControl_Writer
66 (const Standard_CString unit, const Standard_Integer modecr)
67 : theTP (new Transfer_FinderProcess(10000)) ,
68 thedit (IGESSelect_WorkLibrary::DefineProtocol()) ,
69 thecr (modecr) , thest (Standard_False)
71 // faudrait aussi (?) prendre les parametres par defaut ... ?
72 IGESControl_Controller::Init();
73 thedit.SetUnitName(unit);
75 themod = thedit.Model();
78 IGESControl_Writer::IGESControl_Writer
79 (const Handle(IGESData_IGESModel)& model, const Standard_Integer modecr)
80 : theTP (new Transfer_FinderProcess(10000)) ,
82 thedit (model,IGESSelect_WorkLibrary::DefineProtocol()) ,
83 thecr (modecr) , thest (Standard_False) { }
85 Handle(IGESData_IGESModel) IGESControl_Writer::Model () const
90 Handle(Transfer_FinderProcess) IGESControl_Writer::TransferProcess () const
95 void IGESControl_Writer::SetTransferProcess
96 (const Handle(Transfer_FinderProcess)& TP)
101 Standard_Boolean IGESControl_Writer::AddShape (const TopoDS_Shape& theShape)
103 if (theShape.IsNull()) return Standard_False;
105 // for progress indication
106 Handle(Message_ProgressIndicator) progress = theTP->GetProgress();
107 if ( ! progress.IsNull() ) {
108 Standard_Integer nbfaces=0;
109 for( TopExp_Explorer exp(theShape,TopAbs_FACE); exp.More(); exp.Next() )
111 progress->SetScale ( "Faces", 0, nbfaces, 1 );
114 XSAlgo::AlgoContainer()->PrepareForTransfer();
116 // modified by NIZHNY-EAP Tue Aug 29 11:16:54 2000 ___BEGIN___
117 Handle(Standard_Transient) info;
118 Standard_Real Tol = Interface_Static::RVal("write.precision.val");
119 Standard_Real maxTol = Interface_Static::RVal("read.maxprecision.val");
120 TopoDS_Shape Shape = XSAlgo::AlgoContainer()->ProcessShape( theShape, Tol, maxTol,
121 "write.iges.resource.name",
122 "write.iges.sequence", info,
124 // modified by NIZHNY-EAP Tue Aug 29 11:17:01 2000 ___END___
125 Handle(IGESData_IGESEntity) ent;
126 BRepToIGES_BREntity B0; B0.SetTransferProcess(theTP); B0.SetModel(themod);
127 BRepToIGESBRep_Entity B1; B1.SetTransferProcess(theTP); B1.SetModel(themod);
128 if (thecr) ent = B1.TransferShape(Shape);
129 else ent = B0.TransferShape(Shape);
132 return Standard_False;
133 // modified by NIZHNY-EAP Tue Aug 29 11:37:18 2000 ___BEGIN___
134 XSAlgo::AlgoContainer()->MergeTransferInfo(theTP, info);
135 // modified by NIZHNY-EAP Tue Aug 29 11:37:25 2000 ___END___
137 //22.10.98 gka BUC60080
139 Standard_Integer oldnb = themod->NbEntities();
140 Standard_Boolean aent = AddEntity (ent);
141 Standard_Integer newnb = themod->NbEntities();
143 Standard_Real oldtol = themod->GlobalSection().Resolution(), newtol;
145 Standard_Integer tolmod = Interface_Static::IVal("write.precision.mode");
147 newtol = Interface_Static::RVal("write.precision.val");
149 ShapeAnalysis_ShapeTolerance stu;
150 Standard_Real Tolv = stu.Tolerance (Shape, tolmod, TopAbs_VERTEX);
151 Standard_Real Tole = stu.Tolerance (Shape, tolmod, TopAbs_EDGE);
153 if (tolmod == 0 ) { //Average
154 Standard_Real Tol1 = (Tolv + Tole) / 2;
155 newtol = (oldtol * oldnb + Tol1 * (newnb - oldnb)) / newnb;
157 else if (tolmod < 0) {//Least
158 newtol = Min (Tolv, Tole);
159 if (oldnb > 0) newtol = Min (oldtol, newtol);
162 newtol = Max (Tolv, Tole);
163 if (oldnb > 0) newtol = Max (oldtol, newtol);
167 IGESData_GlobalSection gs = themod->GlobalSection();
168 gs.SetResolution (newtol / gs.UnitValue());//rln 28.12.98 CCI60005
170 //#34 22.10.98 rln BUC60081
172 BRepBndLib::Add (Shape, box);
173 Standard_Real aXmin, aYmin, aZmin, aXmax, aYmax, aZmax;
174 box.Get (aXmin, aYmin, aZmin, aXmax, aYmax, aZmax);
175 gs.MaxMaxCoords (gp_XYZ (aXmax / gs.UnitValue(),
176 aYmax / gs.UnitValue(),
177 aZmax / gs.UnitValue()));
178 gs.MaxMaxCoords (gp_XYZ (aXmin / gs.UnitValue(),
179 aYmin / gs.UnitValue(),
180 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)
272 ofstream fout(file,ios::out);
273 if (!fout) return Standard_False;
275 cout<<" Ecriture fichier ("<< (fnes ? "fnes" : "IGES") <<"): "<<file<<endl;
277 Standard_Boolean res = Write (fout,fnes);
281 res = fout.good() && res && !errno;
286 void IGESControl_Writer::PrintStatsTransfer
287 (const Standard_Integer what, const Standard_Integer mode) const
289 XSControl_TransferWriter::PrintStatsProcess (theTP,what,mode);