Draft: Upgrade tool is now available to python scripting

This commit is contained in:
Yorik van Havre
2013-02-17 11:24:12 -03:00
parent c0b0cb708e
commit ef02e37653
2 changed files with 369 additions and 268 deletions

View File

@@ -1200,10 +1200,11 @@ def offset(obj,delta,copy=False,bind=False,sym=False,occ=False):
select(obj)
return newobj
def draftify(objectslist,makeblock=False):
'''draftify(objectslist,[makeblock]): turns each object of the given list
def draftify(objectslist,makeblock=False,delete=True):
'''draftify(objectslist,[makeblock],[delete]): turns each object of the given list
(objectslist can also be a single object) into a Draft parametric
wire. If makeblock is True, multiple objects will be grouped in a block'''
wire. If makeblock is True, multiple objects will be grouped in a block.
If delete = False, old objects are not deleted'''
import DraftGeomUtils, Part
if not isinstance(objectslist,list):
@@ -1226,7 +1227,8 @@ def draftify(objectslist,makeblock=False):
nobj.ViewObject.DisplayMode = "Wireframe"
newobjlist.append(nobj)
formatObject(nobj,obj)
FreeCAD.ActiveDocument.removeObject(obj.Name)
if delete:
FreeCAD.ActiveDocument.removeObject(obj.Name)
FreeCAD.ActiveDocument.recompute()
if makeblock:
return makeBlock(newobjlist)
@@ -1731,8 +1733,359 @@ def heal(objlist=None,delete=True,reparent=True):
if dellist and delete:
for n in dellist:
FreeCAD.ActiveDocument.removeObject(n)
def upgrade(objects,deelte=False,force=None):
"""upgrade(objects,delete=False,force=None): Upgrades the given object(s) (can be
an object or a list of objects). If delete is True, old objects are deleted.
The force attribute can be used to
force a certain way of upgrading. It can be: makeCompound, closeGroupWires,
makeSolid, closeWire, turnToParts, makeFusion, makeShell, makeFaces, turnToDraft,
joinFaces, makeSketchFace, makeWires
Returns a dictionnary containing two lists, a list of new objects and a list
of objects to be deleted"""
import Part, DraftGeomUtils
from DraftTools import msg,translate
if not isinstance(objects,list):
objects = [objects]
global deleteList, newList
deleteList = []
addList = []
# definitions of actions to perform
def makeCompound(objectslist):
"""returns a compound object made from the given objects"""
newobj = makeBlock(objectslist)
addList.append(newobj)
return newobj
def closeGroupWires(groupslist):
"""closes every open wire in the given groups"""
result = False
for grp in groupslist:
for obj in grp.Group:
newobj = closeWire(obj)
# add new objects to their respective groups
if newobj:
result = True
grp.addObject(newobj)
return result
def makeSolid(obj):
"""turns an object into a solid, if possible"""
if obj.Shape.Solids:
return None
sol = None
try:
sol = Part.makeSolid(obj.Shape)
except:
return None
else:
if sol:
if sol.isClosed():
newobj = FreeCAD.ActiveDocument.addObject("Part::Feature","Solid")
newobj.Shape = sol
addList.append(newobj)
deleteList.append(obj)
return newob
def closeWire(obj):
"""closes a wire object, if possible"""
if obj.Shape.Faces:
return None
if len(obj.Shape.Wires) != 1:
return None
if len(obj.Shape.Edges) == 1:
return None
if getType(obj) == "Wire":
obj.Closed = True
return True
else:
w = obj.Shape.Wires[0]
if not w.isClosed():
edges = w.Edges
p0 = w.Vertexes[0].Point
p1 = w.Vertexes[-1].Point
if p0 == p1:
# sometimes an open wire can have its start and end points identical (OCC bug)
# in that case, although it is not closed, face works...
f = Part.Face(w)
newobj = FreeCAD.ActiveDocument.addObject("Part::Feature","Face")
newobj.Shape = f
else:
edges.append(Part.Line(p1,p0).toShape())
w = Part.Wire(DraftGeomUtils.sortEdges(edges))
newobj = FreeCAD.ActiveDocument.addObject("Part::Feature","Wire")
newobj.Shape = w
addList.append(newobj)
deleteList.append(obj)
return newobj
else:
return None
def turnToParts(meshes):
"""turn given meshes to parts"""
result = False
import Arch
for mesh in meshes:
sh = Arch.getShapeFromMesh(mesh.Mesh)
if sh:
newobj = FreeCAD.ActiveDocument.addObject("Part::Feature","Shell")
newobj.Shape = sh
addList.append(newobj)
deleteList.append(mesh)
result = True
return result
def makeFusion(obj1,obj2):
"""makes a Draft or Part fusion between 2 given objects"""
newobj = fuse(obj1,obj2)
if newobj:
addList.append(newobj)
return newobj
return None
def makeShell(objectslist):
"""makes a shell with the given objects"""
faces = []
for obj in objectslist:
faces.append(obj.Shape.Faces)
sh = Part.makeShell(faces)
if sh:
if sh.Faces:
newob = FreeCAD.ActiveDocument.addObject("Part::Feature","Shell")
newob.Shape = sh
addList.append(newobj)
deleteList.extend(objectslist)
return newobj
return None
def joinFaces(objectslist):
"""makes one big face from selected objects, if possible"""
faces = []
for obj in objectslist:
faces.append(obj.Shape.Faces)
u = faces.pop(0)
for f in faces:
u = u.fuse(f)
if DraftGeomUtils.isCoplanar(faces):
u = DraftGeomUtils.concatenate(u)
if not DraftGeomUtils.hasCurves(u):
# several coplanar and non-curved faces: they can becoem a Draft wire
newobj = makeWire(u.Wires[0],closed=True,face=True)
else:
# if not possible, we do a non-parametric union
newobj = FreeCAD.ActiveDocument.addObject("Part::Feature","Union")
newobj.Shape = u
addList.append(newobj)
deleteList.extend(objectslist)
return newobj
return None
def turnToDraft(objectslist):
"""turns each of the objects into a Draft object, if possible"""
newobj = draftify(objects,delete=False)
if newobj:
if not isinstance(newobj,list):
newobj = [newobj]
addList.extend(newobj)
deleteList.extend(objectslist)
return newobj
return None
def makeSketchFace(obj):
"""Makes a Draft face out of a sketch"""
newobj = makeWire(obj.Shape,closed=True)
if newobj:
newobj.Base = obj
obj.ViewObject.Visibility = False
addList.append(newobj)
return newobj
return None
def makeFaces(objectslist):
"""make a face from every closed wire in the list"""
result = False
for o in objectslist:
for w in o.Shape.Wires:
if w.isClosed() and DraftGeomUtils.isPlanar(w):
f = Part.Face(w)
if f:
newobj = FreeCAD.ActiveDocument.addObject("Part::Feature","Face")
newobj.Shape = f
addList.append(newobj)
result = True
return result
def makeWires(objectslist):
"""joins edges in the given objects list into wires"""
edges = []
for o in objectslist:
for e in o.Shape.Edges:
edges.append(e)
try:
nedges = DraftGeomUtils.sortEdges(edges[:])
# for e in nedges: print "debug: ",e.Curve,e.Vertexes[0].Point,e.Vertexes[-1].Point
w = Part.Wire(nedges)
except:
return None
else:
if len(w.Edges) == len(edges):
newobj = FreeCAD.ActiveDocument.addObject("Part::Feature","Wire")
newobj.Shape = w
addList.append(newobj)
deleteList.extend(objectslist)
return True
return None
# analyzing what we have in our selection
edges = []
wires = []
openwires = []
faces = []
groups = []
parts = []
curves = []
facewires = []
loneedges = []
meshes = []
for ob in objects:
if ob.Type == "App::DocumentObjectGroup":
groups.append(ob)
elif ob.isDerivedFrom("Part::Feature"):
parts.append(ob)
faces.extend(ob.Shape.Faces)
wires.extend(ob.Shape.Wires)
edges.extend(ob.Shape.Edges)
for f in ob.Shape.Faces:
facewires.extend(f.Wires)
wirededges = []
for w in ob.Shape.Wires:
if len(w.Edges) > 1:
for e in w.Edges:
wirededges.append(e.hashCode())
if not w.isClosed():
openwires.append(w)
for e in ob.Shape.Edges:
if not isinstance(e.Curve,Part.Line):
curves.append(e)
if not e.hashCode() in wirededges:
loneedges.append(e)
elif ob.isDerivedFrom("Mesh::Feature"):
meshes.append(ob)
objects = parts
print "objects:",objects," edges:",edges," wires:",wires," openwires:",openwires," faces:",faces
print "groups:",groups," curves:",curves," facewires:",facewires, "loneedges:", loneedges
if force:
if force in ["makeCompound","closeGroupWires","makeSolid","closeWire","turnToParts","makeFusion",
"makeShell","makeFaces","turnToDraft","joinFaces","makeSketchFace","makeWires"]:
result = eval(force)(objects)
else:
msg(translate("Upgrade: Unknow force method:")+" "+force)
result = None
else:
# applying transformations automatically
result = None
# if we have a group: turn each closed wire inside into a face
if groups:
result = closeGroupWires(groups)
if result: msg(translate("draft", "Found groups: closing each open object inside\n"))
# if we have meshes, we try to turn them into shapes
elif meshes:
result = turnToParts(meshes)
if result: msg(translate("draft", "Found mesh(es): turning into Part shapes\n"))
# we have only faces here, no lone edges
elif faces and (len(wires) + len(openwires) == len(facewires)):
# we have one shell: we try to make a solid
if (len(objects) == 1) and (len(faces) > 3):
result = makeSolid(objects[0])
if result: msg(translate("draft", "Found 1 solidificable object: solidifying it\n"))
# we have exactly 2 objects: we fuse them
elif (len(objects) == 2) and (not curves):
result = makeFusion(objects[0],objects[1])
if result: msg(translate("draft", "Found 2 objects: fusing them\n"))
# we have many separate faces: we try to make a shell
elif (len(objects) > 2) and (len(faces) > 1) and (not loneedges):
result = makeShell(objects)
if result: msg(translate("draft", "Found several objects: making a shell\n"))
# we have faces: we try to join them if they are coplanar
elif len(faces) > 1:
result = joinFaces(objects)
if result: msg(translate("draft", "Found several coplanar objects or faces: making one face\n"))
# only one object: if not parametric, we "draftify" it
elif len(objects) == 1:
result = turnToDraft(objects)
if result: msg(translate("draft", "Found 1 non-parametric objects: draftifying it\n"))
# we have only closed wires, no faces
elif wires and (not faces) and (not openwires):
# we have a sketch: Extract a face
if (len(objects) == 1) and objects[0].isDerivedFrom("Sketcher::SketchObject") and (not curves):
result = makeSketchFace(objects[0])
if result: msg(translate("draft", "Found 1 closed sketch object: making a face from it\n"))
# only closed wires
else:
result = makeFaces(objects)
if result: msg(translate("draft", "Found closed wires: making faces\n"))
# special case, we have only one open wire. We close it, unless it has only 1 edge!"
elif (len(openwires) == 1) and (not faces) and (not loneedges):
result = closeWire(objects[0])
if result: msg(translate("draft", "Found 1 open wire: closing it\n"))
# only open wires and edges: we try to join their edges
elif openwires and (not wires) and (not faces):
result = makeWires(objects)
if result: msg(translate("draft", "Found several open wires: joining them\n"))
# only loneedges: we try to join them
elif loneedges and (not facewires):
result = makeWires(objects)
if result: msg(translate("draft", "Found several edges: wiring them\n"))
# only one selected object, do not make a compound
elif (len(objects) == 1):
result = None
# all other cases
else:
result = makeCompound(objects)
if result: msg(translate("draft", "Found several non-treatable objects: making compound\n"))
# no result has been obtained
if not result:
msg(translate("draft", "Unable to upgrade these objects\n"))
if delete:
names = []
for o in deleteList:
names.append(o.Name)
deleteList = []
for n in names:
FreeCAD.ActiveDocument.removeObject(n)
return [addList,deleteList]
#---------------------------------------------------------------------------
# Python Features definitions
#---------------------------------------------------------------------------