Previewing Solidworks files from Linux

From ASCEND
Jump to: navigation, search
Update: since changes were made to SolidWorks 2015, it is no longer possible to use the following instructions to preview Solidworks files. This is because Solidworks is no longer using a standard structured file format that gsf can read. If you figure out a solution to this, let us know. Some info here.

This page gives some brief instructions on how to get basic preview support for Solidworks files in Linux. The instructions should work on any Freedesktop-based linux, eg Ubuntu, Fedora, etc. This approach only lets you see a low/medium resolution image of the object from a single point of view; you can't manipulate the object, but at least you can see more or less what the file contains.

Before you start, you will need to install some packages:

sudo apt-get install libgsf-bin imagemagick

Firstly, add a mime type, by creating a file $HOME/.local/share/mime/packages/x-solidworks.xml:

<?xml version="1.0" encoding="utf-8"?>
<mime-info xmlns="http://www.freedesktop.org/standards/shared-mime-info">
<mime-type type="application/x-solidworks"> 
  <glob pattern="*.sldprt"/>
  <glob pattern="*.sldasm"/>
  <glob pattern="*.slddrw"/>
</mime-type>
</mime-info>

Next, run update-mime-database $HOME/.local/share/mime

Check that your mime type is working with some solidworks file you have:

mimetype partfile.SLDPRT

and you should see application/x-solidworks in the output.

Next, add a thumbnailer for this type. First create the following script file and save it in /usr/local/bin/solidworks-thumbnailer:

#!/usr/bin/env python
import sys
import subprocess
from PIL import Image
import tempfile

#log = open("/tmp/solidworks-thumbnailer",'a')
#log.write(" ".join(sys.argv) + "\n")
#log.close()

inputname, outputname, size = sys.argv[1:]

f = tempfile.TemporaryFile()

P = subprocess.Popen(['/usr/bin/gsf','cat',inputname,'PreviewPNG'],stdout=f)

out,err = P.communicate()

f.seek(0)

s = int(size)
#print "resizing to %d"%s

im = Image.open(f)
im.thumbnail((s,s), Image.ANTIALIAS)
im.save(outputname, "PNG")

Set the file executable:

chmod +x /usr/local/bin/solidworks-thumbnailer

Now associate this thumbnailer with the mime type, by creating a file $HOME/.local/share/thumbnailers/solidworks.thumbnailer:

[Thumbnailer Entry]
TryExec=/usr/local/bin/solidworks-thumbnailer
Exec=/usr/local/bin/solidworks-thumbnailer %i %o %s
MimeType=application/x-solidworks;

You need to restart Nautilus using nautilus -q. After that, you should be seeing little thumbnails for all your solidworks files, similar to below:

Thumbnail-solidworks.png

This tiny thumbnail is lower than the resolution embedded in the solidworks file. We can also set up a viewer so that the full resolution preview image is displayed.

Firstly, create /usr/local/bin/solidworks-viewer with the following content. Set the file to executable using chmod +x /usr/local/bin/solidworks-viewer.

#!/usr/bin/env python
import subprocess
import sys

inputname = sys.argv[1]

P1 = subprocess.Popen(['/usr/bin/gsf','cat',inputname,'PreviewPNG'],stdout=subprocess.PIPE)
P2 = subprocess.Popen(["/usr/bin/display"],stdin=P1.stdout)
P2.wait()

Secondly, create a file $HOME/.local/share/applications/solidworks.desktop containing the following (and also set this file as executable):

[Desktop Entry]
Encoding=UTF-8
Version=1.0
Type=Application
Exec=/usr/local/bin/solidworks-viewer %f
MimeType=application/x-solidworks;
Name=Solidworks Preview
Name[en]=Solidworks Preview
GenericName=Solidworks Preview
Comment=Solidworks Preview
Icon=solidworks
Categories=Education
StartupNotify=false

Next, update you desktop database using update-desktop-database ~/.local/share/applications/. You will need to restart nautilus again (nautilus -q), then a double-click on your Solidworks file should pop up an image viewing window:

Solidworks-preview.png

For some notes on possibly getting more information from these files, see here. Perhaps it might be possible to convert the solidworks file into some format that allows more detail to be visible. But that would be a much bigger job.