0030480: Visualization - Clear of Select3D_SensitiveGroup does not update internal...
[occt.git] / src / TDataXtd / TDataXtd_PatternStd.cxx
1 // Created on: 2009-04-06
2 // Created by: Sergey ZARITCHNY
3 // Copyright (c) 2009-2014 OPEN CASCADE SAS
4 //
5 // This file is part of Open CASCADE Technology software library.
6 //
7 // This library is free software; you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License version 2.1 as published
9 // by the Free Software Foundation, with special exception defined in the file
10 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11 // distribution for complete text of the license and disclaimer of any warranty.
12 //
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
15
16
17 #include <gp_Ax1.hxx>
18 #include <gp_Lin.hxx>
19 #include <gp_Pln.hxx>
20 #include <gp_Trsf.hxx>
21 #include <gp_Vec.hxx>
22 #include <Standard_GUID.hxx>
23 #include <Standard_Type.hxx>
24 #include <TDataStd.hxx>
25 #include <TDataStd_Integer.hxx>
26 #include <TDataStd_Real.hxx>
27 #include <TDataXtd_Geometry.hxx>
28 #include <TDataXtd_PatternStd.hxx>
29 #include <TDF_Attribute.hxx>
30 #include <TDF_DataSet.hxx>
31 #include <TDF_Label.hxx>
32 #include <TDF_RelocationTable.hxx>
33 #include <TNaming_NamedShape.hxx>
34 #include <TopoDS_Shape.hxx>
35
36 IMPLEMENT_STANDARD_RTTIEXT(TDataXtd_PatternStd,TDataXtd_Pattern)
37
38 //=======================================================================
39 //function : GetPatternID
40 //purpose  : 
41 //=======================================================================
42 const Standard_GUID& TDataXtd_PatternStd::GetPatternID() 
43 {
44   static Standard_GUID TDataXtd_PatternStdID("2a96b61b-ec8b-11d0-bee7-080009dc3333");
45   return TDataXtd_PatternStdID;
46 }
47
48
49 //=======================================================================
50 //function : Set
51 //purpose  : 
52 //=======================================================================
53
54 Handle(TDataXtd_PatternStd) TDataXtd_PatternStd::Set (const TDF_Label& L)
55
56   Handle(TDataXtd_PatternStd) A; 
57   if (!L.FindAttribute(TDataXtd_Pattern::GetID(), A)) {
58     A = new TDataXtd_PatternStd(); 
59     L.AddAttribute(A);
60   }
61   return A;
62 }
63
64 //=======================================================================
65 //function : TDataXtd_PatternStd
66 //purpose  : 
67 //=======================================================================
68
69 TDataXtd_PatternStd::TDataXtd_PatternStd()
70      : mySignature      (0),
71        myAxis1Reversed  (Standard_False),
72        myAxis2Reversed  (Standard_False)
73 {}
74
75 //=======================================================================
76 //function : Signature
77 //purpose  : 
78 //=======================================================================
79
80 void TDataXtd_PatternStd::Signature(const Standard_Integer signature) 
81 {
82   // OCC2932 correction
83   if(mySignature == signature) return;
84
85   Backup();
86   mySignature = signature;
87 }
88
89 //=======================================================================
90 //function : Axis1
91 //purpose  : 
92 //=======================================================================
93
94 void TDataXtd_PatternStd::Axis1(const Handle(TNaming_NamedShape)& Axis1) 
95 {
96   // OCC2932 correction
97   if(!myAxis1.IsNull())
98     if(myAxis1->Get() == Axis1->Get())
99       return;
100
101   Backup();
102   myAxis1 = Axis1;
103 }
104
105 //=======================================================================
106 //function : Axis2
107 //purpose  : 
108 //=======================================================================
109
110 void TDataXtd_PatternStd::Axis2(const Handle(TNaming_NamedShape)& Axis2) 
111 {
112   // OCC2932 correction
113   if(!myAxis2.IsNull())
114     if(myAxis2->Get() == Axis2->Get())
115       return;
116
117
118   Backup();
119   myAxis2 = Axis2;
120 }
121
122 //=======================================================================
123 //function : Axis1Reversed
124 //purpose  : 
125 //=======================================================================
126
127 void TDataXtd_PatternStd::Axis1Reversed(const Standard_Boolean Axis1Reversed) 
128 {
129   // OCC2932 correction
130   if(myAxis1Reversed == Axis1Reversed) return;
131
132   Backup();
133   myAxis1Reversed = Axis1Reversed;
134 }
135
136 //=======================================================================
137 //function : Axis2Reversed
138 //purpose  : 
139 //=======================================================================
140
141 void TDataXtd_PatternStd::Axis2Reversed(const Standard_Boolean Axis2Reversed) 
142 {
143   // OCC2932 correction
144   if(myAxis2Reversed == Axis2Reversed) return;
145
146   Backup();
147   myAxis2Reversed = Axis2Reversed;
148 }
149
150 //=======================================================================
151 //function : Value1
152 //purpose  : 
153 //=======================================================================
154
155 void TDataXtd_PatternStd::Value1(const Handle(TDataStd_Real)& value)
156 {
157   // OCC2932 correction
158   if(!myValue1.IsNull())
159     if(myValue1->Get() == value->Get())
160       return;
161
162   Backup();
163   myValue1 = value;
164 }
165
166
167 //=======================================================================
168 //function : Value2
169 //purpose  : 
170 //=======================================================================
171
172 void TDataXtd_PatternStd::Value2(const Handle(TDataStd_Real)& value)
173 {
174   // OCC2932 correction
175   if(!myValue2.IsNull())
176     if(myValue2->Get() == value->Get())
177       return;
178
179   Backup();
180   myValue2 = value;
181 }
182
183
184 //=======================================================================
185 //function : NbInstances1
186 //purpose  : 
187 //=======================================================================
188
189 void TDataXtd_PatternStd::NbInstances1(const Handle(TDataStd_Integer)& NbInstances1) 
190 {
191   // OCC2932 correction
192   if(!myNb1.IsNull())
193     if(myNb1->Get() == NbInstances1->Get())
194       return;
195
196   Backup();
197   myNb1 = NbInstances1;
198 }
199
200 //=======================================================================
201 //function : NbInstances2
202 //purpose  : 
203 //=======================================================================
204
205 void TDataXtd_PatternStd::NbInstances2(const Handle(TDataStd_Integer)& NbInstances2) 
206 {
207   // OCC2932 correction
208   if(!myNb2.IsNull())
209     if(myNb2->Get() == NbInstances2->Get())
210       return;
211
212   Backup();
213   myNb2 = NbInstances2;
214 }
215
216 //=======================================================================
217 //function : Mirror
218 //purpose  : 
219 //=======================================================================
220
221 void TDataXtd_PatternStd::Mirror(const Handle(TNaming_NamedShape)& plane)
222 {
223   // OCC2932 correction
224   if(!myMirror.IsNull()) {
225     if(myMirror->Get() == plane->Get())
226       return;
227   }
228
229   Backup();
230   myMirror = plane;
231 }
232
233 //=======================================================================
234 //function : NbTrsfs
235 //purpose  : 
236 //=======================================================================
237
238 Standard_Integer TDataXtd_PatternStd::NbTrsfs() const
239 {
240   Standard_Integer nb = 1;
241   if (mySignature < 5) {
242     if (!myNb1.IsNull()) nb = myNb1->Get();
243     if (!myNb2.IsNull()) nb = nb*myNb2->Get();
244     nb--;
245   }
246   return nb;
247 }
248
249
250 //=======================================================================
251 //function : ComputeTrsfs
252 //purpose  : 
253 //=======================================================================
254
255 void TDataXtd_PatternStd::ComputeTrsfs(TDataXtd_Array1OfTrsf& Trsfs) const
256 {
257   Standard_Integer nb = 0;
258   gp_Trsf trsf;
259
260   if (mySignature < 5) {
261
262     // recover direction and step
263     gp_Ax1 axis1;
264     TDataXtd_Geometry::Axis(myAxis1, axis1);
265     if (myAxis1Reversed) axis1.Reverse();
266     
267     Standard_Real value1 = myValue1->Get();
268     
269     for (Standard_Integer i=2; i<=myNb1->Get(); i++) {
270       if (mySignature != 2) {
271         gp_Vec vec(axis1.Direction());
272         vec *= (value1*(i-1));
273         trsf.SetTranslation(vec);
274       }
275       else {
276         trsf.SetRotation(axis1, value1*(i-1));
277       }
278       Trsfs(++nb) = trsf;
279     }
280     
281     if (mySignature == 3 || mySignature == 4) {
282       // recover direction and step
283       gp_Ax1 axis2;
284       TDataXtd_Geometry::Axis(myAxis2, axis2);
285       if (myAxis2Reversed) axis2.Reverse();
286       
287       Standard_Real value2 = myValue2->Get();
288       
289       for (Standard_Integer j=2; j<=myNb2->Get(); j++) {
290         gp_Trsf trsf2;
291         if (mySignature ==3) {
292           gp_Vec vec(axis2.Direction());
293           vec *= (value2*(j-1));
294           trsf2.SetTranslation(vec);
295         }
296         else {
297           trsf2.SetRotation(axis2, value2*(j-1));
298         }
299         
300         Trsfs(++nb) = trsf2;    
301         for (Standard_Integer i=2; i<=myNb1->Get(); i++) {
302           trsf = trsf2;
303           trsf.Multiply(Trsfs(i-1));
304           Trsfs(++nb) = trsf;
305         }
306       }
307     }  
308   }
309   else {
310     gp_Pln pln;
311     TDataXtd_Geometry::Plane(myMirror, pln);
312     trsf.SetMirror(pln.Position().Ax2());
313     Trsfs(++nb) = trsf;
314   }
315 }
316
317 //=======================================================================
318 //function : PatternID
319 //purpose  : 
320 //=======================================================================
321
322 const Standard_GUID& TDataXtd_PatternStd::PatternID() const
323 {
324   return GetPatternID ();
325 }
326
327
328 //=======================================================================
329 //function : Restore
330 //purpose  : 
331 //=======================================================================
332
333 void TDataXtd_PatternStd::Restore(const Handle(TDF_Attribute)& With) 
334 {
335   Handle(TDataXtd_PatternStd) PatternStd = Handle(TDataXtd_PatternStd)::DownCast(With);
336
337   mySignature = PatternStd->Signature();
338   myAxis1Reversed = PatternStd->Axis1Reversed();
339   myAxis2Reversed = PatternStd->Axis2Reversed();
340   
341   myAxis1 = PatternStd->Axis1();
342   myAxis2 = PatternStd->Axis2();
343   myValue1 = PatternStd->Value1();
344   myValue2 = PatternStd->Value2();
345   myNb1 = PatternStd->NbInstances1();
346   myNb2 = PatternStd->NbInstances2();
347   myMirror = PatternStd->Mirror();
348 }
349
350 //=======================================================================
351 //function : NewEmpty
352 //purpose  : 
353 //=======================================================================
354
355 Handle(TDF_Attribute) TDataXtd_PatternStd::NewEmpty() const
356 {
357   return new TDataXtd_PatternStd();
358 }
359
360 //=======================================================================
361 //function : Paste
362 //purpose  : 
363 //=======================================================================
364
365 void TDataXtd_PatternStd::Paste(const Handle(TDF_Attribute)& Into,
366                                 const Handle(TDF_RelocationTable)& RT) const
367 {
368   Handle(TDataXtd_PatternStd) intof = Handle(TDataXtd_PatternStd)::DownCast(Into);
369
370   intof->Signature(mySignature);
371   intof->Axis1Reversed(myAxis1Reversed);
372   intof->Axis2Reversed(myAxis2Reversed);
373     
374   if (mySignature < 5) {
375     Handle(TNaming_NamedShape) axis;
376     Handle(TDataStd_Real) value;
377     Handle(TDataStd_Integer) nb;
378
379     RT->HasRelocation(myAxis1, axis);
380     intof->Axis1(axis);
381     RT->HasRelocation(myValue1, value);
382     intof->Value1(value);
383     RT->HasRelocation(myNb1, nb);
384     intof->NbInstances1(nb);
385
386     if (mySignature > 2) {
387       RT->HasRelocation(myAxis2, axis);
388       intof->Axis2(axis);    
389       RT->HasRelocation(myValue2, value);
390       intof->Value2(value);
391       RT->HasRelocation(myNb2, nb);
392       intof->NbInstances2(nb);
393     }
394   }
395   else {
396     Handle(TNaming_NamedShape) plane;
397     RT->HasRelocation(myMirror, plane);
398     intof->Mirror(plane);
399   }
400 }
401
402 //=======================================================================
403 //function : References
404 //purpose  : 
405 //=======================================================================
406
407 void TDataXtd_PatternStd::References(const Handle(TDF_DataSet)& aDataSet) const
408 {
409   if (mySignature < 5) {
410     aDataSet->AddAttribute(myAxis1);
411     aDataSet->AddAttribute(myValue1);
412     aDataSet->AddAttribute(myNb1);
413     if (mySignature > 2) {
414       aDataSet->AddAttribute(myAxis2);
415       aDataSet->AddAttribute(myValue2);
416       aDataSet->AddAttribute(myNb2);
417     }
418   }
419   else {
420     aDataSet->AddAttribute(myMirror);
421   }
422 }
423
424 //=======================================================================
425 //function : Dump
426 //purpose  : 
427 //=======================================================================
428
429 Standard_OStream& TDataXtd_PatternStd::Dump(Standard_OStream& anOS) const
430 {
431   anOS << "TDataXtd_PatternStd";
432   return anOS;
433 }
434
435
436