0029915: Porting to VC 2017 : Regressions in Modeling Algorithms on VC 2017
[occt.git] / src / TDataXtd / TDataXtd_Constraint.cxx
1 // Created on: 2009-04-06
2 // Copyright (c) 2009-2014 OPEN CASCADE SAS
3 //
4 // This file is part of Open CASCADE Technology software library.
5 //
6 // This library is free software; you can redistribute it and/or modify it under
7 // the terms of the GNU Lesser General Public License version 2.1 as published
8 // by the Free Software Foundation, with special exception defined in the file
9 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
10 // distribution for complete text of the license and disclaimer of any warranty.
11 //
12 // Alternatively, this file may be used under the terms of Open CASCADE
13 // commercial license or contractual agreement.
14
15
16 #include <Standard_GUID.hxx>
17 #include <Standard_Type.hxx>
18 #include <TDataStd.hxx>
19 #include <TDataStd_Real.hxx>
20 #include <TDataXtd.hxx>
21 #include <TDataXtd_Constraint.hxx>
22 #include <TDataXtd_ConstraintEnum.hxx>
23 #include <TDF_Attribute.hxx>
24 #include <TDF_ChildIterator.hxx>
25 #include <TDF_DataSet.hxx>
26 #include <TDF_Label.hxx>
27 #include <TDF_RelocationTable.hxx>
28 #include <TNaming_NamedShape.hxx>
29 #include <TopoDS_Shape.hxx>
30
31 IMPLEMENT_STANDARD_RTTIEXT(TDataXtd_Constraint,TDF_Attribute)
32
33 // for symmetry midpoint the third argument is the axis or the point
34 //=======================================================================
35 //function : GetID
36 //purpose  : 
37 //=======================================================================
38 const Standard_GUID& TDataXtd_Constraint::GetID () 
39
40   static Standard_GUID TDataXtd_ConstraintID("2a96b602-ec8b-11d0-bee7-080009dc3333");
41   return TDataXtd_ConstraintID; 
42 }
43
44
45 //=======================================================================
46 //function : Set
47 //purpose  : 
48 //=======================================================================
49
50 Handle(TDataXtd_Constraint) TDataXtd_Constraint::Set (const TDF_Label& L)
51 {
52   Handle (TDataXtd_Constraint) A;
53   if (!L.FindAttribute (TDataXtd_Constraint::GetID (), A)) {    
54     A = new TDataXtd_Constraint ();
55     L.AddAttribute(A);
56   }
57   return A;
58 }
59
60
61 //=======================================================================
62 //function : TDataXtd_Constraint
63 //purpose  : 
64 //=======================================================================
65
66 TDataXtd_Constraint::TDataXtd_Constraint()
67      : myType           (TDataXtd_RADIUS),
68        myIsReversed     (Standard_False),
69        myIsInverted     (Standard_False),
70        myIsVerified     (Standard_True)
71 {}
72
73  //=======================================================================
74  //function : Set
75  //purpose  : 
76  //=======================================================================
77
78  void TDataXtd_Constraint::Set(const TDataXtd_ConstraintEnum type,
79                               const Handle(TNaming_NamedShape)& G1) 
80  {
81    // OCC2932 correction
82    if(myType == type)
83    {
84      Handle(TNaming_NamedShape) aShape =
85        Handle(TNaming_NamedShape)::DownCast(myGeometries[0]);
86      if (aShape.IsNull() == Standard_False && G1.IsNull() == Standard_False)
87        if (aShape -> Get() == G1 -> Get()) 
88          return;
89    }
90
91    Backup();
92    myType = type;
93    myGeometries[0] = G1;
94  }
95
96 // =======================================================================
97  //function : Set
98  //purpose  : 
99  //=======================================================================
100
101  void TDataXtd_Constraint::Set(const TDataXtd_ConstraintEnum type,
102                                const Handle(TNaming_NamedShape)& G1,
103                                const Handle(TNaming_NamedShape)& G2) 
104  {   
105    // OCC2932 correction
106    if(myType == type)
107    {
108      Handle(TNaming_NamedShape) aShape1 =
109        Handle(TNaming_NamedShape)::DownCast(myGeometries[0]);
110      Handle(TNaming_NamedShape) aShape2 =
111        Handle(TNaming_NamedShape)::DownCast(myGeometries[1]);
112      if (aShape1.IsNull() == Standard_False && G1.IsNull() == Standard_False &&
113          aShape2.IsNull() == Standard_False && G2.IsNull() == Standard_False)
114        if (aShape1->Get() == G1->Get() && aShape2->Get() == G2->Get())
115          return;
116    }
117
118    Backup(); 
119    myType = type;
120    myGeometries[0] = G1; 
121    myGeometries[1] = G2;
122  }
123
124  //=======================================================================
125  //function : Set
126  //purpose  : 
127  //=======================================================================
128
129  void TDataXtd_Constraint::Set(const TDataXtd_ConstraintEnum type,
130                               const Handle(TNaming_NamedShape)& G1,
131                               const Handle(TNaming_NamedShape)& G2,
132                               const Handle(TNaming_NamedShape)& G3) 
133  {   
134    // OCC2932 correction
135    if (myType == type)
136    {
137      Handle(TNaming_NamedShape) aShape1 =
138        Handle(TNaming_NamedShape)::DownCast(myGeometries[0]);
139      Handle(TNaming_NamedShape) aShape2 =
140        Handle(TNaming_NamedShape)::DownCast(myGeometries[1]);
141      Handle(TNaming_NamedShape) aShape3 =
142        Handle(TNaming_NamedShape)::DownCast(myGeometries[2]);
143      if (aShape1.IsNull() == Standard_False && G1.IsNull() == Standard_False &&
144          aShape2.IsNull() == Standard_False && G2.IsNull() == Standard_False &&
145          aShape3.IsNull() == Standard_False && G3.IsNull() == Standard_False)
146        if (aShape1->Get() == G1->Get() &&
147            aShape2->Get() == G2->Get() &&
148            aShape3->Get() == G3->Get())
149          return;
150    }
151
152    Backup(); 
153    myType = type;
154    myGeometries[0] = G1; 
155    myGeometries[1] = G2;  
156    myGeometries[2] = G3;
157  }
158
159  //=======================================================================
160  //function : Set
161  //purpose  : 
162  //=======================================================================
163
164  void TDataXtd_Constraint::Set(const TDataXtd_ConstraintEnum type,
165                               const Handle(TNaming_NamedShape)& G1,
166                               const Handle(TNaming_NamedShape)& G2,
167                               const Handle(TNaming_NamedShape)& G3,
168                               const Handle(TNaming_NamedShape)& G4)
169  {   
170    // OCC2932 correction
171    if (myType == type)
172    {
173      Handle(TNaming_NamedShape) aShape1 =
174        Handle(TNaming_NamedShape)::DownCast(myGeometries[0]);
175      Handle(TNaming_NamedShape) aShape2 =
176        Handle(TNaming_NamedShape)::DownCast(myGeometries[1]);
177      Handle(TNaming_NamedShape) aShape3 =
178        Handle(TNaming_NamedShape)::DownCast(myGeometries[2]);
179      Handle(TNaming_NamedShape) aShape4 =
180        Handle(TNaming_NamedShape)::DownCast(myGeometries[3]);
181      if (aShape1.IsNull() == Standard_False && G1.IsNull() == Standard_False &&
182          aShape2.IsNull() == Standard_False && G2.IsNull() == Standard_False &&
183          aShape3.IsNull() == Standard_False && G3.IsNull() == Standard_False &&
184          aShape4.IsNull() == Standard_False && G4.IsNull() == Standard_False)
185        if (aShape1->Get() == G1->Get() &&
186            aShape2->Get() == G2->Get() &&
187            aShape3->Get() == G3->Get() &&
188            aShape3->Get() == G4->Get())
189          return;
190    }
191
192    Backup(); 
193    myType = type;
194    myGeometries[0] = G1; 
195    myGeometries[1] = G2;   
196    myGeometries[2] = G3;
197    myGeometries[3] = G4;  
198  }
199
200 //=======================================================================
201 //function : SetPlane
202 //purpose  : 
203 //=======================================================================
204 void TDataXtd_Constraint::SetPlane(const Handle(TNaming_NamedShape)& plane)
205 {
206   // OCC2932 correction
207   if (! myPlane.IsNull() && ! plane.IsNull() && myPlane->Get() == plane->Get())
208       return;
209
210   Backup();
211   myPlane = plane;
212 }
213
214 //=======================================================================
215 //function : Plane
216 //purpose  : 
217 //=======================================================================
218 const Handle(TNaming_NamedShape)&  TDataXtd_Constraint::GetPlane() const 
219 {
220   return myPlane;
221 }
222
223 //=======================================================================
224 //function : SetType 
225 //purpose  : 
226 //=======================================================================
227
228 void TDataXtd_Constraint::SetType (const TDataXtd_ConstraintEnum CTR) 
229 {  
230   // OCC2932 correction
231   if(myType == CTR) return;
232
233   Backup();
234   myType = CTR;
235 }
236
237
238 //=======================================================================
239 //function : GetType
240 //purpose  : 
241 //=======================================================================
242
243 TDataXtd_ConstraintEnum TDataXtd_Constraint::GetType () const
244 {
245   return myType;
246 }
247
248
249 //=======================================================================
250 //function : ClearGeometries
251 //purpose  : 
252 //=======================================================================
253
254 void TDataXtd_Constraint::ClearGeometries () 
255 {   
256   // OCC2932 correction
257   if(myGeometries[0].IsNull() && myGeometries[1].IsNull() && 
258      myGeometries[2].IsNull() && myGeometries[3].IsNull()) 
259     return;
260
261
262   Backup();
263   (myGeometries [0]).Nullify ();
264   (myGeometries [1]).Nullify ();
265   (myGeometries [2]).Nullify ();
266   (myGeometries [3]).Nullify ();
267 }
268
269
270 //=======================================================================
271 //function : SetGeometry
272 //purpose  : 
273 //=======================================================================
274
275 void TDataXtd_Constraint::SetGeometry (const Standard_Integer Index, 
276                                        const Handle(TNaming_NamedShape)& G) 
277 {  
278   // OCC2932 correction
279   Handle(TNaming_NamedShape) aGeom =
280     Handle(TNaming_NamedShape)::DownCast(myGeometries[Index - 1]);
281   if (aGeom.IsNull() == Standard_False && G.IsNull() == Standard_False) 
282     if (aGeom -> Get() == G->Get())
283       return;
284
285   Backup();
286   myGeometries [Index-1] = G;
287 }
288
289
290 //=======================================================================
291 //function : GetGeometry
292 //purpose  : 
293 //=======================================================================
294
295 Handle(TNaming_NamedShape) TDataXtd_Constraint::GetGeometry
296                                         (const Standard_Integer Index) const
297 {
298   return Handle(TNaming_NamedShape)::DownCast (myGeometries [Index-1]);
299 }
300
301
302 //=======================================================================
303 //function : NbGeometries
304 //purpose  : 
305 //=======================================================================
306
307 Standard_Integer TDataXtd_Constraint::NbGeometries () const 
308 {
309   Standard_Integer num_geom = 0 ;
310   while (num_geom < 4 && ! myGeometries[num_geom].IsNull()) {
311       num_geom += 1 ;
312   }
313   return num_geom ;
314 }
315
316
317 //=======================================================================
318 //function : IsDimension
319 //purpose  : 
320 //=======================================================================
321 Standard_Boolean TDataXtd_Constraint::IsDimension () const 
322 {
323   return !myValue.IsNull();
324 }
325
326
327 //=======================================================================
328 //function : IsPlanar
329 //purpose  : 
330 //=======================================================================
331 Standard_Boolean TDataXtd_Constraint::IsPlanar () const 
332 {
333   return !myPlane.IsNull();
334 }
335
336 //=======================================================================
337 //function : SetValue
338 //purpose  : 
339 //=======================================================================
340
341 void TDataXtd_Constraint::SetValue (const Handle(TDataStd_Real)& V) 
342 {
343   // OCC2932 correction
344   if (myValue.IsNull() == Standard_False && V.IsNull() == Standard_False)
345     if(myValue->Get() == V->Get()) return;
346
347   Backup();
348   myValue = V;
349 }
350
351 //=======================================================================
352 //function : GetValue
353 //purpose  : 
354 //=======================================================================
355
356 const Handle(TDataStd_Real)& TDataXtd_Constraint::GetValue () const
357 {
358   return myValue;
359 }
360
361
362 //=======================================================================
363 //function : ID
364 //purpose  : 
365 //=======================================================================
366
367 const Standard_GUID& TDataXtd_Constraint::ID () const { return GetID(); }
368
369
370 //=======================================================================
371 //function : NewEmpty
372 //purpose  : 
373 //=======================================================================
374
375 Handle(TDF_Attribute) TDataXtd_Constraint::NewEmpty () const
376 {  
377   return new TDataXtd_Constraint (); 
378 }
379
380
381 //=======================================================================
382 //function : Restore
383 //purpose  : 
384 //=======================================================================
385
386 void TDataXtd_Constraint::Restore(const Handle(TDF_Attribute)& with) 
387 {
388   Handle(TDataXtd_Constraint) CTR =Handle(TDataXtd_Constraint)::DownCast(with); 
389   myGeometries [0] = CTR->GetGeometry (1);
390   myGeometries [1] = CTR->GetGeometry (2);
391   myGeometries [2] = CTR->GetGeometry (3);
392   myGeometries [3] = CTR->GetGeometry (4);
393   myType = CTR->GetType ();
394   myValue = CTR->GetValue ();
395   myIsVerified = CTR->Verified();
396   myIsInverted = CTR->Inverted();
397   myIsReversed = CTR->Reversed();
398   myPlane = CTR->GetPlane();
399 }
400
401
402
403 //=======================================================================
404 //function : Paste
405 //purpose  : 
406 //=======================================================================
407
408 void TDataXtd_Constraint::Paste (const Handle(TDF_Attribute)& into,
409                                  const Handle(TDF_RelocationTable)& RT) const
410 {  
411   Handle(TDataXtd_Constraint) CTR =Handle(TDataXtd_Constraint)::DownCast (into);
412   Handle(TNaming_NamedShape) G1, G2, G3, G4, plane;  
413   RT->HasRelocation (myGeometries[0], G1);
414   CTR->SetGeometry (1, G1);
415   RT->HasRelocation (myGeometries[1], G2);
416   CTR->SetGeometry (2, G2);
417   RT->HasRelocation (myGeometries[2], G3);
418   CTR->SetGeometry (3, G3);
419   RT->HasRelocation (myGeometries[3], G4);
420   CTR->SetGeometry (4, G4);
421
422   RT->HasRelocation (myPlane, plane);
423   CTR->SetPlane(plane);
424
425   Handle(TDataStd_Real) Value;
426   RT->HasRelocation (myValue,Value);
427   CTR->SetValue (Value);
428
429   CTR->SetType (myType);
430   CTR->Verified(Verified());
431   CTR->Inverted(Inverted());
432   CTR->Reversed(Reversed());
433 }    
434
435
436 //=======================================================================
437 //function : References
438 //purpose  : 
439 //=======================================================================
440
441 void TDataXtd_Constraint::References(const Handle(TDF_DataSet)& DS) const
442
443
444 //bidouille en attendant traitement des contraintes d assemblage en dehors de la part 
445 // l attribut placement devrait oriente vers les contraintes de placement en dehors
446   Standard_Integer Lim;
447   if (myType >= TDataXtd_MATE && myType<=TDataXtd_FACES_ANGLE)  Lim =1;
448   else Lim =3;
449
450   for (Standard_Integer i=0; i<=Lim; i++) {
451     if (!myGeometries [i].IsNull()) DS->AddAttribute (myGeometries[i]);
452   }
453
454   if (!myValue.IsNull()) DS->AddAttribute (myValue);
455   if (!myPlane.IsNull()) DS->AddAttribute (myPlane);
456 }
457
458 //=======================================================================
459 //function : Verified
460 //purpose  : 
461 //=======================================================================
462 void TDataXtd_Constraint::Verified(const Standard_Boolean status)
463 {
464   // OCC2932 correction
465   if(myIsVerified == status) return;
466
467   Backup();
468   myIsVerified = status;
469 }
470
471 //=======================================================================
472 //function : Verified
473 //purpose  : 
474 //=======================================================================
475 Standard_Boolean TDataXtd_Constraint::Verified() const 
476 {
477   return myIsVerified;
478 }
479
480 //=======================================================================
481 //function : Reversed
482 //purpose  : 
483 //=======================================================================
484 void TDataXtd_Constraint::Reversed(const Standard_Boolean status)
485 {
486   // OCC2932 correction
487   if(myIsReversed == status ) return;
488
489   Backup();
490   myIsReversed = status;
491 }
492
493 //=======================================================================
494 //function : Reversed
495 //purpose  : 
496 //=======================================================================
497 Standard_Boolean TDataXtd_Constraint::Reversed() const 
498 {
499   return myIsReversed;
500 }
501
502 //=======================================================================
503 //function : Inverted
504 //purpose  : 
505 //=======================================================================
506 void TDataXtd_Constraint::Inverted(const Standard_Boolean status)
507 {
508   // OCC2932 correction
509   if(myIsInverted == status) return;
510
511   Backup();
512   myIsInverted = status;
513 }
514
515 //=======================================================================
516 //function : Inverted
517 //purpose  : 
518 //=======================================================================
519 Standard_Boolean TDataXtd_Constraint::Inverted() const 
520 {
521   return myIsInverted;
522 }
523
524
525 //=======================================================================
526 //function : CollectChildConstraints
527 //purpose  : 
528 //=======================================================================
529
530 void TDataXtd_Constraint::CollectChildConstraints(const TDF_Label& aLabel,
531                                                   TDF_LabelList& LL)  
532 {
533   TDF_ChildIterator it(aLabel,Standard_True);
534   Handle(TDataXtd_Constraint) aConstraint;
535   for (; it.More(); it.Next()) {
536     if (it.Value().FindAttribute(TDataXtd_Constraint::GetID(), aConstraint)) {
537       LL.Append(it.Value());
538     }
539   }
540   
541 }
542
543
544 //=======================================================================
545 //function : Dump
546 //purpose  : 
547 //=======================================================================
548
549 Standard_OStream& TDataXtd_Constraint::Dump (Standard_OStream& anOS) const
550 {  
551   anOS << "Constraint ";
552   TDataXtd::Print(GetType(),anOS);
553   return anOS;
554 }
555