Skip to content

Commit

Permalink
OpenSCAD: Support empty unions and SVG import
Browse files Browse the repository at this point in the history
  • Loading branch information
chennes committed Sep 11, 2022
1 parent 3d7fa72 commit 88ee6bb
Showing 1 changed file with 54 additions and 4 deletions.
58 changes: 54 additions & 4 deletions src/Mod/OpenSCAD/importCSG.py
Expand Up @@ -32,17 +32,24 @@

printverbose = False

import FreeCAD
import io
import os

import ply.lex as lex
import ply.yacc as yacc
import xml.sax

import FreeCAD
import Part
import Draft

from OpenSCADFeatures import *
from OpenSCADUtils import *

# Save the native open function to avoid collisions
if open.__module__ in ['__builtin__', 'io']:
pythonopen = open
import ply.lex as lex
import ply.yacc as yacc

params = FreeCAD.ParamGet("User parameter:BaseApp/Preferences/Mod/OpenSCAD")
printverbose = params.GetBool('printVerbose', False)

Expand Down Expand Up @@ -462,6 +469,7 @@ def p_offset_action(p):
offset = float(p[3]['r'])
if 'delta' in p[3]:
offset = float(p[3]['delta'])
checkObjShape(subobj)
if subobj.Shape.Volume == 0 :
newobj=doc.addObject("Part::Offset2D",'Offset2D')
newobj.Source = subobj
Expand Down Expand Up @@ -628,6 +636,14 @@ def fuse(lst,name):
myfuse.Tool.ViewObject.hide()
return(myfuse)

def p_empty_union_action(p):
'union_action : union LPAREN RPAREN SEMICOL'
if printverbose: print("empty union")
newpart = fuse([],p[1])
if printverbose: print("Push Union Result")
p[0] = [newpart]
if printverbose: print("End Union")

def p_union_action(p):
'union_action : union LPAREN RPAREN OBRACE block_list EBRACE'
if printverbose: print("union")
Expand Down Expand Up @@ -862,10 +878,44 @@ def process_import_file(fname,ext,layer):
obj=process_mesh_file(fname,ext)
elif ext.lower() == 'dxf' :
obj=processDXF(fname,layer)
elif ext.lower() == 'svg':
obj=processSVG(fname, ext)
else:
raise ValueError("Unsupported file extension %s" % ext)
return(obj)

def processSVG(fname, ext):
from importSVG import svgHandler
if printverbose: print("SVG Handler")
doc = FreeCAD.ActiveDocument
docSVG = FreeCAD.newDocument(fname+'_tmp')
FreeCAD.ActiveDocument = docSVG

# Set up the parser
parser = xml.sax.make_parser()
parser.setFeature(xml.sax.handler.feature_external_ges, False)
parser.setContentHandler(svgHandler())
parser._cont_handler.doc = docSVG

# pathName is a Global
filename = os.path.join(pathName,fname+'.'+ext)
# Use the native Python open which was saved as `pythonopen`
parser.parse(pythonopen(filename))

#combine SVG objects into one
shapes = []
for obj in FreeCAD.ActiveDocument.Objects:
if printverbose: print(obj.Name)
if printverbose: print(obj.Shape)
shapes.append(obj.Shape)
#compoundSVG = Part.makeCompound(shapes)
#compoundSVG = Draft.join(objects)
FreeCAD.closeDocument(docSVG.Name)
FreeCAD.ActiveDocument=doc
obj=doc.addObject('Part::Feature',fname)
obj.Shape=Part.Compound(shapes)
return obj

def process_mesh_file(fname,ext):
import Mesh,Part
fullname = fname+'.'+ext
Expand Down Expand Up @@ -1335,8 +1385,8 @@ def p_polyhedron_action(p) :
pp =[v2(v[k]) for k in i]
# Add first point to end of list to close polygon
pp.append(pp[0])
w = Part.makePolygon(pp)
try:
w = Part.makePolygon(pp)
f = Part.Face(w)
except Exception:
secWireList = w.Edges[:]
Expand Down

0 comments on commit 88ee6bb

Please sign in to comment.