Pages

Friday, October 7, 2011

OOSheet as FreeCAD and LibreOffice bridge

Access to LibreOffice (OpenOffice.org) docs is able from Python. You can do it with PyUNO. When you need Calc spreadsheet, situation is even simpler. In this case OOSheet Python module is useful. 

Examples

As example I will show script which gets LibreOffice spreadsheet data, modifies FreeCAD wrench model and additionally returns model volume to the spreadsheet.

OOSheet installation under Ubuntu

I think that all readers have LibreOffice and Python installed. Fastest OOSheet installation method, type in terminal:

sudo apt-get install python-pip
sudo pip install oosheet

Actual FreeCAD version installation

I recommend add PPA repository:

sudo add-apt-repository ppa:freecad-maintainers/freecad-daily
sudo apt-get update
sudo apt-get upgrade
sudo apt-get install freecad freecad-doc

What do you need to proceed

  1. File klucz_en.ods with our data.
  2. Model file klucz.fcstd. Everything should be saved in the fctest directory.

Example

Our solid is based on a single sketch pad. Script for rows 2-12 will:
  • change pad (wrench thickness),
  • change sketch constraints: 45 (wrench gap), 51 (wrench length), 54 (wrench connector width),
  • recompute model,
  • export solid to STEP - file number equal to row number,
  • export model views to SVG file,
  • compute solid volume and return it to last spreadsheet column.
Open LibreOffice spreadsheet with 2002 port listening by typing in terminal:
oocalc fctest/klucz_en.ods -accept="socket,host=localhost,port=2002;urp;StarOffice.ServiceManager"

Then open in FreeCAD klucz.fcstd model file. Put below script in FreeCAD Python console. You should see new STEP and SVG files in fctest directory. Last sheet column should be filled by computed volume values.

Script listing:

from oosheet import OOSheet as S # for connection with z OO Calc
import ImportGui # for STEP export
for keynumber in range (2,13):
     skey = str(keynumber)
     App.ActiveDocument.Sketch.setDatum(54,S('b'+skey).value) #wrench connector width from sheet
     App.ActiveDocument.Sketch.setDatum(51,S('a'+skey).value) #wrench length
     App.ActiveDocument.Sketch.setDatum(45,S('d'+skey).value) #wrench gap
     App.ActiveDocument.getObject("Pad").Length = S('c'+skey).value #wrench thickness
     App.ActiveDocument.recompute() #model recompute
     __objs__=[] #STEP export
     __objs__.append(FreeCAD.getDocument("klucz").getObject("Pad"))
     ImportGui.export(__objs__,'fctest/klucz'+skey+'.stp')
     del __objs__
     PageFile = open(App.activeDocument().Page.PageResult,'r') #SVG export
     OutFile = open('fctest/klucz'+skey+'.svg','w')
     OutFile.write(PageFile.read())
     del OutFile,PageFile
     S('e'+skey).value = App.ActiveDocument.getObject("Pad").Shape.Volume #save volume in the sheet

Download Video.
Source post.

1 comment: