From c55c89c7d19af81369ebfd7ecc0571e5ef027d36 Mon Sep 17 00:00:00 2001 From: Yorik van Havre Date: Wed, 9 Sep 2020 14:36:20 +0200 Subject: [PATCH] Arch: Better support for colors in multicore IFC importer --- src/Mod/Arch/importIFCHelper.py | 15 ++++++++++++-- src/Mod/Arch/importIFCmulticore.py | 32 +++++++++++++++++++++++++++--- 2 files changed, 42 insertions(+), 5 deletions(-) diff --git a/src/Mod/Arch/importIFCHelper.py b/src/Mod/Arch/importIFCHelper.py index e8acf81865..bf99b334ef 100644 --- a/src/Mod/Arch/importIFCHelper.py +++ b/src/Mod/Arch/importIFCHelper.py @@ -383,6 +383,16 @@ def buildRelMaterialColors(ifcfile, prodrepr): pass +def getColorFromProduct(product): + + if product.Representation: + for rep in product.Representation.Representations: + for item in rep.Items: + for style in item.StyledByItem: + color = getColorFromStyledItem(style) + if color: + return color + def getColorFromMaterial(material): if material.HasRepresentation: @@ -410,12 +420,13 @@ def getColorFromStyledItem(styled_item): Return `None` if `styled_item` is not of type `'IfcStyledItem'` or if there is any other problem getting a color. """ - if not styled_item.is_a("IfcStyledItem"): - return None if styled_item.is_a("IfcStyledRepresentation"): styled_item = styled_item.Items[0] + if not styled_item.is_a("IfcStyledItem"): + return None + rgb_color = None transparency = None col = None diff --git a/src/Mod/Arch/importIFCmulticore.py b/src/Mod/Arch/importIFCmulticore.py index 1d9f2abee1..dd8ef5e7c7 100644 --- a/src/Mod/Arch/importIFCmulticore.py +++ b/src/Mod/Arch/importIFCmulticore.py @@ -41,6 +41,7 @@ materials = {} #ifcid : Arch_Material objects = {} #ifcid : Arch_Component subs = {} #host_ifcid: [child_ifcid,...] adds = {} #host_ifcid: [child_ifcid,...] +colors = {} # objname : (r,g,b) def open(filename): @@ -117,8 +118,11 @@ def insert(filename,docname=None,preferences=None): if not iterator.next(): break - # finished + # post-processing processRelationships() + storeColorDict() + + # finished progressbar.stop() FreeCAD.ActiveDocument.recompute() endtime = round(time.time()-starttime,1) @@ -146,7 +150,7 @@ def writeProgress(count=None,total=None,starttime=None): else: eta = "--:--" hashes = '#'*int(r*10)+' '*int(10-r*10) - fstring = '\rImporting '+str(total)+' products... [{0}] {1}%, ETA: {2}' + fstring = '\rImporting '+str(total)+' products [{0}] {1}%, ETA: {2}' sys.stdout.write(fstring.format(hashes, int(r*100),eta)) @@ -171,6 +175,7 @@ def createProduct(ifcproduct,brep): createMaterial(obj,ifcproduct) createModelStructure(obj,ifcproduct) setRelationships(obj,ifcproduct) + setColor(obj,ifcproduct) return obj @@ -209,6 +214,18 @@ def setProperties(obj,ifcproduct): propvalue = ";;".join(v) +def setColor(obj,ifcproduct): + + """sets the color of an object""" + + global colors + + color = importIFCHelper.getColorFromProduct(ifcproduct) + colors[obj.Name] = color + if FreeCAD.GuiUp: + obj.ViewObject.ShapeColor = color + + def createLayer(obj,ifcproduct): """sets the layer of a component""" @@ -282,7 +299,7 @@ def processRelationships(): """process all stored relationships""" for dom in ((subs,"Subtractions"),(adds,"Additions")): - for key,vals in dom[0]: + for key,vals in dom[0].items(): if key in objects: for val in vals: if val in objects: @@ -291,3 +308,12 @@ def processRelationships(): g.append(val) setattr(objects[key],dom[1],g) +def storeColorDict(): + + """stores the color dictionary in the document Meta if non-GUI mode""" + + if colors and not FreeCAD.GuiUp: + import json + d = FreeCAD.ActiveDocument.Meta + d["colordict"] = json.dumps(colors) + FreeCAD.ActiveDocument.Meta = d