0028832: MMgt_TShared can be replaced by Standard_Transient
[occt.git] / src / IGESData / IGESData_IGESEntity.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 <gp_GTrsf.hxx>
16 #include <IGESData_ColorEntity.hxx>
17 #include <IGESData_DirChecker.hxx>
18 #include <IGESData_GeneralModule.hxx>
19 #include <IGESData_IGESEntity.hxx>
20 #include <IGESData_IGESReaderTool.hxx>
21 #include <IGESData_IGESType.hxx>
22 #include <IGESData_LabelDisplayEntity.hxx>
23 #include <IGESData_LevelListEntity.hxx>
24 #include <IGESData_LineFontEntity.hxx>
25 #include <IGESData_NameEntity.hxx>
26 #include <IGESData_ReadWriteModule.hxx>
27 #include <IGESData_SingleParentEntity.hxx>
28 #include <IGESData_TransfEntity.hxx>
29 #include <IGESData_ViewKindEntity.hxx>
30 #include <Interface_EntityIterator.hxx>
31 #include <Interface_EntityList.hxx>
32 #include <Interface_InterfaceError.hxx>
33 #include <Interface_Macros.hxx>
34 #include <Standard_NoSuchObject.hxx>
35 #include <Standard_PCharacter.hxx>
36 #include <Standard_Type.hxx>
37 #include <TCollection_HAsciiString.hxx>
38
39 #include <stdio.h>
40 IMPLEMENT_STANDARD_RTTIEXT(IGESData_IGESEntity,Standard_Transient)
41
42 #define ThisEntity  Handle(IGESData_IGESEntity)::DownCast(This())
43
44 namespace
45 {
46   static const Standard_Integer IGESFlagAssocs  = 131072;
47   static const Standard_Integer IGESFlagProps   = 262144;
48   static const Standard_Integer IGESFourStatus  = 65535;
49   static const Standard_Integer IGESStatusField = 15;
50   static const Standard_Integer IGESShiftSubord = 4;
51   static const Standard_Integer IGESShiftUse    = 8;
52   static const Standard_Integer IGESShiftHier   = 12;
53 }
54
55 //=======================================================================
56 //function : IGESData_IGESEntity
57 //purpose  : Default constructor.
58 //=======================================================================
59 IGESData_IGESEntity::IGESData_IGESEntity()
60 : theType       (0),
61   theForm       (0),
62   theDefLevel   (0),
63   theStatusNum  (0),
64   theLWeightNum (0),
65   theLWeightVal (0.0),
66   theSubScriptN (0)
67 {
68   theRes1[0] = theRes2[0] = '\0';
69 }
70
71 //=======================================================================
72 //function : Clear
73 //purpose  : 
74 //=======================================================================
75 void IGESData_IGESEntity::Clear ()
76 {
77 //  Handle et DefSwitch
78   theStructure.Nullify();
79   theDefLineFont.SetVoid();  theLineFont.Nullify();
80   theDefColor.SetVoid();     theColor.Nullify();
81   theDefLevel   = 0;         theLevelList.Nullify();
82   theView.Nullify();         theTransf.Nullify();      theLabDisplay.Nullify();
83   theSubScriptN = 0;         theShortLabel.Nullify();
84
85 //  Autres Valeurs, Listes
86   theType       = theForm       = 0;
87   theStatusNum  = theLWeightNum = 0;
88   theLWeightVal = 0.;
89 //  theRes1[0]    = theRes2[0]    = '\0';
90   theProps.Clear(); theAssocs.Clear();
91 }
92
93
94 //  #########################################################################
95 //  ....                Definition IGES : Directory Entry                ....
96
97     IGESData_IGESType IGESData_IGESEntity::IGESType () const
98       {  return IGESData_IGESType(theType,theForm);  }
99
100     Standard_Integer IGESData_IGESEntity::TypeNumber () const
101       {  return theType;  }
102
103     Standard_Integer IGESData_IGESEntity::FormNumber () const
104       {  return theForm;  }
105
106
107     Handle(IGESData_IGESEntity)  IGESData_IGESEntity::DirFieldEntity
108   (const Standard_Integer num) const
109 {
110   Handle(IGESData_IGESEntity) ent;
111   if (num ==  3) ent = theStructure;
112   if (num ==  4) ent = theLineFont;
113   if (num ==  5) ent = theLevelList;
114   if (num ==  6) ent = theView;
115   if (num ==  7) ent = theTransf;
116   if (num ==  8) ent = theLabDisplay;
117   if (num == 13) ent = theColor;
118   return ent;
119 }
120
121     Standard_Boolean IGESData_IGESEntity::HasStructure () const
122       {  return (!theStructure.IsNull());  }
123
124     Handle(IGESData_IGESEntity) IGESData_IGESEntity::Structure () const
125       {  return theStructure;  }
126
127     IGESData_DefType IGESData_IGESEntity::DefLineFont () const
128       {  return theDefLineFont.DefType();  }
129
130     Standard_Integer IGESData_IGESEntity::RankLineFont () const
131       {  return theDefLineFont.Value();  }
132
133     Handle(IGESData_LineFontEntity) IGESData_IGESEntity::LineFont () const
134       {  return GetCasted(IGESData_LineFontEntity,theLineFont);  }
135
136     IGESData_DefList IGESData_IGESEntity::DefLevel () const
137 {
138   if (theDefLevel >  0) return IGESData_DefOne;
139   if (theDefLevel <  0) return IGESData_DefSeveral;
140   return IGESData_DefNone;
141 }
142
143     Standard_Integer IGESData_IGESEntity::Level () const
144       {  return theDefLevel;  }
145
146     Handle(IGESData_LevelListEntity) IGESData_IGESEntity::LevelList () const
147       {  return GetCasted(IGESData_LevelListEntity,theLevelList);  }
148
149
150     IGESData_DefList IGESData_IGESEntity::DefView () const
151 {
152   if (View().IsNull())    return IGESData_DefNone;
153   if (View()->IsSingle()) return IGESData_DefOne;
154   else                    return IGESData_DefSeveral;
155 }
156
157     Handle(IGESData_ViewKindEntity) IGESData_IGESEntity::View () const
158       {  return GetCasted(IGESData_ViewKindEntity,theView);  }
159
160     Handle(IGESData_ViewKindEntity) IGESData_IGESEntity::SingleView () const
161 {
162   Handle(IGESData_ViewKindEntity) nulvue;
163   if (DefView() != IGESData_DefOne) return nulvue;
164   return View();
165 }
166
167     Handle(IGESData_ViewKindEntity) IGESData_IGESEntity::ViewList () const
168 {
169   Handle(IGESData_ViewKindEntity) nulvue;
170   if (DefView() != IGESData_DefSeveral) return nulvue;
171   return View();
172 }
173
174
175     Standard_Boolean IGESData_IGESEntity::HasTransf () const
176       {  return (!theTransf.IsNull());  }
177
178     Handle(IGESData_TransfEntity) IGESData_IGESEntity::Transf () const
179       {  return GetCasted(IGESData_TransfEntity,theTransf);  }
180
181
182     Standard_Boolean IGESData_IGESEntity::HasLabelDisplay () const
183       {  return (!theLabDisplay.IsNull());  }
184
185     Handle(IGESData_LabelDisplayEntity) IGESData_IGESEntity::LabelDisplay
186       () const
187       {  return GetCasted(IGESData_LabelDisplayEntity,theLabDisplay);  }
188
189 // Status : un Integer pour BlankStatus,SubrodinateStatus,USeFlag,HierarchySt.
190 // Decoupage : 4 bits chacun (BlankStatus tout a droite, etc)
191
192     Standard_Integer IGESData_IGESEntity::BlankStatus () const
193       {  return (theStatusNum & IGESStatusField);  }
194
195     Standard_Integer IGESData_IGESEntity::SubordinateStatus () const
196       {  return ((theStatusNum >> IGESShiftSubord) & IGESStatusField);  }
197
198     Standard_Integer IGESData_IGESEntity::UseFlag () const
199       {  return ((theStatusNum >> IGESShiftUse) & IGESStatusField);  }
200
201     Standard_Integer IGESData_IGESEntity::HierarchyStatus () const
202       {  return ((theStatusNum >> IGESShiftHier) & IGESStatusField);  }
203
204     Standard_Integer IGESData_IGESEntity::LineWeightNumber () const
205       {  return theLWeightNum;  }
206
207     Standard_Real IGESData_IGESEntity::LineWeight () const
208       {  return theLWeightVal;  }
209
210
211     IGESData_DefType IGESData_IGESEntity::DefColor () const
212       {  return theDefColor.DefType();  }
213
214     Standard_Integer IGESData_IGESEntity::RankColor () const
215       {  return theDefColor.Value();  }
216
217     Handle(IGESData_ColorEntity) IGESData_IGESEntity::Color () const
218       {  return GetCasted(IGESData_ColorEntity,theColor);  }
219
220
221 //=======================================================================
222 //function : CResValues
223 //purpose  : 
224 //=======================================================================
225 Standard_Boolean IGESData_IGESEntity::CResValues (const Standard_CString res1, 
226                                                   const Standard_CString res2) const
227 {
228   Standard_Boolean res = Standard_False;
229   Standard_PCharacter pres1, pres2;
230   //
231   pres1=(Standard_PCharacter)res1;
232   pres2=(Standard_PCharacter)res2;
233   //
234   for (Standard_Integer i = 0; i < 8; i ++) {
235     pres1[i] = theRes1[i]; 
236     pres2[i] = theRes2[i];
237     if (theRes1[i] > ' ' || theRes2[i] > ' ') {
238       res = Standard_True;
239     }
240   }
241   pres1[8] = '\0'; 
242   pres2[8] = '\0';
243   //
244   return res;
245 }
246
247     Standard_Boolean IGESData_IGESEntity::HasShortLabel () const
248       {  return (!theShortLabel.IsNull());  }
249
250     Handle(TCollection_HAsciiString) IGESData_IGESEntity::ShortLabel () const
251       {  return theShortLabel;  }
252
253     Standard_Boolean IGESData_IGESEntity::HasSubScriptNumber () const
254       {  return (theSubScriptN >= 0);  }  // =0 nul mais defini, <0 absent
255
256     Standard_Integer IGESData_IGESEntity::SubScriptNumber () const
257 {
258   if (theSubScriptN < 0) return 0;
259   return theSubScriptN;
260 }
261
262 //  ....                (Re)Initialisation du Directory                 ....
263
264     void  IGESData_IGESEntity::InitTypeAndForm
265   (const Standard_Integer typenum, const Standard_Integer formnum)
266       {  theType = typenum;  theForm = formnum;  }
267
268     void  IGESData_IGESEntity::InitDirFieldEntity
269   (const Standard_Integer num, const Handle(IGESData_IGESEntity)& ent)
270 {
271   if (num ==  3) theStructure  = ent;
272   if (num ==  4) theLineFont   = ent;
273   if (num ==  5) theLevelList  = ent;
274   if (num ==  6) theView       = ent;
275   if (num ==  7) theTransf     = ent;
276   if (num ==  8) theLabDisplay = ent;
277   if (num == 13) theColor      = ent;
278 }
279
280     void  IGESData_IGESEntity::InitTransf
281   (const Handle(IGESData_TransfEntity)& ent)
282 {  theTransf = ent;  }
283
284     void  IGESData_IGESEntity::InitView
285   (const Handle(IGESData_ViewKindEntity)& ent)
286 {  theView = ent;  }
287
288     void  IGESData_IGESEntity::InitLineFont
289   (const Handle(IGESData_LineFontEntity)& ent, const Standard_Integer rank)
290 {  theDefLineFont.SetRank((ent.IsNull() ? rank : -1));  theLineFont = ent;  }
291
292     void  IGESData_IGESEntity::InitLevel
293   (const Handle(IGESData_LevelListEntity)& ent, const Standard_Integer val)
294 {  theLevelList = ent;  theDefLevel = (ent.IsNull() ? val : -1);  }
295
296     void  IGESData_IGESEntity::InitColor
297   (const Handle(IGESData_ColorEntity)& ent, const Standard_Integer rank)
298 {  theDefColor.SetRank((ent.IsNull() ? rank : -1));  theColor = ent;  }
299
300     void  IGESData_IGESEntity::InitStatus
301   (const Standard_Integer blank,   const Standard_Integer subordinate,
302    const Standard_Integer useflag, const Standard_Integer hierarchy)
303 {
304   theStatusNum  = (theStatusNum & (!IGESFourStatus));
305   theStatusNum += (blank       & IGESStatusField) |
306                 ((subordinate  & IGESStatusField) << IGESShiftSubord) |
307                 ((useflag      & IGESStatusField) << IGESShiftUse) |
308                 ((hierarchy    & IGESStatusField) << IGESShiftHier);
309 }
310
311     void  IGESData_IGESEntity::SetLabel
312   (const Handle(TCollection_HAsciiString)& label, const Standard_Integer sub)
313 {  theShortLabel = label;  theSubScriptN = sub;  }
314
315     void  IGESData_IGESEntity::InitMisc
316   (const Handle(IGESData_IGESEntity)& str,
317    const Handle(IGESData_LabelDisplayEntity)& lab,
318    const Standard_Integer weightnum)
319 {
320   theStructure = str;  theLabDisplay = lab;
321   if (theLWeightNum != 0)  theLWeightVal *= (weightnum/theLWeightNum);
322   else if (weightnum == 0) theLWeightVal  = 0;
323   theLWeightNum = weightnum;
324 }
325
326 //  ....                  Notions derivees importantes                  ....
327
328
329 //  SingleParent : ici on ne traite que l Associativity SingleParent
330 //  Pour considerer le partage implicite, il faut remonter au Modele ...
331
332     Standard_Boolean IGESData_IGESEntity::HasOneParent () const
333 {
334   return (NbTypedProperties(STANDARD_TYPE(IGESData_SingleParentEntity)) == 1);
335 }
336
337     Handle(IGESData_IGESEntity) IGESData_IGESEntity::UniqueParent () const
338 {
339   if (NbTypedProperties(STANDARD_TYPE(IGESData_SingleParentEntity)) != 1)
340     throw Interface_InterfaceError("IGESEntity : UniqueParent");
341   else {
342     DeclareAndCast(IGESData_SingleParentEntity,PP,
343                    TypedProperty(STANDARD_TYPE(IGESData_SingleParentEntity)));
344     return PP->SingleParent();
345   }
346 }
347
348
349     gp_GTrsf IGESData_IGESEntity::Location () const
350 {
351   //szv#4:S4163:12Mar99 unreachcble eliminated
352   //if (!HasTransf()) return gp_GTrsf();    // Identite
353   //else return Transf()->Value();          // c-a-d Compoound
354   if (!HasTransf()) return gp_GTrsf();    // Identite
355   Handle(IGESData_TransfEntity) trsf = Transf(); 
356   return (trsf.IsNull())? gp_GTrsf() : trsf->Value(); 
357 }
358
359     gp_GTrsf IGESData_IGESEntity::VectorLocation () const
360 {
361   if (!HasTransf()) return gp_GTrsf();    // Identite
362 //    Prendre Location et anuler TranslationPart
363   gp_GTrsf loca = Transf()->Value();      // c-a-d Compoound
364   loca.SetTranslationPart (gp_XYZ(0.,0.,0.));
365   return loca;
366 }
367
368     gp_GTrsf IGESData_IGESEntity::CompoundLocation () const
369 {
370   gp_GTrsf loca = Location();
371   if (!HasOneParent()) return loca;
372   gp_GTrsf locp = UniqueParent()->CompoundLocation();
373   loca.PreMultiply(locp);
374   return loca;
375 }
376
377
378     Standard_Boolean IGESData_IGESEntity::HasName () const
379 {
380   if (HasShortLabel()) return Standard_True;
381   return (NbTypedProperties(STANDARD_TYPE(IGESData_NameEntity)) == 1);
382 }
383
384 Handle(TCollection_HAsciiString) IGESData_IGESEntity::NameValue () const
385 {
386   Handle(TCollection_HAsciiString) nom;  // au depart vide
387   //   Question : concatene-t-on le SubScript ?  Oui, forme label(subscript)
388   Standard_Integer nbname = NbTypedProperties(STANDARD_TYPE(IGESData_NameEntity));
389   if (nbname == 0) {
390     if (!HasShortLabel()) return nom;
391     if (theSubScriptN < 0) return theShortLabel;
392     char lenom[50];
393     sprintf (lenom,"%s(%d)",theShortLabel->ToCString(),theSubScriptN);
394     nom = new TCollection_HAsciiString (lenom);
395   }
396   else if (nbname > 0) {
397     DeclareAndCast(IGESData_NameEntity,name,
398                    TypedProperty(STANDARD_TYPE(IGESData_NameEntity), 1));
399     nom = name->Value();
400   }
401
402   return nom;
403 }
404
405
406 //  ....            Listes d'infos Optionnelles (Assocs,Props)            ....
407
408     Standard_Boolean IGESData_IGESEntity::ArePresentAssociativities () const
409 {
410   if (!theAssocs.IsEmpty()) return Standard_True;
411   return (theStatusNum & IGESFlagAssocs) != 0;
412 }
413
414     Standard_Integer IGESData_IGESEntity::NbAssociativities () const
415 {
416   if (theAssocs.IsEmpty()) return 0;
417   return theAssocs.NbEntities();
418 }
419
420     Interface_EntityIterator IGESData_IGESEntity::Associativities () const
421 {
422   Interface_EntityIterator iter;
423   theAssocs.FillIterator(iter);
424   return iter;
425 }
426
427     Standard_Integer IGESData_IGESEntity::NbTypedAssociativities
428   (const Handle(Standard_Type)& atype) const
429       {  return theAssocs.NbTypedEntities(atype);  }
430
431     Handle(IGESData_IGESEntity) IGESData_IGESEntity::TypedAssociativity
432   (const Handle(Standard_Type)& atype) const
433 {  return GetCasted(IGESData_IGESEntity,theAssocs.TypedEntity(atype));  }
434
435     void IGESData_IGESEntity::AddAssociativity
436   (const Handle(IGESData_IGESEntity)& ent)
437       {  theAssocs.Append(ent);  }
438
439     void IGESData_IGESEntity::RemoveAssociativity
440   (const Handle(IGESData_IGESEntity)& ent)
441       {  theAssocs.Remove(ent);  }
442
443     void IGESData_IGESEntity::LoadAssociativities
444   (const Interface_EntityList& list)
445       {  theAssocs = list;  theStatusNum = (theStatusNum | IGESFlagAssocs);  }
446
447     void IGESData_IGESEntity::ClearAssociativities ()
448       {  theAssocs.Clear();  }
449
450     void IGESData_IGESEntity::Associate
451   (const Handle(IGESData_IGESEntity)& ent) const
452       {  if (!ent.IsNull()) ent->AddAssociativity(ThisEntity);  }
453
454     void IGESData_IGESEntity::Dissociate
455   (const Handle(IGESData_IGESEntity)& ent) const
456       {  if (!ent.IsNull()) ent->RemoveAssociativity(ThisEntity);  }
457
458
459     Standard_Boolean IGESData_IGESEntity::ArePresentProperties () const
460 {
461   if (!theProps.IsEmpty()) return Standard_True;
462   return (theStatusNum & IGESFlagProps) != 0;
463 }
464
465     Standard_Integer IGESData_IGESEntity::NbProperties () const
466 {
467   if (theProps.IsEmpty()) return 0;
468   return theProps.NbEntities();
469 }
470
471     Interface_EntityIterator IGESData_IGESEntity::Properties () const
472 {
473   Interface_EntityIterator iter;
474   theProps.FillIterator(iter);
475   return iter;
476 }
477
478     Standard_Integer IGESData_IGESEntity::NbTypedProperties
479   (const Handle(Standard_Type)& atype) const
480       {  return theProps.NbTypedEntities(atype);  }
481
482     Handle(IGESData_IGESEntity) IGESData_IGESEntity::TypedProperty
483   (const Handle(Standard_Type)& atype, const Standard_Integer anum) const
484 {  return GetCasted(IGESData_IGESEntity,theProps.TypedEntity(atype, anum));  }
485
486     void IGESData_IGESEntity::AddProperty
487   (const Handle(IGESData_IGESEntity)& ent)
488       {  theProps.Append(ent);  }
489
490     void IGESData_IGESEntity::RemoveProperty
491   (const Handle(IGESData_IGESEntity)& ent)
492       {  theProps.Remove(ent);  }
493
494     void IGESData_IGESEntity::LoadProperties
495   (const Interface_EntityList& list)
496       {  theProps = list;    theStatusNum = (theStatusNum | IGESFlagProps);  }
497
498     void IGESData_IGESEntity::ClearProperties ()
499       {  theProps.Clear();  }
500
501 // ....                     Actions liees au Transfert                     ....
502
503     void IGESData_IGESEntity::SetLineWeight
504       (const Standard_Real defw, const Standard_Real maxw,
505        const Standard_Integer gradw)
506 {
507   if (theLWeightNum == 0) theLWeightVal = defw;
508   else if (gradw == 1) theLWeightVal = maxw*theLWeightNum;
509   else theLWeightVal = (maxw*theLWeightNum)/gradw;
510 }