File size: 11,901 Bytes
985c397 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 | # SPDX-License-Identifier: LGPL-2.1-or-later
#***************************************************************************
#* Copyright (c) 2012 Keith Sloan <keith@sloan-home.co.uk> *
#* *
#* This program is free software; you can redistribute it and/or modify *
#* it under the terms of the GNU Lesser General Public License (LGPL) *
#* as published by the Free Software Foundation; either version 2 of *
#* the License, or (at your option) any later version. *
#* for detail see the LICENCE text file. *
#* *
#* This program is distributed in the hope that it will be useful, *
#* but WITHOUT ANY WARRANTY; without even the implied warranty of *
#* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
#* GNU Library General Public License for more details. *
#* *
#* You should have received a copy of the GNU Library General Public *
#* License along with this program; if not, write to the Free Software *
#* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 *
#* USA *
#* *
#* Acknowledgements : *
#* *
#* Thanks to shoogen on the FreeCAD forum for programming advice *
#* and some code. *
#* *
#***************************************************************************
__title__ = "FreeCAD OpenSCAD Workbench - CSG exporter Version"
__author__ = "Keith Sloan <keith@sloan-home.co.uk>"
__url__ = ["http://www.sloan-home.co.uk/Export/Export.html"]
import FreeCAD
from builtins import open as pyopen
if FreeCAD.GuiUp:
gui = True
else:
gui = False
#***************************************************************************
# Tailor following to your requirements ( Should all be strings ) *
#fafs = '$fa = 12, $fs = 2'
#convexity = 'convexity = 10'
params = FreeCAD.ParamGet("User parameter:BaseApp/Preferences/Mod/OpenSCAD")
fa = params.GetFloat('exportFa', 12.0)
fs = params.GetFloat('exportFs', 2.0)
conv = params.GetInt('exportConvexity', 10)
fafs = '$fa = %f, $fs = %f' % (fa, fs)
convexity = 'convexity = %d' % conv
#***************************************************************************
# Radius values not fixed for value apart from cylinder & Cone
# no doubt there will be a problem when they do implement Value
def center(b):
if b == 2:
return 'true'
else:
return 'false'
def check_multmatrix(csg, ob, x, y, z):
b = FreeCAD.Vector(x,y,z)
if ob.Placement.isNull():
return 0 # center = false no mm
elif ob.Placement.Rotation.isNull() and \
(ob.Placement.Base - b).Length < 1e-6:
return 2 # center = true and no mm
else:
m = ob.Placement.toMatrix()
# adjust position for center displacements
csg.write("multmatrix([["+str(m.A11)+", "+str(m.A12)+", "+str(m.A13)+",\
"+str(m.A14)+"], ["\
+str(m.A21)+", "+str(m.A22)+", "+str(m.A23)+", "+str(m.A24)+"], ["\
+str(m.A31)+", "+str(m.A32)+", "+str(m.A33)+", "+str(m.A34)+"], [\
0, 0, 0, 1]]){\n")
return 1 # center = false and mm
def mesh2polyhedron(mesh):
pointstr = ','.join(['[%f,%f,%f]' % tuple(vec) for vec in mesh.Topology[0]])
trianglestr = ','.join(['[%d,%d,%d]' % tuple(tri) for tri in mesh.Topology[1]])
#avoid deprecation warning by changing triangles to faces
#return 'polyhedron ( points = [%s], triangles = [%s]);' % (pointstr, trianglestr)
return 'polyhedron ( points = [%s], faces = [%s]);' % (pointstr, trianglestr)
def vector2d(v):
return [v[0],v[1]]
def vertexs2polygon(vertex):
pointstr = ','.join(['[%f, %f]' % tuple(vector2d(v.Point)) for v in vertex])
return 'polygon ( points = [%s], paths = undef, convexity = 1);}' % pointstr
def shape2polyhedron(shape):
import MeshPart
return mesh2polyhedron(MeshPart.meshFromShape(Shape=shape,\
Deflection = params.GetFloat('meshdeflection', 0.0)))
def process_object(csg,ob):
print("Placement")
print("Pos : "+str(ob.Placement.Base))
print("axis : "+str(ob.Placement.Rotation.Axis))
print("angle : "+str(ob.Placement.Rotation.Angle))
if ob.TypeId == "Part::Sphere":
print("Sphere Radius : "+str(ob.Radius))
check_multmatrix(csg, ob, 0, 0, 0)
csg.write("sphere($fn = 0, "+fafs+", r = "+str(ob.Radius)+");\n")
elif ob.TypeId == "Part::Box":
print("cube : ("+ str(ob.Length)+","+str(ob.Width)+","+str(ob.Height)+")")
mm = check_multmatrix(csg,ob,-ob.Length/2,-ob.Width/2,-ob.Height/2)
csg.write("cube (size = ["+str(ob.Length.Value)+", "+str(ob.Width.Value)+", "+str(ob.Height.Value)+"], center = "+center(mm)+");\n")
if mm == 1 : csg.write("}\n")
elif ob.TypeId == "Part::Cylinder":
print("cylinder : Height "+str(ob.Height) + " Radius "+str(ob.Radius))
mm = check_multmatrix(csg, ob, 0, 0, -ob.Height/2)
csg.write("cylinder($fn = 0, "+fafs+", h = "+str(ob.Height.Value) + ", r1 = "+str(ob.Radius.Value)+\
", r2 = " + str(ob.Radius.Value) + ", center = "+center(mm)+");\n")
if mm == 1 : csg.write("}\n")
elif ob.TypeId == "Part::Cone":
print("cone : Height "+str(ob.Height) + " Radius1 "+str(ob.Radius1)+" Radius2 "+str(ob.Radius2))
mm = check_multmatrix(csg, ob, 0, 0, -ob.Height/2)
csg.write("cylinder($fn = 0, "+fafs+", h = "+str(ob.Height.Value)+ ", r1 = "+str(ob.Radius1.Value)+\
", r2 = "+str(ob.Radius2.Value)+", center = "+center(mm)+");\n")
if mm == 1 : csg.write("}\n")
elif ob.TypeId == "Part::Torus":
print("Torus")
print(ob.Radius1)
print(ob.Radius2)
if ob.Angle3 == 360.00:
mm = check_multmatrix(csg, ob, 0, 0, 0)
csg.write("rotate_extrude("+convexity+", $fn = 0, "+fafs+")\n")
csg.write("multmatrix([[1, 0, 0, "+str(ob.Radius1)+"], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]])\n")
csg.write("circle($fn = 0, "+fafs+", r = "+str(ob.Radius2)+");\n")
if mm == 1 : csg.write("}\n")
else: # Cannot convert to rotate extrude so best effort is polyhedron
csg.write('%s\n' % shape2polyhedron(ob.Shape))
elif ob.TypeId == "Part::Prism":
import math
f = str(ob.Polygon)
r = str(ob.Circumradius) # length seems to be the outer radius
h = str(ob.Height.Value)
mm = check_multmatrix(csg, ob, 0, 0, -float(h)/2)
csg.write("cylinder($fn = "+f+", "+fafs+", h = "+h+", r1 = "+r+\
", r2 = "+r+", center = "+center(mm)+");\n")
if mm == 1: csg.write("}\n")
elif ob.TypeId == "Part::RegularPolygon":
mm = check_multmatrix(csg, ob, 0, 0, -float(h)/2)
csg.write("circle($fn = "+str(ob.NumberOfSides)+", "+fafs+", r = "+str(ob.Radius)+");\n")
if mm == 1: csg.write("}\n")
elif ob.TypeId == "Part::Extrusion":
print("Extrusion")
print(ob.Base)
print(ob.Base.Name)
if ob.Base.isDerivedFrom('Part::Part2DObjectPython') and \
hasattr(ob.Base,'Proxy') and hasattr(ob.Base.Proxy, 'TypeId'):
ptype = ob.Base.Proxy.TypeId
if ptype == "Polygon":
f = str(ob.Base.FacesNumber)
r = str(ob.Base.Radius)
h = str(ob.Dir[2])
print("Faces : " + f)
print("Radius : " + r)
print("Height : " + h)
mm = check_multmatrix(csg,ob,0,0,-float(h)/2)
csg.write("cylinder($fn = "+f+", "+fafs+", h = "+h+", r1 = "+r+\
", r2 = "+r+", center = "+center(mm)+");\n")
if mm == 1: csg.write("}\n")
elif ptype == "Circle":
r = str(ob.Base.Radius)
h = str(ob.Dir[2])
print("Radius : " + r)
print("Height : " + h)
mm = check_multmatrix(csg,ob,0,0,-float(h)/2)
csg.write("cylinder($fn = 0, "+fafs+", h = "+h+", r1 = "+r+\
", r2 = "+r+", center = "+center(mm)+");\n")
if mm == 1: csg.write("}\n")
elif ptype == "Wire":
print("Wire extrusion")
print(ob.Base)
mm = check_multmatrix(csg, ob, 0, 0, 0)
csg.write("linear_extrude(height = "+str(ob.Dir[2])+", center = "+center(mm)+", "+convexity+", twist = 0, slices = 2, $fn = 0, "+fafs+")\n{\n")
csg.write(vertexs2polygon(ob.Base.Shape.Vertexes))
if mm == 1: csg.write("}\n")
elif ob.Base.isDerivedFrom('Part::Plane'):
mm = check_multmatrix(csg,ob,0,0,0)
csg.write("linear_extrude(height = "+str(ob.Dir[2])+", center = true, "+convexity+", twist = 0, slices = 2, $fn = 0, "+fafs+")\n{\n")
csg.write("square (size = ["+str(ob.Base.Length.Value)+", "+str(ob.Base.Width.Value)+"], center = "+center(mm)+");\n}\n")
if mm == 1: csg.write("}\n")
elif ob.Base.Name.startswith('this_is_a_bad_idea'):
pass
else:
pass # There should be a fallback solution
elif ob.TypeId == "Part::Cut":
print("Cut")
csg.write("difference() {\n")
process_object(csg,ob.Base)
process_object(csg,ob.Tool)
csg.write("}\n")
elif ob.TypeId == "Part::Fuse":
print("union")
csg.write("union() {\n")
process_object(csg,ob.Base)
process_object(csg,ob.Tool)
csg.write("}\n")
elif ob.TypeId == "Part::Common":
print("intersection")
csg.write("intersection() {\n")
process_object(csg,ob.Base)
process_object(csg,ob.Tool)
csg.write("}\n")
elif ob.TypeId == "Part::MultiFuse":
print("Multi Fuse / union")
csg.write("union() {\n")
for subobj in ob.Shapes:
process_object(csg,subobj)
csg.write("}\n")
elif ob.TypeId == "Part::MultiCommon":
print("Multi Common / intersection")
csg.write("intersection() {\n")
for subobj in ob.Shapes:
process_object(csg,subobj)
csg.write("}\n")
elif ob.isDerivedFrom('Part::Feature'):
print("Part::Feature")
mm = check_multmatrix(csg,ob,0,0,0)
csg.write('%s\n' % shape2polyhedron(ob.Shape))
if mm == 1 : csg.write("}\n")
def export(exportList, filename):
"called when FreeCAD exports a file"
# process Objects
print("\nStart Export 0.1d\n")
print("Open Output File")
csg = pyopen(filename,'w')
print("Write Initial Output")
# Not sure if comments as per scad are allowed in csg file
csg.write("// CSG file generated from FreeCAD %s\n" % \
'.'.join(FreeCAD.Version()[0:3]))
#write initial group statements - not sure if required
csg.write("group() {\n group(){\n")
for ob in exportList:
print(ob)
print("Name : " + ob.Name)
print("Type : " + ob.TypeId)
print("Shape : ")
print(ob.Shape)
process_object(csg, ob)
# write closing group braces
csg.write("}\n}\n")
# close file
csg.close()
FreeCAD.Console.PrintMessage("successfully exported" + " " + filename)
|