Skip to content

Commit 4996184

Browse files
authored
Merge pull request tpaviot#615 from tpaviot/review/fix-import-step-colors
Review/fix import step colors
2 parents 5812b02 + 68fc4f3 commit 4996184

4 files changed

Lines changed: 48 additions & 47 deletions

File tree

src/Display/WebGl/threejs_renderer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
from OCC.Extend.TopologyUtils import is_edge, is_wire, discretize_edge, discretize_wire
3131
from OCC.Display.WebGl.simple_server import start_server
3232

33-
THREEJS_RELEASE = "r99"
33+
THREEJS_RELEASE = "r100"
3434

3535
def spinning_cursor():
3636
while True:

src/Display/WebGl/x3dom_renderer.py

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -263,23 +263,19 @@ def to_x3dfile_string(self, shape_id):
263263
<Scene>
264264
""" % (OCC_VERSION, OCC_VERSION, OCC_VERSION)
265265
for triangle_set in self._triangle_sets:
266-
x3dfile_str += '<Shape DEF="shape%i" onclick="' % shape_id
266+
x3dfile_str += "<Switch whichChoice='0' id='swBRP'><Transform scale='1 1 1'><Shape DEF='shape%i' onclick='" % shape_id
267267
x3dfile_str += "select(this);"
268-
x3dfile_str += '"><Appearance>\n'
268+
x3dfile_str += "'><Appearance>\n"
269269
#
270270
# set Material or shader
271271
#
272272
if self._vs is None and self._fs is None:
273273
x3dfile_str += "<Material id='color' diffuseColor="
274-
x3dfile_str += "'%g %g %g'" % (self._color[0],
275-
self._color[1],
276-
self._color[2])
274+
x3dfile_str += "'%g %g %g'" % (self._color[0], self._color[1], self._color[2])
277275
x3dfile_str += " shininess="
278276
x3dfile_str += "'%g'" % self._shininess
279277
x3dfile_str += " specularColor="
280-
x3dfile_str += "'%g %g %g'" % (self._specular_color[0],
281-
self._specular_color[1],
282-
self._specular_color[2])
278+
x3dfile_str += "'%g %g %g'" % (self._specular_color[0], self._specular_color[1], self._specular_color[2])
283279
x3dfile_str += " transparency='%g'>\n" % self._transparency
284280
x3dfile_str += "</Material>\n"
285281
else: # set shaders
@@ -292,20 +288,20 @@ def to_x3dfile_string(self, shape_id):
292288
x3dfile_str += '</Appearance>\n'
293289
# export triangles
294290
x3dfile_str += triangle_set
295-
x3dfile_str += "</Shape>\n"
291+
x3dfile_str += "</Shape></Transform></Switch>\n"
296292
# and now, process edges
297293
if self._export_edges:
298294
edge_id = 0
299295
# below '0' means show all
300296
# -1 means doesn't show line
301297
# the "Switch" node selects the group to be displayed
302-
x3dfile_str += "<Switch whichChoice='0'>\n"
298+
x3dfile_str += "<Switch whichChoice='0' id='swBRP'>\n"
303299
x3dfile_str += "\t<Group>\n"
304300
for line_set in self._line_sets:
305-
x3dfile_str += '\t\t<Shape DEF="edg%i">\n' % edge_id
306-
x3dfile_str += '\t\t\t<Appearance><Material emissiveColor="0 0 0"/></Appearance>\n\t\t' # empty appearance, but the x3d validator complains if nothing set
301+
x3dfile_str += "\t\t<Transform scale='1 1 1'><Shape DEF='edg%i'>\n" % edge_id
302+
x3dfile_str += "\t\t\t<Appearance><Material emissiveColor='0 0 0'/></Appearance>\n\t\t" # empty appearance, but the x3d validator complains if nothing set
307303
x3dfile_str += line_set
308-
x3dfile_str += "\t\t</Shape>\n"
304+
x3dfile_str += "\t\t</Shape></Transform>\n"
309305
edge_id += 1
310306
x3dfile_str += "\t</Group>\n"
311307
x3dfile_str += "</Switch>\n"
@@ -332,8 +328,8 @@ def DisplayShape(self,
332328
vertex_shader=None,
333329
fragment_shader=None,
334330
export_edges=False,
335-
color=(0.65, 0.65, 0.65),
336-
specular_color=(1, 1, 1),
331+
color=(0.65, 0.65, 0.7),
332+
specular_color=(0.2, 0.2, 0.2),
337333
shininess=0.9,
338334
transparency=0.,
339335
line_color=(0, 0., 0.),

src/Extend/DataExchange.py

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
from OCC.Core.TDF import TDF_LabelSequence, TDF_Label, TDF_Tool
3434
from OCC.Core.TDataStd import TDataStd_Name, TDataStd_Name_GetID
3535
from OCC.Core.TCollection import TCollection_ExtendedString, TCollection_AsciiString
36-
from OCC.Core.Quantity import Quantity_Color
36+
from OCC.Core.Quantity import Quantity_Color, Quantity_TOC_RGB
3737
from OCC.Core.TopLoc import TopLoc_Location
3838
from OCC.Core.BRepBuilderAPI import BRepBuilderAPI_Transform
3939

@@ -43,7 +43,7 @@
4343
##########################
4444
# Step import and export #
4545
##########################
46-
def read_step_file(filename, return_as_shapes=False, verbosity=False):
46+
def read_step_file(filename, return_as_shapes=False, verbosity=True):
4747
""" read the STEP file and returns a compound
4848
filename: the file path
4949
return_as_shapes: optional, False by default. If True returns a list of shapes,
@@ -112,7 +112,8 @@ def read_step_file_with_names_colors(filename):
112112
if not os.path.isfile(filename):
113113
raise FileNotFoundError("%s not found." % filename)
114114
# the list:
115-
output_shapes = []
115+
output_shapes = {}
116+
116117
# create an handle to a document
117118
doc = TDocStd_Document(TCollection_ExtendedString("pythonocc-doc"))
118119

@@ -127,14 +128,13 @@ def read_step_file_with_names_colors(filename):
127128
step_reader.SetLayerMode(True)
128129
step_reader.SetNameMode(True)
129130
step_reader.SetMatMode(True)
131+
step_reader.SetGDTMode(True)
130132

131133
status = step_reader.ReadFile(filename)
132134
if status == IFSelect_RetDone:
133135
step_reader.Transfer(doc)
134136

135-
shape_tool.SetAutoNaming(True)
136137

137-
138138
#lvl = 0
139139
locs = []
140140
#cnt = 0
@@ -144,7 +144,7 @@ def _get_label_name(lab):
144144
TDF_Tool.Entry(lab, entry)
145145
n = TDataStd_Name()
146146
lab.FindAttribute(TDataStd_Name_GetID(), n)
147-
if n:
147+
if n is not None:
148148
return n.Get().PrintToString()
149149
return "No Name"
150150

@@ -174,6 +174,8 @@ def _get_sub_shapes(lab, loc):
174174
shape_tool.GetComponents(lab, l_comps)
175175
#print("Nb components :", l_comps.Length())
176176
#print()
177+
name = _get_label_name(lab)
178+
print("Name :", name)
177179

178180
if shape_tool.IsAssembly(lab):
179181
l_c = TDF_LabelSequence()
@@ -232,9 +234,7 @@ def _get_sub_shapes(lab, loc):
232234
#print(" X :", tran.X())
233235
#print(" Y :", tran.Y())
234236
#print(" Z :", tran.Z())
235-
shape = BRepBuilderAPI_Transform(shape, loc.Transformation()).Shape()
236-
237-
c = Quantity_Color()
237+
c = Quantity_Color(0.5, 0.5, 0.5, Quantity_TOC_RGB) # default color
238238
colorSet = False
239239
if (color_tool.GetInstanceColor(shape, 0, c) or
240240
color_tool.GetInstanceColor(shape, 1, c) or
@@ -258,12 +258,15 @@ def _get_sub_shapes(lab, loc):
258258
#n = c.Name(c.Red(), c.Green(), c.Blue())
259259
#print(' shape color Name & RGB: ', c, n, c.Red(), c.Green(), c.Blue())
260260

261+
shape_disp = BRepBuilderAPI_Transform(shape, loc.Transformation()).Shape()
262+
if not shape_disp in output_shapes:
263+
output_shapes[shape_disp] = [_get_label_name(lab), c]
261264
for i in range(l_subss.Length()):
262265
lab = l_subss.Value(i+1)
263-
print("\n######## simpleshape subshape label :", lab)
266+
#print("\n######## simpleshape subshape label :", lab)
264267
shape_sub = shape_tool.GetShape(lab)
265268

266-
c = Quantity_Color()
269+
c = Quantity_Color(0.5, 0.5, 0.5, Quantity_TOC_RGB) # default color
267270
colorSet = False
268271
if (color_tool.GetInstanceColor(shape_sub, 0, c) or
269272
color_tool.GetInstanceColor(shape_sub, 1, c) or
@@ -283,8 +286,10 @@ def _get_sub_shapes(lab, loc):
283286

284287
n = c.Name(c.Red(), c.Green(), c.Blue())
285288
#print(' shape color Name & RGB: ', c, n, c.Red(), c.Green(), c.Blue())
286-
287-
output_shapes.append([shape, _get_label_name(lab), c])
289+
shape_to_disp = BRepBuilderAPI_Transform(shape_sub, loc.Transformation()).Shape()
290+
# position the subshape to display
291+
if not shape_to_disp in output_shapes:
292+
output_shapes[shape_to_disp] = [_get_label_name(lab), c]
288293

289294

290295
def _get_shapes():
@@ -296,9 +301,9 @@ def _get_shapes():
296301
print()
297302
print("Number of shapes at root :", labels.Length())
298303
print()
299-
root = labels.Value(1)
300-
301-
_get_sub_shapes(root, None)
304+
for i in range(labels.Length()):
305+
root_item = labels.Value(i+1)
306+
_get_sub_shapes(root_item, None)
302307
_get_shapes()
303308
return output_shapes
304309

src/Visualization/Tesselator.cpp

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -52,17 +52,17 @@ Tesselator::Tesselator(TopoDS_Shape aShape,
5252
Standard_Real aScaleV,
5353
Standard_Real aRotationAngle) :
5454
//set local variables
55+
myShape(aShape),
56+
myTxtMapType(aTxtMapType),
57+
myAutoScaleSizeOnU(anAutoScaleSizeOnU),
58+
myAutoScaleSizeOnV(anAutoScaleSizeOnV),
5559
myDeviation(aDeviation),
5660
myUOrigin(aUOrigin),
5761
myVOrigin(aVOrigin),
5862
myURepeat(aURepeat),
5963
myVRepeat(aVRepeat),
6064
myScaleU(aScaleU),
6165
myScaleV(aScaleV),
62-
myAutoScaleSizeOnU(anAutoScaleSizeOnU),
63-
myAutoScaleSizeOnV(anAutoScaleSizeOnV),
64-
myTxtMapType(aTxtMapType),
65-
myShape(aShape),
6666
myRotationAngle(aRotationAngle)
6767
{
6868
//prepare bbox tex coords
@@ -361,7 +361,7 @@ void Tesselator::ComputeDefaultDeviation()
361361
Standard_Real yDim = std::abs((long)aYmax - (long)aYmin);
362362
Standard_Real zDim = std::abs((long)aZmax - (long)aZmin);
363363

364-
Standard_Real adeviation = std::max(aXmax-aXmin, std::max(aYmax-aYmin, aZmax-aZmin)) * 2e-2 ;
364+
Standard_Real adeviation = std::max(xDim, std::max(yDim, zDim)) * 2e-2 ;
365365
myDeviation = adeviation;
366366
}
367367

@@ -446,7 +446,7 @@ std::string formatFloatNumber(float f)
446446
{
447447
// returns string representation of the float number f.
448448
// set epsilon to 1e-3
449-
float epsilon = 1e-3;
449+
float epsilon = 1e-3f;
450450
std::stringstream formatted_float;
451451
if (std::abs(f) < epsilon) {
452452
f = 0.;
@@ -524,28 +524,28 @@ std::string Tesselator::ExportShapeToX3DIndexedFaceSet()
524524
// First Vertex
525525
str_vertices << formatFloatNumber(locVertexcoord[vertices_idx[0]]) << " ";
526526
str_vertices << formatFloatNumber(locVertexcoord[vertices_idx[0]+1]) <<" ";
527-
str_vertices << formatFloatNumber(locVertexcoord[vertices_idx[0]+2]) <<"\n";
527+
str_vertices << formatFloatNumber(locVertexcoord[vertices_idx[0]+2]) <<" ";
528528
// Second vertex
529529
str_vertices << formatFloatNumber(locVertexcoord[vertices_idx[1]]) << " ";
530530
str_vertices << formatFloatNumber(locVertexcoord[vertices_idx[1]+1]) << " ";
531-
str_vertices << formatFloatNumber(locVertexcoord[vertices_idx[1]+2]) << "\n";
531+
str_vertices << formatFloatNumber(locVertexcoord[vertices_idx[1]+2]) << " ";
532532
// Third vertex
533533
str_vertices << formatFloatNumber(locVertexcoord[vertices_idx[2]]) << " ";
534534
str_vertices << formatFloatNumber(locVertexcoord[vertices_idx[2]+1]) << " ";
535-
str_vertices << formatFloatNumber(locVertexcoord[vertices_idx[2]+2]) << "\n";
535+
str_vertices << formatFloatNumber(locVertexcoord[vertices_idx[2]+2]) << " ";
536536
// NORMALS
537537
// First normal
538538
str_normals << formatFloatNumber(locNormalcoord[normals_idx[0]]) << " ";
539539
str_normals << formatFloatNumber(locNormalcoord[normals_idx[0]+1]) << " ";
540-
str_normals << formatFloatNumber(locNormalcoord[normals_idx[0]+2]) << "\n";
540+
str_normals << formatFloatNumber(locNormalcoord[normals_idx[0]+2]) << " ";
541541
// Second normal
542542
str_normals << formatFloatNumber(locNormalcoord[normals_idx[1]]) << " ";
543543
str_normals << formatFloatNumber(locNormalcoord[normals_idx[1]+1]) << " ";
544-
str_normals << formatFloatNumber(locNormalcoord[normals_idx[1]+2]) << "\n";
544+
str_normals << formatFloatNumber(locNormalcoord[normals_idx[1]+2]) << " ";
545545
// Third normal
546546
str_normals << formatFloatNumber(locNormalcoord[normals_idx[2]]) << " ";
547547
str_normals << formatFloatNumber(locNormalcoord[normals_idx[2]+1]) << " ";
548-
str_normals << formatFloatNumber(locNormalcoord[normals_idx[2]+2]) << "\n";
548+
str_normals << formatFloatNumber(locNormalcoord[normals_idx[2]+2]) << " ";
549549
}
550550
str_ifs << "<TriangleSet solid='false'>\n";
551551
// write points coordinates
@@ -577,11 +577,11 @@ void Tesselator::ExportShapeToX3D(char * filename, int diffR, int diffG, int dif
577577
X3Dfile << "<Head>";
578578
X3Dfile << "<meta name='generator' content='pythonOCC, http://www.pythonocc.org'/>";
579579
X3Dfile << "</Head>";
580-
X3Dfile << "<Scene><Shape><Appearance><Material DEF='Shape_Mat' diffuseColor='0.65 0.65 0.65' ";
581-
X3Dfile << "shininess='0.9' specularColor='1 1 1'></Material></Appearance>";
580+
X3Dfile << "<Scene><Transform scale='1 1 1'><Shape><Appearance><Material DEF='Shape_Mat' diffuseColor='0.65 0.65 0.7' ";
581+
X3Dfile << "specularColor='0.2 0.2 0.2'></Material></Appearance>";
582582
// write tesselation
583583
X3Dfile << ExportShapeToX3DIndexedFaceSet();
584-
X3Dfile << "</Shape></Scene></X3D>\n";
584+
X3Dfile << "</Shape></Transform></Scene></X3D>\n";
585585
X3Dfile.close();
586586

587587
}

0 commit comments

Comments
 (0)