0033661: Data Exchange, Step Import - Tessellated GDTs are not imported
[occt.git] / src / AIS / AIS_ColoredShape.cxx
1 // Created on: 2014-04-24
2 // Created by: Kirill Gavrilov
3 // Copyright (c) 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 #include <AIS_ColoredShape.hxx>
17
18 #include <AIS_InteractiveContext.hxx>
19 #include <BRep_Builder.hxx>
20 #include <BRepTools.hxx>
21 #include <BRepMesh_IncrementalMesh.hxx>
22 #include <gp_Pnt2d.hxx>
23 #include <Graphic3d_AspectFillArea3d.hxx>
24 #include <Graphic3d_AspectLine3d.hxx>
25 #include <Graphic3d_ArrayOfTriangles.hxx>
26 #include <Graphic3d_ArrayOfSegments.hxx>
27 #include <Graphic3d_Group.hxx>
28 #include <Graphic3d_StructureManager.hxx>
29 #include <Graphic3d_Texture2Dmanual.hxx>
30 #include <Precision.hxx>
31 #include <Prs3d.hxx>
32 #include <Prs3d_LineAspect.hxx>
33 #include <Prs3d_IsoAspect.hxx>
34 #include <Prs3d_Presentation.hxx>
35 #include <Prs3d_ShadingAspect.hxx>
36 #include <Prs3d_Root.hxx>
37 #include <PrsMgr_PresentationManager3d.hxx>
38 #include <Standard_ErrorHandler.hxx>
39 #include <StdSelect_BRepSelectionTool.hxx>
40 #include <StdPrs_ShadedShape.hxx>
41 #include <StdPrs_ToolTriangulatedShape.hxx>
42 #include <StdPrs_WFShape.hxx>
43 #include <TopExp_Explorer.hxx>
44 #include <TopoDS.hxx>
45 #include <TopoDS_Compound.hxx>
46 #include <TopoDS_Iterator.hxx>
47
48 IMPLEMENT_STANDARD_RTTIEXT(AIS_ColoredShape,AIS_Shape)
49 IMPLEMENT_STANDARD_RTTIEXT(AIS_ColoredDrawer,Prs3d_Drawer)
50
51 namespace
52 {
53   //! Collect all sub-compounds into map.
54   static void collectSubCompounds (TopTools_MapOfShape& theMap,
55                                    const TopoDS_Shape&  theShape)
56   {
57     for (TopoDS_Iterator aChildIter (theShape); aChildIter.More(); aChildIter.Next())
58     {
59       const TopoDS_Shape& aShape = aChildIter.Value();
60       if (aShape.ShapeType() == TopAbs_COMPOUND
61        && theMap.Add (aShape))
62       {
63         collectSubCompounds (theMap, aShape);
64       }
65     }
66   }
67 }
68
69 //=======================================================================
70 //function : AIS_ColoredShape
71 //purpose  :
72 //=======================================================================
73 AIS_ColoredShape::AIS_ColoredShape (const TopoDS_Shape& theShape)
74 : AIS_Shape (theShape)
75 {
76   // disable dedicated line aspects
77   myDrawer->SetFreeBoundaryAspect  (myDrawer->LineAspect());
78   myDrawer->SetUnFreeBoundaryAspect(myDrawer->LineAspect());
79   myDrawer->SetSeenLineAspect      (myDrawer->LineAspect());
80   myDrawer->SetFaceBoundaryAspect  (myDrawer->LineAspect());
81 }
82
83 //=======================================================================
84 //function : AIS_ColoredShape
85 //purpose  :
86 //=======================================================================
87 AIS_ColoredShape::AIS_ColoredShape (const Handle(AIS_Shape)& theShape)
88 : AIS_Shape (theShape->Shape())
89 {
90   // disable dedicated line aspects
91   myDrawer->SetFreeBoundaryAspect  (myDrawer->LineAspect());
92   myDrawer->SetUnFreeBoundaryAspect(myDrawer->LineAspect());
93   myDrawer->SetSeenLineAspect      (myDrawer->LineAspect());
94   myDrawer->SetFaceBoundaryAspect  (myDrawer->LineAspect());
95   if (theShape->HasMaterial())
96   {
97     SetMaterial (theShape->Material());
98   }
99   if (theShape->HasColor())
100   {
101     Quantity_Color aColor;
102     theShape->Color (aColor);
103     SetColor (aColor);
104   }
105   if (theShape->HasWidth())
106   {
107     SetWidth (theShape->Width());
108   }
109   if (theShape->IsTransparent())
110   {
111     SetTransparency (theShape->Transparency());
112   }
113 }
114
115 //=======================================================================
116 //function : CustomAspects
117 //purpose  :
118 //=======================================================================
119 Handle(AIS_ColoredDrawer) AIS_ColoredShape::CustomAspects (const TopoDS_Shape& theShape)
120 {
121   Handle(AIS_ColoredDrawer) aDrawer;
122   myShapeColors.Find (theShape, aDrawer);
123   if (aDrawer.IsNull())
124   {
125     aDrawer = new AIS_ColoredDrawer (myDrawer);
126     myShapeColors.Bind (theShape, aDrawer);
127     SetToUpdate();
128   }
129   return aDrawer;
130 }
131
132 //=======================================================================
133 //function : ClearCustomAspects
134 //purpose  :
135 //=======================================================================
136 void AIS_ColoredShape::ClearCustomAspects()
137 {
138   if (myShapeColors.IsEmpty())
139   {
140     return;
141   }
142   myShapeColors.Clear();
143   SetToUpdate();
144 }
145
146 //=======================================================================
147 //function : UnsetCustomAspects
148 //purpose  :
149 //=======================================================================
150 void AIS_ColoredShape::UnsetCustomAspects (const TopoDS_Shape&    theShape,
151                                            const Standard_Boolean theToUnregister)
152 {
153   if (!myShapeColors.IsBound (theShape))
154   {
155     return;
156   }
157
158   SetToUpdate();
159   if (theToUnregister)
160   {
161     myShapeColors.UnBind (theShape);
162     return;
163   }
164
165   myShapeColors.ChangeFind (theShape) = new AIS_ColoredDrawer (myDrawer);
166 }
167
168 //=======================================================================
169 //function : SetCustomColor
170 //purpose  :
171 //=======================================================================
172 void AIS_ColoredShape::SetCustomColor (const TopoDS_Shape&   theShape,
173                                        const Quantity_Color& theColor)
174 {
175   if (theShape.IsNull())
176   {
177     return;
178   }
179
180   const Handle(AIS_ColoredDrawer)& aDrawer = CustomAspects (theShape);
181   setColor (aDrawer, theColor);
182   aDrawer->SetOwnColor (theColor);
183 }
184
185 //=======================================================================
186 //function : SetCustomTransparency
187 //purpose  :
188 //=======================================================================
189 void AIS_ColoredShape::SetCustomTransparency (const TopoDS_Shape& theShape,
190                                               Standard_Real theTransparency)
191 {
192   if (theShape.IsNull())
193   {
194     return;
195   }
196
197   const Handle(AIS_ColoredDrawer)& aDrawer = CustomAspects (theShape);
198   setTransparency (aDrawer, theTransparency);
199   aDrawer->SetOwnTransparency (theTransparency);
200 }
201
202 //=======================================================================
203 //function : SetCustomWidth
204 //purpose  :
205 //=======================================================================
206 void AIS_ColoredShape::SetCustomWidth (const TopoDS_Shape& theShape,
207                                        const Standard_Real theLineWidth)
208 {
209   if (theShape.IsNull())
210   {
211     return;
212   }
213
214   const Handle(AIS_ColoredDrawer)& aDrawer = CustomAspects (theShape);
215   setWidth (aDrawer, theLineWidth);
216   aDrawer->SetOwnWidth (theLineWidth);
217 }
218
219 //=======================================================================
220 //function : SetColor
221 //purpose  :
222 //=======================================================================
223
224 void AIS_ColoredShape::SetColor (const Quantity_Color&  theColor)
225 {
226   for (AIS_DataMapOfShapeDrawer::Iterator anIter (myShapeColors); anIter.More(); anIter.Next())
227   {
228     const Handle(AIS_ColoredDrawer)& aDrawer = anIter.Value();
229     if (aDrawer->HasOwnColor())
230     {
231       continue;
232     }
233
234     if (aDrawer->HasOwnShadingAspect())
235     {
236       aDrawer->ShadingAspect()->SetColor (theColor, myCurrentFacingModel);
237     }
238     if (aDrawer->HasOwnLineAspect())
239     {
240       aDrawer->LineAspect()->SetColor (theColor);
241     }
242     if (aDrawer->HasOwnWireAspect())
243     {
244       aDrawer->WireAspect()->SetColor (theColor);
245     }
246     if (aDrawer->HasOwnFaceBoundaryAspect())
247     {
248       aDrawer->FaceBoundaryAspect()->SetColor (theColor);
249     }
250   }
251   AIS_Shape::SetColor (theColor);
252 }
253
254 //=======================================================================
255 //function : SetWidth
256 //purpose  :
257 //=======================================================================
258
259 void AIS_ColoredShape::SetWidth (const Standard_Real    theLineWidth)
260 {
261   for (AIS_DataMapOfShapeDrawer::Iterator anIter (myShapeColors); anIter.More(); anIter.Next())
262   {
263     const Handle(AIS_ColoredDrawer)& aDrawer = anIter.Value();
264     if (aDrawer->HasOwnWidth())
265     {
266       continue;
267     }
268
269     if (aDrawer->HasOwnLineAspect())
270     {
271       aDrawer->LineAspect()->SetWidth (theLineWidth);
272     }
273     if (aDrawer->HasOwnWireAspect())
274     {
275       aDrawer->WireAspect()->SetWidth (theLineWidth);
276     }
277     if (aDrawer->HasOwnFaceBoundaryAspect())
278     {
279       aDrawer->FaceBoundaryAspect()->SetWidth (theLineWidth);
280     }
281   }
282   AIS_Shape::SetWidth (theLineWidth);
283 }
284
285 //=======================================================================
286 //function : UnsetWidth
287 //purpose  :
288 //=======================================================================
289 void AIS_ColoredShape::UnsetWidth()
290 {
291   SetWidth (1.0f);
292 }
293
294 //=======================================================================
295 //function : SetTransparency
296 //purpose  :
297 //=======================================================================
298
299 void AIS_ColoredShape::SetTransparency (const Standard_Real theValue)
300 {
301   for (AIS_DataMapOfShapeDrawer::Iterator anIter (myShapeColors); anIter.More(); anIter.Next())
302   {
303     const Handle(AIS_ColoredDrawer)& aDrawer = anIter.Value();
304     if (aDrawer->HasOwnTransparency())
305     {
306       continue;
307     }
308
309     if (aDrawer->HasOwnShadingAspect())
310     {
311       aDrawer->ShadingAspect()->SetTransparency (theValue, myCurrentFacingModel);
312     }
313   }
314   AIS_Shape::SetTransparency (theValue);
315 }
316
317 //=======================================================================
318 //function : UnsetTransparency
319 //purpose  :
320 //=======================================================================
321 void AIS_ColoredShape::UnsetTransparency()
322 {
323   SetTransparency (0.0f);
324 }
325
326 //=======================================================================
327 //function : SetMaterial
328 //purpose  :
329 //=======================================================================
330
331 void AIS_ColoredShape::SetMaterial (const Graphic3d_MaterialAspect& theMaterial)
332 {
333   for (AIS_DataMapOfShapeDrawer::Iterator anIter (myShapeColors); anIter.More(); anIter.Next())
334   {
335     const Handle(AIS_ColoredDrawer)& aDrawer = anIter.Value();
336     //if (aDrawer->HasOwnMaterial()) continue;
337     if (aDrawer->HasOwnShadingAspect())
338     {
339       setMaterial (aDrawer, theMaterial, aDrawer->HasOwnColor(), aDrawer->HasOwnTransparency());
340     }
341   }
342   AIS_Shape::SetMaterial (theMaterial);
343 }
344
345 //=======================================================================
346 //function : Compute
347 //purpose  :
348 //=======================================================================
349 void AIS_ColoredShape::Compute (const Handle(PrsMgr_PresentationManager3d)& thePrsMgr,
350                                 const Handle(Prs3d_Presentation)&           thePrs,
351                                 const Standard_Integer                      theMode)
352 {
353   if (myshape.IsNull())
354   {
355     return;
356   }
357
358   if (IsInfinite())
359   {
360     thePrs->SetInfiniteState (Standard_True);
361   }
362
363   switch (theMode)
364   {
365     case AIS_WireFrame:
366     {
367       StdPrs_ToolTriangulatedShape::ClearOnOwnDeflectionChange (myshape, myDrawer, Standard_True);
368
369       // After this call if type of deflection is relative
370       // computed deflection coefficient is stored as absolute.
371       Prs3d::GetDeflection (myshape, myDrawer);
372       break;
373     }
374     case AIS_Shaded:
375     {
376       if (myDrawer->IsAutoTriangulation())
377       {
378         // compute mesh for entire shape beforehand to ensure consistency and optimizations (parallelization)
379         StdPrs_ToolTriangulatedShape::ClearOnOwnDeflectionChange (myshape, myDrawer, Standard_True);
380
381         // After this call if type of deflection is relative
382         // computed deflection coefficient is stored as absolute.
383         Standard_Boolean wasRecomputed = StdPrs_ToolTriangulatedShape::Tessellate (myshape, myDrawer);
384
385         // Set to update wireframe presentation on triangulation.
386         if (myDrawer->IsoOnTriangulation() && wasRecomputed)
387         {
388           SetToUpdate (AIS_WireFrame);
389         }
390       }
391       break;
392     }
393     case 2:
394     {
395       AIS_Shape::Compute (thePrsMgr, thePrs, theMode);
396       return;
397     }
398     default:
399     {
400       return;
401     }
402   }
403
404   // Extract myShapeColors map (KeyshapeColored -> Color) to subshapes map (Subshape -> Color).
405   // This needed when colored shape is not part of BaseShape (but subshapes are) and actually container for subshapes.
406   AIS_DataMapOfShapeDrawer aSubshapeDrawerMap;
407   fillSubshapeDrawerMap (aSubshapeDrawerMap);
408
409   Handle(AIS_ColoredDrawer) aBaseDrawer;
410   myShapeColors.Find (myshape, aBaseDrawer);
411
412   // myShapeColors + anOpened --> array[TopAbs_ShapeEnum] of map of color-to-compound
413   DataMapOfDrawerCompd aDispatchedOpened[(size_t)TopAbs_SHAPE];
414   DataMapOfDrawerCompd aDispatchedClosed;
415   dispatchColors (aBaseDrawer, myshape,
416                   aSubshapeDrawerMap, TopAbs_COMPOUND, Standard_False,
417                   aDispatchedOpened, theMode == AIS_Shaded ? aDispatchedClosed : aDispatchedOpened[TopAbs_FACE]);
418   addShapesWithCustomProps (thePrs, aDispatchedOpened, aDispatchedClosed, theMode);
419 }
420
421 //=======================================================================
422 //function : fillSubshapeDrawerMap
423 //purpose  :
424 //=======================================================================
425 void AIS_ColoredShape::fillSubshapeDrawerMap (AIS_DataMapOfShapeDrawer& theSubshapeDrawerMap) const
426 {
427   // unroll compounds specified for grouping sub-shapes with the same style
428   // (e.g. the compounds that are not a part of the main shape)
429   TopTools_MapOfShape aMapOfOwnCompounds;
430   if (myshape.ShapeType() == TopAbs_COMPOUND)
431   {
432     aMapOfOwnCompounds.Add (myshape);
433     collectSubCompounds (aMapOfOwnCompounds, myshape);
434   }
435   for (AIS_DataMapOfShapeDrawer::Iterator aKeyShapeIter (myShapeColors);
436         aKeyShapeIter.More(); aKeyShapeIter.Next())
437   {
438     const TopoDS_Shape& aKeyShape = aKeyShapeIter.Key();
439     if (aKeyShape.ShapeType() != TopAbs_COMPOUND
440      || aMapOfOwnCompounds.Contains (aKeyShape))
441     {
442       continue;
443     }
444
445     for (TopoDS_Iterator aChildIter (aKeyShape); aChildIter.More(); aChildIter.Next())
446     {
447       const TopoDS_Shape& aShape = aChildIter.Value();
448       if (!myShapeColors.IsBound (aShape))
449       {
450         bindSubShapes (theSubshapeDrawerMap, aShape, aKeyShapeIter.Value());
451       }
452     }
453   }
454
455   // assign other sub-shapes with styles
456   for (AIS_DataMapOfShapeDrawer::Iterator aKeyShapeIter (myShapeColors);
457         aKeyShapeIter.More(); aKeyShapeIter.Next())
458   {
459     const TopoDS_Shape& aKeyShape = aKeyShapeIter.Key();
460     if (myshape == aKeyShape
461     || (aKeyShape.ShapeType() == TopAbs_COMPOUND
462     && !aMapOfOwnCompounds.Contains (aKeyShape)))
463     {
464       continue;
465     }
466
467     bindSubShapes (theSubshapeDrawerMap, aKeyShape, aKeyShapeIter.Value());
468   }
469 }
470
471 //=======================================================================
472 //function : ComputeSelection
473 //purpose  :
474 //=======================================================================
475 void AIS_ColoredShape::ComputeSelection (const Handle(SelectMgr_Selection)& theSelection,
476                                          const Standard_Integer theMode)
477 {
478   if (myshape.IsNull())
479   {
480     return;
481   }
482   else if (isShapeEntirelyVisible())
483   {
484     base_type::ComputeSelection (theSelection, theMode);
485     return;
486   }
487
488   const TopAbs_ShapeEnum aTypOfSel   = AIS_Shape::SelectionType (theMode);
489   const Standard_Real    aDeflection = Prs3d::GetDeflection (myshape, myDrawer);
490   const Standard_Real    aDeviationAngle = myDrawer->HLRAngle();
491   const Standard_Integer aPriority   = StdSelect_BRepSelectionTool::GetStandardPriority (myshape, aTypOfSel);
492   if (myDrawer->IsAutoTriangulation()
493   && !BRepTools::Triangulation (myshape, Precision::Infinite()))
494   {
495     BRepMesh_IncrementalMesh aMesher (myshape, aDeflection, Standard_False, aDeviationAngle);
496   }
497
498   AIS_DataMapOfShapeDrawer aSubshapeDrawerMap;
499   fillSubshapeDrawerMap (aSubshapeDrawerMap);
500
501   Handle(StdSelect_BRepOwner) aBrepOwner = new StdSelect_BRepOwner (myshape, aPriority);
502   if (aTypOfSel == TopAbs_SHAPE)
503   {
504     aBrepOwner = new StdSelect_BRepOwner (myshape, aPriority);
505   }
506
507   Handle(AIS_ColoredDrawer) aBaseDrawer;
508   myShapeColors.Find (myshape, aBaseDrawer);
509   computeSubshapeSelection (aBaseDrawer, aSubshapeDrawerMap, myshape, aBrepOwner, theSelection,
510                             aTypOfSel, aPriority, aDeflection, aDeviationAngle);
511
512   Handle(SelectMgr_SelectableObject) aThis (this);
513   for (NCollection_Vector<Handle(SelectMgr_SensitiveEntity)>::Iterator aSelEntIter (theSelection->Entities()); aSelEntIter.More(); aSelEntIter.Next())
514   {
515     const Handle(SelectMgr_EntityOwner)& anOwner = aSelEntIter.Value()->BaseSensitive()->OwnerId();
516     anOwner->SetSelectable (aThis);
517   }
518
519   StdSelect_BRepSelectionTool::PreBuildBVH (theSelection);
520 }
521
522 //=======================================================================
523 //function : computeSubshapeSelection
524 //purpose  :
525 //=======================================================================
526 void AIS_ColoredShape::computeSubshapeSelection (const Handle(AIS_ColoredDrawer)& theParentDrawer,
527                                                  const AIS_DataMapOfShapeDrawer& theShapeDrawerMap,
528                                                  const TopoDS_Shape& theShape,
529                                                  const Handle(StdSelect_BRepOwner)& theOwner,
530                                                  const Handle(SelectMgr_Selection)& theSelection,
531                                                  const TopAbs_ShapeEnum theTypOfSel,
532                                                  const Standard_Integer thePriority,
533                                                  const Standard_Real theDeflection,
534                                                  const Standard_Real theDeflAngle)
535 {
536   Handle(AIS_ColoredDrawer) aDrawer = theParentDrawer;
537   theShapeDrawerMap.Find (theShape, aDrawer);
538   if (!aDrawer.IsNull()
539     && aDrawer->IsHidden())
540   {
541     return;
542   }
543
544   const Standard_Integer aNbPOnEdge = 9;
545   const Standard_Real    aMaximalParameter = 500.0;
546   if (theTypOfSel == TopAbs_SHAPE
547    && theShape.ShapeType() >= TopAbs_FACE)
548   {
549     StdSelect_BRepSelectionTool::ComputeSensitive (theShape, theOwner, theSelection,
550                                                    theDeflection, theDeflAngle, aNbPOnEdge, aMaximalParameter, myDrawer->IsAutoTriangulation());
551     return;
552   }
553   else if (theShape.ShapeType() == theTypOfSel)
554   {
555     const Standard_Boolean isComesFromDecomposition = !theShape.IsEqual (myshape);
556     Handle(StdSelect_BRepOwner) aBrepOwner = new StdSelect_BRepOwner (theShape, thePriority, isComesFromDecomposition);
557     StdSelect_BRepSelectionTool::ComputeSensitive (theShape, aBrepOwner, theSelection,
558                                                    theDeflection, theDeflAngle, aNbPOnEdge, aMaximalParameter, myDrawer->IsAutoTriangulation());
559     return;
560   }
561
562   for (TopoDS_Iterator aSubShapeIter (theShape); aSubShapeIter.More(); aSubShapeIter.Next())
563   {
564     const TopoDS_Shape& aSubShape = aSubShapeIter.Value();
565     computeSubshapeSelection (aDrawer, theShapeDrawerMap, aSubShape,
566                               theOwner, theSelection, theTypOfSel, thePriority,
567                               theDeflection, theDeflAngle);
568   }
569 }
570
571 //=======================================================================
572 //function : addShapesWithCustomProps
573 //purpose  :
574 //=======================================================================
575 void AIS_ColoredShape::addShapesWithCustomProps (const Handle(Prs3d_Presentation)& thePrs,
576                                                  const DataMapOfDrawerCompd* theDrawerOpenedShapePerType,
577                                                  const DataMapOfDrawerCompd& theDrawerClosedFaces,
578                                                  const Standard_Integer theMode)
579 {
580   Handle(Graphic3d_Group) anOpenGroup, aClosedGroup, anEdgesGroup;
581   for (size_t aShType = 0; aShType <= (size_t )TopAbs_SHAPE; ++aShType)
582   {
583     const Standard_Boolean isClosed = aShType == TopAbs_SHAPE;
584     Handle(Graphic3d_Group)& aShadedGroup = isClosed ? aClosedGroup : anOpenGroup;
585     const DataMapOfDrawerCompd& aDrawerShapeMap = isClosed
586                                                 ? theDrawerClosedFaces
587                                                 : theDrawerOpenedShapePerType[aShType];
588     for (DataMapOfDrawerCompd::Iterator aMapIter (aDrawerShapeMap);
589          aMapIter.More(); aMapIter.Next())
590     {
591       const Handle(AIS_ColoredDrawer)& aCustomDrawer = aMapIter.Key();
592       const TopoDS_Compound& aShapeDraw = aMapIter.Value(); // compound of subshapes with <aShType> type
593       Handle(Prs3d_Drawer) aDrawer;
594       if (!aCustomDrawer.IsNull())
595       {
596         aDrawer = aCustomDrawer;
597         if (aCustomDrawer->IsHidden())
598         {
599           continue;
600         }
601       }
602       else
603       {
604         aDrawer = myDrawer;
605       }
606
607       // It is supposed that absolute deflection contains previously computed relative deflection
608       // (if deflection type is relative).
609       // In case of CustomDrawer it is taken from Link().
610       Aspect_TypeOfDeflection aPrevType = aDrawer->TypeOfDeflection();
611       aDrawer->SetTypeOfDeflection (Aspect_TOD_ABSOLUTE);
612
613       // Draw each kind of subshapes and personal-colored shapes in a separate group
614       // since it's necessary to set transparency/material for all subshapes
615       // without affecting their unique colors
616       if (theMode == AIS_Shaded
617        && aShapeDraw.ShapeType() <= TopAbs_FACE
618        && !IsInfinite())
619       {
620         // add wireframe presentation for isolated edges and vertices
621         StdPrs_ShadedShape::AddWireframeForFreeElements (thePrs, aShapeDraw, aDrawer);
622
623         // add special wireframe presentation for faces without triangulation
624         StdPrs_ShadedShape::AddWireframeForFacesWithoutTriangles (thePrs, aShapeDraw, aDrawer);
625
626         Handle(Graphic3d_ArrayOfTriangles) aTriangles = StdPrs_ShadedShape::FillTriangles (aShapeDraw,
627                                                                                            aDrawer->ShadingAspect()->Aspect()->ToMapTexture()
628                                                                                        && !aDrawer->ShadingAspect()->Aspect()->TextureMap().IsNull(),
629                                                                                            myUVOrigin, myUVRepeat, myUVScale);
630         if (!aTriangles.IsNull())
631         {
632           if (aShadedGroup.IsNull())
633           {
634             aShadedGroup = thePrs->NewGroup();
635             aShadedGroup->SetClosed (isClosed);
636           }
637           aShadedGroup->SetPrimitivesAspect (aDrawer->ShadingAspect()->Aspect());
638           aShadedGroup->AddPrimitiveArray (aTriangles);
639         }
640
641         if (aDrawer->FaceBoundaryDraw())
642         {
643           if (Handle(Graphic3d_ArrayOfSegments) aBndSegments = StdPrs_ShadedShape::FillFaceBoundaries (aShapeDraw, aDrawer->FaceBoundaryUpperContinuity()))
644           {
645             if (anEdgesGroup.IsNull())
646             {
647               anEdgesGroup = thePrs->NewGroup();
648             }
649
650             anEdgesGroup->SetPrimitivesAspect (aDrawer->FaceBoundaryAspect()->Aspect());
651             anEdgesGroup->AddPrimitiveArray (aBndSegments);
652           }
653         }
654       }
655       else
656       {
657         StdPrs_WFShape::Add (thePrs, aShapeDraw, aDrawer);
658       }
659       aDrawer->SetTypeOfDeflection (aPrevType);
660     }
661   }
662 }
663
664 //=======================================================================
665 //function : dispatchColors
666 //purpose  :
667 //=======================================================================
668 Standard_Boolean AIS_ColoredShape::dispatchColors (const Handle(AIS_ColoredDrawer)& theParentDrawer,
669                                                    const TopoDS_Shape& theShapeToParse,
670                                                    const AIS_DataMapOfShapeDrawer& theShapeDrawerMap,
671                                                    const TopAbs_ShapeEnum theParentType,
672                                                    const Standard_Boolean theIsParentClosed,
673                                                    DataMapOfDrawerCompd* theDrawerOpenedShapePerType,
674                                                    DataMapOfDrawerCompd& theDrawerClosedFaces)
675 {
676   const TopAbs_ShapeEnum aShapeType = theShapeToParse.ShapeType();
677   if (aShapeType == TopAbs_SHAPE)
678   {
679     return Standard_False;
680   }
681
682   // check own setting of current shape
683   Handle(AIS_ColoredDrawer) aDrawer = theParentDrawer;
684   const Standard_Boolean isOverriden = theShapeDrawerMap.Find (theShapeToParse, aDrawer);
685   if (isOverriden
686    && aDrawer->IsHidden())
687   {
688     return Standard_True;
689   }
690
691   // handle compounds, solids and shells
692   Standard_Boolean isSubOverride = Standard_False;
693   if (aShapeType <= TopAbs_SHELL)
694   {
695     // detect parts of closed solids
696     Standard_Boolean isClosedShell = theParentType == TopAbs_SOLID
697                                   && aShapeType == TopAbs_SHELL
698                                   && BRep_Tool::IsClosed (theShapeToParse)
699                                   && StdPrs_ToolTriangulatedShape::IsTriangulated (theShapeToParse);
700     if (isClosedShell)
701     {
702       for (TopoDS_Iterator aFaceIter (theShapeToParse); aFaceIter.More(); aFaceIter.Next())
703       {
704         const TopoDS_Shape& aFace = aFaceIter.Value();
705         Handle(AIS_ColoredDrawer) aFaceDrawer;
706         if (aFace.ShapeType() != TopAbs_FACE
707         || !theShapeDrawerMap.Find (aFace, aFaceDrawer))
708         {
709           continue;
710         }
711
712         if (aFaceDrawer->IsHidden())
713         {
714           isClosedShell = Standard_False;
715           break;
716         }
717         else if (aFaceDrawer->HasOwnShadingAspect()
718               && aFaceDrawer->ShadingAspect()->Aspect()->AlphaMode() != Graphic3d_AlphaMode_Opaque)
719         {
720           if (aFaceDrawer->ShadingAspect()->Aspect()->AlphaMode() != Graphic3d_AlphaMode_BlendAuto
721            || aFaceDrawer->ShadingAspect()->Aspect()->FrontMaterial().Alpha() < 1.0f
722            || (aFaceDrawer->ShadingAspect()->Aspect()->Distinguish()
723             && aFaceDrawer->ShadingAspect()->Aspect()->BackMaterial().Alpha()  < 1.0f))
724           {
725             isClosedShell = Standard_False;
726             break;
727           }
728         }
729       }
730     }
731
732     for (TopoDS_Iterator aSubShapeIter (theShapeToParse); aSubShapeIter.More(); aSubShapeIter.Next())
733     {
734       const TopoDS_Shape& aSubShape = aSubShapeIter.Value();
735       if (dispatchColors (aDrawer, aSubShape,
736                           theShapeDrawerMap, aShapeType,
737                           isClosedShell,
738                           theDrawerOpenedShapePerType,
739                           theDrawerClosedFaces))
740       {
741         isSubOverride = Standard_True;
742       }
743     }
744     return isOverriden || isSubOverride;
745   }
746
747   // iterate on sub-shapes
748   BRep_Builder aBBuilder;
749   TopoDS_Shape aShapeCopy = theShapeToParse.EmptyCopied();
750   aShapeCopy.Closed (theShapeToParse.Closed());
751   Standard_Integer nbDef = 0;
752   for (TopoDS_Iterator aSubShapeIter (theShapeToParse); aSubShapeIter.More(); aSubShapeIter.Next())
753   {
754     const TopoDS_Shape& aSubShape = aSubShapeIter.Value();
755     if (dispatchColors (aDrawer, aSubShape,
756                         theShapeDrawerMap, aShapeType,
757                         theIsParentClosed,
758                         theDrawerOpenedShapePerType,
759                         theDrawerClosedFaces))
760     {
761       isSubOverride = Standard_True;
762     }
763     else
764     {
765       aBBuilder.Add (aShapeCopy, aSubShape);
766       ++nbDef;
767     }
768   }
769   if (aShapeType == TopAbs_FACE || !isSubOverride)
770   {
771     aShapeCopy = theShapeToParse;
772   }
773   else if (nbDef == 0)
774   {
775     return isOverriden || isSubOverride; // empty compound
776   }
777
778   // if any of styles is overridden regarding to default one, add rest to map
779   if (isOverriden
780   || (isSubOverride && theParentType != TopAbs_WIRE  // avoid drawing edges when vertex color is overridden
781                     && theParentType != TopAbs_FACE) // avoid drawing edges of the same color as face
782   || (theParentType <= TopAbs_SHELL && !(isOverriden || isSubOverride))) // bind original shape to default color
783   {
784     TopoDS_Compound aCompound;
785     DataMapOfDrawerCompd& aDrawerShapeMap = theIsParentClosed
786                                          && aShapeType == TopAbs_FACE
787                                           ? theDrawerClosedFaces
788                                           : theDrawerOpenedShapePerType[(size_t)aShapeType];
789     if (!aDrawerShapeMap.FindFromKey (aDrawer, aCompound))
790     {
791       aBBuilder.MakeCompound (aCompound);
792       aDrawerShapeMap.Add (aDrawer, aCompound);
793     }
794     aBBuilder.Add (aCompound, aShapeCopy);
795   }
796   return isOverriden || isSubOverride;
797 }
798
799 //=======================================================================
800 //function : isShapeEntirelyVisible
801 //purpose  :
802 //=======================================================================
803 Standard_Boolean AIS_ColoredShape::isShapeEntirelyVisible() const
804 {
805   for (AIS_DataMapOfShapeDrawer::Iterator aMapIter (myShapeColors); aMapIter.More(); aMapIter.Next())
806   {
807     if (aMapIter.Value()->IsHidden())
808     {
809       return Standard_False;
810     }
811   }
812   return Standard_True;
813 }
814
815 //=======================================================================
816 //function : bindSubShapes
817 //purpose  :
818 //=======================================================================
819 void AIS_ColoredShape::bindSubShapes (AIS_DataMapOfShapeDrawer& theShapeDrawerMap,
820                                       const TopoDS_Shape& theKeyShape,
821                                       const Handle(AIS_ColoredDrawer)& theDrawer) const
822 {
823   TopAbs_ShapeEnum aShapeWithColorType = theKeyShape.ShapeType();
824   if (aShapeWithColorType == TopAbs_COMPOUND)
825   {
826     theShapeDrawerMap.Bind (theKeyShape, theDrawer);
827   }
828   else if (aShapeWithColorType == TopAbs_SOLID || aShapeWithColorType == TopAbs_SHELL)
829   {
830     for (TopExp_Explorer anExp (theKeyShape, TopAbs_FACE); anExp.More(); anExp.Next())
831     {
832       if (!theShapeDrawerMap.IsBound (anExp.Current()))
833       {
834         theShapeDrawerMap.Bind (anExp.Current(), theDrawer);
835       }
836     }
837   }
838   else if (aShapeWithColorType == TopAbs_WIRE)
839   {
840     for (TopExp_Explorer anExp (theKeyShape, TopAbs_EDGE); anExp.More(); anExp.Next())
841     {
842       if (!theShapeDrawerMap.IsBound (anExp.Current()))
843       {
844         theShapeDrawerMap.Bind (anExp.Current(), theDrawer);
845       }
846     }
847   }
848   else
849   {
850     // bind single face, edge and vertex
851     // force rebind if required due to the color of single shape has
852     // higher priority than the color of "compound" shape (wire is a
853     // compound of edges, shell is a compound of faces) that contains
854     // this single shape.
855     theShapeDrawerMap.Bind (theKeyShape, theDrawer);
856   }
857 }