def makeFlatFace(mobile=[], fixed=[], vert=False): import Part import DraftVecUtils import DraftGeomUtils if not fixed: pol = Part.makePolygon(mobile + [mobile[0]]) pol = DraftGeomUtils.flattenWire(pol) return Part.Face(pol) elif len(fixed) == 3: tempf = Part.Face(Part.makePolygon(fixed + [fixed[0]])) v4 = mobile[0].add( DraftVecUtils.project(tempf.CenterOfMass.sub(mobile[0]), tempf.normalAt(0, 0))) pol = Part.makePolygon([fixed[0], fixed[1], v4, fixed[2], fixed[0]]) pol = DraftGeomUtils.flattenWire(pol) return Part.Face(pol) elif len(fixed) == 2: tp = DraftGeomUtils.findMidpoint( Part.LineSegment(mobile[0], mobile[1]).toShape()) tempf = Part.Face(Part.makePolygon(fixed + [tp, fixed[0]])) v4 = mobile[0].add( DraftVecUtils.project(tempf.CenterOfMass.sub(mobile[0]), tempf.normalAt(0, 0))) v5 = mobile[1].add( DraftVecUtils.project(tempf.CenterOfMass.sub(mobile[1]), tempf.normalAt(0, 0))) if vert: pol = Part.makePolygon([fixed[0], v4, v5, fixed[1], fixed[0]]) else: pol = Part.makePolygon(fixed + [v4, v5, fixed[0]]) pol = DraftGeomUtils.flattenWire(pol) return Part.Face(pol)
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 flatten(self): if hasattr(self, "Object"): if len(self.Object.Shape.Wires) == 1: fw = DraftGeomUtils.flattenWire(self.Object.Shape.Wires[0]) points = [v.Point for v in fw.Vertexes] if len(points) == len(self.Object.Points): if points != self.Object.Points: App.ActiveDocument.openTransaction("Flatten wire") Gui.doCommand( "FreeCAD.ActiveDocument." + self.Object.Name + ".Points=" + str(points).replace( "Vector", "FreeCAD.Vector").replace(" ", "")) App.ActiveDocument.commitTransaction() else: _flat = "This Wire is already flat" _msg(QT_TRANSLATE_NOOP("Draft", _flat))