3333from OCC .Core .TDF import TDF_LabelSequence , TDF_Label , TDF_Tool
3434from OCC .Core .TDataStd import TDataStd_Name , TDataStd_Name_GetID
3535from 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
3737from OCC .Core .TopLoc import TopLoc_Location
3838from OCC .Core .BRepBuilderAPI import BRepBuilderAPI_Transform
3939
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
0 commit comments