0031687: Draw Harness, ViewerTest - extend command vrenderparams with option updating...
[occt.git] / src / AIS / AIS_TexturedShape.cxx
index 93cd361..a764719 100644 (file)
@@ -15,7 +15,6 @@
 
 #include <AIS_TexturedShape.hxx>
 
-#include <AIS_Drawer.hxx>
 #include <AIS_GraphicTool.hxx>
 #include <AIS_InteractiveContext.hxx>
 #include <BRepTools.hxx>
 #include <Graphic3d_Group.hxx>
 #include <Graphic3d_StructureManager.hxx>
 #include <Graphic3d_Texture2Dmanual.hxx>
+#include <Message.hxx>
+#include <Message_Messenger.hxx>
 #include <Precision.hxx>
+#include <Prs3d_Drawer.hxx>
 #include <Prs3d_Presentation.hxx>
-#include <PrsMgr_ModedPresentation.hxx>
 #include <Prs3d_Root.hxx>
 #include <Prs3d_LineAspect.hxx>
 #include <Prs3d_ShadingAspect.hxx>
 #include <PrsMgr_PresentationManager3d.hxx>
 #include <Standard_ErrorHandler.hxx>
+#include <Prs3d_BndBox.hxx>
 #include <StdPrs_ShadedShape.hxx>
-#include <StdPrs_ToolShadedShape.hxx>
-#include <StdPrs_WFDeflectionShape.hxx>
+#include <StdPrs_ToolTriangulatedShape.hxx>
 #include <StdPrs_WFShape.hxx>
 #include <TopExp_Explorer.hxx>
 
-IMPLEMENT_STANDARD_HANDLE (AIS_TexturedShape, AIS_Shape)
-IMPLEMENT_STANDARD_RTTIEXT(AIS_TexturedShape, AIS_Shape)
+
+IMPLEMENT_STANDARD_RTTIEXT(AIS_TexturedShape,AIS_Shape)
 
 //=======================================================================
 //function : AIS_TexturedShape
@@ -50,11 +51,8 @@ AIS_TexturedShape::AIS_TexturedShape (const TopoDS_Shape& theShape)
   myPredefTexture   (Graphic3d_NameOfTexture2D(0)),
   myToMapTexture    (Standard_True),
   myModulate        (Standard_True),
-  myUVOrigin        (0.0, 0.0),
   myIsCustomOrigin  (Standard_True),
-  myUVRepeat        (1.0, 1.0),
   myToRepeat        (Standard_True),
-  myUVScale         (1.0, 1.0),
   myToScale         (Standard_True),
   myToShowTriangles (Standard_False)
 {
@@ -78,10 +76,8 @@ void AIS_TexturedShape::SetTextureFileName (const TCollection_AsciiString& theTe
     }
     else
     {
-#ifdef AIS_DEB
-      std::cout << "Texture " << theTextureFileName << " doesn't exist\n";
-      std::cout << "Using Texture 0 instead ...\n";
-#endif
+      Message::SendFail (TCollection_AsciiString ("Error: texture with ID ") + theTextureFileName
+                       + " is undefined. Texture 0 will be used instead.");
       myPredefTexture = Graphic3d_NameOfTexture2D (0);
     }
     myTextureFile = "";
@@ -202,14 +198,12 @@ void AIS_TexturedShape::SetColor (const Quantity_Color& theColor)
 {
   AIS_Shape::SetColor (theColor);
 
-  for (Standard_Integer aPrsIt = 1; aPrsIt <= Presentations().Length(); ++aPrsIt)
+  for (PrsMgr_Presentations::Iterator aPrsIter (Presentations()); aPrsIter.More(); aPrsIter.Next())
   {
-    const PrsMgr_ModedPresentation& aPrsModed = Presentations().Value (aPrsIt);
-
-    if (aPrsModed.Mode() != 3)
-      continue;
-
-    updateAttributes (aPrsModed.Presentation()->Presentation());
+    if (aPrsIter.Value()->Mode() == 3)
+    {
+      updateAttributes (aPrsIter.Value());
+    }
   }
 }
 
@@ -221,38 +215,6 @@ void AIS_TexturedShape::SetColor (const Quantity_Color& theColor)
 void AIS_TexturedShape::UnsetColor()
 {
   AIS_Shape::UnsetColor();
-
-  for (Standard_Integer aPrsIt = 1; aPrsIt <= Presentations().Length(); ++aPrsIt)
-  {
-    const PrsMgr_ModedPresentation& aPrsModed = Presentations().Value (aPrsIt);
-
-    if (aPrsModed.Mode() != 3)
-      continue;
-    
-    Handle(Prs3d_Presentation) aPrs = aPrsModed.Presentation()->Presentation();
-    Handle(Graphic3d_Group)    aGroup = Prs3d_Root::CurrentGroup (aPrs);
-
-    Handle(Graphic3d_AspectFillArea3d) anAreaAsp = myDrawer->Link()->ShadingAspect()->Aspect();
-    Handle(Graphic3d_AspectLine3d)     aLineAsp  = myDrawer->Link()->LineAspect()->Aspect();
-    Quantity_Color aColor;
-    AIS_GraphicTool::GetInteriorColor (myDrawer->Link(), aColor);
-    anAreaAsp->SetInteriorColor (aColor);
-    aPrs->SetPrimitivesAspect (anAreaAsp);
-    aPrs->SetPrimitivesAspect (aLineAsp);
-    // Check if aspect of given type is set for the group, 
-    // because setting aspect for group with no already set aspect
-    // can lead to loss of presentation data
-    if (aGroup->IsGroupPrimitivesAspectSet (Graphic3d_ASPECT_FILL_AREA))
-    {
-      aGroup->SetGroupPrimitivesAspect (anAreaAsp);
-    }
-    if (aGroup->IsGroupPrimitivesAspectSet (Graphic3d_ASPECT_LINE))
-    {
-      aGroup->SetGroupPrimitivesAspect (aLineAsp);
-    }
-
-    updateAttributes (aPrs);
-  }
 }
 
 //=======================================================================
@@ -263,15 +225,12 @@ void AIS_TexturedShape::UnsetColor()
 void AIS_TexturedShape::SetMaterial (const Graphic3d_MaterialAspect& theMat)
 {
   AIS_Shape::SetMaterial (theMat);
-
-  for (Standard_Integer aPrsIt = 1; aPrsIt <= Presentations().Length(); ++aPrsIt)
+  for (PrsMgr_Presentations::Iterator aPrsIter (Presentations()); aPrsIter.More(); aPrsIter.Next())
   {
-    const PrsMgr_ModedPresentation& aPrsModed = Presentations().Value (aPrsIt);
-    
-    if (aPrsModed.Mode() != 3)
-      continue;
-    
-    updateAttributes (aPrsModed.Presentation()->Presentation());
+    if (aPrsIter.Value()->Mode() == 3)
+    {
+      updateAttributes (aPrsIter.Value());
+    }
   }
 }
 
@@ -282,15 +241,12 @@ void AIS_TexturedShape::SetMaterial (const Graphic3d_MaterialAspect& theMat)
 void AIS_TexturedShape::UnsetMaterial()
 {
   AIS_Shape::UnsetMaterial();
-
-  for (Standard_Integer aPrsIt = 1; aPrsIt <= Presentations().Length(); ++aPrsIt)
+  for (PrsMgr_Presentations::Iterator aPrsIter (Presentations()); aPrsIter.More(); aPrsIter.Next())
   {
-    const PrsMgr_ModedPresentation& aPrsModed = Presentations().Value (aPrsIt);
-
-    if (aPrsModed.Mode() != 3)
-      continue;
-
-    updateAttributes (aPrsModed.Presentation()->Presentation());
+    if (aPrsIter.Value()->Mode() == 3)
+    {
+      updateAttributes (aPrsIter.Value());
+    }
   }
 }
 
@@ -314,72 +270,75 @@ void AIS_TexturedShape::updateAttributes (const Handle(Prs3d_Presentation)& theP
   myAspect = new Graphic3d_AspectFillArea3d (*myDrawer->ShadingAspect()->Aspect());
   if (HasPolygonOffsets())
   {
-    // Issue 23115: copy polygon offset settings passed through myDrawer
     Standard_Integer aMode;
     Standard_ShortReal aFactor, aUnits;
     PolygonOffsets (aMode, aFactor, aUnits);
     myAspect->SetPolygonOffsets (aMode, aFactor, aUnits);
   }
 
-  if (!myToMapTexture)
+  Standard_Boolean hasTexture = Standard_False;
+  if (myToMapTexture)
   {
-    myAspect->SetTextureMapOff();
-    return;
-  }
+    TCollection_AsciiString aTextureDesc;
+    if (!myTexturePixMap.IsNull())
+    {
+      myTexture = new Graphic3d_Texture2Dmanual (myTexturePixMap);
+      aTextureDesc = " (custom image)";
+    }
+    else if (myPredefTexture != Graphic3d_NOT_2D_UNKNOWN)
+    {
+      myTexture = new Graphic3d_Texture2Dmanual (myPredefTexture);
+      aTextureDesc = TCollection_AsciiString(" (predefined texture ") + myTexture->GetId() + ")";
+    }
+    else
+    {
+      myTexture = new Graphic3d_Texture2Dmanual (myTextureFile.ToCString());
+      aTextureDesc = TCollection_AsciiString(" (") + myTextureFile + ")";
+    }
 
-  if (!myTexturePixMap.IsNull())
-  {
-    myTexture = new Graphic3d_Texture2Dmanual (myTexturePixMap);
+    if (myModulate)
+    {
+      myTexture->EnableModulate();
+    }
+    else
+    {
+      myTexture->DisableModulate();
+    }
+
+    if (myTexture->IsDone())
+    {
+      hasTexture = Standard_True;
+    }
+    else
+    {
+      Message::SendFail (TCollection_AsciiString ("Error: texture can not be loaded ") + aTextureDesc);
+    }
   }
-  else if (myPredefTexture != Graphic3d_NOT_2D_UNKNOWN)
+
+  myAspect->SetTextureMap (myTexture);
+  if (hasTexture)
   {
-    myTexture = new Graphic3d_Texture2Dmanual (myPredefTexture);
+    myAspect->SetTextureMapOn();
   }
   else
   {
-    myTexture = new Graphic3d_Texture2Dmanual (myTextureFile.ToCString());
-  }
-
-  myAspect->SetTextureMapOn();
-  myAspect->SetTextureMap (myTexture);
-  if (!myTexture->IsDone())
-  {
-#ifdef AIS_DEB
-    std::cout << "An error occurred while building texture\n";
-#endif
     myAspect->SetTextureMapOff();
-    return;
   }
 
-  if (myModulate)
-    myTexture->EnableModulate();
-  else
-    myTexture->DisableModulate();
-
   if (myToShowTriangles)
-    myAspect->SetEdgeOn();
-  else
-    myAspect->SetEdgeOff();
-
-  // manage back face culling in consistent way (as in StdPrs_ShadedShape::Add())
-  if (StdPrs_ToolShadedShape::IsClosed (myshape))
   {
-    myAspect->SuppressBackFace();
+    myAspect->SetEdgeOn();
   }
   else
   {
-    myAspect->AllowBackFace();
+    myAspect->SetEdgeOff();
   }
 
   // Go through all groups to change fill aspect for all primitives
   for (Graphic3d_SequenceOfGroup::Iterator aGroupIt (thePrs->Groups()); aGroupIt.More(); aGroupIt.Next())
   {
     const Handle(Graphic3d_Group)& aGroup = aGroupIt.Value();
-
-    if (aGroup->IsGroupPrimitivesAspectSet (Graphic3d_ASPECT_FILL_AREA))
-    {
-      aGroup->SetGroupPrimitivesAspect (myAspect);
-    }
+    aGroup->SetGroupPrimitivesAspect (myAspect);
   }
 }
 
@@ -392,8 +351,6 @@ void AIS_TexturedShape::Compute (const Handle(PrsMgr_PresentationManager3d)& /*t
                                  const Handle(Prs3d_Presentation)&           thePrs,
                                  const Standard_Integer                      theMode)
 {
-  thePrs->Clear();
-
   if (myshape.IsNull())
   {
     return;
@@ -423,34 +380,23 @@ void AIS_TexturedShape::Compute (const Handle(PrsMgr_PresentationManager3d)& /*t
   {
     case AIS_WireFrame:
     {
-      StdPrs_WFDeflectionShape::Add (thePrs, myshape, myDrawer);
+      StdPrs_ToolTriangulatedShape::ClearOnOwnDeflectionChange (myshape, myDrawer, Standard_True);
+      StdPrs_WFShape::Add (thePrs, myshape, myDrawer);
       break;
     }
     case AIS_Shaded:
     case 3: // texture mapping on triangulation
     {
-      Standard_Real prevangle;
-      Standard_Real newangle;
-      Standard_Real prevcoeff;
-      Standard_Real newcoeff;
-
-      Standard_Boolean isOwnDeviationAngle       = OwnDeviationAngle(newangle,prevangle);
-      Standard_Boolean isOwnDeviationCoefficient = OwnDeviationCoefficient(newcoeff,prevcoeff);
-      if (((Abs (newangle - prevangle) > Precision::Angular()) && isOwnDeviationAngle) ||
-          ((Abs (newcoeff - prevcoeff) > Precision::Confusion()) && isOwnDeviationCoefficient)) {
-        BRepTools::Clean (myshape);
-      }
-
+      StdPrs_ToolTriangulatedShape::ClearOnOwnDeflectionChange (myshape, myDrawer, Standard_True);
       if (myshape.ShapeType() > TopAbs_FACE)
       {
-        StdPrs_WFDeflectionShape::Add (thePrs, myshape, myDrawer);
+        StdPrs_WFShape::Add (thePrs, myshape, myDrawer);
         break;
       }
 
-      myDrawer->SetShadingAspectGlobal (Standard_False);
       if (IsInfinite())
       {
-        StdPrs_WFDeflectionShape::Add (thePrs, myshape, myDrawer);
+        StdPrs_WFShape::Add (thePrs, myshape, myDrawer);
         break;
       }
       try
@@ -470,9 +416,9 @@ void AIS_TexturedShape::Compute (const Handle(PrsMgr_PresentationManager3d)& /*t
           updateAttributes (thePrs);
         }
       }
-      catch (Standard_Failure)
+      catch (Standard_Failure const&)
       {
-#ifdef AIS_DEB
+#ifdef OCCT_DEBUG
         std::cout << "AIS_TexturedShape::Compute() in ShadingMode failed \n";
 #endif
         StdPrs_WFShape::Add (thePrs, myshape, myDrawer);
@@ -483,11 +429,11 @@ void AIS_TexturedShape::Compute (const Handle(PrsMgr_PresentationManager3d)& /*t
     {
       if (IsInfinite())
       {
-        StdPrs_WFDeflectionShape::Add (thePrs, myshape, myDrawer);
+        StdPrs_WFShape::Add (thePrs, myshape, myDrawer);
       }
       else
       {
-        StdPrs_WFDeflectionRestrictedFace::AddBox (thePrs, BoundingBox(), myDrawer);
+        Prs3d_BndBox::Add (thePrs, BoundingBox(), myDrawer);
       }
       break;
     }