0

I want to have an object, generated by an existing MATLAB script, modelled in FreeCAD. See bolded titles for important content of Intro, Matlab code, python code, Testing via terminal, Matlab error, Notes (OS, versions)

Intro

I have each side of the process worked out, but I am having issues with the calling of the python script through MATLAB.

I have both object constructor, and a script that works in building things in FreeCAD; so essentially, I have either end. What I need now is to interface them.

My test is basic, but should have been enough, or so I assumed.

Matlab:

pyfile              = '~/Desktop/FreeCADworkspace/testvec2.py';
filename            = 'atestname';
call                = "python " + pyfile + " " + filename;
[status,result]     = system("sh ~/Desktop/FreeCADworkspace/freecad.sh")

Python:

#   Import python roots
import sys
import os
sys.path.append('/usr/lib/freecad-python2/lib')
sys.path.append('/usr/lib/freecad/lib')
#   Other imports
import numpy as np
import math

#   Import FreeCAD and parts
import FreeCAD 
from FreeCAD import Base
import Part, Sketcher, Draft

try: 
    filename    = sys.argv[1]
except: # this should never be thrown, exists for testing and other integration
    print('No arguments called into script.')
    print('Please use format: python thisScript.py arg_filename')
    quit() # exit script

print(filename) # checkmeplz

So, essentially what should happen is that I should get a print-out of the definition of filename in MATLAB. In this case, I should see 'atestname'

Testing

If I call this through terminal:

python ~/Desktop/FreeCADworkspace/testvec2.py atestname

I get:

FreeCAD 0.18.1, Libs: 0.18.1R
atestname

Which is exactly as expected. The same cannot be said of my MATLAB

MATLAB ERROR

Traceback (most recent call last):
       File "/home/ashaiden/Desktop/FreeCADworkspace/testvec2.py", line 11, in <module>
         import FreeCAD # no FreeCADGui??
     ImportError: /usr/lib/freecad-python2/lib/libFreeCADBase.so: undefined symbol: _ZN11xercesc_3_111InputSource11setEncodingEPKt

I have also tried to execute the python script from MATLAB via a bash script. Same error.

What is confusing me: why would some command that executes perfectly through the terminal, be failing when calling via MATLAB? I feel like I am misunderstanding how MATLAB system calls are performed. I assumed that the call was sent to, and processed by, the OS itself. But where that the case, it would not make sense to be getting an error. Does MATLAB do some interpreting of files that it is handling?

Notes

  • Operating system: Ubuntu 16.04
  • MATLAB 2018b
  • Python 2.7 -> is this potentially the issue? Python 2 seemed to be the default language for my particular FreeCAD install.
  • FreeCAD 0.18.1

Edit

After much searching by myself and my supervisor, it appears that this might be able to be put down to a conflict between binary files.

MATLAB has libxerces-c.so files defined inside /bin/glnxa64/ and another toolbox folder.

These may be conflicting with the linux binary /usr/lib/x86_64-linux-gnu/libxerces-c.so such that when the terminal is called via matlab, it uses the matlab binary instead of the system binary.

I will continue to investigate further.

BPDESILVA
  • 2,040
  • 5
  • 15
  • 35

1 Answers1

0

Answering my own questions!

Edit 30/7 : This does not actually adequately solve the problem for linux, given that paths may have some slight difference between machines. It also does not solve the problem for mac or windows (the issue does occur on mac, but I have no data if it occurs on windows.

After digging and searching, I edited the question:

MATLAB has libxerces-c.so files defined inside /bin/glnxa64/ and another toolbox folder.

These may be conflicting with the linux binary /usr/lib/x86_64-linux-gnu/libxerces-c.so such that when terminal is called via matlab, it uses the matlab binary instead of the system binary.

I was correct that this was due to a conflict between MATLAB's bin libxerces-c.so and the linux native libxerces-c.so. I have found at least a partial fix for this. I do not know of its robustness. At worst, the path will be edited and then returned to original format each time FreeCAD will be called within my code (gross).

SOLUTION:

  1. Important first step: oldpath = getenv("LD_LIBRARY_PATH") to get and then SAVE some record of the current path. This, for me, is in case some issues or conflicts arise later.

  2. I then removed the section of the path '/usr/local/MATLAB/R2018b/bin/glnxa64/' and saved the string to newpath

  3. I then made this the new path: setenv("LD_LIBRARY_PATH", newpath), where newpath no longer has the reference to the conflicting folder

And now I am getting the expected result in my MATLAB command window:

FreeCAD 0.18.1, Libs: 0.18.1R

atestname

The comparative paths:

Newpath =  '/usr/local/MATLAB/R2018b/sys/opengl/lib/glnxa64:/usr/local/MATLAB/R2018b/sys/os/glnxa64:/usr/local/MATLAB/R2018b/extern/lib/glnxa64:/usr/local/MATLAB/R2018b/runtime/glnxa64:/usr/local/MATLAB/R2018b/sys/java/jre/glnxa64/jre/lib/amd64/native_threads:/usr/local/MATLAB/R2018b/sys/java/jre/glnxa64/jre/lib/amd64/server:/usr/lib/x86_64-linux-gnu/';

Oldpath =  '/usr/local/MATLAB/R2018b/sys/opengl/lib/glnxa64:/usr/local/MATLAB/R2018b/sys/os/glnxa64:/usr/local/MATLAB/R2018b/bin/glnxa64:/usr/local/MATLAB/R2018b/extern/lib/glnxa64:/usr/local/MATLAB/R2018b/runtime/glnxa64:/usr/local/MATLAB/R2018b/sys/java/jre/glnxa64/jre/lib/amd64/native_threads:/usr/local/MATLAB/R2018b/sys/java/jre/glnxa64/jre/lib/amd64/server:/usr/lib/x86_64-linux-gnu/';