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.
17 #include <IGESData_ColorEntity.hxx>
18 #include <IGESData_DefType.hxx>
19 #include <IGESData_DirPart.hxx>
20 #include <IGESData_GlobalSection.hxx>
21 #include <IGESData_IGESEntity.hxx>
22 #include <IGESData_IGESModel.hxx>
23 #include <IGESData_IGESWriter.hxx>
24 #include <IGESData_LabelDisplayEntity.hxx>
25 #include <IGESData_LevelListEntity.hxx>
26 #include <IGESData_LineFontEntity.hxx>
27 #include <IGESData_Protocol.hxx>
28 #include <IGESData_ReadWriteModule.hxx>
29 #include <IGESData_TransfEntity.hxx>
30 #include <IGESData_UndefinedEntity.hxx>
31 #include <IGESData_ViewKindEntity.hxx>
32 #include <IGESData_WriterLib.hxx>
33 #include <Interface_EntityIterator.hxx>
34 #include <Interface_FileParameter.hxx>
35 #include <Interface_FloatWriter.hxx>
36 #include <Interface_InterfaceError.hxx>
37 #include <Interface_InterfaceMismatch.hxx>
38 #include <Interface_Macros.hxx>
39 #include <Interface_ParamList.hxx>
40 #include <Interface_ParamSet.hxx>
41 #include <Interface_ReportEntity.hxx>
42 #include <Message.hxx>
43 #include <Message_Messenger.hxx>
44 #include <Standard_PCharacter.hxx>
45 #include <TCollection_AsciiString.hxx>
46 #include <TCollection_HAsciiString.hxx>
55 // Constructeur complet : taille OK, et se remplit depuis le modele en direct
57 IGESData_IGESWriter::IGESData_IGESWriter
58 (const Handle(IGESData_IGESModel)& amodel)
59 : thedirs(0,amodel->NbEntities()) , thepnum(1,amodel->NbEntities()+1),
60 thecurr (MaxcarsG+1) , themodew (0) , thefloatw (9)
63 thehead = new TColStd_HSequenceOfHAsciiString();
66 thepars = new TColStd_HSequenceOfHAsciiString();
67 thepnum.SetValue(1,1); // debut des parametres de la 1re entite
70 // Format flottant : cf FloatWriter
73 IGESData_IGESWriter::IGESData_IGESWriter ()
74 : thedirs (0,0) , thepnum (1,1) , thecurr (MaxcarsG+1) , thefloatw (9) { }
76 IGESData_IGESWriter::IGESData_IGESWriter (const IGESData_IGESWriter& )
77 : thedirs (0,0) , thepnum (1,1) , thecurr (MaxcarsG+1) , thefloatw (9) { }
80 // .... Controle d Envoi des Flottants ....
82 Interface_FloatWriter& IGESData_IGESWriter::FloatWriter ()
83 { return thefloatw; } // s y reporter
85 Standard_Integer& IGESData_IGESWriter::WriteMode ()
88 // #####################################################################
89 // ######## GENERATION DU FICHIER ########
91 //=======================================================================
92 //function : SendStartLine
94 //=======================================================================
95 void IGESData_IGESWriter::SendStartLine (const Standard_CString startline)
97 Standard_PCharacter pstartline;
99 pstartline=(Standard_PCharacter)startline;
101 Standard_Size lst = strlen (startline);
102 if (lst == 0) return;
103 if (thestar.IsNull()) thestar = new TColStd_HSequenceOfHAsciiString();
104 if (lst <= (Standard_Size)MaxcarsG) {
105 thestar->Append (new TCollection_HAsciiString(startline));
108 // Trop longue : on passe par bouts
109 char startchar = startline[MaxcarsG];
110 pstartline[MaxcarsG] = '\0';
111 SendStartLine(startline);
112 pstartline[MaxcarsG] = startchar;
113 SendStartLine (&startline[MaxcarsG]);
116 void IGESData_IGESWriter::SendModel
117 (const Handle(IGESData_Protocol)& protocol)
119 Handle(Message_Messenger) sout = Message::DefaultMessenger();
120 IGESData_WriterLib lib(protocol);
122 Standard_Integer nb = themodel->NbEntities();
124 sout<< " IGESWriter : " << nb << " Entities (* = 1000 Ent.s)" << endl;
127 Standard_Integer ns = themodel->NbStartLines();
128 Standard_Integer i; // svv Jan11 2000 : porting on DEC
129 for (i = 1; i <= ns; i ++) SendStartLine (themodel->StartLine(i));
130 SectionG (themodel->GlobalSection());
132 for (i = 1; i <= nb; i ++) {
133 Handle(IGESData_IGESEntity) ent = themodel->Entity(i);
134 Handle(IGESData_IGESEntity) cnt = ent;
136 if (i % 1000 == 1) cout << "*" << flush;
138 // Attention aux cas d erreur : contenu redefini
139 if (themodel->IsRedefinedContent(i)) {
140 sout << " -- IGESWriter : Erroneous Entity N0."<<i<<" --"<<endl;
141 Handle(Interface_ReportEntity) rep = themodel->ReportEntity(i);
142 if (!rep.IsNull()) cnt = GetCasted(IGESData_IGESEntity,rep->Content());
143 if (cnt.IsNull()) cnt = ent; // secours
147 OwnParams (ent); // preparation : porte sur le vrai <ent> ...
149 // Envoi proprement dit des Parametres proprement definis
150 Handle(IGESData_ReadWriteModule) module; Standard_Integer CN;
152 if (lib.Select(cnt,module,CN))
153 module->WriteOwnParams (CN,cnt,*this);
154 else if (cnt->IsKind(STANDARD_TYPE(IGESData_UndefinedEntity))) {
155 DeclareAndCast(IGESData_UndefinedEntity,undent,cnt);
156 undent->WriteOwnParams (*this);
158 else sout<<" -- IGESWriter : Not Processed for n0."<<i<<" in file, Type "
159 <<cnt->TypeNumber()<<" Form "<<cnt->FormNumber()<<endl;
161 Associativities (cnt);
166 cout << " Envoi des Entites Termine"<<endl;
172 void IGESData_IGESWriter::SectionS ()
174 if (thesect != 0) throw Interface_InterfaceError("IGESWriter : SectionS");
178 void IGESData_IGESWriter::SectionG (const IGESData_GlobalSection& header)
180 if (thesect != 1) throw Interface_InterfaceError("IGESWriter : SectionG");
182 thesep = header.Separator();
183 theendm = header.EndMark();
184 thecurr.SetMax (MaxcarsG);
185 // Important : les Parametres sont sortis sous leur forme definitive
186 // (c-a-d Hollerith pour les Textes ...)
187 Handle(Interface_ParamSet) gl = header.Params();
188 Standard_Integer nb = gl->NbParams();
189 for (Standard_Integer i = 1; i <= nb; i ++) {
190 const Interface_FileParameter& FP = gl->Param(i);
191 AddString(FP.CValue());
192 if (i < nb) AddChar(thesep);
193 else AddChar(theendm);
195 if (thecurr.Length() > 0) thehead->Append(thecurr.Moved());
198 void IGESData_IGESWriter::SectionsDP ()
200 if (thesect != 2) throw Interface_InterfaceError("IGESWriter : SectionsDP");
202 thecurr.SetMax (MaxcarsP);
203 thestep = IGESData_ReadEnd;
206 void IGESData_IGESWriter::SectionT ()
208 if (thesect != 3) throw Interface_InterfaceError("IGESWriter : SectionT");
210 thepnum.SetValue(thepnum.Length(),thepars->Length()+1);
214 void IGESData_IGESWriter::DirPart
215 (const Handle(IGESData_IGESEntity)& anent)
217 if (thesect != 3 && thestep != IGESData_ReadEnd)
218 throw Interface_InterfaceError("IGESWriter : DirPart");
219 Standard_Integer v[17]; Standard_Character res1[9],res2[9],label[9],snum[9];
220 Standard_Integer nument = themodel->Number(anent);
221 if (nument == 0) return;
222 IGESData_DirPart& DP = thedirs.ChangeValue(nument);
223 // Remplissage du DirPart
224 v[0] = anent->TypeNumber();
225 v[1] = 0; // numero en section P : calcule ulterieurement
226 if (anent->HasStructure()) v[2] = - themodel->DNum(anent->DirFieldEntity(3));
229 IGESData_DefType linet = anent->DefLineFont();
230 if (linet == IGESData_DefReference) v[3] = - themodel->DNum(anent->DirFieldEntity(4));
231 else if (linet == IGESData_DefValue) v[3] = anent->RankLineFont();
234 IGESData_DefList levt = anent->DefLevel();
235 if (levt == IGESData_DefSeveral) v[4] = - themodel->DNum(anent->DirFieldEntity(5));
236 else if (levt == IGESData_DefOne) v[4] = anent->Level();
239 IGESData_DefList viewt = anent->DefView();
240 if (viewt == IGESData_DefSeveral || viewt == IGESData_DefOne)
241 v[5] = themodel->DNum(anent->DirFieldEntity(6));
244 if (anent->HasTransf()) v[6] = themodel->DNum(anent->DirFieldEntity(7));
247 if (anent->HasLabelDisplay()) v[7] = themodel->DNum(anent->DirFieldEntity(8));
250 v[8] = anent->BlankStatus();
251 v[9] = anent->SubordinateStatus();
252 v[10] = anent->UseFlag();
253 v[11] = anent->HierarchyStatus();
254 v[12] = v[0]; // type repete
255 v[13] = anent->LineWeightNumber();
257 IGESData_DefType colt = anent->DefColor();
258 if (colt == IGESData_DefReference) v[14] = - themodel->DNum(anent->DirFieldEntity(13));
259 else if (colt == IGESData_DefValue) v[14] = anent->RankColor();
262 v[15] = 0; // nb lignes section P : calcule plus tard
263 v[16] = anent->FormNumber();
265 anent->CResValues(res1,res2);
266 Standard_Integer i; // svv Jan11 2000 : porting on DEC
267 for (i = 0; i < 8; i ++) label[i] = snum[i] = ' ';
268 if (anent->HasShortLabel()) {
269 Handle(TCollection_HAsciiString) slab = anent->ShortLabel();
270 for (i = 0; i < slab->Length(); i ++) label[i] = slab->Value(i+1);
272 if (anent->HasSubScriptNumber()) {
273 Standard_Integer sn = anent->SubScriptNumber(); // -> cadres a droite
274 snum[7] = '0'; i = 7;
276 snum[i] = (char) ((sn % 10) + 48);
281 DP.Init(v[0],v[1],v[2],v[3],v[4],v[5],v[6],v[7],v[8],v[9],v[10],v[11],v[12],
282 v[13],v[14],v[15],v[16],res1,res2,label,snum);
283 // DP ChangeValue donc mis a jour d office
284 thestep = IGESData_ReadDir;
287 void IGESData_IGESWriter::OwnParams
288 (const Handle(IGESData_IGESEntity)& anent)
291 if (thesect != 3 && thestep != IGESData_ReadDir)
292 throw Interface_InterfaceError("IGESWriter : OwnParams");
293 thepnum.SetValue(themodel->Number(anent),thepars->Length()+1);
295 sprintf(text,"%d",anent->TypeNumber());
297 thestep = IGESData_ReadOwn;
300 void IGESData_IGESWriter::Properties
301 (const Handle(IGESData_IGESEntity)& anent)
303 if (thesect != 3 && thestep != IGESData_ReadOwn)
304 throw Interface_InterfaceError("IGESWriter : Properties");
305 thestep = IGESData_ReadProps;
306 if (!anent->ArePresentProperties()) return;
307 Send(anent->NbProperties());
308 for (Interface_EntityIterator iter = anent->Properties();
309 iter.More(); iter.Next()) {
310 DeclareAndCast(IGESData_IGESEntity,localent,iter.Value());
315 void IGESData_IGESWriter::Associativities
316 (const Handle(IGESData_IGESEntity)& anent)
318 if (thesect != 3 && thestep != IGESData_ReadOwn)
319 throw Interface_InterfaceError("IGESWriter : Associativities");
320 thestep = IGESData_ReadAssocs;
321 if (!anent->ArePresentAssociativities() && !anent->ArePresentProperties())
322 return; // Properties suivent : ne pas les omettre !
323 Send(anent->NbAssociativities());
324 for (Interface_EntityIterator iter = anent->Associativities();
325 iter.More(); iter.Next()) {
326 DeclareAndCast(IGESData_IGESEntity,localent,iter.Value());
329 thestep = IGESData_ReadAssocs;
332 void IGESData_IGESWriter::EndEntity ()
334 if (thesect != 3 && thestep != IGESData_ReadOwn)
335 throw Interface_InterfaceError("IGESWriter : EndEntity");
337 if (thecurr.Length() > 0) thepars->Append(thecurr.Moved());
338 thestep = IGESData_ReadEnd;
341 // .... Alimentation des parametres ....
343 void IGESData_IGESWriter::AddString
344 (const Handle(TCollection_HAsciiString)& val, const Standard_Integer more)
346 if (val.IsNull()) return;
347 AddString (val->ToCString(),val->Length(),more);
350 void IGESData_IGESWriter::AddString
351 (const Standard_CString val, const Standard_Integer lnval,
352 const Standard_Integer more)
354 Standard_Integer lnstr = lnval;
355 if (lnstr <= 0) lnstr = (Standard_Integer)strlen(val);
356 if (!thecurr.CanGet (lnstr + more + 1)) {
357 // + 1 (18-SEP-1996) pour etre sur que le separateur n est pas en tete de ligne
358 if (thesect < 3) thehead->Append(thecurr.Moved());
359 else thepars->Append(thecurr.Moved());
361 Standard_Integer maxcars = (thesect == 3 ? MaxcarsP : MaxcarsG);
362 Standard_Integer n2 = 0;
363 // .. pb de taille limite (30-DEC-1996)
364 while (lnstr > maxcars) {
365 thecurr.Add (&val[n2],lnstr);
366 if (thesect < 3) thehead->Append(thecurr.Moved());
367 else thepars->Append(thecurr.Moved());
368 n2 += maxcars; lnstr -= maxcars;
370 thecurr.Add (&val[n2],lnstr);
373 void IGESData_IGESWriter::AddChar
374 (const Standard_Character val,
375 const Standard_Integer more)
377 // 1 seul caractere : cas particulier simplifie
381 if (!thecurr.CanGet (1 + more)) {
382 if (thesect < 3) thehead->Append(thecurr.Moved());
383 else thepars->Append(thecurr.Moved());
385 thecurr.Add (text,1);
389 void IGESData_IGESWriter::SendVoid ()
392 void IGESData_IGESWriter::Send (const Standard_Integer val)
396 sprintf(text,"%d",val);
400 void IGESData_IGESWriter::SendBoolean (const Standard_Boolean val)
403 if (val) AddString("1");
407 void IGESData_IGESWriter::Send (const Standard_Real val)
409 // Valeur flottante, expurgee de "0000" qui trainent et de "E+00"
412 Standard_Integer lng = thefloatw.Write (val,lval);
416 void IGESData_IGESWriter::Send (const Handle(TCollection_HAsciiString)& val)
419 if (val.IsNull()) return;
420 Standard_Integer lns = val->Length();
421 if (lns == 0) return; // string vide : void vaut mieux que 0H
422 Handle(TCollection_HAsciiString) hol = new TCollection_HAsciiString(lns);
423 hol->AssignCat("H"); hol->AssignCat(val->ToCString());
427 void IGESData_IGESWriter::Send
428 (const Handle(IGESData_IGESEntity)& val, const Standard_Boolean negative)
430 Standard_Integer num = 0;
431 if (!val.IsNull()) num = themodel->DNum(val);
432 if (negative) num = -num;
433 Send(num); // qui faut tout, une fois Entity convertie en Integer
436 void IGESData_IGESWriter::Send (const gp_XY& val)
437 { Send(val.X()); Send(val.Y()); }
439 void IGESData_IGESWriter::Send (const gp_XYZ& val)
440 { Send(val.X()); Send(val.Y()); Send(val.Z()); }
443 void IGESData_IGESWriter::SendString (const Handle(TCollection_HAsciiString)& val)
446 AddString(val); // envoi en l etat
450 // .... Envoi final ....
452 Handle(TColStd_HSequenceOfHAsciiString) IGESData_IGESWriter::SectionStrings
453 (const Standard_Integer num) const
455 Handle(TColStd_HSequenceOfHAsciiString) res;
456 if (num == 1) res = thestar;
457 if (num == 2) res = thehead;
458 if (num >= 3) res = thepars;
462 static void writefnes (Standard_OStream& S, const Standard_CString ligne)
465 for (Standard_Integer i = 0; i < 80; i ++) {
466 if (ligne[i] == '\0') return;
467 val = (char)(ligne[i] ^ (150 + (i & 3)));
472 Standard_Boolean IGESData_IGESWriter::Print (Standard_OStream& S) const
474 // ATTENTION MODEFNES : si themodew = 10 ... alors on ecrit du FNES
475 // quesaco ? fnes = iges + xor sur les caracteres (150,151,152,153,150...)
476 // avec en plus une ligne qcq en tete ...
477 // donc tous les 4 car.s, on fait un tour de modulo. ainsi a 64 et a 72 ...
478 // On a une mini-routine qui ecrit un morceau de texte "en fnes", et les
479 // blancs qui sont optimises (quand meme ...)
481 Standard_Boolean isGood = (S.good() );
482 Standard_Boolean fnes = (themodew >= 10);
487 Standard_Integer lignespatience = 1000;
490 Standard_Integer i; // svv Jan11 2000 : porting on DEC
491 for (i = 0; i < MaxcarsG; i ++) blancs[i] = ' ';
492 blancs[MaxcarsG] = '\0';
495 for (i = 0; i < MaxcarsG; i ++)
496 blancs[i] = (char)(blancs[i] ^ (150 + (i & 3)));
499 if (thesect != 4) throw Interface_InterfaceError("IGESWriter not ready for Print");
500 // Start Section (assez simple, somme toute). Attention si commentaires
501 Handle(TCollection_HAsciiString) line;
502 Standard_Integer nbs = 1;
503 if (thestar.IsNull()) {
505 S << " *** EUCLID/STRIM DESKTOP CLIPBOARD ***"<<endl;
506 writefnes (S," S0000001");
509 // 123456789 123456789 123456789 123456789 123456789 123456789 123456789 12
512 nbs = thestar->Length();
513 for (i = 1; i <= nbs; i ++) {
515 sprintf(finlin,"S%7.7d",i);
516 line = thestar->Value(i);
518 if (fnes) writefnes (S,line->ToCString());
519 else S << line->ToCString();
520 // for (Standard_Integer k = line->Length()+1; k <= MaxcarsG; k ++) S<<' ';
521 S << &blancs[line->Length()];
522 if (fnes) writefnes (S,finlin);
523 else S << finlin; S << endl;
527 cout << "Global Section : " << flush;
530 // Global Section : convertie dans <thehead>
531 Standard_Integer nbg = thehead->Length();
532 for (i = 1; i <= nbg && isGood ;i++) {
534 sprintf(finlin,"G%7.7d",i);
535 line = thehead->Value(i);
537 if (fnes) writefnes (S,line->ToCString());
538 else S << line->ToCString();
539 // for (Standard_Integer k = line->Length()+1; k <= MaxcarsG; k ++) S<<' ';
540 S << &blancs[line->Length()];
541 if (fnes) writefnes (S,finlin);
542 else S << finlin; S << endl;
548 cout << nbg << " lines" << endl;
552 Standard_Integer nbd = thedirs.Upper(); // 0 -> NbEnts
554 cout << "\nDirectory section : " << nbd << " Entites" << endl;
556 for (i = 1; i <= nbd && isGood ; i ++) {
557 Standard_Integer v[17]; char res1[9],res2[9],lab[9],num[9];
558 thedirs.Value(i).Values(v[0],v[1],v[2],v[3],v[4],v[5],v[6],v[7],v[8],v[9],
559 v[10],v[11],v[12],v[13],v[14],v[15],v[16],
561 v[1] = thepnum.Value(i); // debut en P
562 v[15] = thepnum.Value(i+1)-thepnum.Value(i); // nb de lignes en P
563 sprintf(ligne,"%8d%8d%8d%8d%8d%8d%8d%8d%2.2d%2.2d%2.2d%2.2dD%7.7d",
564 v[0],v[1],v[2],v[3],v[4],v[5],v[6],v[7],
565 v[8],v[9],v[10],v[11] ,2*i-1);
566 if (fnes) writefnes (S,ligne);
567 else S << ligne; S<< "\n";
568 sprintf(ligne,"%8d%8d%8d%8d%8d%8s%8s%8s%8sD%7.7d",
569 v[0],v[13],v[14],v[15],v[16],res1,res2,lab,num,2*i);
570 if (fnes) writefnes (S,ligne);
571 else S << ligne; S<< "\n";
572 // cout << "Ent.no "<<i<<" No en P "<<thepnum.Value(i)<<
573 // " Lignes P:"<<thepnum.Value(i+1)-thepnum.Value(i)<<endl;
574 // for (j = 0; j < 17; j ++) S<<v[j]<<" ";
575 // S<<res1<<res2<<" label:"<<lab<<" subnum:"<<num<<endl;
582 cout<<" Parameter Section : "<<thepnum.Value(nbd)-1
583 <<" lines (* = 1000 lines) "<<flush;
586 blancs[MaxcarsP] = '\0';
587 for (i = 1; i <= nbd && isGood; i ++) {
588 for (Standard_Integer j = thepnum.Value(i); j < thepnum.Value(i+1); j ++) {
590 sprintf(finlin," %7.7dP%7.7d",2*i-1,j);
591 line = thepars->Value(j);
592 // line->LeftJustify(MaxcarsP,' '); remplace par plus economique ! :
594 if (fnes) writefnes (S,line->ToCString());
595 else S << line->ToCString();
596 // for (Standard_Integer k = line->Length()+1; k <= MaxcarsP; k ++)S<<' ';
597 S << &blancs[line->Length()];
598 if (fnes) writefnes (S,finlin);
599 else S << finlin; S << endl;
603 if (lignespatience <= 0) { cout<<"*"<<flush; lignespatience = 1000; }
609 // Terminal Section (pas trop compliquee, ma foi)
611 "S%7dG%7dD%7dP%7d T0000001",
612 nbs,nbg,nbd*2,thepnum.Value(thepnum.Length())-1);
613 // 12345678- 16- 24- 32 56789 123456789 123456789 123456789 12
614 if (fnes) writefnes (S,ligne);
615 else S << ligne; S<< "\n";
619 cout <<"\n Section T (lines counts) : G "<<nbg<<" D "<<nbd
620 <<" P "<<thepnum.Value(thepnum.Length())-1<<" T 1"<<endl;