Unable to perform Boolean operations in mesh mode

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!
tkatsuki
Posts: 5
Joined: Fri Nov 27, 2015 2:45 pm

Unable to perform Boolean operations in mesh mode

Post by tkatsuki »

Hi, I was trying to run a Boolean operation (e.g. difference) in Mesh Mode but FreeCAD gave me an error "Unknown error occurred while running OpenSCAD". The mesh objects were a simple cube and a sphere created in Part Mode and converted into mesh objects in Mesh Mode.

Here is my session info:
OS: Mac OS X
Word size of OS: 64-bit
Word size of FreeCAD: 64-bit
Version: 0.15.4671 (Git)
Branch: releases/FreeCAD-0-15
Hash: 244b3aef360841646cbfe80a1b225c8b39c8380c
Python version: 2.7.9
Qt version: 4.8.6
Coin version: 4.0.0a
OCC version: 6.8.0.oce-0.17

OpenSCAD version is 2015.03-2.

Python code looks like this:

Code: Select all

import WebGui
from StartPage import StartPage
WebGui.openBrowserHTML(StartPage.handle(),'file://' + App.getResourceDir() + 'Mod/Start/StartPage/','Start page')
App.newDocument("Unnamed")
App.setActiveDocument("Unnamed")
App.ActiveDocument=App.getDocument("Unnamed")
Gui.ActiveDocument=Gui.getDocument("Unnamed")
Gui.activateWorkbench("PartWorkbench")
App.ActiveDocument.addObject("Part::Box","Box")
App.ActiveDocument.ActiveObject.Label = "Cube"
App.ActiveDocument.recompute()
Gui.SendMsgToActiveView("ViewFit")
App.ActiveDocument.addObject("Part::Sphere","Sphere")
App.ActiveDocument.ActiveObject.Label = "Sphere"
App.ActiveDocument.recompute()
Gui.SendMsgToActiveView("ViewFit")
Gui.activateWorkbench("MeshWorkbench")
import MeshPartGui, FreeCADGui
FreeCADGui.runCommand('MeshPart_Mesher')

import Mesh
import MeshPart
__doc__=FreeCAD.getDocument("Unnamed")
__mesh__=__doc__.addObject("Mesh::Feature","Mesh")
__mesh__.Mesh=MeshPart.meshFromShape(Shape=__doc__.getObject("Box").Shape,MaxLength=1)
__mesh__.Label="Cube (Meshed)"
__mesh__.ViewObject.CreaseAngle=25.0
del __doc__, __mesh__

import MeshPartGui, FreeCADGui
FreeCADGui.runCommand('MeshPart_Mesher')

import Mesh
import MeshPart
__doc__=FreeCAD.getDocument("Unnamed")
__mesh__=__doc__.addObject("Mesh::Feature","Mesh")
__mesh__.Mesh=MeshPart.meshFromShape(Shape=__doc__.getObject("Sphere").Shape,MaxLength=1)
__mesh__.Label="Sphere (Meshed)"
__mesh__.ViewObject.CreaseAngle=25.0
del __doc__, __mesh__

import OpenSCADUtils
mesh = OpenSCADUtils.meshoptempfile('difference',(App.ActiveDocument.Mesh.Mesh,App.ActiveDocument.Mesh001.Mesh))
App.ActiveDocument.addObject("Mesh::Feature","Difference")
App.ActiveDocument.Difference.Mesh = mesh
I'd appreciate your help to solve the problem.

Thanks,

Takeo
wmayer
Founder
Posts: 20241
Joined: Thu Feb 19, 2009 10:32 am
Contact:

Re: Unable to perform Boolean operations in mesh mode

Post by wmayer »

I have tried OpenSCAD 2015.03.2 on Windows and your script works perfectly there. Maybe the way how OpenSCAD is called on OSX inside the Python function OpenSCADUtils.meshoptempfile is wrong.
tkatsuki
Posts: 5
Joined: Fri Nov 27, 2015 2:45 pm

Re: Unable to perform Boolean operations in mesh mode

Post by tkatsuki »

Thanks, wmayer. I'm not sure what fixed the problem but now Mesh Boolean also works on my Mac. What I did first was to go to OpenSCAD mode and run Mesh Boolean from there (I also checked path to the executable from Preference but it looked correct so I didn't change). After that even Boolean from Mesh Mode also worked.

Thanks again for developing such great software.

Takeo
s_hemer
Posts: 7
Joined: Tue May 28, 2019 8:42 pm

Re: Unable to perform Boolean operations in mesh mode

Post by s_hemer »

I just ran across the same issue with FreeCAD 18.1 in combination with 2015.03 on a Ubuntu 18.04. Boolean operation even on simple regular geometry meshes do not work neither in Mesh Design nor in OpenSCAD mode. I could not make OpenSCAD to finally render the Boolean operation on the imported meshes (only preview works), so I am not sure if this is a FreeCAD o OpenSCAD issue (or some misconfiguration).

Code: Select all

Traceback (most recent call last):
  File "/usr/share/freecad/Mod/OpenSCAD/OpenSCADCommands.py", line 371, in doboolean
    newmesh,objsused = meshoponobjs(opname,FreeCADGui.Selection.getSelection())
  File "/usr/share/freecad/Mod/OpenSCAD/OpenSCADUtils.py", line 484, in meshoponobjs
    return (meshoptempfile(opname,meshes),objs)
  File "/usr/share/freecad/Mod/OpenSCAD/OpenSCADUtils.py", line 446, in meshoptempfile
    result = callopenscadmeshstring('%s(){%s}' % (opname,meshimports))
  File "/usr/share/freecad/Mod/OpenSCAD/OpenSCADUtils.py", line 410, in callopenscadmeshstring
    tmpfilename=callopenscadstring(scadstr,'stl')
  File "/usr/share/freecad/Mod/OpenSCAD/OpenSCADUtils.py", line 185, in callopenscadstring
    inputfile.write(scadstr)
TypeError: write() argument 1 must be unicode, not str
jmaustpc
Veteran
Posts: 11207
Joined: Tue Jul 26, 2011 6:28 am
Location: Australia

Re: Unable to perform Boolean operations in mesh mode

Post by jmaustpc »

Your first post, welcome to FreeCAD.
s_hemer wrote: Tue May 28, 2019 9:05 pm I just ran across the same issue with FreeCAD 18.1
Please post your FreeCAD version data in the manner explained in the Forum Rules link above, so that we know what we are dealing with.

Also which version of OpenSCAD did you install and from where?
wmayer
Founder
Posts: 20241
Joined: Thu Feb 19, 2009 10:32 am
Contact:

Re: Unable to perform Boolean operations in mesh mode

Post by wmayer »

Using this Windows version:
OS: Windows 7
Word size of OS: 64-bit
Word size of FreeCAD: 64-bit
Version: 0.18.15998 (Git)
Build type: Release
Branch: master
Hash: 5791c341be797ff2eee99eccac53df71db510fae
Python version: 3.6.6
Qt version: 5.6.2
Coin version: 4.0.0a
OCC version: 7.3.0
Locale: German/Germany (de_DE)

The mesh boolean operations work flawlessly.

Now to test it on Ubuntu 18.04 I started my virtual installation and I realized that Ubuntu doesn't offer OpenSCAD for this version. I wonder where you got your OpenSCAD installation from.

So, I installed it from Launchpad https://launchpad.net/~openscad/+archiv ... u/releases and also using this version it works without problems.
TypeError: write() argument 1 must be unicode, not str
Does your FreeCAD installation depend on Python2?
wmayer
Founder
Posts: 20241
Joined: Thu Feb 19, 2009 10:32 am
Contact:

Re: Unable to perform Boolean operations in mesh mode

Post by wmayer »

When using a Python2-based FreeCAD -- also on Windows -- then I can confirm the failure.

Code: Select all

import OpenSCADUtils
import Mesh
mesh1=Mesh.createBox(10,10,10)
mesh2=Mesh.createCone()
mesh = OpenSCADUtils.meshoptempfile('union',(mesh1,mesh2))
After changing several things inside OpenSCADUtils.py I got it working for Python2. The changes affect the functions check_output2 and callopenscadstring. So replace the file of your FreeCAD installation with this one or switch to a Python3-based FreeCAD version.
Attachments
OpenSCADUtils.py
(25.16 KiB) Downloaded 58 times
User avatar
Kunda1
Veteran
Posts: 13434
Joined: Thu Jan 05, 2017 9:03 pm

Re: Unable to perform Boolean operations in mesh mode

Post by Kunda1 »

wmayer wrote: Thu May 30, 2019 9:35 am After changing several things inside OpenSCADUtils.py I got it working for Python2. The changes affect the functions check_output2 and callopenscadstring. So replace the file of your FreeCAD installation with this one or switch to a Python3-based FreeCAD version.
@wmayer should we offer this as a viable workaround in the code?
Alone you go faster. Together we go farther
Please mark thread [Solved]
Want to contribute back to FC? Checkout:
'good first issues' | Open TODOs and FIXMEs | How to Help FreeCAD | How to report Bugs
jmaustpc
Veteran
Posts: 11207
Joined: Tue Jul 26, 2011 6:28 am
Location: Australia

Re: Unable to perform Boolean operations in mesh mode

Post by jmaustpc »

about a week ago a new version of OpenSCAD was released, 2019.05, the PPA Werner referenced still has an old version in it.

OpenSCAD news
http://www.openscad.org/news.html

the version list in the PPA see Werner's link

2015.03-2+dfsg-2~18.04
https://launchpad.net/~openscad/+archiv ... u/releases

two versions are 4 years apart, by the way.

Apparently the dependencies have changed, I read that in a post on their forum the response to a question about dependencies was to read the "readme" file.

Perhaps to the question is does the new release version work? Perhaps their PPA will be updated soon.
s_hemer
Posts: 7
Joined: Tue May 28, 2019 8:42 pm

Re: Unable to perform Boolean operations in mesh mode

Post by s_hemer »

Thanks for the hints and sorry for not providing more details on the version. I just installed from the respective PPAs and did not realize that there is a freecad-python{2,3}. Seems that it installed freecad-python2 on my machine.
I really wonder that I did not saw the OpenSCAD news. I will check all that.
Post Reply