Skip to content

Commit d29fd7d

Browse files
helaslowwmayer
authored andcommitted
[OpenSCAD] Fix colors not working correctly when polygons are extruded
1 parent 29bbb5e commit d29fd7d

File tree

1 file changed

+66
-3
lines changed

1 file changed

+66
-3
lines changed

src/Mod/OpenSCAD/importCSG.py

Lines changed: 66 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,46 @@
5050

5151
params = FreeCAD.ParamGet("User parameter:BaseApp/Preferences/Mod/OpenSCAD")
5252
printverbose = params.GetBool('printVerbose',False)
53-
53+
hassetcolor=[]
54+
alreadyhidden=[]
5455
printverbose = True
5556

5657
# Get the token map from the lexer. This is required.
5758
import tokrules
5859
from tokrules import tokens
5960

61+
def shallHide(subject):
62+
for obj in subject.OutListRecursive:
63+
if "Matrix_Union" in str(obj.FullName):
64+
return False
65+
if "Extrude" in str(obj.FullName):
66+
return True
67+
return False
68+
69+
def setColorRecursively(obj,color,transp):
70+
if(obj.TypeId=="Part::Fuse" or obj.TypeId=="Part::MultiFuse"):
71+
for currentObject in obj.OutList:
72+
if (currentObject.TypeId=="Part::Fuse" or currentObject.TypeId=="Part::MultiFuse"):
73+
setColorRecursively(currentObject,color,transp)
74+
else:
75+
print("Fixing up colors for: "+str(currentObject.FullName))
76+
if(currentObject not in hassetcolor):
77+
currentObject.ViewObject.ShapeColor=color
78+
currentObject.ViewObject.Transparency=transp
79+
setColorRecursively(currentObject,color,transp)
80+
else:
81+
setColorRecursively(currentObject,color,transp)
82+
83+
def fixVisibility():
84+
for obj in FreeCAD.ActiveDocument.Objects:
85+
if(( obj.TypeId=="Part::Fuse" or obj.TypeId=="Part::MultiFuse") and shallHide(obj)):
86+
if "Group" in obj.FullName:
87+
alreadyhidden.append(obj)
88+
obj.ViewObject.Visibility=False
89+
for currentObject in obj.OutList:
90+
if(currentObject not in alreadyhidden):
91+
currentObject.ViewObject.Visibility=True
92+
6093
if gui:
6194
try:
6295
_encoding = QtGui.QApplication.UnicodeUTF8
@@ -146,6 +179,9 @@ def processcsg(filename):
146179
if printverbose:
147180
print('End Parser')
148181
print(result)
182+
fixVisibility()
183+
hassetcolor.clear()
184+
alreadyhidden.clear()
149185
FreeCAD.Console.PrintMessage('End processing CSG file\n')
150186
doc.recompute()
151187

@@ -179,6 +215,7 @@ def p_group_action1(p):
179215
else :
180216
p[0] = p[5]
181217

218+
182219
def p_group_action2(p) :
183220
'group_action2 : group LPAREN RPAREN SEMICOL'
184221
if printverbose: print("Group2")
@@ -490,8 +527,27 @@ def p_color_action(p):
490527
transp = 100 - int(math.floor(100*float(p[3][3]))) #Alpha
491528
if gui:
492529
for obj in p[6]:
493-
obj.ViewObject.ShapeColor =color
494-
obj.ViewObject.Transparency = transp
530+
if shallHide(obj):
531+
if "Group" in obj.FullName:
532+
obj.ViewObject.Visibility=False
533+
alreadyhidden.append(obj)
534+
if(obj.TypeId=="Part::Fuse" or obj.TypeId=="Part::MultiFuse"):
535+
for currentObject in obj.OutList:
536+
if (currentObject.TypeId=="Part::Fuse" or currentObject.TypeId=="Part::MultiFuse"):
537+
setColorRecursively(currentObject,color,transp)
538+
if(currentObject not in hassetcolor):
539+
currentObject.ViewObject.ShapeColor=color
540+
currentObject.ViewObject.Transparency=transp
541+
setColorRecursively(currentObject,color,transp)
542+
else:
543+
setColorRecursively(currentObject,color,transp)
544+
else:
545+
obj.ViewObject.ShapeColor =color
546+
obj.ViewObject.Transparency = transp
547+
else:
548+
obj.ViewObject.ShapeColor =color
549+
obj.ViewObject.Transparency = transp
550+
hassetcolor.append(obj)
495551
p[0] = p[6]
496552

497553
# Error rule for syntax errors
@@ -794,6 +850,10 @@ def p_multmatrix_action(p):
794850
transform_matrix = FreeCAD.Matrix()
795851
if printverbose: print("Multmatrix")
796852
if printverbose: print(p[3])
853+
if gui:
854+
parentcolor=p[6][0].ViewObject.ShapeColor
855+
parenttransparency=p[6][0].ViewObject.Transparency
856+
797857
m1l=sum(p[3],[])
798858
if any('x' in me for me in m1l): #hexfloats
799859
m1l=[float.fromhex(me) for me in m1l]
@@ -873,6 +933,9 @@ def p_multmatrix_action(p):
873933
p[0] = [newobj]
874934
else :
875935
p[0] = [new_part]
936+
if gui:
937+
new_part.ViewObject.ShapeColor=parentcolor
938+
new_part.ViewObject.Transparency = parenttransparency
876939
if printverbose: print("Multmatrix applied")
877940

878941
def p_matrix(p):

0 commit comments

Comments
 (0)