0029902: Data Exchange, XCAF - provide extended Material definition for visualization...
[occt.git] / src / VrmlData / VrmlData_ShapeConvert.cxx
index 151d5f9..bf5d86f 100644 (file)
@@ -52,7 +52,9 @@
 #include <XCAFDoc_ColorTool.hxx>
 #include <XCAFDoc_DocumentTool.hxx>
 #include <XCAFDoc_ShapeTool.hxx>
-
+#include <XCAFPrs_Style.hxx>
+#include <XCAFDoc_VisMaterial.hxx>
+#include <XCAFDoc_VisMaterialTool.hxx>
 
 //=======================================================================
 //function : AddShape
@@ -546,11 +548,11 @@ Handle(VrmlData_Appearance) VrmlData_ShapeConvert::defaultMaterialFace () const
     const Handle(VrmlData_Material) aMaterial =
       new VrmlData_Material (myScene, 0L, 1.0, 0.022, 0.);
     aMaterial->SetDiffuseColor (Quantity_Color(0.780392, 0.568627, 0.113725,
-                                               Quantity_TOC_RGB));
+                                               Quantity_TOC_sRGB));
     aMaterial->SetEmissiveColor(Quantity_Color(0.329412, 0.223529, 0.027451,
-                                               Quantity_TOC_RGB));
+                                               Quantity_TOC_sRGB));
     aMaterial->SetSpecularColor(Quantity_Color(0.992157, 0.941176, 0.807843,
-                                               Quantity_TOC_RGB));
+                                               Quantity_TOC_sRGB));
     myScene.AddNode (aMaterial, Standard_False);
     anAppearance = new VrmlData_Appearance (myScene, aNodeName);
     anAppearance->SetMaterial (aMaterial);
@@ -597,6 +599,7 @@ void VrmlData_ShapeConvert::addShape (const Handle(VrmlData_Group)& theParent,
 {
   Handle(XCAFDoc_ShapeTool) aShapeTool = XCAFDoc_DocumentTool::ShapeTool(theDoc->Main());
   Handle(XCAFDoc_ColorTool) aColorTool = XCAFDoc_DocumentTool::ColorTool(theDoc->Main());
+  Handle(XCAFDoc_VisMaterialTool) aMatTool = XCAFDoc_DocumentTool::VisMaterialTool(theDoc->Main());
 
   NCollection_DataMap<TopoDS_Shape, TDF_Label> aChildShapeToLabels;
   TDF_LabelSequence aChildLabels;
@@ -661,26 +664,50 @@ void VrmlData_ShapeConvert::addShape (const Handle(VrmlData_Group)& theParent,
       }
 
       // set color
-      TDF_Label aColorL;
-      Standard_Boolean findColor = Standard_False;
-      const TDF_Label* aLabel = aChildShapeToLabels.Seek(anExp.Current());
-      if (aLabel != NULL)
+      XCAFPrs_Style aStyle;
+      Quantity_ColorRGBA aColor;
+      TDF_Label aLabel, anAttribLab;
+      if (aChildShapeToLabels.Find (anExp.Current(), aLabel))
       {
-        findColor = aColorTool->GetColor(*aLabel, XCAFDoc_ColorSurf, aColorL)
-          || aColorTool->GetColor(*aLabel, XCAFDoc_ColorGen, aColorL);
+        Handle(XCAFDoc_VisMaterial) aVisMat = aMatTool->GetShapeMaterial (aLabel);
+        if (!aVisMat.IsNull()
+         && !aVisMat->IsEmpty())
+        {
+          anAttribLab = aVisMat->Label();
+          aStyle.SetMaterial (aVisMat);
+        }
+        else if (aColorTool->GetColor (aLabel, XCAFDoc_ColorSurf, anAttribLab)
+              || aColorTool->GetColor (aLabel, XCAFDoc_ColorGen,  anAttribLab))
+        {
+          aColorTool->GetColor (anAttribLab, aColor);
+          aStyle.SetColorSurf (aColor);
+        }
       }
-      if (!findColor)
+      if (!aStyle.IsSetColorSurf()
+        && aStyle.Material().IsNull())
       {
-        findColor = aColorTool->GetColor(theLabel, XCAFDoc_ColorSurf, aColorL)
-          || aColorTool->GetColor(theLabel, XCAFDoc_ColorGen, aColorL);
+        Handle(XCAFDoc_VisMaterial) aVisMat = aMatTool->GetShapeMaterial (theLabel);
+        if (!aVisMat.IsNull()
+         && !aVisMat->IsEmpty())
+        {
+          anAttribLab = aVisMat->Label();
+          aStyle.SetMaterial (aVisMat);
+        }
+        if (aColorTool->GetColor (theLabel, XCAFDoc_ColorSurf, anAttribLab)
+         || aColorTool->GetColor (theLabel, XCAFDoc_ColorGen,  anAttribLab))
+        {
+          aColorTool->GetColor (anAttribLab, aColor);
+          aStyle.SetColorSurf (aColor);
+        }
       }
-      if (!findColor)
+      if (!aStyle.IsSetColorSurf()
+        && aStyle.Material().IsNull())
       {
         aShapeNode->SetAppearance(defaultMaterialFace());
       }
       else
       {
-        aShapeNode->SetAppearance(makeMaterialFromColor(aColorL, aColorTool));
+        aShapeNode->SetAppearance (makeMaterialFromStyle (aStyle, anAttribLab));
       }
 
       myScene.AddNode(aShapeNode, theParent.IsNull() && aGroup.IsNull());
@@ -877,22 +904,20 @@ void VrmlData_ShapeConvert::ConvertDocument(const Handle(TDocStd_Document) &theD
   }
 }
 
-
 //=======================================================================
-//function : makeMaterialFromColor
-//purpose  : 
+//function : makeMaterialFromStyle
+//purpose  :
 //=======================================================================
-
-Handle(VrmlData_Appearance) VrmlData_ShapeConvert::makeMaterialFromColor(
-  const TDF_Label& theColorL,
-  const Handle(XCAFDoc_ColorTool)& theColorTool) const
+Handle(VrmlData_Appearance) VrmlData_ShapeConvert::makeMaterialFromStyle (const XCAFPrs_Style& theStyle,
+                                                                          const TDF_Label& theAttribLab) const
 {
-  Quantity_ColorRGBA aColor;
-  theColorTool->GetColor(theColorL, aColor);
+  const Quantity_ColorRGBA aColor = !theStyle.Material().IsNull()
+                                   ? theStyle.Material()->BaseColor()
+                                   : theStyle.GetColorSurfRGBA();
 
   TCollection_AsciiString aNodeName = "_materialFace_";
   Handle(TDataStd_Name) aNameAttribute;
-  if (theColorL.FindAttribute(TDataStd_Name::GetID(), aNameAttribute))
+  if (theAttribLab.FindAttribute(TDataStd_Name::GetID(), aNameAttribute))
   {
     aNodeName.AssignCat(aNameAttribute->Get());
     Standard_Integer n = aNodeName.Search(" ");
@@ -903,26 +928,25 @@ Handle(VrmlData_Appearance) VrmlData_ShapeConvert::makeMaterialFromColor(
   }
   else
   {
-    aNodeName.AssignCat(aColor.GetRGB().Red());
+    NCollection_Vec3<Standard_Real> aColor_sRGB;
+    aColor.GetRGB().Values (aColor_sRGB.r(), aColor_sRGB.g(), aColor_sRGB.b(), Quantity_TOC_sRGB);
+    aNodeName.AssignCat(aColor_sRGB.r());
     aNodeName.AssignCat("_");
-    aNodeName.AssignCat(aColor.GetRGB().Green());
+    aNodeName.AssignCat(aColor_sRGB.g());
     aNodeName.AssignCat("_");
-    aNodeName.AssignCat(aColor.GetRGB().Blue());
+    aNodeName.AssignCat(aColor_sRGB.b());
   }
 
-  Handle(VrmlData_Appearance) anAppearance =
-    Handle(VrmlData_Appearance)::DownCast(myScene.FindNode(aNodeName.ToCString()));
-  if (anAppearance.IsNull()) {
-    const Handle(VrmlData_Material) aMaterial =
-      new VrmlData_Material(myScene, 0L);
-    aMaterial->SetDiffuseColor(aColor.GetRGB());
-    myScene.AddNode(aMaterial, Standard_False);
-    anAppearance = new VrmlData_Appearance(myScene, aNodeName.ToCString());
-    anAppearance->SetMaterial(aMaterial);
-    myScene.AddNode(anAppearance, Standard_False);
+  Handle(VrmlData_Appearance) anAppearance = Handle(VrmlData_Appearance)::DownCast(myScene.FindNode(aNodeName.ToCString()));
+  if (anAppearance.IsNull())
+  {
+    Handle(VrmlData_Material) aMaterial = new VrmlData_Material (myScene, 0L);
+    aMaterial->SetDiffuseColor (aColor.GetRGB());
+    myScene.AddNode (aMaterial, Standard_False);
+    anAppearance = new VrmlData_Appearance (myScene, aNodeName.ToCString());
+    anAppearance->SetMaterial (aMaterial);
+    myScene.AddNode (anAppearance, Standard_False);
   }
 
   return anAppearance;
 }
-
-