Vcarve SVG prep work
Posted: Tue Jan 11, 2022 7:22 pm
I wrote a small (and very inefficient) Python script to automate the prep work in order to V-carve a SVG graphic (as described here: https://wiki.freecadweb.org/Path_Vcarve).
As said it's very inefficient and will take forever for more complex shapes but for simple shapes it's definitely faster than running the operation by hand.
Maybe some of you find that script helpful:
As said it's very inefficient and will take forever for more complex shapes but for simple shapes it's definitely faster than running the operation by hand.
Maybe some of you find that script helpful:
Code: Select all
import FreeCAD as App
import Draft
# First convert all wires to faces
objects = App.ActiveDocument.Objects
for object in objects:
print("Name:{}\t Label: {}\t ".format(object.Name,object.Label ) )
print(" => Faces:{}".format(len(object.Shape.Faces)))
faces = len(object.Shape.Faces)
if (faces == 0):
oldname = object.Name
add_list, delete_list = Draft.upgrade(object, delete=True)
add_list[0].Label = oldname
else:
oldname = object.Name
add_list, delete_list = Draft.downgrade(object, delete=True)
add_list1, delete_list1 = Draft.upgrade(add_list[0], delete=True)
add_list1[0].Label = oldname
App.ActiveDocument.recompute()
# Put objects with the same name in a group
objects = App.ActiveDocument.Objects
basenames = {}
for object in objects:
print("Name:{}\t Label: {}\t ".format(object.Name,object.Label ) )
name = object.Label
basenames[name] = [ object ]
if (len(object.Shape.Faces) > 0):
face = object.Shape.Faces[0]
for subobject in objects:
thisname = subobject.Label
if (thisname.startswith(name) and thisname != name):
print ("Candidate for adding: {} to list {}".format(thisname, name))
isinside=True
for vertex in subobject.Shape.Vertexes:
if (face.isInside(vertex.Point, 0.000001, True) == False):
isinside = False
if (isinside):
basenames[name].append( subobject )
print (" ==> Added")
# Test reverse (shorter name contained in longer name)
else:
isinside = True
if (len(subobject.Shape.Faces) > 0):
face = subobject.Shape.Faces[0]
for vertex in object.Shape.Vertexes:
if (face.isInside(vertex.Point, 0.000001, True) == False):
isinside = False
if (isinside):
basenames[name].append( subobject )
print (" ==> Added")
print (basenames)
# Cut away the smaller parts from the larger ones
for group in basenames:
list= sorted(basenames[group], key=lambda x: x.Shape.Faces[0].Area, reverse=True)
if (len(list) > 1):
Draft.downgrade(list, False)
for o in list:
o.Visibility = False
App.ActiveDocument.recompute()