Skip to content

Commit e2262bd

Browse files
committed
Colors not working in dm1-id-214.stp #23
1 parent 36b89cc commit e2262bd

File tree

4 files changed

+69
-32
lines changed

4 files changed

+69
-32
lines changed

dist/occt-import-js.wasm

21 Bytes
Binary file not shown.

occt-import-js/src/importer-xcaf.cpp

Lines changed: 60 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,13 @@
1111
#include <BRep_Tool.hxx>
1212
#include <XCAFDoc_DocumentTool.hxx>
1313

14-
static std::string GetLabelName (const TDF_Label& label)
14+
static std::string GetLabelNameNoRef (const TDF_Label& label)
1515
{
1616
Handle (TDataStd_Name) nameAttribute = new TDataStd_Name ();
1717
if (!label.FindAttribute (nameAttribute->GetID (), nameAttribute)) {
1818
return std::string ();
1919
}
20+
2021
Standard_Integer utf8NameLength = nameAttribute->Get ().LengthOfCString ();
2122
char* nameBuf = new char[utf8NameLength + 1];
2223
nameAttribute->Get ().ToUTF8CString (nameBuf);
@@ -25,20 +26,14 @@ static std::string GetLabelName (const TDF_Label& label)
2526
return name;
2627
}
2728

28-
static std::string GetLabelName (const Handle (XCAFDoc_ShapeTool)& shapeTool, const TDF_Label& label)
29+
static std::string GetLabelName (const TDF_Label& label, const Handle (XCAFDoc_ShapeTool)& shapeTool)
2930
{
3031
if (XCAFDoc_ShapeTool::IsReference (label)) {
31-
TDF_Label referredShape;
32-
shapeTool->GetReferredShape (label, referredShape);
33-
return GetLabelName (shapeTool, referredShape);
32+
TDF_Label referredShapeLabel;
33+
shapeTool->GetReferredShape (label, referredShapeLabel);
34+
return GetLabelName (referredShapeLabel, shapeTool);
3435
}
35-
return GetLabelName (label);
36-
}
37-
38-
static bool IsFreeShape (const TDF_Label& label, const Handle (XCAFDoc_ShapeTool)& shapeTool)
39-
{
40-
TopoDS_Shape tmpShape;
41-
return shapeTool->GetShape (label, tmpShape) && shapeTool->IsFree (label);
36+
return GetLabelNameNoRef (label);
4237
}
4338

4439
static std::string GetShapeName (const TopoDS_Shape& shape, const Handle (XCAFDoc_ShapeTool)& shapeTool)
@@ -47,43 +42,76 @@ static std::string GetShapeName (const TopoDS_Shape& shape, const Handle (XCAFDo
4742
if (!shapeTool->Search (shape, shapeLabel)) {
4843
return std::string ();
4944
}
50-
return GetLabelName (shapeTool, shapeLabel);
45+
return GetLabelName (shapeLabel, shapeTool);
5146
}
5247

53-
static bool GetShapeColor (const TopoDS_Shape& shape, const Handle (XCAFDoc_ColorTool)& colorTool, Color& color)
48+
static bool GetLabelColorNoRef (const TDF_Label& label, const Handle (XCAFDoc_ColorTool)& colorTool, Color& color)
5449
{
50+
static const std::vector<XCAFDoc_ColorType> colorTypes = {
51+
XCAFDoc_ColorSurf,
52+
XCAFDoc_ColorCurv,
53+
XCAFDoc_ColorGen
54+
};
55+
5556
Quantity_Color qColor;
56-
if (colorTool->GetColor (shape, XCAFDoc_ColorSurf, qColor)) {
57-
color = Color (qColor.Red (), qColor.Green (), qColor.Blue ());
58-
return true;
57+
for (XCAFDoc_ColorType colorType : colorTypes) {
58+
if (colorTool->GetColor (label, colorType, qColor)) {
59+
color = Color (qColor.Red (), qColor.Green (), qColor.Blue ());
60+
return true;
61+
}
5962
}
60-
if (colorTool->GetColor (shape, XCAFDoc_ColorCurv, qColor)) {
61-
color = Color (qColor.Red (), qColor.Green (), qColor.Blue ());
63+
64+
return false;
65+
}
66+
67+
static bool GetLabelColor (const TDF_Label& label, const Handle (XCAFDoc_ShapeTool)& shapeTool, const Handle (XCAFDoc_ColorTool)& colorTool, Color& color)
68+
{
69+
if (GetLabelColorNoRef (label, colorTool, color)) {
6270
return true;
6371
}
64-
if (colorTool->GetColor (shape, XCAFDoc_ColorGen, qColor)) {
65-
color = Color (qColor.Red (), qColor.Green (), qColor.Blue ());
66-
return true;
72+
73+
if (XCAFDoc_ShapeTool::IsReference (label)) {
74+
TDF_Label referredShape;
75+
shapeTool->GetReferredShape (label, referredShape);
76+
return GetLabelColor (referredShape, shapeTool, colorTool, color);
6777
}
78+
6879
return false;
6980
}
7081

82+
static bool GetShapeColor (const TopoDS_Shape& shape, const Handle (XCAFDoc_ShapeTool)& shapeTool, const Handle (XCAFDoc_ColorTool)& colorTool, Color& color)
83+
{
84+
TDF_Label shapeLabel;
85+
if (!shapeTool->Search (shape, shapeLabel)) {
86+
return false;
87+
}
88+
return GetLabelColor (shapeLabel, shapeTool, colorTool, color);
89+
}
90+
91+
static bool IsFreeShape (const TDF_Label& label, const Handle (XCAFDoc_ShapeTool)& shapeTool)
92+
{
93+
TopoDS_Shape tmpShape;
94+
return shapeTool->GetShape (label, tmpShape) && shapeTool->IsFree (label);
95+
}
96+
7197
class XcafFace : public OcctFace
7298
{
7399
public:
74-
XcafFace (const TopoDS_Face& face, const Handle (XCAFDoc_ColorTool)& colorTool) :
100+
XcafFace (const TopoDS_Face& face, const Handle (XCAFDoc_ShapeTool)& shapeTool, const Handle (XCAFDoc_ColorTool)& colorTool) :
75101
OcctFace (face),
102+
shapeTool (shapeTool),
76103
colorTool (colorTool)
77104
{
78105

79106
}
80107

81108
virtual bool GetColor (Color& color) const override
82109
{
83-
return GetShapeColor ((const TopoDS_Shape&) face, colorTool, color);
110+
return GetShapeColor ((const TopoDS_Shape&) face, shapeTool, colorTool, color);
84111
}
85112

86113
private:
114+
const Handle (XCAFDoc_ShapeTool)& shapeTool;
87115
const Handle (XCAFDoc_ColorTool)& colorTool;
88116
};
89117

@@ -106,14 +134,14 @@ class XcafShapeMesh : public Mesh
106134

107135
virtual bool GetColor (Color& color) const override
108136
{
109-
return GetShapeColor (shape, colorTool, color);
137+
return GetShapeColor (shape, shapeTool, colorTool, color);
110138
}
111139

112140
virtual void EnumerateFaces (const std::function<void (const Face& face)>& onFace) const override
113141
{
114142
for (TopExp_Explorer ex (shape, TopAbs_FACE); ex.More (); ex.Next ()) {
115143
const TopoDS_Face& face = TopoDS::Face (ex.Current ());
116-
XcafFace outputFace (face, colorTool);
144+
XcafFace outputFace (face, shapeTool, colorTool);
117145
onFace (outputFace);
118146
}
119147
}
@@ -127,9 +155,10 @@ class XcafShapeMesh : public Mesh
127155
class XcafStandaloneFacesMesh : public Mesh
128156
{
129157
public:
130-
XcafStandaloneFacesMesh (const TopoDS_Shape& shape, const Handle (XCAFDoc_ColorTool)& colorTool) :
158+
XcafStandaloneFacesMesh (const TopoDS_Shape& shape, const Handle (XCAFDoc_ShapeTool)& shapeTool, const Handle (XCAFDoc_ColorTool)& colorTool) :
131159
Mesh (),
132160
shape (shape),
161+
shapeTool (shapeTool),
133162
colorTool (colorTool)
134163
{
135164

@@ -155,13 +184,14 @@ class XcafStandaloneFacesMesh : public Mesh
155184
{
156185
for (TopExp_Explorer ex (shape, TopAbs_FACE, TopAbs_SHELL); ex.More (); ex.Next ()) {
157186
const TopoDS_Face& face = TopoDS::Face (ex.Current ());
158-
XcafFace outputFace (face, colorTool);
187+
XcafFace outputFace (face, shapeTool, colorTool);
159188
onFace (outputFace);
160189
}
161190
}
162191

163192
private:
164193
const TopoDS_Shape& shape;
194+
const Handle (XCAFDoc_ShapeTool)& shapeTool;
165195
const Handle (XCAFDoc_ColorTool)& colorTool;
166196
};
167197

@@ -178,7 +208,7 @@ class XcafNode : public Node
178208

179209
virtual std::string GetName () const override
180210
{
181-
return GetLabelName (shapeTool, label);
211+
return GetLabelName (label, shapeTool);
182212
}
183213

184214
virtual std::vector<NodePtr> GetChildren () const override
@@ -263,7 +293,7 @@ class XcafNode : public Node
263293
}
264294

265295
// Create a mesh from faces that are not part of a shell
266-
XcafStandaloneFacesMesh standaloneFacesMesh (shape, colorTool);
296+
XcafStandaloneFacesMesh standaloneFacesMesh (shape, shapeTool, colorTool);
267297
if (standaloneFacesMesh.HasFaces ()) {
268298
onMesh (standaloneFacesMesh);
269299
}

occt-import-js/src/js-interface.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ static TriangulationParams GetTriangulationParams (const emscripten::val& params
182182

183183
if (paramsVal.hasOwnProperty ("angularDeflection")) {
184184
emscripten::val angularDeflection = paramsVal["angularDeflection"];
185-
params.angularDeflection = angularDeflection.as<double> ();;
185+
params.angularDeflection = angularDeflection.as<double> ();
186186
params.automatic = false;
187187
}
188188

@@ -210,7 +210,7 @@ emscripten::val ReadBrepFile (const emscripten::val& content, const emscripten::
210210
return ReadFile (importer, content, triParams);
211211
}
212212

213-
EMSCRIPTEN_BINDINGS (assimpjs)
213+
EMSCRIPTEN_BINDINGS (occtimportjs)
214214
{
215215
emscripten::function<emscripten::val, const emscripten::val&, const emscripten::val&> ("ReadStepFile", &ReadStepFile);
216216
emscripten::function<emscripten::val, const emscripten::val&, const emscripten::val&> ("ReadIgesFile", &ReadIgesFile);

test/test.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,13 @@ it ('dm1-id-214', function () {
145145
let result = LoadStepFile ('./test/testfiles/cax-if/dm1-id-214.stp');
146146
assert (result.success);
147147
assert.strictEqual (result.meshes.length, 7);
148+
assert.deepStrictEqual (result.meshes[0].color, [ 1.0, 0.0, 0.0 ]);
149+
assert.deepStrictEqual (result.meshes[1].color, [ 1.0, 0.39312317967414856, 0.0 ]);
150+
assert.deepStrictEqual (result.meshes[2].color, [ 1.0, 0.39312317967414856, 0.0 ]);
151+
assert.deepStrictEqual (result.meshes[3].color, [ 1.0, 0.39312317967414856, 0.0 ]);
152+
assert.deepStrictEqual (result.meshes[4].color, [ 1.0, 0.08898153156042099, 0.0 ]);
153+
assert.deepStrictEqual (result.meshes[5].color, [ 1.0, 0.08898153156042099, 0.0 ]);
154+
assert.deepStrictEqual (result.meshes[6].color, [ 1.0, 0.08898153156042099, 0.0 ]);
148155
});
149156

150157
it ('sg1-c5-214', function () {

0 commit comments

Comments
 (0)