summaryrefslogtreecommitdiff
path: root/src/IGESData/IGESData_ToolLocation.cxx
blob: 2a14788aeb4dce13ce17f9ddb22c38ab01fa2187 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
//szv#9:PRO19565:04Oct99 loss of rotation matrix corrected
#include <IGESData_ToolLocation.ixx>
#include <Interface_EntityIterator.hxx>
#include <Interface_Macros.hxx>
#include <IGESData_TransfEntity.hxx>
#include <IGESData_SingleParentEntity.hxx>
#include <IGESData_GeneralModule.hxx>
#include <gp_XYZ.hxx>
#include <gp_Pnt.hxx>
#include <gp_Vec.hxx>
#include <gp_Dir.hxx>
#include <gp_Ax3.hxx>
#include <Standard_DomainError.hxx>

#define TYPEFORASSOC 402

  IGESData_ToolLocation::IGESData_ToolLocation (const Handle(IGESData_IGESModel)& amodel,
						const Handle(IGESData_Protocol)& protocol)
: thelib (protocol),
  therefs (0,amodel->NbEntities()),
  theassocs (0,amodel->NbEntities())
{
  theprec  = 1.e-05;
  themodel = amodel;
  therefs.Init(0);  theassocs.Init(0);
  Load();
}

void  IGESData_ToolLocation::Load ()
{
  // Pour chaque Entite, sauf Transf et Assoc (sauf SingleParent), on considere
  // ses "OwnShared" comme etant dependents
  Standard_Integer nb = themodel->NbEntities();
  for (Standard_Integer i = 1; i <= nb; i ++) {
    Handle(IGESData_IGESEntity) ent = themodel->Entity(i);
    if (ent->IsKind(STANDARD_TYPE(IGESData_TransfEntity))) continue;
    // Cas de SingleParentEntity
    if (ent->IsKind(STANDARD_TYPE(IGESData_SingleParentEntity))) {
      DeclareAndCast(IGESData_SingleParentEntity,assoc,ent);
      Standard_Integer nbc = assoc->NbChildren();
      Handle(IGESData_IGESEntity) parent = assoc->SingleParent();
      for (Standard_Integer j = 1; j <= nbc; j ++)
	SetParentAssoc(parent,assoc->Child(j));
      continue;
    }
    if (ent->TypeNumber() == TYPEFORASSOC) continue; // Assoc sauf SingleParent
    // Cas courant
    SetOwnAsDependent (ent); // qui opere
  }
}

void IGESData_ToolLocation::SetPrecision (const Standard_Real prec)
{  theprec = prec;  }

void IGESData_ToolLocation::SetReference (const Handle(IGESData_IGESEntity)& parent,
					  const Handle(IGESData_IGESEntity)& child)
{
  Standard_Integer np = themodel->Number(parent);
  Standard_Integer nc = themodel->Number(child);
  if (np == 0 || nc == 0) return;
  if (therefs(nc) > 0) np = -1;    // note ambigu
  therefs.SetValue(nc,np);
}

void IGESData_ToolLocation::SetParentAssoc (const Handle(IGESData_IGESEntity)& parent,
					    const Handle(IGESData_IGESEntity)& child)
{
  Standard_Integer np = themodel->Number(parent);
  Standard_Integer nc = themodel->Number(child);
  if (np == 0 || nc == 0) return;
  if (theassocs(nc) > 0) np = -1;    // note ambigu
  theassocs.SetValue(nc,np);
}

void  IGESData_ToolLocation::ResetDependences (const Handle(IGESData_IGESEntity)& child)
{
  Standard_Integer nc = themodel->Number(child);
  if (nc == 0) return;
  therefs.SetValue(nc,0);
  theassocs.SetValue(nc,0);
}

void  IGESData_ToolLocation::SetOwnAsDependent (const Handle(IGESData_IGESEntity)& ent)
{
  Handle(IGESData_GeneralModule) module;
  Standard_Integer CN;
  if (!thelib.Select(ent,module,CN)) return;
  Interface_EntityIterator list;
  module->OwnSharedCase(CN,ent,list);
  // Remarque : en toute rigueur, il faudrait ignorer les entites referencees
  // dont le SubordinateStatus vaut 0 ou 2 ...
  // Question : ce Status est-il toujours bien comme il faut ?
  for (list.Start(); list.More(); list.Next())
    SetReference (ent, GetCasted(IGESData_IGESEntity,list.Value()) );
}

//  #########################################################################
//  ########                        RESULTATS                        ########

Standard_Boolean  IGESData_ToolLocation::IsTransf
  (const Handle(IGESData_IGESEntity)& ent) const
{  return ent->IsKind(STANDARD_TYPE(IGESData_TransfEntity));  }

Standard_Boolean  IGESData_ToolLocation::IsAssociativity
  (const Handle(IGESData_IGESEntity)& ent) const
{  return (ent->TypeNumber() == TYPEFORASSOC);  }

Standard_Boolean  IGESData_ToolLocation::HasTransf
  (const Handle(IGESData_IGESEntity)& ent) const
{  return ent->HasTransf();  }

gp_GTrsf  IGESData_ToolLocation::ExplicitLocation
  (const Handle(IGESData_IGESEntity)& ent) const
{  return ent->Location();  }


Standard_Boolean  IGESData_ToolLocation::IsAmbiguous
  (const Handle(IGESData_IGESEntity)& ent) const
{
  Standard_Integer num = themodel->Number(ent);
  if (num == 0) return Standard_False;
  if (therefs(num) <  0 || theassocs(num) <  0) return Standard_True;
  if (therefs(num) != 0 && theassocs(num) != 0) return Standard_True;
  return Standard_False;
}

Standard_Boolean  IGESData_ToolLocation::HasParent
  (const Handle(IGESData_IGESEntity)& ent) const
{
  Standard_Integer num = themodel->Number(ent);
  if (num == 0) return Standard_False;
  if (therefs(num) <  0 || theassocs(num) <  0) Standard_DomainError::Raise
    ("IGESData_ToolLocation : HasParent");
  if (therefs(num) != 0 && theassocs(num) != 0) Standard_DomainError::Raise
    ("IGESData_ToolLocation : HasParent");
  if (therefs(num) != 0 || theassocs(num) != 0) return Standard_True;
  return Standard_False;
}

Handle(IGESData_IGESEntity)  IGESData_ToolLocation::Parent
  (const Handle(IGESData_IGESEntity)& ent) const
{
  Handle(IGESData_IGESEntity) parent;
  Standard_Integer num = themodel->Number(ent);
  if (num == 0) return parent;
  if (therefs(num) <  0 || theassocs(num) <  0) Standard_DomainError::Raise
    ("IGESData_ToolLocation : Parent");
  if (therefs(num) != 0 && theassocs(num) != 0) Standard_DomainError::Raise
    ("IGESData_ToolLocation : Parent");
  if (therefs(num)   != 0) parent = themodel->Entity (therefs   (num));
  if (theassocs(num) != 0) parent = themodel->Entity (theassocs (num));
  return parent;
}

Standard_Boolean  IGESData_ToolLocation::HasParentByAssociativity
  (const Handle(IGESData_IGESEntity)& ent) const
{
  Standard_Integer num = themodel->Number(ent);
  if (num == 0) return Standard_False;
  if (therefs(num) <  0 || theassocs(num) <  0) Standard_DomainError::Raise
    ("IGESData_ToolLocation : HasParentByAssociativity");
  if (therefs(num) != 0 && theassocs(num) != 0) Standard_DomainError::Raise
    ("IGESData_ToolLocation : HasParentByAssociativity");
  if (theassocs(num) != 0) return Standard_True;
  return Standard_False;
}

gp_GTrsf  IGESData_ToolLocation::ParentLocation
  (const Handle(IGESData_IGESEntity)& ent) const
{
  gp_GTrsf locat;    // par defaut, identite
  Handle(IGESData_IGESEntity) parent = Parent(ent);
  // Definition recursive
  if (!parent.IsNull()) locat = EffectiveLocation(parent);
  return locat;
}

gp_GTrsf  IGESData_ToolLocation::EffectiveLocation
  (const Handle(IGESData_IGESEntity)& ent) const
{
  gp_GTrsf locat  = ent->Location();
  // Combiner Transf et ParentLocation
  locat.PreMultiply (ParentLocation(ent));    // ne pas se tromper de sens !
  return locat;
}

Standard_Boolean  IGESData_ToolLocation::AnalyseLocation (const gp_GTrsf& loc,
							  gp_Trsf& result) const
{  return ConvertLocation (theprec,loc,result);  }

Standard_Boolean  IGESData_ToolLocation::ConvertLocation (const Standard_Real prec,
							  const gp_GTrsf& loc,
							  gp_Trsf& result,
							  const Standard_Real unit)
{
  if (result.Form() != gp_Identity) result = gp_Trsf();  // Identite forcee au depart
  // On prend le contenu de <loc>. Attention a l adressage
  gp_XYZ v1 ( loc.Value(1,1), loc.Value(1,2), loc.Value(1,3) );
  gp_XYZ v2 ( loc.Value(2,1), loc.Value(2,2), loc.Value(2,3) );
  gp_XYZ v3 ( loc.Value(3,1), loc.Value(3,2), loc.Value(3,3) );
  // A-t-on affaire a une similitude ?
  Standard_Real m1 = v1.Modulus();
  Standard_Real m2 = v2.Modulus();
  Standard_Real m3 = v3.Modulus();
  // D abord est-elle singuliere cette matrice ?
  if (m1 < prec || m2 < prec || m3 < prec) return Standard_False;
  Standard_Real mm = (m1+m2+m3)/3.; // voici la Norme moyenne, cf Scale
  if ( Abs(m1 - mm) > prec*mm ||
       Abs(m2 - mm) > prec*mm ||
       Abs(m3 - mm) > prec*mm ) return Standard_False;
  v1.Divide(m1);
  v2.Divide(m2);
  v3.Divide(m3);
  if ( Abs(v1.Dot(v2)) > prec ||
       Abs(v2.Dot(v3)) > prec ||
       Abs(v3.Dot(v1)) > prec ) return Standard_False;
  // Ici, Orthogonale et memes normes. En plus on l a Normee
  // Restent les autres caracteristiques :
  if (Abs(mm - 1.) > prec) result.SetScale(gp_Pnt(0,0,0),mm);
  gp_XYZ tp = loc.TranslationPart();
  if (unit != 1.) tp.Multiply(unit);
  if (tp.X() != 0. || tp.Y() != 0. || tp.Z() != 0.) result.SetTranslationPart(tp);
  // On isole le cas de l Identite (tellement facile et avantageux)
  if (v1.X() != 1. || v1.Y() != 0. || v1.Z() != 0. ||
      v2.X() != 0. || v2.Y() != 1. || v2.Z() != 0. ||
      v3.X() != 0. || v3.Y() != 0. || v3.Z() != 1. ) {
    // Pas Identite : vraie construction depuis un Ax3
    gp_Dir d1(v1);
    gp_Dir d2(v2);
    gp_Dir d3(v3);
    gp_Ax3 axes (gp_Pnt(0,0,0),d3,d1);
    d3.Cross(d1);
    if (d3.Dot(d2) < 0) axes.YReverse();
    gp_Trsf transf;
    transf.SetTransformation(axes);
    result *= transf; //szv#9:PRO19565:04Oct99
  }
  return Standard_True;
}