Problem with wiki mesh difference example

Need help, or want to share a macro? Post here!
User avatar
Roy_043
Posts: 1382
Joined: Thu Dec 27, 2018 12:28 pm

Problem with wiki mesh difference example

Postby Roy_043 » Tue May 26, 2020 12:54 pm

On the Mesh_Scripting page there is the following example:

Code: Select all

import Mesh, BuildRegularGeoms
sphere = Mesh.Mesh(BuildRegularGeoms.Sphere(5.0, 50))
cylinder = Mesh.Mesh(BuildRegularGeoms.Cylinder(2.0, 10.0, True, 1.0, 50))
diff = sphere
diff = diff.difference(cylinder)
d = FreeCAD.newDocument()
d.addObject("Mesh::Feature","Diff_Sphere_Cylinder").Mesh=diff
d.recompute()
Running the code I get an unexpected result: 4 triangular faces instead of a sphere with a cylindrical hole.

Is there something wrong with the code? Or is it the Mesh module?

Just to be sure I have installed OpenSCAD (which is required for mesh Boolean operations from the GUI). But this has had no effect for the code listed above.

Version:

Code: Select all

OS: Windows 8.1 (6.3)
Word size of OS: 64-bit
Word size of FreeCAD: 64-bit
Version: 0.19.21097 (Git)
Build type: Release
Branch: master
Hash: a5ab09aa0941cc945e59848897b160f8cbfc0831
Python version: 3.6.8
Qt version: 5.12.1
Coin version: 4.0.0a
OCC version: 7.3.0
Locale: Dutch/Netherlands (nl_NL)
onekk
Posts: 379
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: Problem with wiki mesh difference example

Postby onekk » Thu May 28, 2020 1:26 pm

The code is not working, it produce a mesh with only 25 facets, strange enough, maybe an issue report will be a good example.

Even this code, more refined, it show correctly the sphere and the cylinder, but the boolean is completely wrong:

Code: Select all

import Mesh, BuildRegularGeoms

DOC = FreeCAD.newDocument()

sphere = Mesh.Mesh(BuildRegularGeoms.Sphere(5.0, 50))
cylinder = Mesh.Mesh(BuildRegularGeoms.Cylinder(2.0, 10.0, True, 1.0, 50))

obj = DOC.addObject("Mesh::Feature","Sphere").Mesh = sphere

obj1 = DOC.addObject("Mesh::Feature","cylinder").Mesh = cylinder

obj3 = DOC.addObject("Mesh::Feature","sphcylinder").Mesh = sphere.subtract(cylinder)

DOC.recompute()

Code: Select all

OS: Artix Linux (openbox)
Word size of OS: 64-bit
Word size of FreeCAD: 64-bit
Version: 0.19.20310 (Git) AppImage
Build type: Release
Branch: master
Hash: 2cac4898d7d62276fbbaaef5b4cecb9bfdd29a33
Python version: 3.8.2
Qt version: 5.12.5
Coin version: 4.0.0
OCC version: 7.4.0
Locale: Italian/Italy (it_IT)
Regards

Carlo D.
User avatar
Roy_043
Posts: 1382
Joined: Thu Dec 27, 2018 12:28 pm

Re: Problem with wiki mesh difference example

Postby Roy_043 » Fri May 29, 2020 1:31 pm

Thanks for looking at this but

Code: Select all

sphere.subtract(cylinder)
Does not work.

Code: Select all

OS: Windows 8.1 (6.3)
Word size of OS: 64-bit
Word size of FreeCAD: 64-bit
Version: 0.19.21097 (Git)
Build type: Release
Branch: master
Hash: a5ab09aa0941cc945e59848897b160f8cbfc0831
Python version: 3.6.8
Qt version: 5.12.1
Coin version: 4.0.0a
OCC version: 7.3.0
Locale: Dutch/Netherlands (nl_NL)
onekk
Posts: 379
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: Problem with wiki mesh difference example

Postby onekk » Sat May 30, 2020 8:49 am

Sorry, I was experimenting, to find a solution, and forgot to delete the code, it was difference.

adding this code to my code above modifying the subtract with difference :

Code: Select all

obj3.write("Path to a file")
I obtained this file:

Code: Select all

faces = [
[4.0000,-1.9372,-0.4974],[4.5799,-1.8922,-0.6359],[4.5799,-1.9052,-0.5957],
[4.0000,-1.9372,-0.4974],[4.5799,-1.9052,-0.5957],[4.5799,-1.9372,-0.4974],
[4.0000,-1.8596,-0.7362],[4.5799,-1.7975,-0.8680],[4.5799,-1.8155,-0.8298],
[4.5799,-1.8155,-0.8298],[4.5799,-1.8596,-0.7362],[4.0000,-1.8596,-0.7362],
[4.0000,1.6180,-1.1756],[4.5799,1.6961,-1.0526],[4.5799,1.6734,-1.0883],
[4.0000,1.6180,-1.1756],[4.5799,1.6734,-1.0883],[4.5799,1.6180,-1.1756],
[4.0000,1.9372,0.4974],[4.5799,1.9052,0.5957],[4.5799,1.9372,0.4974],
[4.0000,1.9372,0.4974],[4.5799,1.8922,0.6359],[4.5799,1.9052,0.5957],
[4.0000,1.8596,0.7362],[4.5799,1.8155,0.8298],[4.5799,1.8596,0.7362],
[4.0000,1.8596,0.7362],[4.5799,1.7975,0.8680],[4.5799,1.8155,0.8298],
]
It list the faces that survived to the difference operation. not many.

But why to use a mesh when you could define a proper solid?

Mesh is not well suited for operating on them, A Mesh only define the external surface of an object, it is a handy format to define complex structures, or to be more correct, it was a workaround to describe something in space, they are very popular, as graphic cards and graphics processor are built to deal with triangles, and a mesh is a bunch of connected triangles.

In fact in the page describing the Mesh module there is this warning.
However, in the field of engineering meshes present one big limitation: they are only made of surfaces, and have no mass information, so they don't behave as solids. This means that all solid-based operations, such as addition or subtraction, are difficult to perform on meshes.
I suspect that the boolean operation of difference is more tailored to subtract two coincident surfaces, eliminating the overlapping, when subtracting the cylinder to the sphere, the sphere mesh became not-manifold, although the visualization is completely wrong, so something in the algorithm is not correct.

What are you trying to achieve?

Regards

Carlo D.
User avatar
Roy_043
Posts: 1382
Joined: Thu Dec 27, 2018 12:28 pm

Re: Problem with wiki mesh difference example

Postby Roy_043 » Sat May 30, 2020 1:55 pm

onekk wrote:
Sat May 30, 2020 8:49 am
What are you trying to achieve?
Two things:
1. Check and if necessary correct the wiki page.
2. Learn something more about FreeCAD and Python in the process.
onekk
Posts: 379
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: Problem with wiki mesh difference example

Postby onekk » Sun May 31, 2020 11:21 am

Ok I suppose that the mesh part is not very used, as most of the work is done using the "solid" part.

You may try to signal the page in the wiki section, telling there that you suppose there are some errors, or maybe in the developer corner, or maybe directly in the github page signaling the error in "mesh design module".

To learn about python and freecad this is the right place, but the learning curve is not so fast.

Many thing are "well hidden" in the sources, as scripting is not considered the "master approach" to FreeCAD.

I have "put in place" an introductory guide to FreeCAD but it is in Italian, it is intended to guide the user from the very first step to some modelling skills.

It have some defect, as it is using the documentObject approach that is more clear and fast but use more memory and is more "verbose" in the 3d view tree.

For some advanced topics I have to dig into the sources and maybe also in the OCCT documentation.

https://www.opencascade.com/doc/occt-7. ... _data.html

https://www.opencascade.com/doc/occt-7. ... algos.html

https://www.opencascade.com/doc/occt-7. ... tions.html

are the most interesting readings to cope with the internals of FreeCAD.

Many concept are exposed in the FreeCAD interface, and into the documentation of the code, using the same terms and obviously the same concept as OCCT is the "modelling engine" of FreeCAD.

Hope it helps.

And if it not clear maybe ask here or maybe in PM.

Regards

Carlo D.
User avatar
Roy_043
Posts: 1382
Joined: Thu Dec 27, 2018 12:28 pm

Re: Problem with wiki mesh difference example

Postby Roy_043 » Mon Jun 01, 2020 11:18 am

Thanks.

In the end I decided to replace the code. New code below. Note that even the new code has an unexpected issue: somehow using the flipNormals method is required.

Code: Select all

import FreeCAD, Mesh
cylA = Mesh.createCylinder(2.0, 10.0, True, 1.0, 36)
cylB = Mesh.createCylinder(1.0, 12.0, True, 1.0, 36)
cylB.Placement.Base = (FreeCAD.Vector(-1, 0, 0)) # move cylB to avoid co-planar faces
pipe = cylA
pipe = pipe.difference(cylB)
pipe.flipNormals() # somehow required
d = FreeCAD.ActiveDocument
d.addObject("Mesh::Feature", "Pipe").Mesh = pipe
d.recompute()
onekk
Posts: 379
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: Problem with wiki mesh difference example

Postby onekk » Mon Jun 01, 2020 1:41 pm

Code: Select all

import Part
import Mesh
from FreeCAD import Vector, Rotation
outer =  Part.makeCylinder(2,10)
hole = Part.makeCylinder(1, 11)
hole.Placement = FreeCAD.Placement(Vector(0,0,-0.5), Rotatation())

obj = outer.cut(hole)

# This is the 3d object generated as a solid.

mesh = doc.addObject("Mesh::Feature", "Mesh")
mesh.Mesh = MeshPart.meshFromShape(
        Shape=obj,
        LinearDeflection=0.01,
        AngularDeflection=0.025,
        Relative=False)
mesh.Label = "Mesh_object)
mesh.ViewObject.CreaseAngle = 25.0

LinearDeflection and AngularDeflection set the precision of the model using the convention as the OCCT Documentation

https://www.opencascade.com/doc/occt-7. ... odalg_11_2

In this case only the last passage is involving a mesh, so the mesh is derived from the solid, non need to cope with the normals, as they are generated by the meshing algorithm.

With the two parameters you can tune the precision, less precision (higher values) more coarse tassellation, more precision (lower values) more fine are the tessels.

There is also the inverse operation, you take a mesh and transform it to a solid, then operate on the solid and then retranslate to a mesh.

To transform in an STL you could use the same approach:

Code: Select all

tol = 0.01

filename = u"/Model_name" 

f_name = filename + ".amf"
# "exportAmfCompressed=False" or True if compression is wanted
Mesh.export([mesh], f_name, tolerance=tol, exportAmfCompressed=False)

# f_name = filename + ".stl"
# For STL no options
#Mesh.export([mesh], f_name, tolerance=tol)
The exporter is using the filename extension to determine the type of the exported file, for AMF files there is the option to use an uncompressed format.

For STL no options

If i don't remember wrong it supports also the OBJ format using obj as extension.

Regards

Carlo D.
User avatar
Roy_043
Posts: 1382
Joined: Thu Dec 27, 2018 12:28 pm

Re: Problem with wiki mesh difference example

Postby Roy_043 » Mon Jun 01, 2020 5:00 pm

Thanks again.

It took me a while to get the first code example in your last post working. There are some typos and missing lines. As an example it makes more sense on the Mesh_to_Part page than on the Mesh_Scripting page. I may include it there. Thanks also for the export example.

Code: Select all

import Part
import Mesh
import MeshPart
from FreeCAD import Vector, Rotation

outer =  Part.makeCylinder(2,10)
hole = Part.makeCylinder(1, 11)
hole.Placement = FreeCAD.Placement(Vector(0,0,-0.5), Rotation())

obj = outer.cut(hole)

# This is the 3d object generated as a solid.

doc = FreeCAD.ActiveDocument

mesh = doc.addObject("Mesh::Feature", "Mesh")
mesh.Mesh = MeshPart.meshFromShape(
        Shape=obj,
        LinearDeflection=0.01,
        AngularDeflection=0.025,
        Relative=False)

mesh.Label = "Mesh_object"
mesh.ViewObject.CreaseAngle = 25.0
onekk
Posts: 379
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: Problem with wiki mesh difference example

Postby onekk » Mon Jun 01, 2020 5:22 pm

Yes you are right, it is an example for transforming a part into a mesh, but, if you want "design" something, it have no sense to use meshes that are not taylored for that work.

I have missed import MeshPart (sorry) i have copied by hand some code I'm using.

In my FreeCAD adventure I have learned that not all the pages in the wiki have sense on the actual FreeCAD version, many of them are old pages that are referring old behaviour and are updated scarcely and maybe by a people that is not the original author, so you have to take them with more careful, and maybe signal them as erroneous in the wiki part of this forum.

Sorry if I have misunderstand your intention, but as you told me that you are learning I wanted to share some of my "not to extended" knowledge.

Regards

Carlo D.