OpenSCAD: Minor cleanup of import code

This commit is contained in:
Chris Hennes
2021-11-07 21:50:48 -06:00
parent 81830d850f
commit 1ef20ed984
2 changed files with 136 additions and 137 deletions

View File

@@ -200,13 +200,11 @@ class Resize :
self.createGeometry(fp)
def createGeometry(self, fp) :
print("Resize create Geometry")
import FreeCAD
mat = FreeCAD.Matrix()
mat.A11 = self.Vector[0]
mat.A22 = self.Vector[1]
mat.A33 = self.Vector[2]
print(mat)
fp.Shape = self.Target.Shape.transformGeometry(mat)
def __getstate__(self):
@@ -387,7 +385,7 @@ class Frustum:
fp.Placement = plm
class Twist:
def __init__(self, obj,child=None,h=1.0,angle=0.0,scale=[1.0,1.0]):
def __init__(self, obj, child=None, h=1.0, angle=0.0, scale=[1.0,1.0]):
import FreeCAD
obj.addProperty("App::PropertyLink","Base","Base",
"The base object that must be transformed")
@@ -406,10 +404,10 @@ class Twist:
self.createGeometry(fp)
def onChanged(self, fp, prop):
if prop in ["Angle","Height"]:
if prop in ["Angle","Height","Scale"]:
self.createGeometry(fp)
def createGeometry(self,fp):
def createGeometry(self, fp):
import FreeCAD,Part,math,sys
if fp.Base and fp.Height and fp.Base.Shape.isValid():
solids = []
@@ -443,7 +441,6 @@ class Twist:
pipe_shell.add(wire1)
pipe_shell.add(wire2)
pipe_shell.setAuxiliarySpine(auxiliary_spine,True,0)
print(pipe_shell.getStatus())
assert(pipe_shell.isReady())
pipe_shell.build()
faces.extend(pipe_shell.shape().Faces)
@@ -534,10 +531,9 @@ class PrismaticToroid:
solid = Part.makeSolid (clean_shell)
if solid.Volume < 0:
solid.reverse()
print (f"Solid volume is {solid.Volume}")
solids.append(solid)
except Part.OCCError:
print ("Could not create solid: creating compound instead")
FreeCAD.Console.PrintWarning("Could not create solid: creating compound instead")
solids.append(Part.Compound(faces))
fp.Shape = Part.Compound(solids)
@@ -587,139 +583,146 @@ class CGALFeature:
raise ValueError
def makeSurfaceVolume(filename):
import FreeCAD,Part,sys
import FreeCAD
import Part
import sys
coords = []
with open(filename) as f1:
coords = []
min_z = sys.float_info.max
for line in f1.readlines():
sline=line.strip()
sline = line.strip()
if sline and not sline.startswith('#'):
ycoord=len(coords)
lcoords=[]
ycoord = len(coords)
lcoords = []
for xcoord, num in enumerate(sline.split()):
fnum=float(num)
fnum = float(num)
lcoords.append(FreeCAD.Vector(float(xcoord),float(ycoord),fnum))
min_z = min(fnum,min_z)
coords.append(lcoords)
num_rows = len(coords)
num_cols = len(coords[0])
# OpenSCAD does not spline this surface, so neither do we: just create a bunch of faces,
# using four triangles per quadrilateral
faces = []
for row in range(num_rows-1):
for col in range(num_cols-1):
a = coords[row+0][col+0]
b = coords[row+0][col+1]
c = coords[row+1][col+1]
d = coords[row+1][col+0]
centroid = 0.25 * (a + b + c + d)
ab = Part.makeLine(a,b)
bc = Part.makeLine(b,c)
cd = Part.makeLine(c,d)
da = Part.makeLine(d,a)
num_rows = len(coords)
if num_rows == 0:
FreeCAD.Console.PrintWarning(f"No data found in surface file {filename}")
return None,0,0
num_cols = len(coords[0])
diag_a = Part.makeLine(a, centroid)
diag_b = Part.makeLine(b, centroid)
diag_c = Part.makeLine(c, centroid)
diag_d = Part.makeLine(d, centroid)
# OpenSCAD does not spline this surface, so neither do we: just create a
# bunch of faces,
# using four triangles per quadrilateral
faces = []
for row in range(num_rows - 1):
for col in range(num_cols - 1):
a = coords[row + 0][col + 0]
b = coords[row + 0][col + 1]
c = coords[row + 1][col + 1]
d = coords[row + 1][col + 0]
centroid = 0.25 * (a + b + c + d)
ab = Part.makeLine(a,b)
bc = Part.makeLine(b,c)
cd = Part.makeLine(c,d)
da = Part.makeLine(d,a)
wire1 = Part.Wire([ab,diag_a,diag_b])
wire2 = Part.Wire([bc,diag_b,diag_c])
wire3 = Part.Wire([cd,diag_c,diag_d])
wire4 = Part.Wire([da,diag_d,diag_a])
diag_a = Part.makeLine(a, centroid)
diag_b = Part.makeLine(b, centroid)
diag_c = Part.makeLine(c, centroid)
diag_d = Part.makeLine(d, centroid)
try:
face = Part.Face(wire1)
faces.append(face)
face = Part.Face(wire2)
faces.append(face)
face = Part.Face(wire3)
faces.append(face)
face = Part.Face(wire4)
faces.append(face)
except Exception:
print ("Failed to create the face from {},{},{},{}".format(coords[row+0][col+0],\
coords[row+0][col+1],coords[row+1][col+1],coords[row+1][col+0]))
last_row = num_rows-1
last_col = num_cols-1
wire1 = Part.Wire([ab,diag_a,diag_b])
wire2 = Part.Wire([bc,diag_b,diag_c])
wire3 = Part.Wire([cd,diag_c,diag_d])
wire4 = Part.Wire([da,diag_d,diag_a])
# Create the face to close off the y-min border: OpenSCAD places the lower surface of the shell
# at 1 unit below the lowest coordinate in the surface
lines = []
corner1 = FreeCAD.Vector(coords[0][0].x, coords[0][0].y, min_z-1)
lines.append (Part.makeLine(corner1,coords[0][0]))
for col in range(num_cols-1):
a = coords[0][col]
b = coords[0][col+1]
lines.append (Part.makeLine(a, b))
corner2 = FreeCAD.Vector(coords[0][last_col].x, coords[0][last_col].y, min_z-1)
lines.append (Part.makeLine(corner2,coords[0][last_col]))
lines.append (Part.makeLine(corner1,corner2))
wire = Part.Wire(lines)
face = Part.Face(wire)
faces.append(face)
# Create the face to close off the y-max border
lines = []
corner1 = FreeCAD.Vector(coords[last_row][0].x, coords[last_row][0].y, min_z-1)
lines.append (Part.makeLine(corner1,coords[last_row][0]))
for col in range(num_cols-1):
a = coords[last_row][col]
b = coords[last_row][col+1]
lines.append (Part.makeLine(a, b))
corner2 = FreeCAD.Vector(coords[last_row][last_col].x, coords[last_row][last_col].y, min_z-1)
lines.append (Part.makeLine(corner2,coords[last_row][last_col]))
lines.append (Part.makeLine(corner1,corner2))
wire = Part.Wire(lines)
face = Part.Face(wire)
faces.append(face)
try:
face = Part.Face(wire1)
faces.append(face)
face = Part.Face(wire2)
faces.append(face)
face = Part.Face(wire3)
faces.append(face)
face = Part.Face(wire4)
faces.append(face)
except Exception:
FreeCAD.Console.PrintWarning("Failed to create the face from {},{},{},{}".format(coords[row + 0][col + 0],\
coords[row + 0][col + 1],coords[row + 1][col + 1],coords[row + 1][col + 0]))
# Create the face to close off the x-min border
lines = []
corner1 = FreeCAD.Vector(coords[0][0].x, coords[0][0].y, min_z-1)
lines.append (Part.makeLine(corner1,coords[0][0]))
for row in range(num_rows-1):
a = coords[row][0]
b = coords[row+1][0]
lines.append (Part.makeLine(a, b))
corner2 = FreeCAD.Vector(coords[last_row][0].x, coords[last_row][0].y, min_z-1)
lines.append (Part.makeLine(corner2,coords[last_row][0]))
lines.append (Part.makeLine(corner1,corner2))
wire = Part.Wire(lines)
face = Part.Face(wire)
faces.append(face)
last_row = num_rows - 1
last_col = num_cols - 1
# Create the face to close off the x-max border
lines = []
corner1 = FreeCAD.Vector(coords[0][last_col].x, coords[0][last_col].y, min_z-1)
lines.append (Part.makeLine(corner1,coords[0][last_col]))
for row in range(num_rows-1):
a = coords[row][last_col]
b = coords[row+1][last_col]
lines.append (Part.makeLine(a, b))
corner2 = FreeCAD.Vector(coords[last_row][last_col].x, coords[last_row][last_col].y, min_z-1)
lines.append (Part.makeLine(corner2,coords[last_row][last_col]))
lines.append (Part.makeLine(corner1,corner2))
wire = Part.Wire(lines)
face = Part.Face(wire)
faces.append(face)
# Create the face to close off the y-min border: OpenSCAD places the lower
# surface of the shell
# at 1 unit below the lowest coordinate in the surface
lines = []
corner1 = FreeCAD.Vector(coords[0][0].x, coords[0][0].y, min_z - 1)
lines.append(Part.makeLine(corner1,coords[0][0]))
for col in range(num_cols - 1):
a = coords[0][col]
b = coords[0][col + 1]
lines.append(Part.makeLine(a, b))
corner2 = FreeCAD.Vector(coords[0][last_col].x, coords[0][last_col].y, min_z - 1)
lines.append(Part.makeLine(corner2,coords[0][last_col]))
lines.append(Part.makeLine(corner1,corner2))
wire = Part.Wire(lines)
face = Part.Face(wire)
faces.append(face)
# Create a bottom surface to close off the shell
a = FreeCAD.Vector(coords[0][0].x, coords[0][0].y, min_z-1)
b = FreeCAD.Vector(coords[0][last_col].x, coords[0][last_col].y, min_z-1)
c = FreeCAD.Vector(coords[last_row][last_col].x, coords[last_row][last_col].y, min_z-1)
d = FreeCAD.Vector(coords[last_row][0].x, coords[last_row][0].y, min_z-1)
ab = Part.makeLine(a,b)
bc = Part.makeLine(b,c)
cd = Part.makeLine(c,d)
da = Part.makeLine(d,a)
wire = Part.Wire([ab,bc,cd,da])
face = Part.Face(wire)
faces.append(face)
# Create the face to close off the y-max border
lines = []
corner1 = FreeCAD.Vector(coords[last_row][0].x, coords[last_row][0].y, min_z - 1)
lines.append(Part.makeLine(corner1,coords[last_row][0]))
for col in range(num_cols - 1):
a = coords[last_row][col]
b = coords[last_row][col + 1]
lines.append(Part.makeLine(a, b))
corner2 = FreeCAD.Vector(coords[last_row][last_col].x, coords[last_row][last_col].y, min_z - 1)
lines.append(Part.makeLine(corner2,coords[last_row][last_col]))
lines.append(Part.makeLine(corner1,corner2))
wire = Part.Wire(lines)
face = Part.Face(wire)
faces.append(face)
s = Part.Shell(faces)
solid = Part.Solid(s)
return solid,last_col,last_row
# Create the face to close off the x-min border
lines = []
corner1 = FreeCAD.Vector(coords[0][0].x, coords[0][0].y, min_z - 1)
lines.append(Part.makeLine(corner1,coords[0][0]))
for row in range(num_rows - 1):
a = coords[row][0]
b = coords[row + 1][0]
lines.append(Part.makeLine(a, b))
corner2 = FreeCAD.Vector(coords[last_row][0].x, coords[last_row][0].y, min_z - 1)
lines.append(Part.makeLine(corner2,coords[last_row][0]))
lines.append(Part.makeLine(corner1,corner2))
wire = Part.Wire(lines)
face = Part.Face(wire)
faces.append(face)
# Create the face to close off the x-max border
lines = []
corner1 = FreeCAD.Vector(coords[0][last_col].x, coords[0][last_col].y, min_z - 1)
lines.append(Part.makeLine(corner1,coords[0][last_col]))
for row in range(num_rows - 1):
a = coords[row][last_col]
b = coords[row + 1][last_col]
lines.append(Part.makeLine(a, b))
corner2 = FreeCAD.Vector(coords[last_row][last_col].x, coords[last_row][last_col].y, min_z - 1)
lines.append(Part.makeLine(corner2,coords[last_row][last_col]))
lines.append(Part.makeLine(corner1,corner2))
wire = Part.Wire(lines)
face = Part.Face(wire)
faces.append(face)
# Create a bottom surface to close off the shell
a = FreeCAD.Vector(coords[0][0].x, coords[0][0].y, min_z - 1)
b = FreeCAD.Vector(coords[0][last_col].x, coords[0][last_col].y, min_z - 1)
c = FreeCAD.Vector(coords[last_row][last_col].x, coords[last_row][last_col].y, min_z - 1)
d = FreeCAD.Vector(coords[last_row][0].x, coords[last_row][0].y, min_z - 1)
ab = Part.makeLine(a,b)
bc = Part.makeLine(b,c)
cd = Part.makeLine(c,d)
da = Part.makeLine(d,a)
wire = Part.Wire([ab,bc,cd,da])
face = Part.Face(wire)
faces.append(face)
s = Part.Shell(faces)
solid = Part.Solid(s)
return solid,last_col,last_row

View File

@@ -79,7 +79,7 @@ def setColorRecursively(obj, color, transp):
"Part::Common", "Part::MultiCommon"]
if obj.TypeId in boolean_features:
for currentObject in obj.OutList:
print(f"Fixing up colors for: {currentObject.FullName}")
if printverbose: print(f"Fixing up colors for: {currentObject.FullName}")
if currentObject not in hassetcolor:
setColorRecursively(currentObject, color, transp)
@@ -767,14 +767,15 @@ def p_linear_extrude_with_transform(p):
'linear_extrude_with_transform : linear_extrude LPAREN keywordargument_list RPAREN OBRACE block_list EBRACE'
if printverbose: print("Linear Extrude With Transform")
h = float(p[3]['height'])
s = 1.0
if printverbose: print("Height : ",h)
s = [1.0,1.0]
t = 0.0
if printverbose: print("Twist : ",p[3])
if 'scale' in p[3]:
s = [float(p[3]['scale'][0]), float(p[3]['scale'][1])]
print ("Scale: " + str(s))
if printverbose: print ("Scale: " + str(s))
if 'twist' in p[3]:
t = float(p[3]['twist'])
if printverbose: print("Twist : ",t)
# Test if null object like from null text
if (len(p[6]) == 0) :
p[0] = []
@@ -783,7 +784,7 @@ def p_linear_extrude_with_transform(p):
obj = fuse(p[6],"Linear Extrude Union")
else :
obj = p[6][0]
if t != 0.0 or s != 1.0:
if t != 0.0 or s[0] != 1.0 or s[1] != 1.0:
newobj = process_linear_extrude_with_transform(obj,h,t,s)
else:
newobj = process_linear_extrude(obj,h)
@@ -1286,11 +1287,7 @@ def p_polyhedron_action(p) :
pp =[v2(v[k]) for k in i]
# Add first point to end of list to close polygon
pp.append(pp[0])
print("pp")
print(pp)
w = Part.makePolygon(pp)
print("w")
print(w)
try:
f = Part.Face(w)
except Exception:
@@ -1315,7 +1312,6 @@ def p_projection_action(p) :
for shape in p[6]:
shape.Shape.tessellate(0.05)
bbox.add(shape.Shape.BoundBox)
print (bbox)
plane = doc.addObject("Part::Plane","xy_plane_used_for_projection")
plane.Length = bbox.XLength
plane.Width = bbox.YLength