def getShapeFromMesh(mesh): import Part, MeshPart if mesh.isSolid() and (mesh.countComponents() == 1): # use the best method faces = [] for f in mesh.Facets: p = f.Points + [f.Points[0]] pts = [] for pp in p: pts.append(FreeCAD.Vector(pp[0], pp[1], pp[2])) faces.append(Part.Face(Part.makePolygon(pts))) shell = Part.makeShell(faces) solid = Part.Solid(shell) solid = solid.removeSplitter() return solid faces = [] segments = mesh.getPlanarSegments( 0.001) # use rather strict tolerance here for i in segments: if len(i) > 0: wires = MeshPart.wireFromSegment(mesh, i) if wires: faces.append(makeFace(wires)) try: se = Part.makeShell(faces) except: return None else: try: solid = Part.Solid(se) except: return se else: return solid
def getShapeFromMesh(mesh): import Part, MeshPart if mesh.isSolid() and (mesh.countComponents() == 1): # use the best method faces = [] for f in mesh.Facets: p=f.Points+[f.Points[0]] pts = [] for pp in p: pts.append(FreeCAD.Vector(pp[0],pp[1],pp[2])) faces.append(Part.Face(Part.makePolygon(pts))) shell = Part.makeShell(faces) solid = Part.Solid(shell) solid = solid.removeSplitter() return solid faces = [] segments = mesh.getPlanarSegments(0.001) # use rather strict tolerance here for i in segments: if len(i) > 0: wires = MeshPart.wireFromSegment(mesh, i) if wires: faces.append(makeFace(wires)) try: se = Part.makeShell(faces) except: return None else: try: solid = Part.Solid(se) except: return se else: return solid
def wiresFromFaceGroups(mesh, faceGroups): wires = [] for faceGroup in faceGroups: wires.append(MeshPart.wireFromSegment(mesh, faceGroup.toSegment())) return wires
def getShapeFromMesh(mesh,fast=True,tolerance=0.001,flat=False,cut=True): import Part, MeshPart, DraftGeomUtils if mesh.isSolid() and (mesh.countComponents() == 1) and fast: # use the best method faces = [] for f in mesh.Facets: p=f.Points+[f.Points[0]] pts = [] for pp in p: pts.append(FreeCAD.Vector(pp[0],pp[1],pp[2])) try: f = Part.Face(Part.makePolygon(pts)) except: pass else: faces.append(f) shell = Part.makeShell(faces) solid = Part.Solid(shell) solid = solid.removeSplitter() return solid faces = [] segments = mesh.getPlanarSegments(tolerance) #print len(segments) for i in segments: if len(i) > 0: wires = MeshPart.wireFromSegment(mesh, i) if wires: if flat: nwires = [] for w in wires: nwires.append(DraftGeomUtils.flattenWire(w)) wires = nwires try: faces.append(makeFace(wires,method=int(cut)+1)) except: return None try: se = Part.makeShell(faces) se = se.removeSplitter() if flat: return se except Part.OCCError: try: cp = Part.makeCompound(faces) except Part.OCCError: return None else: return cp else: try: solid = Part.Solid(se) except Part.OCCError: return se else: return solid
def convert_meshes_to_part_objects_2(): import Part doc = FreeCAD.newDocument("Box_5") box = doc.addObject("Part::Box", "myBox") box.Height = 5 box.Length = 5 box.Width = 5 doc.recompute() import Mesh import Part import MeshPart obj = box # a Mesh object must be preselected mesh = obj.Mesh segments = mesh.getPlanarSegments( 0.00001) # use rather strict tolerance here faces = [] for i in segments: if len(i) > 0: # a segment can have inner holes wires = MeshPart.wireFromSegment(mesh, i) # we assume that the exterior boundary is that one with the biggest bounding box if len(wires) > 0: ext = None max_length = 0 for i in wires: if i.BoundBox.DiagonalLength > max_length: max_length = i.BoundBox.DiagonalLength ext = i wires.remove(ext) # all interior wires mark a hole and must reverse their orientation, otherwise Part.Face fails for i in wires: i.reverse() # make sure that the exterior wires comes as first in the list wires.insert(0, ext) faces.append(Part.Face(wires)) solid = Part.Solid(Part.Shell(faces)) Part.show(solid) setview()
def meshToShape(obj,mark=True): '''meshToShape(object,[mark]): turns a mesh into a shape, joining coplanar facets. If mark is True (default), non-solid objects will be marked in red''' name = obj.Name import Part,MeshPart from draftlibs import fcgeo if "Mesh" in obj.PropertiesList: faces = [] mesh = obj.Mesh plac = obj.Placement segments = mesh.getPlanes(0.001) # use rather strict tolerance here print len(segments)," segments ",segments for i in segments: print "treating",segments.index(i),i if len(i) > 0: wires = MeshPart.wireFromSegment(mesh, i) print "wire done" print wires if wires: faces.append(makeFace(wires)) print "done facing" print "faces",faces try: se = Part.makeShell(faces) solid = Part.Solid(se) except: pass else: if solid.isClosed(): FreeCAD.ActiveDocument.removeObject(name) newobj = FreeCAD.ActiveDocument.addObject("Part::Feature",name) newobj.Shape = solid newobj.Placement = plac if not solid.isClosed(): if mark: newobj.ViewObject.ShapeColor = (1.0,0.0,0.0,1.0) return newobj return None
def meshToShape(obj, mark=True): '''meshToShape(object,[mark]): turns a mesh into a shape, joining coplanar facets. If mark is True (default), non-solid objects will be marked in red''' name = obj.Name import Part, MeshPart, DraftGeomUtils if "Mesh" in obj.PropertiesList: faces = [] mesh = obj.Mesh plac = obj.Placement segments = mesh.getPlanes(0.001) # use rather strict tolerance here print len(segments), " segments ", segments for i in segments: print "treating", segments.index(i), i if len(i) > 0: wires = MeshPart.wireFromSegment(mesh, i) print "wire done" print wires if wires: faces.append(makeFace(wires)) print "done facing" print "faces", faces try: se = Part.makeShell(faces) solid = Part.Solid(se) except: pass else: if solid.isClosed(): FreeCAD.ActiveDocument.removeObject(name) newobj = FreeCAD.ActiveDocument.addObject("Part::Feature", name) newobj.Shape = solid newobj.Placement = plac if not solid.isClosed(): if mark: newobj.ViewObject.ShapeColor = (1.0, 0.0, 0.0, 1.0) return newobj return None
# FreeCAD TemplatePyMod module # (c) 2010 Werner Mayer LGPL import Mesh, Part, MeshPart faces = [] mesh = App.ActiveDocument.ActiveObject.Mesh segments = mesh.getPlanarSegments(0.00001) # use rather strict tolerance here for i in segments: if len(i) > 0: # a segment can have inner holes wires = MeshPart.wireFromSegment(mesh, i) # we assume that the exterior boundary is that one with the biggest bounding box if len(wires) > 0: ext = None max_length = 0 for i in wires: if i.BoundBox.DiagonalLength > max_length: max_length = i.BoundBox.DiagonalLength ext = i wires.remove(ext) # all interior wires mark a hole and must reverse their orientation, otherwise Part.Face fails for i in wires: i.reverse() # make sure that the exterior wires comes as first in the lsit wires.insert(0, ext) faces.append(Part.Face(wires))
def meshToShape(obj, mark=True): '''meshToShape(object,[mark]): turns a mesh into a shape, joining coplanar facets. If mark is True (default), non-solid objects will be marked in red''' name = obj.Name import Part, MeshPart from draftlibs import fcgeo if "Mesh" in obj.PropertiesList: faces = [] mesh = obj.Mesh plac = obj.Placement segments = mesh.getPlanes(0.001) # use rather strict tolerance here print len(segments), " segments ", segments for i in segments: print "treating", segments.index(i), i if len(i) > 0: wires = MeshPart.wireFromSegment(mesh, i) print "wire done" print wires if len(wires) > 1: # a segment can have inner holes print "inner wires found" ext = None max_length = 0 # cleaning up rubbish in wires for i in range(len(wires)): wires[i] = fcgeo.removeInterVertices(wires[i]) for w in wires: # we assume that the exterior boundary is that one with # the biggest bounding box if w.BoundBox.DiagonalLength > max_length: max_length = w.BoundBox.DiagonalLength ext = w print "exterior wire", ext wires.remove(ext) # all interior wires mark a hole and must reverse # their orientation, otherwise Part.Face fails for w in wires: print "reversing", w #w.reverse() print "reversed" # make sure that the exterior wires comes as first in the list wires.insert(0, ext) print "done sorting", wires if wires: faces.append(Part.Face(wires)) print "done facing" print "faces", faces try: se = Part.makeShell(faces) solid = Part.Solid(se) except: pass else: if solid.isClosed(): FreeCAD.ActiveDocument.removeObject(name) newobj = FreeCAD.ActiveDocument.addObject("Part::Feature", name) newobj.Shape = solid newobj.Placement = plac if not solid.isClosed(): if mark: newobj.ViewObject.ShapeColor = (1.0, 0.0, 0.0, 1.0) return newobj return None
# FreeCAD TemplatePyMod module # (c) 2010 Werner Mayer LGPL import Mesh,Part,MeshPart faces = [] mesh = App.ActiveDocument.ActiveObject.Mesh segments = mesh.getPlanarSegments(0.00001) # use rather strict tolerance here for i in segments: if len(i) > 0: # a segment can have inner holes wires = MeshPart.wireFromSegment(mesh, i) # we assume that the exterior boundary is that one with the biggest bounding box if len(wires) > 0: ext=None max_length=0 for i in wires: if i.BoundBox.DiagonalLength > max_length: max_length = i.BoundBox.DiagonalLength ext = i wires.remove(ext) # all interior wires mark a hole and must reverse their orientation, otherwise Part.Face fails for i in wires: i.reverse() # make sure that the exterior wires comes as first in the list wires.insert(0, ext) faces.append(Part.Face(wires))