Import: fix STEP color problem caused by OCC 7.5

Fixes #0004744
This commit is contained in:
Zheng, Lei
2021-10-07 14:23:46 +08:00
parent bea2fff850
commit c19e1398f2
2 changed files with 39 additions and 18 deletions

View File

@@ -95,6 +95,12 @@
#include <App/DocumentObject.h> #include <App/DocumentObject.h>
#include <App/DocumentObjectGroup.h> #include <App/DocumentObjectGroup.h>
#if OCC_VERSION_HEX >= 0x070500
// See https://dev.opencascade.org/content/occt-3d-viewer-becomes-srgb-aware
# define OCC_COLOR_SPACE Quantity_TOC_sRGB
#else
# define OCC_COLOR_SPACE Quantity_TOC_RGB
#endif
using namespace Import; using namespace Import;
@@ -321,7 +327,7 @@ int ExportOCAF::saveShape(Part::Feature* part, const std::vector<App::Color>& co
mat[0] = color.r; mat[0] = color.r;
mat[1] = color.g; mat[1] = color.g;
mat[2] = color.b; mat[2] = color.b;
col.SetValues(mat[0],mat[1],mat[2],Quantity_TOC_RGB); col.SetValues(mat[0],mat[1],mat[2],OCC_COLOR_SPACE);
aColorTool->SetColor(faceLabel, col, XCAFDoc_ColorSurf); aColorTool->SetColor(faceLabel, col, XCAFDoc_ColorSurf);
} }
} }
@@ -334,7 +340,7 @@ int ExportOCAF::saveShape(Part::Feature* part, const std::vector<App::Color>& co
mat[0] = color.r; mat[0] = color.r;
mat[1] = color.g; mat[1] = color.g;
mat[2] = color.b; mat[2] = color.b;
col.SetValues(mat[0],mat[1],mat[2],Quantity_TOC_RGB); col.SetValues(mat[0],mat[1],mat[2],OCC_COLOR_SPACE);
aColorTool->SetColor(shapeLabel, col, XCAFDoc_ColorGen); aColorTool->SetColor(shapeLabel, col, XCAFDoc_ColorGen);
} }
@@ -437,7 +443,7 @@ void ExportOCAF::reallocateFreeShape(std::vector <App::DocumentObject*> hierarch
mat[0] = color.r; mat[0] = color.r;
mat[1] = color.g; mat[1] = color.g;
mat[2] = color.b; mat[2] = color.b;
col.SetValues(mat[0],mat[1],mat[2],Quantity_TOC_RGB); col.SetValues(mat[0],mat[1],mat[2],OCC_COLOR_SPACE);
aColorTool->SetColor(faceLabel, col, XCAFDoc_ColorSurf); aColorTool->SetColor(faceLabel, col, XCAFDoc_ColorSurf);
} }
} }
@@ -451,7 +457,7 @@ void ExportOCAF::reallocateFreeShape(std::vector <App::DocumentObject*> hierarch
mat[0] = color.r; mat[0] = color.r;
mat[1] = color.g; mat[1] = color.g;
mat[2] = color.b; mat[2] = color.b;
col.SetValues(mat[0],mat[1],mat[2],Quantity_TOC_RGB); col.SetValues(mat[0],mat[1],mat[2],OCC_COLOR_SPACE);
aColorTool->SetColor(label, col, XCAFDoc_ColorGen); aColorTool->SetColor(label, col, XCAFDoc_ColorGen);
} }
} }

View File

@@ -71,12 +71,31 @@
#include <App/DocumentObject.h> #include <App/DocumentObject.h>
#include <App/DocumentObjectGroup.h> #include <App/DocumentObjectGroup.h>
#if OCC_VERSION_HEX >= 0x070500
// See https://dev.opencascade.org/content/occt-3d-viewer-becomes-srgb-aware
# define OCC_COLOR_SPACE Quantity_TOC_sRGB
#else
# define OCC_COLOR_SPACE Quantity_TOC_RGB
#endif
FC_LOG_LEVEL_INIT("Import",true,true) FC_LOG_LEVEL_INIT("Import",true,true)
using namespace Import; using namespace Import;
///////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////
static inline App::Color convertColor(const Quantity_Color &c)
{
Standard_Real r, g, b;
c.Values(r, g, b, OCC_COLOR_SPACE);
return App::Color(static_cast<float>(r), static_cast<float>(g), static_cast<float>(b));
}
static inline Quantity_Color convertColor(const App::Color &c)
{
return Quantity_Color(c.r, c.g, c.b, OCC_COLOR_SPACE);
}
static std::string labelName(TDF_Label label) { static std::string labelName(TDF_Label label) {
std::string txt; std::string txt;
Handle(TDataStd_Name) name; Handle(TDataStd_Name) name;
@@ -229,12 +248,11 @@ void ImportOCAF2::setObjectName(Info &info, TDF_Label label) {
} }
} }
bool ImportOCAF2::getColor(const TopoDS_Shape &shape, Info &info, bool check, bool noDefault) { bool ImportOCAF2::getColor(const TopoDS_Shape &shape, Info &info, bool check, bool noDefault) {
bool ret = false; bool ret = false;
Quantity_Color aColor; Quantity_Color aColor;
if(aColorTool->GetColor(shape, XCAFDoc_ColorSurf, aColor)) { if(aColorTool->GetColor(shape, XCAFDoc_ColorSurf, aColor)) {
App::Color c(aColor.Red(),aColor.Green(),aColor.Blue()); App::Color c = convertColor(aColor);
if(!check || info.faceColor!=c) { if(!check || info.faceColor!=c) {
info.faceColor = c; info.faceColor = c;
info.hasFaceColor = true; info.hasFaceColor = true;
@@ -242,7 +260,7 @@ bool ImportOCAF2::getColor(const TopoDS_Shape &shape, Info &info, bool check, bo
} }
} }
if(!noDefault && !info.hasFaceColor && aColorTool->GetColor(shape, XCAFDoc_ColorGen, aColor)) { if(!noDefault && !info.hasFaceColor && aColorTool->GetColor(shape, XCAFDoc_ColorGen, aColor)) {
App::Color c(aColor.Red(),aColor.Green(),aColor.Blue()); App::Color c = convertColor(aColor);
if(!check || info.faceColor!=c) { if(!check || info.faceColor!=c) {
info.faceColor = c; info.faceColor = c;
info.hasFaceColor = true; info.hasFaceColor = true;
@@ -250,7 +268,7 @@ bool ImportOCAF2::getColor(const TopoDS_Shape &shape, Info &info, bool check, bo
} }
} }
if(aColorTool->GetColor(shape, XCAFDoc_ColorCurv, aColor)) { if(aColorTool->GetColor(shape, XCAFDoc_ColorCurv, aColor)) {
App::Color c(aColor.Red(),aColor.Green(),aColor.Blue()); App::Color c = convertColor(aColor);
// Some STEP include a curve color with the same value of the face // Some STEP include a curve color with the same value of the face
// color. And this will look weird in FC. So for shape with face // color. And this will look weird in FC. So for shape with face
// we'll ignore the curve color, if it is the same as the face color. // we'll ignore the curve color, if it is the same as the face color.
@@ -369,11 +387,11 @@ bool ImportOCAF2::createObject(App::Document *doc, TDF_Label label,
if(aColorTool->GetColor(l, XCAFDoc_ColorSurf, aColor) || if(aColorTool->GetColor(l, XCAFDoc_ColorSurf, aColor) ||
aColorTool->GetColor(l, XCAFDoc_ColorGen, aColor)) aColorTool->GetColor(l, XCAFDoc_ColorGen, aColor))
{ {
faceColor = App::Color(aColor.Red(),aColor.Green(),aColor.Blue()); faceColor = convertColor(aColor);
foundFaceColor = true; foundFaceColor = true;
} }
if(aColorTool->GetColor(l, XCAFDoc_ColorCurv, aColor)) { if(aColorTool->GetColor(l, XCAFDoc_ColorCurv, aColor)) {
edgeColor = App::Color(aColor.Red(),aColor.Green(),aColor.Blue()); edgeColor = convertColor(aColor);
foundEdgeColor = true; foundEdgeColor = true;
if(j==0 && foundFaceColor && faceColors.size() && edgeColor==faceColor) { if(j==0 && foundFaceColor && faceColors.size() && edgeColor==faceColor) {
// Do not set edge the same color as face // Do not set edge the same color as face
@@ -652,7 +670,7 @@ void ImportOCAF2::getSHUOColors(TDF_Label label,
if(aColorTool->GetColor(slabel, XCAFDoc_ColorSurf, aColor) || if(aColorTool->GetColor(slabel, XCAFDoc_ColorSurf, aColor) ||
aColorTool->GetColor(slabel, XCAFDoc_ColorGen, aColor)) aColorTool->GetColor(slabel, XCAFDoc_ColorGen, aColor))
{ {
colors.emplace(subname,App::Color(aColor.Red(),aColor.Green(),aColor.Blue())); colors.emplace(subname,convertColor(aColor));
} }
} }
} }
@@ -784,10 +802,7 @@ bool ImportOCAF2::createAssembly(App::Document *_doc,
childInfo.plas.emplace_back(Part::TopoShape::convert(childShape.Location().Transformation())); childInfo.plas.emplace_back(Part::TopoShape::convert(childShape.Location().Transformation()));
Quantity_Color aColor; Quantity_Color aColor;
if (aColorTool->GetColor(childShape, XCAFDoc_ColorSurf, aColor)) { if (aColorTool->GetColor(childShape, XCAFDoc_ColorSurf, aColor)) {
auto &color = childInfo.colors[childInfo.plas.size()-1]; childInfo.colors[childInfo.plas.size()-1] = convertColor(aColor);
color.r = (float)aColor.Red();
color.g = (float)aColor.Green();
color.b = (float)aColor.Blue();
} }
} }
assert(visibilities.size() == children.size()); assert(visibilities.size() == children.size());
@@ -1036,7 +1051,7 @@ void ExportOCAF2::setupObject(TDF_Label label, App::DocumentObject *obj,
continue; continue;
} }
const App::Color& c = vv.second; const App::Color& c = vv.second;
Quantity_Color color(c.r,c.g,c.b,Quantity_TOC_RGB); Quantity_Color color = convertColor(c);
auto colorType = vv.first[0]=='F'?XCAFDoc_ColorSurf:XCAFDoc_ColorCurv; auto colorType = vv.first[0]=='F'?XCAFDoc_ColorSurf:XCAFDoc_ColorCurv;
if(vv.first=="Face" || vv.first=="Edge") { if(vv.first=="Face" || vv.first=="Edge") {
aColorTool->SetColor(nodeLabel, color, colorType); aColorTool->SetColor(nodeLabel, color, colorType);
@@ -1277,7 +1292,7 @@ TDF_Label ExportOCAF2::exportObject(App::DocumentObject* parentObj,
// Work around OCCT bug. If no color setting here, it will crash. // Work around OCCT bug. If no color setting here, it will crash.
// The culprit is at STEPCAFControl_Writer::1093 as shown below // The culprit is at STEPCAFControl_Writer::1093 as shown below
// //
// surfColor = Styles.EncodeColor(Quantity_Color(1,1,1,Quantity_TOC_RGB),DPDCs,ColRGBs); // surfColor = Styles.EncodeColor(Quantity_Color(1,1,1,OCC_COLOR_SPACE),DPDCs,ColRGBs);
// PSA = Styles.MakeColorPSA ( item, surfColor, curvColor, isComponent ); // PSA = Styles.MakeColorPSA ( item, surfColor, curvColor, isComponent );
// if ( isComponent ) // if ( isComponent )
// setDefaultInstanceColor( override, PSA); // setDefaultInstanceColor( override, PSA);
@@ -1293,7 +1308,7 @@ TDF_Label ExportOCAF2::exportObject(App::DocumentObject* parentObj,
!aColorTool->GetInstanceColor(childShape,XCAFDoc_ColorCurv,col)) !aColorTool->GetInstanceColor(childShape,XCAFDoc_ColorCurv,col))
{ {
auto &c = defaultColor; auto &c = defaultColor;
aColorTool->SetColor(childLabel, Quantity_Color(c.r,c.g,c.b,Quantity_TOC_RGB), XCAFDoc_ColorGen); aColorTool->SetColor(childLabel, convertColor(c), XCAFDoc_ColorGen);
FC_WARN(labelName(childLabel) << " set default color"); FC_WARN(labelName(childLabel) << " set default color");
} }
aColorTool->SetVisibility(childLabel,Standard_False); aColorTool->SetVisibility(childLabel,Standard_False);