From 2329ac38e88f5fcdbd892000b362e2e14665ba04 Mon Sep 17 00:00:00 2001 From: looooo Date: Tue, 3 Oct 2017 22:52:49 +0200 Subject: [PATCH] MeshFlattening: add function to unwrap mesh --- src/Mod/Mesh/Gui/AppMeshGui.cpp | 7 +++ src/Mod/Mesh/Gui/Workbench.cpp | 2 +- src/Mod/Mesh/InitGui.py | 7 +-- src/Mod/MeshPart/App/MeshFlattening.cpp | 3 +- src/Mod/MeshPart/Gui/MeshFlatteningCommand.py | 50 ++++++++++++------- 5 files changed, 44 insertions(+), 25 deletions(-) diff --git a/src/Mod/Mesh/Gui/AppMeshGui.cpp b/src/Mod/Mesh/Gui/AppMeshGui.cpp index b6b8628c68..d85fccf5d9 100644 --- a/src/Mod/Mesh/Gui/AppMeshGui.cpp +++ b/src/Mod/Mesh/Gui/AppMeshGui.cpp @@ -112,6 +112,13 @@ PyMOD_INIT_FUNC(MeshGui) // instantiating the commands CreateMeshCommands(); (void)new MeshGui::CleanupHandler; + + // try to instantiate flat-mesh commands + try{ + Base::Interpreter().runString("import MeshFlatteningCommand"); + } catch (Base::PyException &err){ + err.ReportException(); + } // register preferences pages (void)new Gui::PrefPageProducer ("Display"); diff --git a/src/Mod/Mesh/Gui/Workbench.cpp b/src/Mod/Mesh/Gui/Workbench.cpp index b3fe938555..3545df2135 100644 --- a/src/Mod/Mesh/Gui/Workbench.cpp +++ b/src/Mod/Mesh/Gui/Workbench.cpp @@ -193,7 +193,7 @@ Gui::MenuItem* Workbench::setupMenuBar() const << "Mesh_Merge" << "Mesh_PolySelect" << "Mesh_PolyCut" << "Mesh_PolySplit" << "Mesh_PolySegm" << "Mesh_PolyTrim" << "Separator" << "Mesh_TrimByPlane" << "Mesh_SectionByPlane" << "Mesh_Segmentation" - << "Mesh_VertexCurvature"; + << "Mesh_VertexCurvature" << "CreateFlatMesh"; return root; } diff --git a/src/Mod/Mesh/InitGui.py b/src/Mod/Mesh/InitGui.py index fd4b85696a..51c683f5e4 100644 --- a/src/Mod/Mesh/InitGui.py +++ b/src/Mod/Mesh/InitGui.py @@ -39,13 +39,8 @@ class MeshWorkbench (Workbench): def Initialize(self): import Mesh import MeshGui - # try: import MeshFlatteningCommand - toolbar = MeshFlatteningCommand.initialize() - self.appendToolbar(toolbar) - # except ImportError: - # import FreeCAD as app - # app.Message("MeshPart not found") + def GetClassName(self): return "MeshGui::Workbench" diff --git a/src/Mod/MeshPart/App/MeshFlattening.cpp b/src/Mod/MeshPart/App/MeshFlattening.cpp index e520a7461e..3125026f76 100644 --- a/src/Mod/MeshPart/App/MeshFlattening.cpp +++ b/src/Mod/MeshPart/App/MeshFlattening.cpp @@ -174,7 +174,8 @@ void FaceUnwrapper::findFlatNodes() std::vector fixed_pins; //TODO: INPUT LscmRelax mesh_flattener(this->xyz_nodes.transpose(), this->tris.transpose(), fixed_pins); mesh_flattener.lscm(); - mesh_flattener.relax(0.9); + for (int j=0; j<9; j++) + mesh_flattener.relax(0.9); this->ze_nodes = mesh_flattener.flat_vertices.transpose(); } diff --git a/src/Mod/MeshPart/Gui/MeshFlatteningCommand.py b/src/Mod/MeshPart/Gui/MeshFlatteningCommand.py index 5667fe4cb6..1b0229f3aa 100644 --- a/src/Mod/MeshPart/Gui/MeshFlatteningCommand.py +++ b/src/Mod/MeshPart/Gui/MeshFlatteningCommand.py @@ -1,42 +1,58 @@ -import numpy as np +import FreeCADGui as gui import Mesh -import FreeCAD as app -import flatMesh -import Part - - +import FreeCAD as App +import FreeCADGui as gui class BaseCommand(object): def __init__(self): pass def IsActive(self): - if FreeCAD.ActiveDocument is None: + if App.ActiveDocument is None: return False else: return True class CreateFlatMesh(BaseCommand): - """create an involute gear""" + """create flat wires from a meshed face""" def GetResources(self): - return {'Pixmap': 'not_yet.svg', 'MenuText': 'unwrap mesh', 'ToolTip': 'find a flat representation of a mesh'} + return {'MenuText': 'Unwrap Mesh', 'ToolTip': 'find a flat representation of a mesh'} def Activated(self): - obj = Gui.Selection.getSelection()[0] # obj must be a Mesh (Mesh-Design->Meshes->Create-Mesh) + import numpy as np + import flatmesh + import Part + obj = gui.Selection.getSelection()[0] # obj must be a Mesh (Mesh-Design->Meshes->Create-Mesh) mesh = Mesh.Mesh(obj.Mesh) # copy of the mesh to set new vertices later on points = np.array([[i.x, i.y, i.z] for i in obj.Mesh.Points]) faces = np.array([list(i) for i in obj.Mesh.Topology[1]]) - flattener = flatMesh.FaceUnwrapper(points, faces) + print(faces) + flattener = flatmesh.FaceUnwrapper(points, faces) flattener.findFlatNodes() boundaries = flattener.getFlatBoundaryNodes() wires = [] for edge in boundaries: - pi = Part.makePolygon([app.Vector(*node) for node in edge]) - wires.append(pi) - Part.show(Part.Wire(wires)) + pi = Part.makePolygon([App.Vector(*node) for node in edge]) + Part.show(Part.Wire(pi)) + def IsActive(self): + assert(super(CreateFlatMesh, self).IsActive()) + assert(isinstance(gui.Selection.getSelection()[0].Mesh, Mesh.Mesh)) + return True -def initialize(): - Gui.addCommand('CreateFlatMesh', CreateFlatMesh()) - return ["CreateFlatMesh"] \ No newline at end of file +class CreateFlatFace(BaseCommand): + """create a flat face from a single face + only full faces are supported right now""" + + def GetResources(self): + return {'MenuText': 'Unwrap Face', 'ToolTip': 'find a flat representation of a mesh'} + + def IsActive(self): + assert(super(CreateFlatMesh, self).IsActive()) + assert(isinstance(gui.Selection.getSelection()[0], Part.Face)) + return True + + + +gui.addCommand('CreateFlatMesh', CreateFlatMesh())