Skip to content

Instantly share code, notes, and snippets.

@5263
Created January 15, 2013 17:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save 5263/4540334 to your computer and use it in GitHub Desktop.
Save 5263/4540334 to your computer and use it in GitHub Desktop.
Automaticly import and check OpenSCAD files into FreeCAD
import FreeCAD
import importCSG
#import prototype
importmod=importCSG
dirpath=''
def comparewithstl(filename,obj,doc):
# import MeshPart
import Mesh
import OpenSCADUtils
import os
tmpfilename=OpenSCADUtils.callopenscad(filename,outputext='stl')
if tmpfilename:
#doc=FreeCAD.activeDocument() or FreeCAD.newDocument()
import Mesh
Mesh.insert(tmpfilename,doc.Name)
try:
os.unlink(tmpfilename)
except IOError,WindowsError:
pass
meshdocname=os.path.split(tmpfilename)[1]
meshname,meshextension = meshdocname.split('.',1)
mesh = doc.getObject(meshname.replace('-','_'))
#if not mesh:
#41print meshname
volfac=obj.Shape.Volume/mesh.Mesh.Volume
import math
if abs(math.log(volfac)) > 0.008:
return 'different volume %s' %('%f %f' % (volfac,math.log(volfac)))
mbb=mesh.Mesh.BoundBox
pbb=obj.Shape.BoundBox
bbdistance=pbb.Center-mbb.Center
if any([abs(f) > 0.05 for f in (bbdistance)]):
return 'different BoundBox center %s' % str(bbdistance)
#next some larger bounding boxes
mbbl=FreeCAD.BoundBox(mbb)
pbbl=FreeCAD.BoundBox(pbb)
mbbl.enlarge(max((mbb.DiagonalLength*0.005,0.01)))
pbbl.enlarge(max((pbb.DiagonalLength*0.005,0.01)))
if not (mbbl.isInside(pbb) and pbbl.isInside(mbb)):
return 'different BoundBox %s ' % ('%s\%s'%(pbb,mbb))
def testimport(filename,closedoc=True):
try:
importmod.open(filename)
except Exception, e:
#raise
return ('exception on import', e)
doc=App.ActiveDocument
finalobjects=[obj for obj in doc.Objects if not obj.InList]
if len(finalobjects) == 0:
return 'no object'
if len(finalobjects) > 1:
return 'multiple objects'
try:
if not all([aobj.Shape.isValid() for aobj in doc.Objects if hasattr(obj,'Shape')]):
return 'invalid shapes'
except Exception, e:
return ('exception on shape check', e)
obj=finalobjects[0]
#if obj.Shape.isValid()
return comparewithstl(filename,obj,doc)
#if closedoc:
# FreeCAD.closeDocument(doc.Name) #does not work
import os
import time
#import random
dirlist=list(os.listdir(dirpath))
#random.shuffle(dirlist)
for dirent in (dirlist):
print dirent
if dirent.lower().endswith('.scad'):
shortname = dirent.split('.',1)[0]
print dirent
result=testimport(dirpath+dirent)
if result is not None:
from PyQt4 import QtGui
QtGui.QMessageBox.critical(None, 'error' ,str(result))
print result
break
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment