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