Skip to content

Commit eeb1a76

Browse files
Merge pull request #7 from RomainGuimbal/fixes
fixing cache, rebuild and hierarchy with single shape
2 parents 975736c + cf5b7ed commit eeb1a76

4 files changed

Lines changed: 58 additions & 47 deletions

File tree

‎__init__.py‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ class STEP_OT_ImportStepCADOperator(bpy.types.Operator, ImportHelper):
151151
),
152152
],
153153
name="Tree hierarchy",
154-
default=str(int(HierarchyType.EMPTIES_TREE)),
154+
default=str(int(HierarchyType.COLLECTION_TREE)),
155155
description="Organization style of objects",
156156
)
157157

@@ -300,7 +300,6 @@ class STEP_OT_ClearCache(bpy.types.Operator):
300300
bl_description = "Clear STEP cache, enabling the reload of a file"
301301

302302
def execute(self, context):
303-
# utils.memorytrace_print()
304303
# global GLOBAL_FILE_CACHE
305304
# items = list(GLOBAL_FILE_CACHE.values())
306305
# for entry in items:
@@ -437,7 +436,6 @@ def execute(self, context):
437436
other_obj.select_set(True)
438437

439438
# Reload files if not in cache
440-
reload_needed = False
441439
for o in selected_objects:
442440
if o["STEP_file"] not in GLOBAL_FILE_CACHE:
443441
bpy.ops.object.occ_reload_step()

‎importer.py‎

Lines changed: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656
XCAFDoc_DocumentTool,
5757
XCAFDoc_ColorGen,
5858
XCAFDoc_ColorSurf,
59-
# XCAFDoc_ColorCurv,
59+
XCAFDoc_ColorCurv,
6060
)
6161
from OCP.XSControl import XSControl_WorkSession
6262

@@ -251,23 +251,24 @@ def query_color(self, lab, overwrite=False):
251251
lab: shape label
252252
"""
253253
# default color = pink
254-
c = Quantity_Color(1.0, 0.0, 1.0, Quantity_TOC_RGB)
254+
color = Quantity_Color(1.0, 0.0, 1.0, Quantity_TOC_RGB)
255255
iscolorset = False
256256
colortype = None
257257

258258
shape = self.shape_tool.GetShape_s(lab)
259-
print(shape.ShapeType())
259+
# print(shape.ShapeType())
260260

261-
c_gen = self.color_tool.GetColor(shape, XCAFDoc_ColorGen, c)
262-
c_surf = self.color_tool.GetColor(shape, XCAFDoc_ColorSurf, c)
263-
c_curv = False # self.color_tool.GetColor(shape, XCAFDoc_ColorCurv, c) # TODO uncomment once priority are working
261+
# Overwrites each type
262+
c_gen_exists = self.color_tool.GetColor(shape, XCAFDoc_ColorGen, color)
263+
c_surf_exists = self.color_tool.GetColor(shape, XCAFDoc_ColorSurf, color)
264+
c_curv_exists = False # self.color_tool.GetColor(shape, XCAFDoc_ColorCurv, color) Supposed to be a fallback but overwrite't
264265

265-
if c_gen or c_surf or c_curv:
266+
if c_gen_exists or c_surf_exists or c_curv_exists:
266267
iscolorset = True
267268
# Color priority (1/type) is the same as CAD assistant material tree display
268-
colortype = c_gen * 1 + c_surf * 2 + c_curv * 3
269+
colortype = c_gen_exists * 1 + c_surf_exists * 2 + c_curv_exists * 3
269270

270-
return c, colortype, iscolorset
271+
return color, colortype, iscolorset
271272

272273
def print_all_colors(self):
273274
tcol = Quantity_Color(1.0, 0.0, 1.0, Quantity_TOC_RGB)
@@ -424,27 +425,27 @@ def transfer_with_units(self, filename):
424425

425426
self.doc = doc
426427

427-
def transfer_simple(self, fname):
428-
# see stepanalyzer.py for license details
429-
print("Init simple transfer")
430-
431-
# Create the application, empty document and shape_tool
432-
doc = TDocStd_Document(TCollection_ExtendedString("STEP"))
433-
app = XCAFApp_Application.GetApplication()
434-
app.NewDocument("MDTV-XCAF", doc)
435-
436-
# Read file and return populated doc
437-
step_reader = STEPCAFControl_Reader()
438-
step_reader.SetColorMode(True)
439-
step_reader.SetLayerMode(True)
440-
step_reader.SetNameMode(True)
441-
step_reader.SetMatMode(True)
442-
status = step_reader.ReadFile(fname)
443-
if status == IFSelect_RetDone:
444-
step_reader.Transfer(doc)
445-
self.scale = 0.001
446-
447-
self.doc = doc
428+
# def transfer_simple(self, fname):
429+
# # see stepanalyzer.py for license details
430+
# print("Init simple transfer")
431+
432+
# # Create the application, empty document and shape_tool
433+
# doc = TDocStd_Document(TCollection_ExtendedString("STEP"))
434+
# app = XCAFApp_Application.GetApplication()
435+
# app.NewDocument("MDTV-XCAF", doc)
436+
437+
# # Read file and return populated doc
438+
# step_reader = STEPCAFControl_Reader()
439+
# step_reader.SetColorMode(True)
440+
# step_reader.SetLayerMode(True)
441+
# step_reader.SetNameMode(True)
442+
# step_reader.SetMatMode(True)
443+
# status = step_reader.ReadFile(fname)
444+
# if status == IFSelect_RetDone:
445+
# step_reader.Transfer(doc)
446+
# self.scale = 0.001
447+
448+
# self.doc = doc
448449

449450
def init_reader(self, filename):
450451
if not os.path.isfile(filename):

‎object_generator.py‎

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
from .importer import ShapeTreeNode
2121

2222
GLOBAL_FILE_CACHE = {}
23+
ROOT_PARENT = -1
2324

2425

2526
class HierarchyType(IntEnum):
@@ -262,6 +263,10 @@ def build_mesh(
262263
trimesh.filter_zero_area()
263264
trimesh.filter_same_face()
264265
trimesh.fill_empty_color()
266+
267+
# Temporary fix. Original addon was apparently smarter at replacing only the rebuilt faces
268+
obj.data.clear_geometry()
269+
265270
trimesh.add_to_mesh(obj.data)
266271
obj.data.update()
267272

@@ -310,7 +315,9 @@ def node_parse(
310315
# link objects to tree
311316
if len(hierarchy_collections.items()) > 0:
312317
for obj in created_objs:
313-
hierarchy_collections[obj["STEP_parent"]].objects.link(obj)
318+
parent_col_uuid = obj.get("STEP_parent", ROOT_PARENT)
319+
parent_col = hierarchy_collections.get(parent_col_uuid, hierarchy_collections[-1])
320+
parent_col.objects.link(obj)
314321
global_t = tree.nodes[obj["STEP_tree_location"]].global_transform
315322
set_obj_matrix_world(obj, global_t)
316323

@@ -389,7 +396,7 @@ def build_collection_instances(instanced_objects, hierarchy_collections, scale):
389396
component_col.objects.link(source_obj)
390397

391398
# instance original
392-
empty = bpy.data.objects.new(source_obj.name + "_instance", None)
399+
empty = bpy.data.objects.new(source_obj.name, None)
393400
empty.instance_type = "COLLECTION"
394401
empty.instance_collection = component_col
395402
empty.matrix_world = trsf
@@ -400,13 +407,13 @@ def build_collection_instances(instanced_objects, hierarchy_collections, scale):
400407
# instance copies
401408
for shape_name, local_t, global_t, parent_uuid in instances_info:
402409
# create instance
403-
empty = bpy.data.objects.new(shape_name + "_instance", None)
410+
empty = bpy.data.objects.new(source_obj.name + "_instance", None)
404411
empty.instance_type = "COLLECTION"
405412
empty.instance_collection = component_col
406413
empty.empty_display_size = scale
407414
scale_translation(global_t, scale)
408415
set_obj_matrix_world(empty, global_t)
409-
parent_col = hierarchy_collections[parent_uuid]
416+
parent_col = hierarchy_collections.get(parent_uuid, hierarchy_collections[-1])
410417
parent_col.objects.link(empty)
411418

412419

@@ -422,8 +429,9 @@ def load_step(
422429
):
423430
# Find file
424431
from . import importer
425-
426432
filename = "".join(ntpath.basename(filepath).split(".")[:-1])
433+
434+
# Try retrieve step_reader from cache
427435
if filepath not in GLOBAL_FILE_CACHE:
428436
try:
429437
step_reader = importer.ReadSTEP(filepath)
@@ -458,7 +466,7 @@ def load_step(
458466
all_shapes = tree.get_shapes()
459467
total = len(all_shapes)
460468

461-
# Build shapes
469+
# Build shapes objects
462470
wm.progress_begin(0, total)
463471
for i, (shp, node_index) in enumerate(all_shapes):
464472
parent_uuid, self_uuid, tag, obj_name, _, local_t, global_t = tree.nodes[
@@ -488,12 +496,14 @@ def load_step(
488496
source_obj = created_names[shape_name]
489497
if htypes == HierarchyType.COLLECTION_INSTANCES:
490498
if source_obj in instanced_objects:
499+
parent_for_instance = parent_uuid if parent_uuid != 0 else ROOT_PARENT
491500
instanced_objects[source_obj].append(
492-
(shape_name, local_t, global_t, parent_uuid)
501+
(shape_name, local_t, global_t, parent_for_instance)
493502
)
494503
else:
504+
parent_for_instance = parent_uuid if parent_uuid != 0 else ROOT_PARENT
495505
instanced_objects[source_obj] = [
496-
(shape_name, local_t, global_t, parent_uuid)
506+
(shape_name, local_t, global_t, parent_for_instance)
497507
]
498508
else:
499509
obj = source_obj.copy()
@@ -526,7 +536,8 @@ def load_step(
526536
if obj:
527537
# assign property to obj
528538
obj["STEP_tag"] = tag
529-
obj["STEP_parent"] = parent_uuid
539+
obj_parent = parent_uuid if parent_uuid != 0 else ROOT_PARENT
540+
obj["STEP_parent"] = obj_parent
530541
obj["STEP_uuid"] = self_uuid
531542
obj["STEP_file"] = filepath
532543
obj["STEP_name"] = obj_name

‎trimesh.py‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import numpy as np
22
import bpy
33
from dataclasses import dataclass
4+
from mathutils import Vector
45

56

67
def make_tri_hash(f):
@@ -210,16 +211,16 @@ def set_material_name(self, name):
210211
self.tris.material_name = [name] * len(self.tris)
211212

212213
def fill_empty_color(self):
213-
"Fill color==None with undef_color (currently pink)"
214-
# TODO : make undef black and add attributes to tell if face is colored so the shader can know
215-
undef_color = (1.0, 0.0, 1.0) # pink
214+
"""
215+
Fill tris empty colors
216+
"""
216217
if len(self.tris) == 0:
217-
# Empty mesh
218218
return
219+
undef_color = (0.8, 0.8, 0.8)
219220
self.tris.colors = [c if c else undef_color for c in self.tris.colors]
220221

221222
def add_to_mesh(self, mesh: bpy.types.Mesh):
222-
mesh.from_pydata(self.verts, [], self.tris.indices)
223+
mesh.from_pydata([Vector(v) for v in self.verts], [], self.tris.indices)
223224

224225
def get_loop_colors(self):
225226
"Return colors in triangle loop creation order"

0 commit comments

Comments
 (0)