69de9afc1f366a9cb423db02fdbb6b43b91c2c65
[occt.git] / src / StepData / StepData_StepModel.cxx
1 // Copyright (c) 1999-2014 OPEN CASCADE SAS
2 //
3 // This file is part of Open CASCADE Technology software library.
4 //
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.
10 //
11 // Alternatively, this file may be used under the terms of Open CASCADE
12 // commercial license or contractual agreement.
13
14
15 #include <Interface_Check.hxx>
16 #include <Interface_CopyTool.hxx>
17 #include <Interface_EntityIterator.hxx>
18 #include <Interface_GeneralLib.hxx>
19 #include <Interface_GeneralModule.hxx>
20 #include <Interface_InterfaceModel.hxx>
21 #include <Interface_Macros.hxx>
22 #include <Interface_ShareTool.hxx>
23 #include <Message_Messenger.hxx>
24 #include <Standard_NoSuchObject.hxx>
25 #include <Standard_Transient.hxx>
26 #include <Standard_Type.hxx>
27 #include <StepData.hxx>
28 #include <StepData_Protocol.hxx>
29 #include <StepData_StepModel.hxx>
30 #include <StepData_StepWriter.hxx>
31 #include <TCollection_HAsciiString.hxx>
32
33 #include <stdio.h>
34 IMPLEMENT_STANDARD_RTTIEXT(StepData_StepModel,Interface_InterfaceModel)
35
36 // Entete de fichier : liste d entites
37 StepData_StepModel::StepData_StepModel ()  {  }
38
39
40 Handle(Standard_Transient) StepData_StepModel::Entity
41 (const Standard_Integer num) const
42 {  return Value(num);  }      // nom plus joli
43
44 void StepData_StepModel::GetFromAnother
45 (const Handle(Interface_InterfaceModel)& other)
46 {
47   theheader.Clear();
48   DeclareAndCast(StepData_StepModel,another,other);
49   if (another.IsNull()) return;
50   Interface_EntityIterator iter = another->Header();
51   //  recopier le header. Attention, header distinct du contenu ...
52   Interface_CopyTool TC (this,StepData::HeaderProtocol());
53   for (; iter.More(); iter.Next()) {
54     Handle(Standard_Transient) newhead;
55     if (!TC.Copy(iter.Value(),newhead,Standard_False,Standard_False)) continue;
56     if (!newhead.IsNull()) theheader.Append(newhead);
57   }
58 }
59
60 Handle(Interface_InterfaceModel) StepData_StepModel::NewEmptyModel () const
61 {  return new StepData_StepModel;  }
62
63
64 Interface_EntityIterator StepData_StepModel::Header () const
65 {
66   Interface_EntityIterator iter;
67   theheader.FillIterator(iter);
68   return iter;
69 }
70
71 Standard_Boolean StepData_StepModel::HasHeaderEntity
72 (const Handle(Standard_Type)& atype) const
73 {  return (theheader.NbTypedEntities(atype) == 1);  }
74
75 Handle(Standard_Transient) StepData_StepModel::HeaderEntity
76 (const Handle(Standard_Type)& atype) const
77 {  return theheader.TypedEntity(atype);  }
78
79
80 //   Remplissage du Header
81
82 void StepData_StepModel::ClearHeader ()
83 {  theheader.Clear();  }
84
85
86 void StepData_StepModel::AddHeaderEntity
87 (const Handle(Standard_Transient)& ent)
88 {  theheader.Append(ent);  }
89
90
91 void StepData_StepModel::VerifyCheck(Handle(Interface_Check)& ach) const
92 {
93   Interface_GeneralLib lib(StepData::HeaderProtocol());
94   Handle(StepData_StepModel) me (this);
95   Handle(Interface_Protocol) aHP = StepData::HeaderProtocol();
96   Interface_ShareTool sh(me,aHP);
97   Handle(Interface_GeneralModule) module;  Standard_Integer CN;
98   for (Interface_EntityIterator iter = Header(); iter.More(); iter.Next()) {
99     Handle(Standard_Transient) head = iter.Value();
100     if (!lib.Select(head,module,CN)) continue;
101     module->CheckCase(CN,head,sh,ach);
102   }
103 }
104
105
106 void StepData_StepModel::DumpHeader
107 (const Handle(Message_Messenger)& S, const Standard_Integer /*level*/) const
108 {
109   //  NB : level n est pas utilise
110
111   Handle(StepData_Protocol) stepro = StepData::HeaderProtocol();
112   Standard_Boolean iapro = !stepro.IsNull();
113   if (!iapro) S<<" -- WARNING : StepModel DumpHeader, Protocol not defined\n";
114
115   Interface_EntityIterator iter = Header();
116   Standard_Integer nb = iter.NbEntities();
117   S << " --  Step Model Header : " <<iter.NbEntities() << " Entities :\n";
118   for (iter.Start(); iter.More(); iter.Next()) {
119     S << "  "  << iter.Value()->DynamicType()->Name() << "\n";
120   }
121   if (!iapro || nb == 0) return;
122   S << " --  --        STEP MODEL    HEADER  CONTENT      --  --" << Message_EndLine;
123   S << " --   Dumped with Protocol : " << stepro->DynamicType()->Name()
124     << "   --"<<Message_EndLine;
125
126   Standard_SStream aSStream;
127   Handle(StepData_StepModel) me (this);
128   StepData_StepWriter SW(me);
129   SW.SendModel(stepro,Standard_True);    // envoi HEADER seul
130   SW.Print(aSStream);
131   S << aSStream.str().c_str();
132 }
133
134
135 void  StepData_StepModel::ClearLabels ()
136 {  theidnums.Nullify();  }
137
138 void  StepData_StepModel::SetIdentLabel
139 (const Handle(Standard_Transient)& ent, const Standard_Integer ident)
140 {
141   Standard_Integer num = Number(ent);
142   if (!num) 
143     return;
144   Standard_Integer nbEnt = NbEntities();
145   if(theidnums.IsNull())
146   {
147     theidnums = new TColStd_HArray1OfInteger(1,nbEnt);
148     theidnums->Init(0);
149   }
150   else if(nbEnt > theidnums->Length())
151   {
152     Standard_Integer prevLength = theidnums->Length();
153     Handle(TColStd_HArray1OfInteger) idnums1 = new TColStd_HArray1OfInteger(1,nbEnt);
154     idnums1->Init(0);
155     Standard_Integer k =1;
156     for( ; k <= prevLength; k++)
157       idnums1->SetValue(k , theidnums->Value(k));
158     theidnums = idnums1;
159   }
160   theidnums->SetValue(num,ident);
161
162 }
163
164 Standard_Integer  StepData_StepModel::IdentLabel
165 (const Handle(Standard_Transient)& ent) const
166 {
167   if(theidnums.IsNull())
168     return 0;
169   Standard_Integer num = Number(ent);
170   return (!num ? 0 : theidnums->Value(num));
171  }
172
173 void  StepData_StepModel::PrintLabel
174 (const Handle(Standard_Transient)& ent, const Handle(Message_Messenger)& S) const
175 {
176   Standard_Integer num = (theidnums.IsNull() ? 0 : Number(ent));
177   Standard_Integer  nid = (!num ? 0 : theidnums->Value(num));
178   if      (nid > 0) S<<"#"<<nid;
179   else if (num > 0) S<<"(#"<<num<<")";
180   else              S<<"(#0..)";
181 }
182
183 Handle(TCollection_HAsciiString) StepData_StepModel::StringLabel
184 (const Handle(Standard_Transient)& ent) const
185 {
186   Handle(TCollection_HAsciiString) label;
187   char text[20];
188   Standard_Integer num = (theidnums.IsNull() ? 0 : Number(ent));
189   Standard_Integer  nid = (!num ? 0 : theidnums->Value(num));
190
191   if      (nid > 0) sprintf (text, "#%d",nid);
192   else if (num > 0) sprintf (text, "(#%d)",num);
193   else              sprintf (text, "(#0..)");
194
195   label = new TCollection_HAsciiString(text);
196   return label;
197 }