+//******************************************************
+double getResultDeflection(const ResultPtr& theResult)
+{
+ double aDeflection = ModelAPI_Tools::getDeflection(theResult);
+ if (aDeflection < 0)
+ aDeflection = PartSet_Tools::getDefaultDeflection(theResult);
+ return aDeflection;
+}
+
+//******************************************************
+double getResultTransparency(const ResultPtr& theResult)
+{
+ double aTransparency = ModelAPI_Tools::getTransparency(theResult);
+ if (aTransparency < 0)
+ aTransparency = PartSet_Tools::getDefaultTransparency();
+ 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 AISObjectPtr& thePrs,
+ const ResultPtr& theResult)
+{
+ 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()) {
+ /// set color to white and change material aspect,
+ /// in order to keep a natural apect of the image.
+ thePrs->setColor(255, 255, 255);
+ Quantity_Color myShadingColor(NCollection_Vec3<float>(1., 1., 1.));
+ Handle(AIS_Shape) anAISShape = Handle(AIS_Shape)::DownCast(anAIS);
+ if (!anAISShape.IsNull())
+ {
+ auto myDrawer = anAISShape->Attributes();
+
+ myDrawer->ShadingAspect()->SetColor(myShadingColor);
+ myDrawer->ShadingAspect()->Aspect()->SetDistinguishOn();
+ Graphic3d_MaterialAspect aMatAspect(Graphic3d_NOM_PLASTIC);
+ aMatAspect.SetTransparency(0.0);
+ myDrawer->ShadingAspect()->Aspect()->SetFrontMaterial(aMatAspect);
+ myDrawer->ShadingAspect()->Aspect()->SetBackMaterial(aMatAspect);
+
+ //aPixmap = OCCViewer_Utilities::imageToPixmap( px.toImage());
+ Handle(Image_PixMap) aPixmap = new Image_PixMap();
+#if OCC_VERSION_LARGE < 0x07070000
+ aPixmap->InitTrash(Image_PixMap::ImgBGRA, aWidth, aHeight);
+#else
+ aPixmap->InitTrash(Image_Format_BGRA, aWidth, aHeight);
+#endif
+ 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));
+ anAISShape->Attributes()->ShadingAspect()->Aspect()->SetTextureMapOn();
+
+ anAISShape->SetDisplayMode(AIS_Shaded);
+ }
+ }
+}
+
+//******************************************************
+void PartSet_Module::customizePresentation(const ObjectPtr& theObject,
+ const AISObjectPtr& thePrs) const
+{
+ if (mySketchMgr->isObjectOfSketch(theObject)) {
+ mySketchMgr->customizeSketchPresentation(theObject, thePrs);
+ }
+ else {
+ ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
+ FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
+ if (aResult.get()) {
+ std::vector<int> aColor;
+ bool isSameDoc = (ModelAPI_Session::get()->activeDocument() == aResult->document());
+ // Get user defined color for the object
+ ModelAPI_Tools::getColor(aResult, aColor);
+ if (isSameDoc) {
+ bool isCustomized = false;
+ if (aColor.empty() && aFeature.get()) {
+ GeomCustomPrsPtr aCustPrs = std::dynamic_pointer_cast<GeomAPI_ICustomPrs>(aFeature);
+ if (aCustPrs.get()) {
+ isCustomized = aCustPrs->customisePresentation(aResult, thePrs);
+ }
+ }
+ if (!isCustomized) {
+ if (aColor.empty()) {
+ PartSet_Tools::getDefaultColor(aResult, false, aColor);
+ }
+ thePrs->setColor(aColor[0], aColor[1], aColor[2]);
+ }
+ }
+ else {
+ if (aColor.empty()) {
+ PartSet_Tools::getDefaultColor(aResult, false, aColor);
+ }
+ QColor aQColor(aColor[0], aColor[1], aColor[2]);
+ QColor aNewColor =
+ QColor::fromHsvF(aQColor.hueF(), aQColor.saturationF() / 3., aQColor.valueF());
+ thePrs->setColor(aNewColor.red(), aNewColor.green(), aNewColor.blue());
+ }
+ thePrs->setDeflection(getResultDeflection(aResult));
+ thePrs->setTransparency(getResultTransparency(aResult));
+
+ /// set texture parameters, if any
+ setTexture(thePrs, aResult);
+ }
+ if (aFeature.get() && (aFeature->getKind() == SketchPlugin_Sketch::ID())) {
+ thePrs->setWidth(2);
+ }
+ }
+}
+
+