]> SALOME platform Git repositories - modules/shaper.git/blobdiff - src/PartSet/PartSet_Module.cpp
Salome HOME
Merge branch 'cadbld'
[modules/shaper.git] / src / PartSet / PartSet_Module.cpp
index 8a703fa0fd813e926275e89c47715180c84035d0..70935fa37dcf2b7ada4632aaeeb8182c8d87bebf 100644 (file)
@@ -82,6 +82,7 @@
 #include <ModelAPI_Tools.h>
 #include <ModelAPI_ResultConstruction.h>
 #include <ModelAPI_AttributeIntArray.h>
+#include <ModelAPI_AttributeImage.h>
 #include <ModelAPI_ResultGroup.h>
 #include <ModelAPI_ResultParameter.h>
 
 #include <SelectMgr_ListIteratorOfListOfFilter.hxx>
 #include <Graphic3d_Texture2Dmanual.hxx>
 
-#ifdef HAVE_SALOME
-#include <OCCViewer_Utilities.h>
-#endif
 
 #define FEATURE_ITEM_COLOR "0,0,225"
 
@@ -1408,13 +1406,46 @@ double getResultTransparency(const ResultPtr& theResult)
   return aTransparency;
 }
 
+static AttributeImagePtr findImage(const ObjectPtr& theResult)
+{
+  AttributeImagePtr anImageAttr;
+
+  if (theResult.get()) {
+    ResultBodyPtr aResultBody =
+      std::dynamic_pointer_cast<ModelAPI_ResultBody>(theResult);
+    if (aResultBody.get()) {
+      anImageAttr = aResultBody->data()->image(ModelAPI_ResultBody::IMAGE_ID());
+      if (!anImageAttr.get() || !anImageAttr->hasTexture()) {
+        // try to find an image attribute in parents
+        ObjectPtr aParent = theResult->document()->parent(theResult);
+        anImageAttr = findImage(aParent);
+      }
+    }
+  }
+
+  return anImageAttr;
+}
+
 //******************************************************
-void PartSet_Module::setTexture(const std::string & theTextureFile, const AISObjectPtr& thePrs)
+void PartSet_Module::setTexture(const AISObjectPtr& thePrs,
+                                const ResultPtr& theResult)
 {
-#ifdef HAVE_SALOME
+  ResultBodyPtr aResultBody =
+    std::dynamic_pointer_cast<ModelAPI_ResultBody>(theResult);
+  if (!aResultBody.get())
+    return;
+
+  AttributeImagePtr anImageAttr = findImage(theResult);
+  if (!anImageAttr.get() || !anImageAttr->hasTexture())
+    return;
+
+  int aWidth, aHeight;
+  std::string aFormat;
+  std::list<unsigned char> aByteList;
+  anImageAttr->texture(aWidth, aHeight, aByteList, aFormat);
+
   Handle(AIS_InteractiveObject) anAIS = thePrs->impl<Handle(AIS_InteractiveObject)>();
-  if (!anAIS.IsNull())
-  {
+  if (!anAIS.IsNull()) {
     /// set color to white and change material aspect,
     /// in order to keep a natural apect of the image.
     thePrs->setColor(255, 255, 255);
@@ -1431,11 +1462,21 @@ void PartSet_Module::setTexture(const std::string & theTextureFile, const AISObj
       myDrawer->ShadingAspect()->Aspect()->SetFrontMaterial(aMatAspect);
       myDrawer->ShadingAspect()->Aspect()->SetBackMaterial(aMatAspect);
 
-      Handle(Image_PixMap) aPixmap;
-      QPixmap px(theTextureFile.c_str());
-
-      if (!px.isNull() )
-        aPixmap = OCCViewer_Utilities::imageToPixmap( px.toImage());
+      //aPixmap = OCCViewer_Utilities::imageToPixmap( px.toImage());
+      Handle(Image_PixMap) aPixmap = new Image_PixMap();
+      aPixmap->InitTrash(Image_PixMap::ImgBGRA, aWidth, aHeight);
+      std::list<unsigned char>::iterator aByteIter = aByteList.begin();
+      for (int aLine = 0; aLine < aHeight; ++aLine) {
+        // convert pixels from ARGB to renderer-compatible RGBA
+        for (int aByte = 0; aByte < aWidth; ++aByte) {
+          Image_ColorBGRA& aPixmapBytes = aPixmap->ChangeValue<Image_ColorBGRA>(aLine, aByte);
+
+          aPixmapBytes.b() = (Standard_Byte) *aByteIter++;
+          aPixmapBytes.g() = (Standard_Byte) *aByteIter++;
+          aPixmapBytes.r() = (Standard_Byte) *aByteIter++;
+          aPixmapBytes.a() = (Standard_Byte) *aByteIter++;
+        }
+      }
 
       anAISShape->Attributes()->ShadingAspect()->Aspect()->SetTextureMap
           (new Graphic3d_Texture2Dmanual(aPixmap));
@@ -1444,7 +1485,6 @@ void PartSet_Module::setTexture(const std::string & theTextureFile, const AISObj
       anAISShape->SetDisplayMode(AIS_Shaded);
     }
   }
-#endif
 }
 
 //******************************************************
@@ -1489,10 +1529,8 @@ void PartSet_Module::customizePresentation(const ObjectPtr& theObject,
       thePrs->setDeflection(getResultDeflection(aResult));
       thePrs->setTransparency(getResultTransparency(aResult));
 
-      /// set texture  parameters
-      if(aResult->hasTextureFile()) {
-        setTexture(aResult->getTextureFile(), thePrs);
-      }
+      /// set texture parameters, if any
+      setTexture(thePrs, aResult);
     }
     if (aFeature.get() && (aFeature->getKind() == SketchPlugin_Sketch::ID())) {
         thePrs->setWidth(2);
@@ -1574,11 +1612,29 @@ void PartSet_Module::addObjectBrowserMenu(QMenu* theMenu) const
 
       } else if (aObject->document() == aMgr->activeDocument()) {
         if (hasParameter || hasFeature) {
-          myMenuMgr->action("EDIT_CMD")->setEnabled(true);
-          theMenu->addAction(myMenuMgr->action("EDIT_CMD"));
-          if (aCurrentOp && aFeature.get()) {
-            if (aCurrentOp->id().toStdString() == aFeature->getKind())
+
+          // disable Edit menu for groups under ImportResult feature
+          bool isEnabled = true;
+          if (aFeature.get() && aFeature->getKind() == "Group")
+          {
+            std::shared_ptr<ModelAPI_CompositeFeature> anOwner =
+              ModelAPI_Tools::compositeOwner (aFeature);
+
+            if (anOwner.get() && anOwner->getKind() == "ImportResult")
+            {
               myMenuMgr->action("EDIT_CMD")->setEnabled(false);
+              isEnabled = false;
+            }
+          }
+
+          if (isEnabled)
+          {
+            myMenuMgr->action("EDIT_CMD")->setEnabled(true);
+            theMenu->addAction(myMenuMgr->action("EDIT_CMD"));
+            if (aCurrentOp && aFeature.get()) {
+              if (aCurrentOp->id().toStdString() == aFeature->getKind())
+                myMenuMgr->action("EDIT_CMD")->setEnabled(false);
+            }
           }
         }
       }