Skip to content

Commit

Permalink
Merge pull request #406 from CadQuery/spline-dxf
Browse files Browse the repository at this point in the history
dxf splines and ellipse import support
  • Loading branch information
jmwright authored Jul 17, 2020
2 parents a8707ce + 6d6da05 commit 9ab2678
Show file tree
Hide file tree
Showing 5 changed files with 5,146 additions and 1 deletion.
80 changes: 79 additions & 1 deletion cadquery/occ_impl/importers.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,26 @@
from collections import OrderedDict
from math import pi

from .. import cq
from .geom import Vector
from .shapes import Shape, Edge, Face, sortWiresByBuildOrder
from .shapes import Shape, Edge, Face, sortWiresByBuildOrder, DEG2RAD

import ezdxf

from OCP.STEPControl import STEPControl_Reader
from OCP.ShapeAnalysis import ShapeAnalysis_FreeBounds
from OCP.TopTools import TopTools_HSequenceOfShape
from OCP.gp import gp_Pnt
from OCP.Geom import Geom_BSplineCurve
from OCP.TColgp import TColgp_Array1OfPnt
from OCP.TColStd import TColStd_Array1OfReal, TColStd_Array1OfInteger
from OCP.BRepBuilderAPI import BRepBuilderAPI_MakeEdge


import OCP.IFSelect

RAD2DEG = 360.0 / (2 * pi)


class ImportTypes:
STEP = "STEP"
Expand Down Expand Up @@ -104,12 +115,79 @@ def _dxf_polyline(el):
return (e[0] for e in rv if e)


def _dxf_spline(el):

try:
degree = el.dxf.degree
periodic = el.closed
rational = False

knots_unique = OrderedDict()
for k in el.knots:
if k in knots_unique:
knots_unique[k] += 1
else:
knots_unique[k] = 1

# assmble knots
knots = TColStd_Array1OfReal(1, len(knots_unique))
multiplicities = TColStd_Array1OfInteger(1, len(knots_unique))
for i, (k, m) in enumerate(knots_unique.items()):
knots.SetValue(i + 1, k)
multiplicities.SetValue(i + 1, m)

# assemble wieghts if present:
if el.weights:
rational = True

weights = OCP.TColStd.TColStd_Array1OfReal(1, len(el.weights))
for i, w in enumerate(el.weights):
weights.SetValue(i + 1, w)

# assmeble conotrol points
pts = TColgp_Array1OfPnt(1, len(el.control_points))
for i, p in enumerate(el.control_points):
pts.SetValue(i + 1, gp_Pnt(*p))

if rational:
spline = Geom_BSplineCurve(
pts, weights, knots, multiplicities, degree, periodic
)
else:
spline = Geom_BSplineCurve(pts, knots, multiplicities, degree, periodic)

return (Edge(BRepBuilderAPI_MakeEdge(spline).Edge()),)

except Exception:
return ()


def _dxf_ellipse(el):

try:

return (
Edge.makeEllipse(
el.dxf.major_axis.magnitude,
el.minor_axis.magnitude,
pnt=Vector(el.dxf.center.xyz),
xdir=Vector(el.dxf.major_axis.xyz),
angle1=el.dxf.start_param * RAD2DEG,
angle2=el.dxf.end_param * RAD2DEG,
),
)
except Exception:
return ()


DXF_CONVERTERS = {
"LINE": _dxf_line,
"CIRCLE": _dxf_circle,
"ARC": _dxf_arc,
"POLYLINE": _dxf_polyline,
"LWPOLYLINE": _dxf_polyline,
"SPLINE": _dxf_spline,
"ELLIPSE": _dxf_ellipse,
}


Expand Down
21 changes: 21 additions & 0 deletions tests/test_importers.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,27 @@ def testImportDXF(self):
obj = importers.importDXF(filename)
self.assertTrue(obj.val().isValid())

# test spline import

filename = os.path.join(testdataDir, "spline.dxf")
obj = importers.importDXF(filename, tol=1)
self.assertTrue(obj.val().isValid())
self.assertEqual(obj.faces().size(), 1)
self.assertEqual(obj.wires().size(), 2)

# test rational spline import
filename = os.path.join(testdataDir, "rational_spline.dxf")
obj = importers.importDXF(filename)
self.assertTrue(obj.val().isValid())
self.assertEqual(obj.faces().size(), 1)
self.assertEqual(obj.edges().size(), 1)

# importing of a complex shape exported from Inkscape
filename = os.path.join(testdataDir, "genshi.dxf")
obj = importers.importDXF(filename)
self.assertTrue(obj.val().isValid())
self.assertEqual(obj.faces().size(), 1)


if __name__ == "__main__":
import unittest
Expand Down
Loading

0 comments on commit 9ab2678

Please sign in to comment.