0031431: Visualization, PrsMgr_PresentableObject - simplify HLR computing interface
[occt.git] / src / AIS / AIS_Shape.cxx
1 // Created on: 1996-12-20
2 // Created by: Robert COUBLANC
3 // Copyright (c) 1996-1999 Matra Datavision
4 // Copyright (c) 1999-2014 OPEN CASCADE SAS
5 //
6 // This file is part of Open CASCADE Technology software library.
7 //
8 // This library is free software; you can redistribute it and/or modify it under
9 // the terms of the GNU Lesser General Public License version 2.1 as published
10 // by the Free Software Foundation, with special exception defined in the file
11 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
12 // distribution for complete text of the license and disclaimer of any warranty.
13 //
14 // Alternatively, this file may be used under the terms of Open CASCADE
15 // commercial license or contractual agreement.
16
17 #include <AIS_Shape.hxx>
18
19 #include <AIS_GraphicTool.hxx>
20 #include <AIS_InteractiveContext.hxx>
21 #include <Aspect_TypeOfLine.hxx>
22 #include <BRep_Builder.hxx>
23 #include <BRepBndLib.hxx>
24 #include <BRepTools.hxx>
25 #include <BRepTools_ShapeSet.hxx>
26 #include <Geom_Transformation.hxx>
27 #include <gp_Pnt.hxx>
28 #include <Graphic3d_ArrayOfPolylines.hxx>
29 #include <Graphic3d_AspectFillArea3d.hxx>
30 #include <Graphic3d_AspectLine3d.hxx>
31 #include <Graphic3d_AspectMarker3d.hxx>
32 #include <Graphic3d_AspectText3d.hxx>
33 #include <Graphic3d_Group.hxx>
34 #include <Graphic3d_MaterialAspect.hxx>
35 #include <Graphic3d_SequenceOfGroup.hxx>
36 #include <Graphic3d_Structure.hxx>
37 #include <Message.hxx>
38 #include <Message_Messenger.hxx>
39 #include <HLRBRep.hxx>
40 #include <OSD_Timer.hxx>
41 #include <Precision.hxx>
42 #include <Prs3d.hxx>
43 #include <Prs3d_Drawer.hxx>
44 #include <Prs3d_IsoAspect.hxx>
45 #include <Prs3d_Presentation.hxx>
46 #include <Prs3d_Root.hxx>
47 #include <Prs3d_ShadingAspect.hxx>
48 #include <StdPrs_BndBox.hxx>
49 #include <StdPrs_ToolTriangulatedShape.hxx>
50 #include <Quantity_Color.hxx>
51 #include <Select3D_SensitiveBox.hxx>
52 #include <Select3D_SensitiveEntity.hxx>
53 #include <Standard_ErrorHandler.hxx>
54 #include <Standard_Failure.hxx>
55 #include <Standard_Type.hxx>
56 #include <StdPrs_HLRPolyShape.hxx>
57 #include <StdPrs_HLRShape.hxx>
58 #include <StdPrs_ShadedShape.hxx>
59 #include <StdPrs_WFShape.hxx>
60 #include <StdSelect.hxx>
61 #include <StdSelect_BRepOwner.hxx>
62 #include <StdSelect_BRepSelectionTool.hxx>
63 #include <StdSelect_DisplayMode.hxx>
64 #include <TColStd_ListIteratorOfListOfInteger.hxx>
65 #include <TopExp.hxx>
66
67 IMPLEMENT_STANDARD_RTTIEXT(AIS_Shape,AIS_InteractiveObject)
68
69 // Auxiliary macros
70 #define replaceAspectWithDef(theMap, theAspect) \
71   if (myDrawer->Link()->theAspect()->Aspect() != myDrawer->theAspect()->Aspect()) \
72   { \
73     theMap.Bind (myDrawer->theAspect()->Aspect(), myDrawer->Link()->theAspect()->Aspect()); \
74   }
75
76 // Auxiliary macros for replaceWithNewOwnAspects()
77 #define replaceAspectWithOwn(theMap, theAspect) \
78   if (myDrawer->Link()->theAspect()->Aspect() != myDrawer->theAspect()->Aspect()) \
79   { \
80     theMap.Bind (myDrawer->Link()->theAspect()->Aspect(), myDrawer->theAspect()->Aspect()); \
81   }
82
83 //=======================================================================
84 //function : replaceWithNewOwnAspects
85 //purpose  :
86 //=======================================================================
87 void AIS_Shape::replaceWithNewOwnAspects()
88 {
89   Graphic3d_MapOfAspectsToAspects aReplaceMap;
90
91   replaceAspectWithOwn (aReplaceMap, ShadingAspect);
92   replaceAspectWithOwn (aReplaceMap, LineAspect);
93   replaceAspectWithOwn (aReplaceMap, WireAspect);
94   replaceAspectWithOwn (aReplaceMap, FreeBoundaryAspect);
95   replaceAspectWithOwn (aReplaceMap, UnFreeBoundaryAspect);
96   replaceAspectWithOwn (aReplaceMap, SeenLineAspect);
97   replaceAspectWithOwn (aReplaceMap, FaceBoundaryAspect);
98   replaceAspectWithOwn (aReplaceMap, PointAspect);
99
100   replaceAspects (aReplaceMap);
101 }
102
103 //==================================================
104 // Function: AIS_Shape
105 // Purpose :
106 //==================================================
107 AIS_Shape::AIS_Shape(const TopoDS_Shape& theShape)
108 : AIS_InteractiveObject (PrsMgr_TOP_ProjectorDependant),
109   myshape (theShape),
110   myUVOrigin(0.0, 0.0),
111   myUVRepeat(1.0, 1.0),
112   myUVScale (1.0, 1.0),
113   myInitAng (0.0),
114   myCompBB (Standard_True)
115 {
116   //
117 }
118
119 //=======================================================================
120 //function : Compute
121 //purpose  : 
122 //=======================================================================
123 void AIS_Shape::Compute(const Handle(PrsMgr_PresentationManager3d)& /*aPresentationManager*/,
124                         const Handle(Prs3d_Presentation)& aPrs,
125                         const Standard_Integer theMode)
126 {  
127   if(myshape.IsNull()) return;
128
129   // wire,edge,vertex -> pas de HLR + priorite display superieure
130   Standard_Integer TheType = (Standard_Integer) myshape.ShapeType();
131   if(TheType>4 && TheType<8) {
132     aPrs->SetVisual(Graphic3d_TOS_ALL);
133     aPrs->SetDisplayPriority(TheType+2);
134   }
135   // Shape vide -> Assemblage vide.
136   if (myshape.ShapeType() == TopAbs_COMPOUND && myshape.NbChildren() == 0)
137   {
138     return;
139   }
140
141   if (IsInfinite())
142   {
143     aPrs->SetInfiniteState (Standard_True); //not taken in account during FITALL
144   }
145
146   switch (theMode)
147   {
148     case AIS_WireFrame:
149     {
150       StdPrs_ToolTriangulatedShape::ClearOnOwnDeflectionChange (myshape, myDrawer, Standard_True);
151       try
152       {
153         OCC_CATCH_SIGNALS
154         StdPrs_WFShape::Add (aPrs, myshape, myDrawer);
155       }
156       catch (Standard_Failure const& anException)
157       {
158         Message::DefaultMessenger()->Send (TCollection_AsciiString()
159                                          + "Error: AIS_Shape::Compute() wireframe presentation builder has failed ("
160                                          + anException.GetMessageString() + ")", Message_Fail);
161       }
162       break;
163     }
164     case AIS_Shaded:
165     {
166       StdPrs_ToolTriangulatedShape::ClearOnOwnDeflectionChange (myshape, myDrawer, Standard_True);
167       if ((Standard_Integer) myshape.ShapeType() > 4)
168       {
169         StdPrs_WFShape::Add (aPrs, myshape, myDrawer);
170       }
171       else
172       {
173         if (IsInfinite())
174         {
175           StdPrs_WFShape::Add (aPrs, myshape, myDrawer);
176         }
177         else
178         {
179           try
180           {
181             OCC_CATCH_SIGNALS
182             StdPrs_ShadedShape::Add (aPrs, myshape, myDrawer,
183                                      myDrawer->ShadingAspect()->Aspect()->ToMapTexture()
184                                  && !myDrawer->ShadingAspect()->Aspect()->TextureMap().IsNull(),
185                                      myUVOrigin, myUVRepeat, myUVScale);
186           }
187           catch (Standard_Failure const& anException)
188           {
189             Message::DefaultMessenger()->Send (TCollection_AsciiString()
190                                                + "Error: AIS_Shape::Compute() shaded presentation builder has failed ("
191                                                + anException.GetMessageString() + ")", Message_Fail);
192             StdPrs_WFShape::Add (aPrs, myshape, myDrawer);
193           }
194         }
195       }
196       Standard_Real aTransparency = Transparency() ;
197       if (aTransparency > 0.0)
198       {
199         SetTransparency (aTransparency);
200       }
201       break;
202     }
203
204     // Bounding box.
205     case 2:
206     {
207       if (IsInfinite())
208       {
209         StdPrs_WFShape::Add (aPrs, myshape, myDrawer);
210       }
211       else
212       {
213         StdPrs_BndBox::Add (aPrs, BoundingBox(), myDrawer);
214       }
215     }
216   }
217
218   // Recompute hidden line presentation (if necessary).
219   aPrs->ReCompute();
220 }
221
222 //=======================================================================
223 //function : computeHlrPresentation
224 //purpose  :
225 //=======================================================================
226 void AIS_Shape::computeHlrPresentation (const Handle(Graphic3d_Camera)& theProjector,
227                                         const Handle(Prs3d_Presentation)& thePrs,
228                                         const TopoDS_Shape& theShape,
229                                         const Handle(Prs3d_Drawer)& theDrawer)
230 {
231   if (theShape.IsNull())
232   {
233     return;
234   }
235
236   switch (theShape.ShapeType())
237   {
238     case TopAbs_VERTEX:
239     case TopAbs_EDGE:
240     case TopAbs_WIRE:
241     {
242       thePrs->SetDisplayPriority (4);
243       StdPrs_WFShape::Add (thePrs, theShape, theDrawer);
244       return;
245     }
246     case TopAbs_COMPOUND:
247     {
248       if (theShape.NbChildren() == 0)
249       {
250         return;
251       }
252       break;
253     }
254     default:
255     {
256       break;
257     }
258   }
259
260   const Handle(Prs3d_Drawer)& aDefDrawer = theDrawer->Link();
261   if (aDefDrawer->DrawHiddenLine())
262   {
263     theDrawer->EnableDrawHiddenLine();
264   }
265   else
266   {
267     theDrawer->DisableDrawHiddenLine();
268   }
269
270   const Aspect_TypeOfDeflection aPrevDef = aDefDrawer->TypeOfDeflection();
271   aDefDrawer->SetTypeOfDeflection (Aspect_TOD_RELATIVE);
272   if (theDrawer->IsAutoTriangulation())
273   {
274     StdPrs_ToolTriangulatedShape::ClearOnOwnDeflectionChange (theShape, theDrawer, Standard_True);
275   }
276
277   {
278     try
279     {
280       OCC_CATCH_SIGNALS
281       switch (theDrawer->TypeOfHLR())
282       {
283         case Prs3d_TOH_Algo:
284         {
285           StdPrs_HLRShape aBuilder;
286           aBuilder.ComputeHLR (thePrs, theShape, theDrawer, theProjector);
287           break;
288         }
289         case Prs3d_TOH_PolyAlgo:
290         case Prs3d_TOH_NotSet:
291         {
292           StdPrs_HLRPolyShape aBuilder;
293           aBuilder.ComputeHLR (thePrs, theShape, theDrawer, theProjector);
294           break;
295         }
296       }
297     }
298     catch (Standard_Failure const& anException)
299     {
300       Message::DefaultMessenger()->Send (TCollection_AsciiString()
301                                        + "Error: AIS_Shape::Compute() HLR Algorithm has failed ("
302                                        + anException.GetMessageString() + ")", Message_Fail);
303       StdPrs_WFShape::Add (thePrs, theShape, theDrawer);
304     }
305   }
306
307   aDefDrawer->SetTypeOfDeflection (aPrevDef);
308 }
309
310 //=======================================================================
311 //function : ComputeSelection
312 //purpose  : 
313 //=======================================================================
314
315 void AIS_Shape::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
316                                               const Standard_Integer aMode)
317 {
318   if(myshape.IsNull()) return;
319   if (myshape.ShapeType() == TopAbs_COMPOUND && myshape.NbChildren() == 0)
320   {
321     // empty Shape -> empty Assembly.
322     return;
323   }
324
325   TopAbs_ShapeEnum TypOfSel = AIS_Shape::SelectionType(aMode);
326   TopoDS_Shape shape = myshape;
327
328 // POP protection against crash in low layers
329
330   Standard_Real aDeflection = Prs3d::GetDeflection(shape, myDrawer);
331   try
332   {
333     OCC_CATCH_SIGNALS
334     StdSelect_BRepSelectionTool::Load(aSelection,
335                                       this,
336                                       shape,
337                                       TypOfSel,
338                                       aDeflection,
339                                       myDrawer->HLRAngle(),
340                                       myDrawer->IsAutoTriangulation());
341   }
342   catch (Standard_Failure const& anException)
343   {
344     Message::DefaultMessenger()->Send (TCollection_AsciiString()
345                                        + "Error: AIS_Shape::ComputeSelection(" + aMode + ") has failed ("
346                                        + anException.GetMessageString() + ")", Message_Fail);
347     if (aMode == 0)
348     {
349       aSelection->Clear();
350       Bnd_Box B = BoundingBox();
351       Handle(StdSelect_BRepOwner) aOwner = new StdSelect_BRepOwner(shape,this);
352       Handle(Select3D_SensitiveBox) aSensitiveBox = new Select3D_SensitiveBox(aOwner,B);
353       aSelection->Add(aSensitiveBox);
354     }
355   }
356
357   // insert the drawer in the BrepOwners for hilight...
358   StdSelect::SetDrawerForBRepOwner(aSelection,myDrawer);
359 }
360
361 void AIS_Shape::Color( Quantity_Color& aColor ) const {
362   aColor = myDrawer->ShadingAspect()->Color(myCurrentFacingModel);
363 }
364
365 Graphic3d_NameOfMaterial AIS_Shape::Material() const {
366   return (myDrawer->ShadingAspect()->Material(myCurrentFacingModel)).Name();
367 }
368
369 Standard_Real AIS_Shape::Transparency() const {
370   return myDrawer->ShadingAspect()->Transparency(myCurrentFacingModel);
371 }
372
373 //=======================================================================
374 //function : setColor
375 //purpose  :
376 //=======================================================================
377
378 bool AIS_Shape::setColor (const Handle(Prs3d_Drawer)& theDrawer,
379                           const Quantity_Color&       theColor) const
380 {
381   bool toRecompute = false;
382   toRecompute = theDrawer->SetupOwnShadingAspect() || toRecompute;
383   toRecompute = theDrawer->SetOwnLineAspects() || toRecompute;
384   toRecompute = theDrawer->SetupOwnPointAspect() || toRecompute;
385
386   // override color
387   theDrawer->ShadingAspect()->SetColor (theColor, myCurrentFacingModel);
388   theDrawer->LineAspect()->SetColor (theColor);
389   theDrawer->WireAspect()->SetColor (theColor);
390   theDrawer->PointAspect()->SetColor (theColor);
391   theDrawer->FreeBoundaryAspect()->SetColor (theColor);
392   theDrawer->UnFreeBoundaryAspect()->SetColor (theColor);
393   theDrawer->SeenLineAspect()->SetColor (theColor);
394   theDrawer->FaceBoundaryAspect()->SetColor (theColor);
395   return toRecompute;
396 }
397
398 //=======================================================================
399 //function : SetColor
400 //purpose  :
401 //=======================================================================
402
403 void AIS_Shape::SetColor (const Quantity_Color& theColor)
404 {
405   const bool toRecompute = setColor (myDrawer, theColor);
406   myDrawer->SetColor (theColor);
407   hasOwnColor = Standard_True;
408
409   if (!toRecompute
410    || !myDrawer->HasLink())
411   {
412     SynchronizeAspects();
413   }
414   else
415   {
416     replaceWithNewOwnAspects();
417   }
418   recomputeComputed();
419 }
420
421 //=======================================================================
422 //function : UnsetColor
423 //purpose  :
424 //=======================================================================
425
426 void AIS_Shape::UnsetColor()
427 {
428   if (!HasColor())
429   {
430     return;
431   }
432
433   hasOwnColor = Standard_False;
434   myDrawer->SetColor (myDrawer->HasLink() ? myDrawer->Link()->Color() : Quantity_Color (Quantity_NOC_WHITE));
435
436   Graphic3d_MapOfAspectsToAspects aReplaceMap;
437   if (!HasWidth())
438   {
439     replaceAspectWithDef (aReplaceMap, LineAspect);
440     replaceAspectWithDef (aReplaceMap, WireAspect);
441     replaceAspectWithDef (aReplaceMap, FreeBoundaryAspect);
442     replaceAspectWithDef (aReplaceMap, UnFreeBoundaryAspect);
443     replaceAspectWithDef (aReplaceMap, SeenLineAspect);
444     replaceAspectWithDef (aReplaceMap, FaceBoundaryAspect);
445     myDrawer->SetLineAspect          (Handle(Prs3d_LineAspect)());
446     myDrawer->SetWireAspect          (Handle(Prs3d_LineAspect)());
447     myDrawer->SetFreeBoundaryAspect  (Handle(Prs3d_LineAspect)());
448     myDrawer->SetUnFreeBoundaryAspect(Handle(Prs3d_LineAspect)());
449     myDrawer->SetSeenLineAspect      (Handle(Prs3d_LineAspect)());
450     myDrawer->SetFaceBoundaryAspect  (Handle(Prs3d_LineAspect)());
451   }
452   else
453   {
454     Quantity_Color aColor = Quantity_NOC_YELLOW;
455     if (myDrawer->HasLink())
456     {
457       AIS_GraphicTool::GetLineColor (myDrawer->Link(), AIS_TOA_Line,   aColor);
458     }
459     myDrawer->LineAspect()->SetColor (aColor);
460     aColor = Quantity_NOC_RED;
461     if (myDrawer->HasLink())
462     {
463       AIS_GraphicTool::GetLineColor (myDrawer->Link(), AIS_TOA_Wire,   aColor);
464     }
465     myDrawer->WireAspect()->SetColor (aColor);
466     aColor = Quantity_NOC_GREEN;
467     if (myDrawer->HasLink())
468     {
469       AIS_GraphicTool::GetLineColor (myDrawer->Link(), AIS_TOA_Free,   aColor);
470     }
471     myDrawer->FreeBoundaryAspect()->SetColor (aColor);
472     aColor = Quantity_NOC_YELLOW;
473     if (myDrawer->HasLink())
474     {
475       AIS_GraphicTool::GetLineColor (myDrawer->Link(), AIS_TOA_UnFree, aColor);
476     }
477     myDrawer->UnFreeBoundaryAspect()->SetColor (aColor);
478     if (myDrawer->HasLink())
479     {
480       AIS_GraphicTool::GetLineColor (myDrawer->Link(), AIS_TOA_Seen,   aColor);
481     }
482     myDrawer->SeenLineAspect()->SetColor (aColor);
483     aColor = Quantity_NOC_BLACK;
484     if (myDrawer->HasLink())
485     {
486       AIS_GraphicTool::GetLineColor (myDrawer->Link(), AIS_TOA_FaceBoundary, aColor);
487     }
488     myDrawer->FaceBoundaryAspect()->SetColor (aColor);
489   }
490
491   if (!myDrawer->HasOwnShadingAspect())
492   {
493     //
494   }
495   else if (HasMaterial()
496         || IsTransparent()
497         || myDrawer->ShadingAspect()->Aspect()->ToMapTexture())
498   {
499     const Graphic3d_MaterialAspect aDefaultMat (Graphic3d_NOM_BRASS);
500     Graphic3d_MaterialAspect mat = aDefaultMat;
501     Quantity_Color anInteriorColors[2] = {Quantity_NOC_CYAN1, Quantity_NOC_CYAN1};
502     if (myDrawer->HasLink())
503     {
504       anInteriorColors[0] = myDrawer->Link()->ShadingAspect()->Aspect()->InteriorColor();
505       anInteriorColors[1] = myDrawer->Link()->ShadingAspect()->Aspect()->BackInteriorColor();
506     }
507     if (HasMaterial() || myDrawer->HasLink())
508     {
509       const Handle(Graphic3d_AspectFillArea3d)& aSrcAspect = (HasMaterial() ? myDrawer : myDrawer->Link())->ShadingAspect()->Aspect();
510       mat = myCurrentFacingModel != Aspect_TOFM_BACK_SIDE
511           ? aSrcAspect->FrontMaterial()
512           : aSrcAspect->BackMaterial();
513     }
514     if (HasMaterial())
515     {
516       const Quantity_Color aColor = myDrawer->HasLink()
517                                   ? myDrawer->Link()->ShadingAspect()->Color (myCurrentFacingModel)
518                                   : aDefaultMat.AmbientColor();
519       mat.SetColor (aColor);
520     }
521     if (IsTransparent())
522     {
523       Standard_Real aTransp = myDrawer->ShadingAspect()->Transparency (myCurrentFacingModel);
524       mat.SetTransparency (Standard_ShortReal(aTransp));
525     }
526     myDrawer->ShadingAspect()->SetMaterial (mat, myCurrentFacingModel);
527     myDrawer->ShadingAspect()->Aspect()->SetInteriorColor    (anInteriorColors[0]);
528     myDrawer->ShadingAspect()->Aspect()->SetBackInteriorColor(anInteriorColors[1]);
529   }
530   else
531   {
532     replaceAspectWithDef (aReplaceMap, ShadingAspect);
533     myDrawer->SetShadingAspect (Handle(Prs3d_ShadingAspect)());
534   }
535   if (myDrawer->HasOwnPointAspect())
536   {
537     replaceAspectWithDef (aReplaceMap, PointAspect);
538     myDrawer->SetPointAspect (Handle(Prs3d_PointAspect)());
539   }
540   replaceAspects (aReplaceMap);
541   SynchronizeAspects();
542   recomputeComputed();
543 }
544
545 //=======================================================================
546 //function : setWidth
547 //purpose  :
548 //=======================================================================
549
550 bool AIS_Shape::setWidth (const Handle(Prs3d_Drawer)& theDrawer,
551                           const Standard_Real         theLineWidth) const
552 {
553   bool toRecompute = theDrawer->SetOwnLineAspects();
554
555   // override width
556   theDrawer->LineAspect()->SetWidth (theLineWidth);
557   theDrawer->WireAspect()->SetWidth (theLineWidth);
558   theDrawer->FreeBoundaryAspect()->SetWidth (theLineWidth);
559   theDrawer->UnFreeBoundaryAspect()->SetWidth (theLineWidth);
560   theDrawer->SeenLineAspect()->SetWidth (theLineWidth);
561   theDrawer->FaceBoundaryAspect()->SetWidth (theLineWidth);
562   return toRecompute;
563 }
564
565 //=======================================================================
566 //function : SetWidth
567 //purpose  : 
568 //=======================================================================
569
570 void AIS_Shape::SetWidth (const Standard_Real theLineWidth)
571 {
572   myOwnWidth = (Standard_ShortReal )theLineWidth;
573
574   if (!setWidth (myDrawer, theLineWidth)
575    || !myDrawer->HasLink())
576   {
577     SynchronizeAspects();
578   }
579   else
580   {
581     replaceWithNewOwnAspects();
582   }
583   recomputeComputed();
584 }
585
586 //=======================================================================
587 //function : UnsetWidth
588 //purpose  :
589 //=======================================================================
590
591 void AIS_Shape::UnsetWidth()
592 {
593   if (myOwnWidth == 0.0f)
594   {
595     return;
596   }
597
598   myOwnWidth = 0.0f;
599   if (!HasColor())
600   {
601     Graphic3d_MapOfAspectsToAspects aReplaceMap;
602     replaceAspectWithDef (aReplaceMap, LineAspect);
603     replaceAspectWithDef (aReplaceMap, WireAspect);
604     replaceAspectWithDef (aReplaceMap, FreeBoundaryAspect);
605     replaceAspectWithDef (aReplaceMap, UnFreeBoundaryAspect);
606     replaceAspectWithDef (aReplaceMap, SeenLineAspect);
607     replaceAspectWithDef (aReplaceMap, FaceBoundaryAspect);
608     myDrawer->SetLineAspect          (Handle(Prs3d_LineAspect)());
609     myDrawer->SetWireAspect          (Handle(Prs3d_LineAspect)());
610     myDrawer->SetFreeBoundaryAspect  (Handle(Prs3d_LineAspect)());
611     myDrawer->SetUnFreeBoundaryAspect(Handle(Prs3d_LineAspect)());
612     myDrawer->SetSeenLineAspect      (Handle(Prs3d_LineAspect)());
613     myDrawer->SetFaceBoundaryAspect  (Handle(Prs3d_LineAspect)());
614     replaceAspects (aReplaceMap);
615   }
616   else
617   {
618     myDrawer->LineAspect()          ->SetWidth (myDrawer->HasLink() ?
619       AIS_GraphicTool::GetLineWidth (myDrawer->Link(), AIS_TOA_Line) : 1.);
620     myDrawer->WireAspect()          ->SetWidth (myDrawer->HasLink() ?
621       AIS_GraphicTool::GetLineWidth (myDrawer->Link(), AIS_TOA_Wire) : 1.);
622     myDrawer->FreeBoundaryAspect()  ->SetWidth (myDrawer->HasLink() ?
623       AIS_GraphicTool::GetLineWidth (myDrawer->Link(), AIS_TOA_Free) : 1.);
624     myDrawer->UnFreeBoundaryAspect()->SetWidth (myDrawer->HasLink() ?
625       AIS_GraphicTool::GetLineWidth (myDrawer->Link(), AIS_TOA_UnFree) : 1.);
626     myDrawer->SeenLineAspect()      ->SetWidth (myDrawer->HasLink() ?
627       AIS_GraphicTool::GetLineWidth (myDrawer->Link(), AIS_TOA_Seen) : 1.);
628     myDrawer->FaceBoundaryAspect()      ->SetWidth (myDrawer->HasLink() ?
629       AIS_GraphicTool::GetLineWidth (myDrawer->Link(), AIS_TOA_FaceBoundary) : 1.);
630     SynchronizeAspects();
631   }
632   recomputeComputed();
633 }
634
635 //=======================================================================
636 //function : setMaterial
637 //purpose  :
638 //=======================================================================
639
640 void AIS_Shape::setMaterial (const Handle(Prs3d_Drawer)&     theDrawer,
641                              const Graphic3d_MaterialAspect& theMaterial,
642                              const Standard_Boolean          theToKeepColor,
643                              const Standard_Boolean          theToKeepTransp) const
644 {
645   const Quantity_Color aColor  = theDrawer->ShadingAspect()->Color (myCurrentFacingModel);
646   const Standard_Real  aTransp = theDrawer->ShadingAspect()->Transparency (myCurrentFacingModel);
647   theDrawer->SetupOwnShadingAspect();
648   theDrawer->ShadingAspect()->SetMaterial (theMaterial, myCurrentFacingModel);
649
650   if (theToKeepColor)
651   {
652     theDrawer->ShadingAspect()->SetColor (aColor, myCurrentFacingModel);
653   }
654   if (theToKeepTransp)
655   {
656     theDrawer->ShadingAspect()->SetTransparency (aTransp, myCurrentFacingModel);
657   }
658 }
659
660 //=======================================================================
661 //function : SetMaterial
662 //purpose  :
663 //=======================================================================
664
665 void AIS_Shape::SetMaterial (const Graphic3d_MaterialAspect& theMat)
666 {
667   const bool toRecompute = !myDrawer->HasOwnShadingAspect();
668   setMaterial (myDrawer, theMat, HasColor(), IsTransparent());
669   hasOwnMaterial = Standard_True;
670
671   if (!toRecompute
672    || !myDrawer->HasLink())
673   {
674     SynchronizeAspects();
675   }
676   else
677   {
678     replaceWithNewOwnAspects();
679   }
680 }
681
682 //=======================================================================
683 //function : UnsetMaterial
684 //purpose  :
685 //=======================================================================
686
687 void AIS_Shape::UnsetMaterial()
688 {
689   if (!HasMaterial())
690   {
691     return;
692   }
693
694   if (!myDrawer->HasOwnShadingAspect())
695   {
696     //
697   }
698   else if (HasColor()
699         || IsTransparent()
700         || myDrawer->ShadingAspect()->Aspect()->ToMapTexture())
701   {
702     if(myDrawer->HasLink())
703     {
704       myDrawer->ShadingAspect()->SetMaterial (myDrawer->Link()->ShadingAspect()->Material (myCurrentFacingModel),
705                                               myCurrentFacingModel);
706     }
707     if (HasColor())
708     {
709       myDrawer->ShadingAspect()->SetColor        (myDrawer->Color(),        myCurrentFacingModel);
710       myDrawer->ShadingAspect()->SetTransparency (myDrawer->Transparency(), myCurrentFacingModel);
711     }
712     SynchronizeAspects();
713   }
714   else
715   {
716     Graphic3d_MapOfAspectsToAspects aReplaceMap;
717     replaceAspectWithDef (aReplaceMap, ShadingAspect);
718     myDrawer->SetShadingAspect (Handle(Prs3d_ShadingAspect)());
719     replaceAspects (aReplaceMap);
720   }
721 }
722
723 //=======================================================================
724 //function : setTransparency
725 //purpose  :
726 //=======================================================================
727
728 void AIS_Shape::setTransparency (const Handle(Prs3d_Drawer)& theDrawer,
729                                  const Standard_Real         theValue) const
730 {
731   theDrawer->SetupOwnShadingAspect();
732   // override transparency
733   theDrawer->ShadingAspect()->SetTransparency (theValue, myCurrentFacingModel);
734 }
735
736 //=======================================================================
737 //function : SetTransparency
738 //purpose  :
739 //=======================================================================
740
741 void AIS_Shape::SetTransparency (const Standard_Real theValue)
742 {
743   const bool toRecompute = !myDrawer->HasOwnShadingAspect();
744   setTransparency (myDrawer, theValue);
745   myDrawer->SetTransparency ((Standard_ShortReal )theValue);
746
747   if (!toRecompute
748    || !myDrawer->HasLink())
749   {
750     SynchronizeAspects();
751   }
752   else
753   {
754     replaceWithNewOwnAspects();
755   }
756 }
757
758 //=======================================================================
759 //function : UnsetTransparency
760 //purpose  :
761 //=======================================================================
762
763 void AIS_Shape::UnsetTransparency()
764 {
765   myDrawer->SetTransparency (0.0f);
766   if (!myDrawer->HasOwnShadingAspect())
767   {
768     return;
769   }
770   else if (HasColor()
771         || HasMaterial()
772         || myDrawer->ShadingAspect()->Aspect()->ToMapTexture())
773   {
774     myDrawer->ShadingAspect()->SetTransparency (0.0, myCurrentFacingModel);
775     SynchronizeAspects();
776   }
777   else
778   {
779     Graphic3d_MapOfAspectsToAspects aReplaceMap;
780     replaceAspectWithDef (aReplaceMap, ShadingAspect);
781     myDrawer->SetShadingAspect (Handle(Prs3d_ShadingAspect)());
782     replaceAspects (aReplaceMap);
783   }
784 }
785
786 //=======================================================================
787 //function : BoundingBox
788 //purpose  : 
789 //=======================================================================
790
791 const Bnd_Box& AIS_Shape::BoundingBox()  
792 {
793   if (myshape.ShapeType() == TopAbs_COMPOUND && myshape.NbChildren() == 0)
794   {
795     // empty Shape  -> empty Assembly.
796     myBB.SetVoid ();
797     return myBB;
798   }
799
800   if(myCompBB) {
801     BRepBndLib::Add (myshape, myBB, false);
802     myCompBB = Standard_False;
803   }
804   return myBB;
805 }
806
807 //*****
808 //***** Reset
809 //=======================================================================
810 //function : SetOwnDeviationCoefficient
811 //purpose  : resets myhasOwnDeviationCoefficient to Standard_False and
812 //           returns Standard_True if it change
813 //=======================================================================
814
815 Standard_Boolean AIS_Shape::SetOwnDeviationCoefficient ()
816 {
817   Standard_Boolean itSet = myDrawer->HasOwnDeviationCoefficient();
818   if(itSet)  myDrawer->SetDeviationCoefficient();
819   return itSet;
820 }
821
822 //=======================================================================
823 //function : SetHLROwnDeviationCoefficient
824 //purpose  : resets myhasOwnHLRDeviationCoefficient to Standard_False and
825 //           returns Standard_True if it change
826 //=======================================================================
827
828 Standard_Boolean AIS_Shape::SetOwnHLRDeviationCoefficient ()
829 {
830   Standard_Boolean itSet = myDrawer->HasOwnHLRDeviationCoefficient();
831   if(itSet)  myDrawer->SetHLRDeviationCoefficient();
832   return itSet;
833
834 }
835
836 //=======================================================================
837 //function : SetOwnDeviationAngle
838 //purpose  : resets myhasOwnDeviationAngle to Standard_False and
839 //           returns Standard_True if it change
840 //=======================================================================
841
842 Standard_Boolean AIS_Shape::SetOwnDeviationAngle ()
843 {
844   Standard_Boolean itSet = myDrawer->HasOwnDeviationAngle();
845   if(itSet)  myDrawer->SetDeviationAngle();
846   return itSet;
847
848 }
849
850 //=======================================================================
851 //function : SetOwnHLRDeviationAngle
852 //purpose  : resets myhasOwnHLRDeviationAngle to Standard_False and
853 //           returns Standard_True if it change
854 //=======================================================================
855
856 Standard_Boolean AIS_Shape::SetOwnHLRDeviationAngle ()
857 {
858   Standard_Boolean itSet = myDrawer->HasOwnHLRDeviationAngle();
859   if(itSet)  myDrawer->SetHLRAngle();
860   return itSet;
861
862 }
863 //***** SetOwn
864 //=======================================================================
865 //function : SetOwnDeviationCoefficient
866 //purpose  : 
867 //=======================================================================
868
869 void AIS_Shape::SetOwnDeviationCoefficient ( const Standard_Real  aCoefficient )
870 {
871   myDrawer->SetDeviationCoefficient( aCoefficient );
872   SetToUpdate();
873 }
874
875 //=======================================================================
876 //function : SetOwnHLRDeviationCoefficient
877 //purpose  : 
878 //=======================================================================
879
880 void AIS_Shape::SetOwnHLRDeviationCoefficient ( const Standard_Real  aCoefficient )
881 {
882   myDrawer->SetHLRDeviationCoefficient( aCoefficient );
883   
884 }
885
886 //=======================================================================
887 //function : SetOwnDeviationAngle
888 //purpose  : 
889 //=======================================================================
890
891 void AIS_Shape::SetOwnDeviationAngle (const Standard_Real theAngle)
892 {
893   myDrawer->SetDeviationAngle (theAngle);
894   SetToUpdate (AIS_WireFrame);
895 }
896 //=======================================================================
897 //function : SetOwnDeviationAngle
898 //purpose  : 
899 //=======================================================================
900
901 void AIS_Shape::SetAngleAndDeviation ( const Standard_Real  anAngle )
902 {
903   Standard_Real OutAngl,OutDefl;
904   HLRBRep::PolyHLRAngleAndDeflection(anAngle,OutAngl,OutDefl);
905   SetOwnDeviationAngle(anAngle) ;
906   SetOwnDeviationCoefficient(OutDefl) ;
907   myInitAng = anAngle;
908   SetToUpdate();
909 }
910
911 //=======================================================================
912 //function : UserAngle
913 //purpose  : 
914 //=======================================================================
915
916 Standard_Real AIS_Shape::UserAngle() const
917 {
918   return myInitAng ==0. ? GetContext()->DeviationAngle(): myInitAng;
919 }
920
921
922 //=======================================================================
923 //function : SetHLRAngleAndDeviation
924 //purpose  : 
925 //=======================================================================
926
927 void AIS_Shape::SetHLRAngleAndDeviation ( const Standard_Real  anAngle )
928 {
929   Standard_Real OutAngl,OutDefl;
930   HLRBRep::PolyHLRAngleAndDeflection(anAngle,OutAngl,OutDefl);
931   SetOwnHLRDeviationAngle( OutAngl );
932   SetOwnHLRDeviationCoefficient(OutDefl);
933
934 }
935 //=======================================================================
936 //function : SetOwnHLRDeviationAngle
937 //purpose  : 
938 //=======================================================================
939
940 void AIS_Shape::SetOwnHLRDeviationAngle ( const Standard_Real  anAngle )
941 {
942   myDrawer->SetHLRAngle( anAngle );
943 }
944
945 //***** GetOwn
946 //=======================================================================
947 //function : OwnDeviationCoefficient
948 //purpose  : 
949 //=======================================================================
950
951 Standard_Boolean AIS_Shape::OwnDeviationCoefficient ( Standard_Real &  aCoefficient,
952                                                       Standard_Real & aPreviousCoefficient ) const
953 {
954   aCoefficient = myDrawer->DeviationCoefficient();
955   aPreviousCoefficient = myDrawer->PreviousDeviationCoefficient ();
956   return myDrawer->HasOwnDeviationCoefficient() ;
957 }
958
959 //=======================================================================
960 //function : OwnHLRDeviationCoefficient
961 //purpose  : 
962 //=======================================================================
963
964 Standard_Boolean AIS_Shape::OwnHLRDeviationCoefficient ( Standard_Real & aCoefficient,
965                                                          Standard_Real & aPreviousCoefficient ) const
966 {
967   aCoefficient = myDrawer->HLRDeviationCoefficient();
968   aPreviousCoefficient = myDrawer->PreviousHLRDeviationCoefficient ();
969   return myDrawer->HasOwnHLRDeviationCoefficient();
970
971 }
972
973 //=======================================================================
974 //function : OwnDeviationAngle
975 //purpose  : 
976 //=======================================================================
977
978 Standard_Boolean AIS_Shape::OwnDeviationAngle ( Standard_Real &  anAngle,
979                                                 Standard_Real & aPreviousAngle ) const
980 {
981   anAngle = myDrawer->DeviationAngle();
982   aPreviousAngle = myDrawer->PreviousDeviationAngle (); 
983   return myDrawer->HasOwnDeviationAngle();
984 }
985
986 //=======================================================================
987 //function : OwnHLRDeviationAngle
988 //purpose  : 
989 //=======================================================================
990
991 Standard_Boolean AIS_Shape::OwnHLRDeviationAngle ( Standard_Real &  anAngle,
992                                                    Standard_Real & aPreviousAngle ) const
993 {
994   anAngle = myDrawer->HLRAngle();
995   aPreviousAngle = myDrawer->PreviousHLRDeviationAngle (); 
996   return myDrawer->HasOwnHLRDeviationAngle();
997 }
998
999 //=======================================================================
1000 //function : DumpJson
1001 //purpose  : 
1002 //=======================================================================
1003 void AIS_Shape::DumpJson (Standard_OStream& theOStream, Standard_Integer theDepth) const
1004 {
1005   OCCT_DUMP_TRANSIENT_CLASS_BEGIN (theOStream)
1006   OCCT_DUMP_BASE_CLASS (theOStream, theDepth, AIS_InteractiveObject)
1007
1008   OCCT_DUMP_FIELD_VALUES_DUMPED (theOStream, theDepth, &myshape)
1009   OCCT_DUMP_FIELD_VALUES_DUMPED (theOStream, theDepth, &myBB)
1010
1011   OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myInitAng)
1012   OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myCompBB)
1013 }