Integration of OCCT 6.5.0 from SVN
[occt.git] / src / AIS2D / AIS2D_InteractiveObject.cxx
1 #include <AIS2D_InteractiveObject.ixx>
2 #include <Prs2d_AspectLine.hxx>
3 #include <Prs2d_Point.hxx>
4 #include <V2d_Viewer.hxx>
5 #include <AIS2D_ProjShape.hxx>
6 #include <AIS2D_PrimitiveArchit.hxx>
7 #include <Graphic2d_Segment.hxx>
8 #include <Graphic2d_InfiniteLine.hxx>
9 #include <Graphic2d_Polyline.hxx>
10 #include <Graphic2d_PolylineMarker.hxx>
11 #include <Graphic2d_Circle.hxx>
12 #include <Graphic2d_CircleMarker.hxx>
13 #include <Graphic2d_Ellips.hxx>
14 #include <Graphic2d_EllipsMarker.hxx>
15 #include <GGraphic2d_Curve.hxx>
16 #include <stdio.h>
17
18 #define NUM_ASPECTROOT 29
19
20
21 AIS2D_InteractiveObject::AIS2D_InteractiveObject() 
22
23  : Graphic2d_GraphicObject(),
24    myDrawer( new Prs2d_Drawer() ),
25    myHighlightMode( AIS2D_TOD_PRIMITIVE ),
26    myICTX(),
27    myDisplayMode( 0 ),
28    mySelectionMode( 0 ),
29    myAspects(),
30    myState( 0 ),
31    myDetectedPrimSeq( new AIS2D_HSequenceOfPrimArchit() ),
32    mySelectedPrimSeq( new AIS2D_HSequenceOfPrimArchit() ) 
33    
34 {
35    AIS2D_DataMapOfPrimAspects theAspect(NUM_ASPECTROOT);
36    myAspects = theAspect;
37 printf(" $$$ Creates AIS2D_InteractiveObject()\n");
38
39
40 void AIS2D_InteractiveObject::SetAttributes( const Handle(Prs2d_Drawer)& aDrawer ) {
41          myDrawer = aDrawer;
42 }
43
44 void AIS2D_InteractiveObject::UnsetAttributes() {
45    myDrawer = new Prs2d_Drawer();
46 }
47
48 void AIS2D_InteractiveObject::SetAspect( const Handle(Prs2d_AspectRoot)& anAspect ) {
49
50   if ( ! PrimitiveExist(anAspect->GetAspectName()) ) return;
51
52   switch ( anAspect->GetAspectName() ) {
53    
54     case Prs2d_AN_LINE: {
55
56     if ( GetContext().IsNull() ) {
57         myDrawer->SetAspect( anAspect, Prs2d_AN_LINE );
58         return;
59     }
60     Handle(Prs2d_AspectLine) theAspLine = 
61          Handle(Prs2d_AspectLine)::DownCast( anAspect );
62
63     Quantity_Color     aColor;
64     Aspect_TypeOfLine  aType;
65     Aspect_WidthOfLine aWidth;
66   
67     Quantity_Color aIntColor;
68     Graphic2d_TypeOfPolygonFilling aTypeFill;
69     Standard_Integer aTile;
70     Standard_Boolean aDrawEdge;
71   
72     theAspLine->ValuesOfLine( aColor, aType, aWidth );
73     theAspLine->ValuesOfPoly( aIntColor, aTypeFill, aTile, aDrawEdge );
74    
75     Standard_Integer colorInd = theAspLine->ColorIndex();
76     Standard_Integer typeInd = theAspLine->TypeIndex();
77     Standard_Integer widthInd = theAspLine->WidthIndex();
78     Standard_Integer colorIntInd = theAspLine->InterColorIndex();
79
80     if ( !colorInd ) {
81        colorInd = GetContext()->InitializeColor( aColor );
82        theAspLine->SetColorIndex( colorInd );
83     }
84
85     if ( !typeInd ) {
86        typeInd = GetContext()->InitializeStyle( aType );
87        theAspLine->SetTypeIndex( typeInd );
88     }
89
90     if ( !widthInd ) {
91        widthInd = GetContext()->InitializeWidth( aWidth );
92        theAspLine->SetWidthIndex( widthInd );
93     }
94     
95     if ( !colorIntInd ) {
96        colorIntInd = GetContext()->InitializeColor( aIntColor );
97        theAspLine->SetIntColorInd( colorIntInd );
98     }
99
100     if ( this->IsKind(STANDARD_TYPE(AIS2D_ProjShape) ) ) {
101
102      Handle(AIS2D_ProjShape) thePS = Handle(AIS2D_ProjShape)::DownCast(this);
103      Handle(Graphic2d_Line) theLines = thePS->GetPrimitives();
104      theLines->SetColorIndex( colorInd );
105      theLines->SetTypeIndex( typeInd );
106      theLines->SetWidthIndex( widthInd );
107
108      if ( thePS->IsHLMode() ) {
109        Handle(Graphic2d_Line) theHLines = thePS->GetHLPrimitives();
110        theHLines->SetColorIndex( colorInd );
111        theHLines->SetWidthIndex( widthInd );
112        Standard_Integer theDashInd = GetContext()->InitializeStyle( Aspect_TOL_DASH );
113        theHLines->SetTypeIndex( theDashInd );          
114      }
115
116
117     } else 
118
119      for ( Standard_Integer i = 1; i <= Length(); i++ ) {
120              
121        if ( Primitive(i)->IsKind(STANDARD_TYPE(Graphic2d_Line)) ) {
122        
123          Handle(Graphic2d_Line) theLine = 
124              Handle(Graphic2d_Line)::DownCast( Primitive(i) );
125                           
126          theLine->SetColorIndex( colorInd );
127          theLine->SetTypeIndex( typeInd );
128          theLine->SetWidthIndex( widthInd );
129
130          theLine->SetInteriorColorIndex( colorIntInd );
131          theLine->SetTypeOfPolygonFilling( aTypeFill );
132          theLine->SetInteriorPattern( aTile );
133          theLine->SetDrawEdge( aDrawEdge );
134
135          if ( !myAspects.IsBound(theLine) ) 
136              myAspects.Bind( theLine, theAspLine );
137          else
138              myAspects( theLine ) = theAspLine;
139   
140        } // end if STANDARD_TYPE is Graphic2d_Line
141      
142       } // end for
143      
144      break;  
145
146    } // end case Prs2d_AN_LINE
147
148    case Prs2d_AN_HIDDENLINE: {
149        }
150    case Prs2d_AN_TEXT: {
151        }
152    case Prs2d_AN_HIDINGPOLY: {
153        }
154    case Prs2d_AN_HIDINGTEXT: {
155        }
156    case Prs2d_AN_FRAMEDTEXT: {
157        }
158    case Prs2d_AN_LAST: {
159        }
160    case Prs2d_AN_UNKNOWN: {
161        }
162   }
163   myDrawer->SetAspect( anAspect, anAspect->GetAspectName() );
164   if ( !GetContext().IsNull() ) 
165      GetContext()->CurrentViewer()->Update();
166
167 }
168
169 void AIS2D_InteractiveObject::SetAspect( const Handle(Prs2d_AspectRoot)& anAspect,
170                                          const Handle(Graphic2d_Primitive)& aPrim ) {
171                
172   if ( ( GetContext().IsNull() ) || ( !IsIn(aPrim) ) ) return;
173
174    switch ( anAspect->GetAspectName() ) {
175    
176    case Prs2d_AN_LINE: {
177
178     if ( aPrim->IsKind(STANDARD_TYPE(Graphic2d_Line)) ) {
179    
180       Handle(Prs2d_AspectLine) theAspLine = 
181          Handle(Prs2d_AspectLine)::DownCast( anAspect );
182
183       Quantity_Color     aColor;
184       Aspect_TypeOfLine  aType;
185       Aspect_WidthOfLine aWidth;
186    
187       Quantity_Color aIntColor;
188       Graphic2d_TypeOfPolygonFilling aTypeFill;
189       Standard_Integer aTile;
190       Standard_Boolean aDrawEdge;
191   
192       theAspLine->ValuesOfLine( aColor, aType, aWidth );
193       theAspLine->ValuesOfPoly( aIntColor, aTypeFill, aTile, aDrawEdge );
194    
195       Standard_Integer colorInd = theAspLine->ColorIndex();
196       Standard_Integer typeInd = theAspLine->TypeIndex();
197       Standard_Integer widthInd = theAspLine->WidthIndex();
198       Standard_Integer colorIntInd = theAspLine->InterColorIndex();
199
200       if ( !colorInd ) {
201        colorInd = GetContext()->InitializeColor( aColor );
202        theAspLine->SetColorIndex( colorInd );
203       }
204
205       if ( !typeInd ) {
206        typeInd = GetContext()->InitializeStyle( aType );
207        theAspLine->SetTypeIndex( typeInd );
208       }
209
210       if ( !widthInd ) {
211        widthInd = GetContext()->InitializeWidth( aWidth );
212        theAspLine->SetWidthIndex( widthInd );
213       }
214
215       if ( !colorIntInd ) {
216        colorIntInd = GetContext()->InitializeColor( aIntColor );
217        theAspLine->SetIntColorInd( colorIntInd );
218       }
219
220       Handle(Graphic2d_Line) theLine = Handle(Graphic2d_Line)::DownCast( aPrim );
221                 
222       theLine->SetColorIndex( colorInd );
223       theLine->SetTypeIndex( typeInd );
224       theLine->SetWidthIndex( widthInd );
225
226       theLine->SetInteriorColorIndex( colorIntInd );
227       theLine->SetTypeOfPolygonFilling( aTypeFill );
228       theLine->SetInteriorPattern( aTile );
229       theLine->SetDrawEdge( aDrawEdge );
230
231       if ( !myAspects.IsBound(aPrim) )
232          myAspects.Bind( theLine, theAspLine );
233       else
234          myAspects( theLine ) = theAspLine;
235
236     } // end if STANDARD_TYPE is Graphic2d_Line
237     break;
238     
239    } // end case Prs2d_AN_LINE
240
241    case Prs2d_AN_HIDDENLINE: {
242        break;
243        }
244    case Prs2d_AN_TEXT: {
245        break;
246        }
247    case Prs2d_AN_HIDINGPOLY: {
248        break;
249        }
250    case Prs2d_AN_HIDINGTEXT: {
251        break;
252        }
253    case Prs2d_AN_FRAMEDTEXT: {
254        break;
255        }
256    case Prs2d_AN_LAST: {
257        break;
258        }
259    case Prs2d_AN_UNKNOWN: {
260        break;
261        }
262    
263    } // end switch
264
265   myDrawer->SetAspect( anAspect, anAspect->GetAspectName() );
266   GetContext()->CurrentViewer()->Update();
267
268 }
269
270 Handle(Prs2d_AspectRoot) AIS2D_InteractiveObject::GetAspect
271                        ( const Handle(Graphic2d_Primitive)& aPrim ) const {
272     
273    return myAspects( aPrim );
274 }
275
276 Standard_Boolean AIS2D_InteractiveObject::HasAspect
277             ( const Handle(Graphic2d_Primitive)& aPrim ) const {
278
279   return myAspects.IsBound( aPrim );
280 }
281
282 Standard_Boolean AIS2D_InteractiveObject::HasInteractiveContext() const {
283   return ( ! myICTX.IsNull() ); 
284 }
285
286 Handle(AIS2D_InteractiveContext) AIS2D_InteractiveObject::GetContext() const {
287         return myICTX;
288 }
289
290 void AIS2D_InteractiveObject::SetContext( const Handle(AIS2D_InteractiveContext)& aCtx) {
291   
292   myICTX = aCtx;
293   if ( myDrawer.IsNull() ) 
294     myDrawer = new Prs2d_Drawer;
295    
296 }
297  
298 void AIS2D_InteractiveObject::SelectPrimitive (const Standard_Integer anIndex, const Standard_Boolean append ) 
299 {
300   if ( !append ) 
301   { // unselect all 
302     for ( Standard_Integer i = 1; i <= mySelectedPrimSeq->Length(); i++ )
303       mySelectedPrimSeq->Value( i )->GetPrimitive()->Unhighlight();
304     mySelectedPrimSeq->Clear();
305     SetState( 0 );
306   }
307   // select new primitive 
308   SetPickedIndex( anIndex );
309 }
310
311 Standard_Boolean AIS2D_InteractiveObject::PrimitiveExist( const Prs2d_AspectName aName) const {
312
313    Standard_Boolean isFound = Standard_False;
314    
315    for ( Standard_Integer i = 1; i <= Length(); i++ )
316
317      switch ( aName ) {
318      case Prs2d_AN_LINE: {
319        if ( Primitive(i)->IsKind(STANDARD_TYPE(Graphic2d_Line)) )
320            isFound = Standard_True;
321        break;
322      }
323      case Prs2d_AN_HIDDENLINE: {
324          break;
325      }
326      case Prs2d_AN_TEXT: {
327          break;
328      }
329      case Prs2d_AN_HIDINGPOLY: {
330          break;
331      }
332      case Prs2d_AN_HIDINGTEXT: {
333          break;
334      }
335      case Prs2d_AN_FRAMEDTEXT: {
336          break;
337      }
338      case Prs2d_AN_LAST: {
339          break;
340      }
341      case Prs2d_AN_UNKNOWN: {
342          break;
343      }
344     } // end switch
345
346       
347    return isFound;
348 }
349
350 void AIS2D_InteractiveObject::AddDetectPrim ( const Handle(Graphic2d_Primitive)& aPrim,
351                                               const Standard_Integer anInd) {
352
353   Handle(AIS2D_PrimitiveArchit) thePA = new AIS2D_PrimitiveArchit( aPrim, anInd );
354   myDetectedPrimSeq->Append( thePA );
355
356 }
357
358 void AIS2D_InteractiveObject::AddSelectPrim ( const Handle(Graphic2d_Primitive)& aPrim,
359                                               const Standard_Integer anInd) {
360
361   Handle(AIS2D_PrimitiveArchit) thePA = new AIS2D_PrimitiveArchit( aPrim, anInd );
362   mySelectedPrimSeq->Append( thePA );
363
364 }
365
366 void AIS2D_InteractiveObject::RemoveSelectPrim ( const Handle(Graphic2d_Primitive)& aPrim,
367                                                  const Standard_Integer anInd) {
368
369     if ( mySelectedPrimSeq->IsEmpty() ) return;
370     for ( int i = 1; i <= mySelectedPrimSeq->Length(); ++i )
371       if ( mySelectedPrimSeq->Value(i)->GetPrimitive() == aPrim && 
372           mySelectedPrimSeq->Value(i)->GetIndex() == anInd ) {
373           mySelectedPrimSeq->Remove(i);
374           return;
375       }   
376 }
377
378 void AIS2D_InteractiveObject::SetSelSeqPrim( const Handle(AIS2D_HSequenceOfPrimArchit)& aSeq ) {
379
380    mySelectedPrimSeq = aSeq;
381
382 }
383
384 void AIS2D_InteractiveObject::ClearSeqDetPrim() {
385
386    myDetectedPrimSeq->Clear();
387
388 }
389
390 void AIS2D_InteractiveObject::ClearSeqSelPrim() {
391
392    mySelectedPrimSeq->Clear();
393
394 }
395
396 Handle(AIS2D_HSequenceOfPrimArchit) AIS2D_InteractiveObject::GetDetectSeqPrim() const {
397
398    return myDetectedPrimSeq;
399
400 }
401
402 Handle(AIS2D_HSequenceOfPrimArchit) AIS2D_InteractiveObject::GetSelectedSeqPrim() const {
403
404   return mySelectedPrimSeq;
405
406 }
407
408 void AIS2D_InteractiveObject::Save(Aspect_FStream& aFStream) const
409 {
410         int lnOfPr = Length();
411
412         *aFStream << "AIS2D_InteractiveObject" << endl;
413         for (int j = 1; j <= lnOfPr; j++)
414         {
415                 Handle(Graphic2d_Primitive) thePrm = this->Primitive(j);
416                 thePrm->Save(aFStream);
417         }
418 }
419
420 void AIS2D_InteractiveObject::Retrieve(Aspect_IFStream& anIFStream) const
421 {
422         char buf[100];
423         anIFStream->getline(buf, 100);
424         while (! anIFStream->eof())
425         {
426                 if (! strcmp(buf, "Graphic2d_Segment"))
427                 {
428                         Graphic2d_Segment::Retrieve(anIFStream, this);
429                 }
430                 else if (! strcmp(buf, "Graphic2d_InfiniteLine"))
431                 {
432                         Graphic2d_InfiniteLine::Retrieve(anIFStream, this);
433                 }
434                 else if (! strcmp(buf, "Graphic2d_Polyline"))
435                 {
436                         Graphic2d_Polyline::Retrieve(anIFStream, this);
437                 }
438                 else if (! strcmp(buf, "Graphic2d_PolylineMarker"))
439                 {
440                         Graphic2d_PolylineMarker::Retrieve(anIFStream, this);
441                 }
442                 else if (! strcmp(buf, "Graphic2d_Circle"))
443                 {
444                         Graphic2d_Circle::Retrieve(anIFStream, this);
445                 }
446                 else if (! strcmp(buf, "Graphic2d_CircleMarker"))
447                 {
448                         Graphic2d_CircleMarker::Retrieve(anIFStream, this);
449                 }
450                 else if (! strcmp(buf, "Graphic2d_Ellips"))
451                 {
452                         Graphic2d_Ellips::Retrieve(anIFStream, this);
453                 }
454                 else if (! strcmp(buf, "Graphic2d_EllipsMarker"))
455                 {
456                         Graphic2d_EllipsMarker::Retrieve(anIFStream, this);
457                 }
458                 else if (! strcmp(buf, "GGraphic2d_Curve"))
459                 {
460                         GGraphic2d_Curve::Retrieve(anIFStream, this);
461                 }
462                 anIFStream->getline(buf, 100);
463         }
464 }
465