From 78b644e313f682427cc67addbdff1c7f02751d40 Mon Sep 17 00:00:00 2001 From: Ajinkya Dahale Date: Sat, 14 Aug 2021 16:04:02 -0400 Subject: [PATCH] [OpenSCAD] Fixes cut color while importing CSG (#4920) * [OpenSCAD] Fixes cut color while importing CSG Fixes #4684. For some reason colors of "base" and "tool" need to be set in order for the cut to have colors as well, just like fuse and multifuse. Also tidied up the relevant code portions a little. Some testing might be needed. * [OpenSCAD] Color settings for other booleans in importCSG As per Keith's comments, this change extends recursive color setting to other boolean features viz. `Part::Common` and `Part::MultiCommon`. Also possible to add any other features needing the same treatment. * Update importCSG.py Remove unneeded parentheses and fix its/it's mixup Co-authored-by: Kurt Kremitzki --- src/Mod/OpenSCAD/importCSG.py | 46 +++++++++++++---------------------- 1 file changed, 17 insertions(+), 29 deletions(-) diff --git a/src/Mod/OpenSCAD/importCSG.py b/src/Mod/OpenSCAD/importCSG.py index 8b82d6d8b9..98197cec53 100644 --- a/src/Mod/OpenSCAD/importCSG.py +++ b/src/Mod/OpenSCAD/importCSG.py @@ -66,19 +66,22 @@ def shallHide(subject): return True return False -def setColorRecursively(obj,color,transp): - if(obj.TypeId=="Part::Fuse" or obj.TypeId=="Part::MultiFuse"): - for currentObject in obj.OutList: - if (currentObject.TypeId=="Part::Fuse" or currentObject.TypeId=="Part::MultiFuse"): - setColorRecursively(currentObject,color,transp) - else: - print("Fixing up colors for: "+str(currentObject.FullName)) - if(currentObject not in hassetcolor): - currentObject.ViewObject.ShapeColor=color - currentObject.ViewObject.Transparency=transp - setColorRecursively(currentObject,color,transp) - else: - setColorRecursively(currentObject,color,transp) +def setColorRecursively(obj, color, transp): + ''' + For some reason a part made by cutting or fusing other parts do not have a color + unless its constituents are also colored. This code sets colors for those + constituents unless already set elsewhere. + ''' + obj.ViewObject.ShapeColor = color + obj.ViewObject.Transparency = transp + # Add any other relevant features to this list + boolean_features = ["Part::Fuse", "Part::MultiFuse", "Part::Cut", + "Part::Common", "Part::MultiCommon"] + if obj.TypeId in boolean_features: + for currentObject in obj.OutList: + print(f"Fixing up colors for: {currentObject.FullName}") + if currentObject not in hassetcolor: + setColorRecursively(currentObject, color, transp) def fixVisibility(): for obj in FreeCAD.ActiveDocument.Objects: @@ -555,22 +558,7 @@ def p_color_action(p): if "Group" in obj.FullName: obj.ViewObject.Visibility=False alreadyhidden.append(obj) - if(obj.TypeId=="Part::Fuse" or obj.TypeId=="Part::MultiFuse"): - for currentObject in obj.OutList: - if (currentObject.TypeId=="Part::Fuse" or currentObject.TypeId=="Part::MultiFuse"): - setColorRecursively(currentObject,color,transp) - if(currentObject not in hassetcolor): - currentObject.ViewObject.ShapeColor=color - currentObject.ViewObject.Transparency=transp - setColorRecursively(currentObject,color,transp) - else: - setColorRecursively(currentObject,color,transp) - else: - obj.ViewObject.ShapeColor =color - obj.ViewObject.Transparency = transp - else: - obj.ViewObject.ShapeColor =color - obj.ViewObject.Transparency = transp + setColorRecursively(obj, color, transp) hassetcolor.append(obj) p[0] = p[6]