Flipping Horizontally (Mirroring) A Mesh...

Post here for help on using FreeCAD's graphical user interface (GUI).
Forum rules
and Helpful information
IMPORTANT: Please click here and read this first, before asking for help

Also, be nice to others! Read the FreeCAD code of conduct!
R23D
Posts: 135
Joined: Sat Jan 25, 2020 5:23 am

Flipping Horizontally (Mirroring) A Mesh...

Post by R23D »

OS: Windows 7
Word size of OS: 64-bit
Word size of FreeCAD: 64-bit
Version: 0.18.4 (GitTag)
Build type: Release
Branch: releases/FreeCAD-0-18
Hash: 980bf9060e28555fecd9e3462f68ca74007b70f8
Python version: 3.6.6
Qt version: 5.6.2
Coin version: 4.0.0a
OCC version: 7.3.0
Locale: English/Australia (en_AU)

Ok... I give up. How do you mirror a mesh object? I want to create a mirror image of my mesh in the x-y direction. I figure it's such a basic operation and not too mathematically difficult to perform on a mesh that IT MUST BE POSSIBLE... but I'll be damned if I can work out how. It's driving me crazy. :shock: I don't want to have a mirrored copy of the shape, I just want to mirror the original mesh. Any help would be GREATLY appreciated. Thankyou in advance. :)
TheMarkster
Veteran
Posts: 5512
Joined: Thu Apr 05, 2018 1:53 am

Re: Flipping Horizontally (Mirroring) A Mesh...

Post by TheMarkster »

I don't think this can be done at this time in FreeCAD without converting to Part object, mirroring, and then converting back to mesh. Blender can do it directly, I believe.
R23D
Posts: 135
Joined: Sat Jan 25, 2020 5:23 am

Re: Flipping Horizontally (Mirroring) A Mesh...

Post by R23D »

:( Ok, thanks. I will try Blender. At least I feel like less of an idiot now for not being able to work it out. I should really post a feature request because in my opinion it's a little bit crazy that you can't do that... You can do just about anything else.
chrisb
Veteran
Posts: 54168
Joined: Tue Mar 17, 2015 9:14 am

Re: Flipping Horizontally (Mirroring) A Mesh...

Post by chrisb »

R23D wrote: Sun Jul 19, 2020 9:23 am I should really post a feature request because in my opinion it's a little bit crazy that you can't do that... You can do just about anything else.
No, it's not crazy. FreeCAD is a parametric modeler, which uses a completely different internal representation of solids. A Mesh is usually a deterioration, because it lacks precision, yet it has much more surfaces to deal with.
Working with meshes has the main purpose to transform them to or from normal FreeCAD shapes.
A Sketcher Lecture with in-depth information is available in English, auf Deutsch, en français, en español.
User avatar
Roy_043
Veteran
Posts: 8541
Joined: Thu Dec 27, 2018 12:28 pm

Re: Flipping Horizontally (Mirroring) A Mesh...

Post by Roy_043 »

IMO this would be a useful feature. The Mesh_Scale command could probably be extended to accept 3 scale factors (X, Y and Z) instead of just one.
ulrich1a
Veteran
Posts: 1957
Joined: Sun Jul 07, 2013 12:08 pm

Re: Flipping Horizontally (Mirroring) A Mesh...

Post by ulrich1a »

R23D wrote: Sun Jul 19, 2020 9:23 am I should really post a feature request because in my opinion it's a little bit crazy that you can't do that... You can do just about anything else.
You need to use python, than FreeCAD can do nearly all what you want. ;) There is the workbench OpenSCAD, which uses the program OpenSCAD to do operations with meshes. OpenSCAD does have a mirror operation, see here: https://en.wikibooks.org/wiki/OpenSCAD_ ... ons#mirror

Unfortunatly the mirror operation is not available directly in the OpenSCAD workbench. But after some digging in the python source, there is the command "callopenscadmeshstring", which can be used for this purpose. Here is a script, that can be used as a macro to mirror a FreeCAD mesh.
Just select the mesh and call the macro. I have tested it with a simple cube, where it works as expected. It does not test, if the selected object is a mesh. The mirror operation is done with a plane defined with a normal in x-direction through the origin. The coordinates of the mirror command in the script have to be adopted for other mirror planes.

Ulrich

Code: Select all

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import OpenSCADUtils
from exportCSG import mesh2polyhedron

mylist = FreeCAD.Gui.Selection.getSelectionEx()
for o in mylist:
  #print (o.ObjectName)
  #print (o.Object)
  poly = mesh2polyhedron(o.Object.Mesh)
  mi = OpenSCADUtils.callopenscadmeshstring('%s{%s}' % ('mirror([1,0,0])',''.join(poly)))
  mi.flipNormals()
  newMeshObj = d.addObject('Mesh::Feature', 'MirrorMesh')
  newMeshObj.Mesh = mi
TheMarkster
Veteran
Posts: 5512
Joined: Thu Apr 05, 2018 1:53 am

Re: Flipping Horizontally (Mirroring) A Mesh...

Post by TheMarkster »

ulrich1a wrote: Sun Jul 19, 2020 4:24 pm
R23D wrote: Sun Jul 19, 2020 9:23 am I should really post a feature request because in my opinion it's a little bit crazy that you can't do that... You can do just about anything else.
You need to use python, than FreeCAD can do nearly all what you want. ;) There is the workbench OpenSCAD, which uses the program OpenSCAD to do operations with meshes. OpenSCAD does have a mirror operation, see here: https://en.wikibooks.org/wiki/OpenSCAD_ ... ons#mirror

Unfortunatly the mirror operation is not available directly in the OpenSCAD workbench. But after some digging in the python source, there is the command "callopenscadmeshstring", which can be used for this purpose. Here is a script, that can be used as a macro to mirror a FreeCAD mesh.
Just select the mesh and call the macro. I have tested it with a simple cube, where it works as expected. It does not test, if the selected object is a mesh. The mirror operation is done with a plane defined with a normal in x-direction through the origin. The coordinates of the mirror command in the script have to be adopted for other mirror planes.

Ulrich

Code: Select all

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import OpenSCADUtils
from exportCSG import mesh2polyhedron

mylist = FreeCAD.Gui.Selection.getSelectionEx()
for o in mylist:
  #print (o.ObjectName)
  #print (o.Object)
  poly = mesh2polyhedron(o.Object.Mesh)
  mi = OpenSCADUtils.callopenscadmeshstring('%s{%s}' % ('mirror([1,0,0])',''.join(poly)))
  mi.flipNormals()
  newMeshObj = d.addObject('Mesh::Feature', 'MirrorMesh')
  newMeshObj.Mesh = mi
Nice find, Ulrich. Minor change (d = App.ActiveDocument):

Code: Select all

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import OpenSCADUtils
from exportCSG import mesh2polyhedron
d = App.ActiveDocument
mylist = FreeCAD.Gui.Selection.getSelectionEx()
for o in mylist:
  #print (o.ObjectName)
  #print (o.Object)
  poly = mesh2polyhedron(o.Object.Mesh)
  mi = OpenSCADUtils.callopenscadmeshstring('%s{%s}' % ('mirror([1,0,0])',''.join(poly)))
  mi.flipNormals()
  newMeshObj = d.addObject('Mesh::Feature', 'MirrorMesh')
  newMeshObj.Mesh = mi
TheMarkster
Veteran
Posts: 5512
Joined: Thu Apr 05, 2018 1:53 am

Re: Flipping Horizontally (Mirroring) A Mesh...

Post by TheMarkster »

This PR will add the mirror mesh feature command to OpenSCAD workbench. Also fixes an issue with the refine shape command in that same workbench.

https://github.com/FreeCAD/FreeCAD/pull/3720
Snip macro screenshot-977e72.png
Snip macro screenshot-977e72.png (151.13 KiB) Viewed 2680 times
Snip macro screenshot-018438.png
Snip macro screenshot-018438.png (158.15 KiB) Viewed 2680 times
Snip macro screenshot-aae3d7.png
Snip macro screenshot-aae3d7.png (104.55 KiB) Viewed 2680 times
ulrich1a
Veteran
Posts: 1957
Joined: Sun Jul 07, 2013 12:08 pm

Re: Flipping Horizontally (Mirroring) A Mesh...

Post by ulrich1a »

TheMarkster wrote: Sun Jul 19, 2020 8:56 pm This PR will add the mirror mesh feature command to OpenSCAD workbench.
I had a short look at your pull request. FreeCAD has a paradigm in separating functions and gui operations, in order to allow call functions without the gui. This allows FreeCAD to run in server mode. So can you change your pull request to separate the mirror function from calls to the gui?

Ulrich
TheMarkster
Veteran
Posts: 5512
Joined: Thu Apr 05, 2018 1:53 am

Re: Flipping Horizontally (Mirroring) A Mesh...

Post by TheMarkster »

ulrich1a wrote: Sun Jul 19, 2020 9:24 pm
TheMarkster wrote: Sun Jul 19, 2020 8:56 pm This PR will add the mirror mesh feature command to OpenSCAD workbench.
I had a short look at your pull request. FreeCAD has a paradigm in separating functions and gui operations, in order to allow call functions without the gui. This allows FreeCAD to run in server mode. So can you change your pull request to separate the mirror function from calls to the gui?

Ulrich
I'll look into it. Thanks.

Edit: PR updated. Please have another look and let me know.

With the update you can now call via python OpenSCADUtils.mirror(msh, vec) where msh is the mesh object and vec is a FreeCAD.Base.Vector. Then if you want to show the new mesh in the tree:

Code: Select all

import Mesh
Mesh.show(msh)
Edit2: I renamed mirror() to mirrormesh()
Post Reply