LibreOffice with Python 3.5

Having trouble installing or compiling FreeCAD? Get help here.
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
User avatar
salp
Posts: 250
Joined: Thu Nov 21, 2013 11:58 pm
Location: Putnam, NY

LibreOffice with Python 3.5

Post by salp »

I have been working on a macro to incorporate LibreOffice into FreeCAD 0.17, and from what I'm reading it is a much simpler process using Python 3. I was able to access LibreOffice from Python 3, and now I am trying to do the same from inside FreeCAD (and failing miserably :( ).

I have been trying to compile freecad with Python 3.5, and everything seems to complete successfully. However when I check the Python version inside the python console it shows that its using version 2.7.

I have installed the python3 dev packages and edited the cmake Python variables as described in the Python3 wiki and read the lengthy Porting to Python3 post, but apparently I'm getting lost in the process.

Can someone point to a post or article which explains what needs to be configured so a novice can follow?

The system I work on is:
OS: Linux Mint 18.2 Sonya
Word size of OS: 64-bit
Word size of FreeCAD: 64-bit
Version: 0.17.12697 (Git)
Build type: None
Branch: master
Hash: 8bbcfd6dce297917f39452392dc9e5722f95d7cb
Python version: 2.7.12
Qt version: 4.8.7
Coin version: 4.0.0a
OCC version: 7.1.0
Locale: English/UnitedStates (en_US)

The development is a VirtualBox guest running Ubuntu 16.04 with Xfce desktop manager.

Thanks
Sal
Last edited by jmaustpc on Sat Dec 09, 2017 11:55 pm, edited 1 time in total.
Reason: Changed the title of the topic
User avatar
looo
Veteran
Posts: 3941
Joined: Mon Nov 11, 2013 5:29 pm

Re: Compile with Python 3.5

Post by looo »

maybe you can have a look at cmake-configuration with cmake-gui. With your description I don't have a clue where the problem is.

An easier solution would be to install FreeCAD with conda.
wmayer
Founder
Posts: 20242
Joined: Thu Feb 19, 2009 10:32 am
Contact:

Re: Compile with Python 3.5

Post by wmayer »

Recently I made a fresh build using Python 3.4 and there it crashed too when starting FreeCAD because by default it links to the shiboken version linked against Python 2.7.

The steps to avoid a mixture of Python 3 and Python 2 libs is:
  1. in cmake set PYTHON-ExECUTABLE to e.g. /usr/bin/python3.4
  2. set PYTHON_BASENAME to .cpython-34m
  3. set PYTHON_SUFFIX to .cpython-34m
With the first step cmake uses the right Python executable, header and libraries. Step two and three are needed to use the matching shiboken and PySide libs.

Now you are using Python 3.5 and it's straightforward to adjust step one. For steps two and three you have to figure out the correct suffix.
User avatar
salp
Posts: 250
Joined: Thu Nov 21, 2013 11:58 pm
Location: Putnam, NY

Re: Compile with Python 3.5

Post by salp »

looo, wmayer,

Thanks for the replies, I know the post is very vague.

I will go through the process on a clean machine and I'll document my steps as I go along, and I'll post my results here in detail.

Sal
User avatar
salp
Posts: 250
Joined: Thu Nov 21, 2013 11:58 pm
Location: Putnam, NY

Re: Compile with Python 3.5

Post by salp »

I got freecad to successfully compile with python 3.5.

The following is the what I installed (in the order that they are listed) on a clean Ubuntu 16.04 machine. Maybe some one else can use the information.

Code: Select all

    install FreeCAD dependencies
	build-essential
	cmake
	python
	python-matplotlib
	libtool
	libcoin80-dev
	libsoqt4-dev
	libxerces-c-dev
	libboost-dev
	libboost-filesystem-dev
	libboost-regex-dev
	libboost-program-options-dev
	libboost-signals-dev
	libboost-thread-dev
	libboost-python-dev
	libqt4-dev
	libqt4-opengl-dev
	qt4-dev-tools
	python-dev
	python-pyside
	pyside-tools
	libeigen3-dev
	libqtwebkit-dev
	libshiboken-dev
	libpyside-dev
	libode-dev
	swig
	libzipios++-dev
	libfreetype6
	libfreetype6-dev
	liboce-foundation-dev
	liboce-modeling-dev
	liboce-ocaf-dev
	liboce-visualization-dev
	liboce-ocaf-lite-dev
	libsimage-dev
	checkinstall
	python-pivy
	python-qt4
	doxygen
	libspnav-dev
	oce-draw
	liboce-foundation-dev
	liboce-foundation10
	liboce-modeling-dev
	liboce-modeling10
	liboce-ocaf-dev
	liboce-ocaf-lite-dev
	liboce-ocaf-lite10
	liboce-ocaf10
	liboce-visualization-dev
	liboce-visualization10
	libmedc-dev
	libvtk6-dev
	libproj-dev
        graphviz

# install python3 development package
	python3-dev

# install PySide for python3 (all sub packages)
	python3-pyside.*

#Install pivy from this location
        git clone pivy from "https://github.com/looooo/pivy" pivy
	cd pivy
	sudo python3 setup.py install

# I searched for *shiboken* and found: /usr/lib/x86_64-linux-gnu/cmake/Shiboken-1.2.2/ShibokenConfig.cpython-35m-x86_64-linux-gnu.cmake

# Edit CMakeCache.txt file
	PYTHON_BASENAME:STRING=.cpython-35m-x86_64-linux-gnu
	PYTHON_EXECUTABLE:FILEPATH=/usr/bin/python3.5
	PYTHON_INCLUDE_DIR:PATH=/usr/include/python3.5m
	PYTHON_LIBRARY:FILEPATH=/usr/lib/x86_64-linux-gnu/libpython3.5m.so
	PYTHON_LIBRARY_DEBUG:FILEPATH=PYTHON_LIBRARY_DEBUG-NOTFOUND
	PYTHON_SUFFIX:STRING=.cpython-35m-x86_64-linux-gnu
	PYTHON_EXECUTABLE-ADVANCED:INTERNAL=1
	PYTHON_INCLUDE_DIR-ADVANCED:INTERNAL=1
	PYTHON_LIBRARY-ADVANCED:INTERNAL=1
	PYTHON_LIBRARY_DEBUG-ADVANCED:INTERNAL=1


run make

run the executable "bin/FreeCAD"

output from freeccad
OS: Ubuntu 16.04.3 LTS
Word size of OS: 64-bit
Word size of FreeCAD: 64-bit
Version: 0.17.12754 (Git)
Build type: Unknown
Branch: master
Hash: 1cfae464b4af12c5483c2495fae360bbf3b4c0cd
Python version: 3.5.2
Qt version: 4.8.7
Coin version: 4.0.0a
OCC version: 6.8.0.oce-0.17
Locale: English/UnitedStates (en_US)
jmaustpc
Veteran
Posts: 11207
Joined: Tue Jul 26, 2011 6:28 am
Location: Australia

Re: Compile with Python 3.5

Post by jmaustpc »

Hi Sal
salp wrote: Mon Dec 04, 2017 1:05 am I have been working on a macro to incorporate LibreOffice into FreeCAD 0.17, and from what I'm reading it is a much simpler process using Python 3. I was able to access LibreOffice from Python 3, and now I am trying to do the same from inside FreeCAD (and failing miserably :( ).
I can't believe that its that long ago but way back in 2011 kwahooo (adrian przekwas) posted a blog with details of how to get LibreOffice to interface with FreeCAD. His example succeeded in getting Calc to talk to FreeCAD. I tried it at the time and it worked well. :)

Here is his blog, the post is still there
http://freecad-tutorial.blogspot.com.au ... ffice.html

Of course was not Python 3 back then, so I don't know how much help it will be. But it certainly worked back then. :)

Jim
User avatar
salp
Posts: 250
Joined: Thu Nov 21, 2013 11:58 pm
Location: Putnam, NY

Re: Compile with Python 3.5

Post by salp »

Hi Jim,

I saw you had referenced kwahooo (adrian przekwas) in an earlier post, I looked it up and it was very helpful.

Python 3 tools have greatly simplified the process, I have tested some code to manipulate LibreOffice (any module not just calc) and it's pretty straight forward in a regular python session.

From what I've seen the current spreadsheet workbench is extremely useful but limited, if we can automate the process of launching LibreOffice in the background ad establish the communications link with FreeCAD automatically without any user intervention then we will gain all the functionality of LibreOffice and we can concentrate on the code to only transfer data in both directions.

I know this is very vague, I'm hoping to be able to post the initial macro code by the end of the month.

Sal
User avatar
salp
Posts: 250
Joined: Thu Nov 21, 2013 11:58 pm
Location: Putnam, NY

Re: Compile with Python 3.5

Post by salp »

Some test code to show how simple LibreOffice can be manipulated from inside FreeCAD.
This is working on Ubuntu 16.04.

I have a question, would it be difficult to get the properties of all the objects in the FreeCAD document?
To make the object properties visible to the spreadsheet, I thought of dedicating a tab in the spreadsheet for the freecad object values, and make it read only. That way the user can browse the available data.

I'll keep this thread updated, let me know if it should ne moved to a different forum section.

Sal

Code: Select all

import time
import shlex
import subprocess
command_line = '/usr/bin/soffice --calc --accept="socket,host=localhost,port=2002;urp;StarOffice.ServiceManager"'
args = shlex.split(command_line)
p = subprocess.Popen(args)

# wait for LibreOffice to initialize
time.sleep(2)

## code to initialize OfficeLibre ##
import uno
localContext = uno.getComponentContext()
resolver = localContext.ServiceManager.createInstanceWithContext(  
				"com.sun.star.bridge.UnoUrlResolver", localContext )

# connect to the running office
ctx = resolver.resolve( "uno:socket,host=localhost,port=2002;urp;StarOffice.ComponentContext" )
smgr = ctx.ServiceManager
desktop = smgr.createInstanceWithContext( "com.sun.star.frame.Desktop",ctx)
model = desktop.getCurrentComponent()

# access the active sheet
active_sheet = model.CurrentController.ActiveSheet
cell1 = active_sheet.getCellRangeByName("C4")
cell1.String = "Hello world"
cell2 = active_sheet.getCellRangeByName("E6")
cell2.Value = cell2.Value + 1
OS: Ubuntu 16.04.3 LTS
Word size of OS: 64-bit
Word size of FreeCAD: 64-bit
Version: 0.17.12754 (Git)
Build type: Unknown
Branch: master
Hash: 1cfae464b4af12c5483c2495fae360bbf3b4c0cd
Python version: 3.5.2
Qt version: 4.8.7
Coin version: 4.0.0a
OCC version: 6.8.0.oce-0.17
Locale: English/UnitedStates (en_US)
User avatar
Kunda1
Veteran
Posts: 13434
Joined: Thu Jan 05, 2017 9:03 pm

Re: Compile with Python 3.5

Post by Kunda1 »

Very cool. Need to test this out. BTW, can you please change the title of this thread to reflect the subject better? Thanks.
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
User avatar
Kunda1
Veteran
Posts: 13434
Joined: Thu Jan 05, 2017 9:03 pm

Re: Compile with Python 3.5

Post by Kunda1 »

I ran the whole code block above, this is the first error:

Code: Select all

>>> import uno
Traceback (most recent call last):
  File "<input>", line 1, in <module>
ImportError: No module named uno
>>> localContext = uno.getComponentContext()
Traceback (most recent call last):
  File "<input>", line 1, in <module>
NameError: name 'uno' is not defined
>>> resolver = localContext.ServiceManager.createInstanceWithContext(  
... 				"com.sun.star.bridge.UnoUrlResolver", localContext )
Traceback (most recent call last):
  File "<input>", line 1, in <module>
NameError: name 'localContext' is not defined
OS: "Manjaro Linux"
Word size of OS: 64-bit
Word size of FreeCAD: 64-bit
Version: 0.17.12697 (Git)
Build type: None
Branch: master
Hash: 8bbcfd6dce297917f39452392dc9e5722f95d7cb
Python version: 2.7.6
Qt version: 4.8.6
Coin version: 4.0.0a
OCC version: 7.1.0
Locale: English/UnitedStates (en_US)
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
Post Reply