paths2openscad.py 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006
  1. #!/usr/bin/env python
  2. # openscad.py
  3. # This is an Inkscape extension to output paths to extruded OpenSCAD polygons
  4. # The Inkscape objects must first be converted to paths (Path > Object to Path).
  5. # Some paths may not work well -- the paths have to be polygons. As such,
  6. # paths derived from text may meet with mixed results.
  7. # Written by Daniel C. Newman ( dan dot newman at mtbaldy dot us )
  8. # 10 June 2012
  9. # 15 June 2012
  10. # Updated by Dan Newman to handle a single level of polygon nesting.
  11. # This is sufficient to handle most fonts.
  12. # If you want to nest two polygons, combine them into a single path
  13. # within Inkscape with "Path > Combine Path".
  14. # This program is free software; you can redistribute it and/or modify
  15. # it under the terms of the GNU General Public License as published by
  16. # the Free Software Foundation; either version 2 of the License, or
  17. # (at your option) any later version.
  18. #
  19. # This program is distributed in the hope that it will be useful,
  20. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. # GNU General Public License for more details.
  23. #
  24. # You should have received a copy of the GNU General Public License
  25. # along with this program; if not, write to the Free Software
  26. # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  27. import math
  28. import os.path
  29. import inkex
  30. import simplepath
  31. import simplestyle
  32. import simpletransform
  33. import cubicsuperpath
  34. import cspsubdiv
  35. import bezmisc
  36. import re
  37. DEFAULT_WIDTH = 100
  38. DEFAULT_HEIGHT = 100
  39. def parseLengthWithUnits( str ):
  40. '''
  41. Parse an SVG value which may or may not have units attached
  42. This version is greatly simplified in that it only allows: no units,
  43. units of px, and units of %. Everything else, it returns None for.
  44. There is a more general routine to consider in scour.py if more
  45. generality is ever needed.
  46. '''
  47. u = 'px'
  48. s = str.strip()
  49. if s[-2:] == 'px':
  50. s = s[:-2]
  51. elif s[-1:] == '%':
  52. u = '%'
  53. s = s[:-1]
  54. try:
  55. v = float( s )
  56. except:
  57. return None, None
  58. return v, u
  59. def pointInBBox( pt, bbox ):
  60. '''
  61. Determine if the point pt=[x, y] lies on or within the bounding
  62. box bbox=[xmin, xmax, ymin, ymax].
  63. '''
  64. # if ( x < xmin ) or ( x > xmax ) or ( y < ymin ) or ( y > ymax )
  65. if ( pt[0] < bbox[0] ) or ( pt[0] > bbox[1] ) or \
  66. ( pt[1] < bbox[2] ) or ( pt[1] > bbox[3] ):
  67. return False
  68. else:
  69. return True
  70. def bboxInBBox( bbox1, bbox2 ):
  71. '''
  72. Determine if the bounding box bbox1 lies on or within the
  73. bounding box bbox2. NOTE: we do not test for strict enclosure.
  74. Structure of the bounding boxes is
  75. bbox1 = [ xmin1, xmax1, ymin1, ymax1 ]
  76. bbox2 = [ xmin2, xmax2, ymin2, ymax2 ]
  77. '''
  78. # if ( xmin1 < xmin2 ) or ( xmax1 > xmax2 ) or ( ymin1 < ymin2 ) or ( ymax1 > ymax2 )
  79. if ( bbox1[0] < bbox2[0] ) or ( bbox1[1] > bbox2[1] ) or \
  80. ( bbox1[2] < bbox2[2] ) or ( bbox1[3] > bbox2[3] ):
  81. return False
  82. else:
  83. return True
  84. def pointInPoly( p, poly, bbox=None ):
  85. '''
  86. Use a ray casting algorithm to see if the point p = [x, y] lies within
  87. the polygon poly = [[x1,y1],[x2,y2],...]. Returns True if the point
  88. is within poly, lies on an edge of poly, or is a vertex of poly.
  89. '''
  90. if ( p is None ) or ( poly is None ):
  91. return False
  92. # Check to see if the point lies outside the polygon's bounding box
  93. if not bbox is None:
  94. if not pointInBBox( p, bbox ):
  95. return False
  96. # Check to see if the point is a vertex
  97. if p in poly:
  98. return True
  99. # Handle a boundary case associated with the point
  100. # lying on a horizontal edge of the polygon
  101. x = p[0]
  102. y = p[1]
  103. p1 = poly[0]
  104. p2 = poly[1]
  105. for i in range( len( poly ) ):
  106. if i != 0:
  107. p1 = poly[i-1]
  108. p2 = poly[i]
  109. if ( y == p1[1] ) and ( p1[1] == p2[1] ) and \
  110. ( x > min( p1[0], p2[0] ) ) and ( x < max( p1[0], p2[0] ) ):
  111. return True
  112. n = len( poly )
  113. inside = False
  114. p1_x,p1_y = poly[0]
  115. for i in range( n + 1 ):
  116. p2_x,p2_y = poly[i % n]
  117. if y > min( p1_y, p2_y ):
  118. if y <= max( p1_y, p2_y ):
  119. if x <= max( p1_x, p2_x ):
  120. if p1_y != p2_y:
  121. intersect = p1_x + (y - p1_y) * (p2_x - p1_x) / (p2_y - p1_y)
  122. if x <= intersect:
  123. inside = not inside
  124. else:
  125. inside = not inside
  126. p1_x,p1_y = p2_x,p2_y
  127. return inside
  128. def polyInPoly( poly1, bbox1, poly2, bbox2 ):
  129. '''
  130. Determine if polygon poly2 = [[x1,y1],[x2,y2],...]
  131. contains polygon poly1.
  132. The bounding box information, bbox=[xmin, xmax, ymin, ymax]
  133. is optional. When supplied it can be used to perform rejections.
  134. Note that one bounding box containing another is not sufficient
  135. to imply that one polygon contains another. It's necessary, but
  136. not sufficient.
  137. '''
  138. # See if poly1's bboundin box is NOT contained by poly2's bounding box
  139. # if it isn't, then poly1 cannot be contained by poly2.
  140. if ( not bbox1 is None ) and ( not bbox2 is None ):
  141. if not bboxInBBox( bbox1, bbox2 ):
  142. return False
  143. # To see if poly1 is contained by poly2, we need to ensure that each
  144. # vertex of poly1 lies on or within poly2
  145. for p in poly1:
  146. if not pointInPoly( p, poly2, bbox2 ):
  147. return False
  148. # Looks like poly1 is contained on or in Poly2
  149. return True
  150. def subdivideCubicPath( sp, flat, i=1 ):
  151. '''
  152. [ Lifted from eggbot.py with impunity ]
  153. Break up a bezier curve into smaller curves, each of which
  154. is approximately a straight line within a given tolerance
  155. (the "smoothness" defined by [flat]).
  156. This is a modified version of cspsubdiv.cspsubdiv(): rewritten
  157. because recursion-depth errors on complicated line segments
  158. could occur with cspsubdiv.cspsubdiv().
  159. '''
  160. while True:
  161. while True:
  162. if i >= len( sp ):
  163. return
  164. p0 = sp[i - 1][1]
  165. p1 = sp[i - 1][2]
  166. p2 = sp[i][0]
  167. p3 = sp[i][1]
  168. b = ( p0, p1, p2, p3 )
  169. if cspsubdiv.maxdist( b ) > flat:
  170. break
  171. i += 1
  172. one, two = bezmisc.beziersplitatt( b, 0.5 )
  173. sp[i - 1][2] = one[1]
  174. sp[i][0] = two[2]
  175. p = [one[2], one[3], two[1]]
  176. sp[i:1] = [p]
  177. def closed_p(path):
  178. """ path[0] is the path to check """
  179. result = False
  180. thepath = path[0]
  181. if type(thepath[0][0]) == type([1,2]):
  182. result = True
  183. if (thepath[0][0] == thepath[-1][0]) and (thepath[0][1] == thepath[-1][1]):
  184. result = True
  185. return result
  186. def msg_linear_extrude(id, prefix):
  187. msg = ' linear_extrude(height=h)\n' + \
  188. ' polygon(%s_%d_points);\n' % (id,prefix)
  189. return msg
  190. def msg_linear_extrude_by_paths(id, prefix):
  191. msg = ' linear_extrude(height=h)\n' + \
  192. ' polygon(%s_%d_points, %s_%d_paths);\n' % (id, prefix, id, prefix)
  193. return msg
  194. def msg_extrude_by_hull(id, prefix):
  195. msg = ' for (t = [0: len(%s_%d_points)-2]) {\n' %(id,prefix) + \
  196. ' hull() {\n' + \
  197. ' translate(%s_%d_points[t]) \n' %(id,prefix) + \
  198. ' cylinder(h=h, r=w/2, $fn=res);\n' + \
  199. ' translate(%s_%d_points[t + 1]) \n' %(id,prefix) + \
  200. ' cylinder(h=h, r=w/2, $fn=res);\n' + \
  201. ' }\n' + \
  202. ' }\n'
  203. return msg
  204. class OpenSCAD( inkex.Effect ):
  205. def __init__( self ):
  206. inkex.Effect.__init__( self )
  207. self.OptionParser.add_option( "--tab", #NOTE: value is not used.
  208. action="store", type="string",
  209. dest="tab", default="splash",
  210. help="The active tab when Apply was pressed" )
  211. self.OptionParser.add_option("-s",'--smoothness', dest='smoothness',
  212. type='float', default=float( 0.2 ), action='store',
  213. help='Curve smoothing (less for more)' )
  214. self.OptionParser.add_option("-x",'--height', dest='height',
  215. type='string', default='5', action='store',
  216. help='Height (mm)' )
  217. self.OptionParser.add_option("-w",'--line_width', dest='line_width',
  218. type='float', default=float( 1 ), action='store',
  219. help='Line width for non closed curves (mm)' )
  220. self.OptionParser.add_option("-l","--force_line",
  221. action="store", type="inkbool",
  222. dest="force_line", default=False,
  223. help="Force line output")
  224. self.OptionParser.add_option("-f",'--fname', dest='fname',
  225. type='string', default='~/inkscape.scad',
  226. action='store',
  227. help='Curve smoothing (less for more)' )
  228. self.cx = float( DEFAULT_WIDTH ) / 2.0
  229. self.cy = float( DEFAULT_HEIGHT ) / 2.0
  230. self.xmin, self.xmax = ( 1.0E70, -1.0E70 )
  231. self.ymin, self.ymax = ( 1.0E70, -1.0E70 )
  232. # Dictionary of paths we will construct. It's keyed by the SVG node
  233. # it came from. Such keying isn't too useful in this specific case,
  234. # but it can be useful in other applications when you actually want
  235. # to go back and update the SVG document
  236. self.paths = {}
  237. # Output file handling
  238. self.call_list = []
  239. self.pathid = int( 0 )
  240. # Output file
  241. self.f = None
  242. # For handling an SVG viewbox attribute, we will need to know the
  243. # values of the document's <svg> width and height attributes as well
  244. # as establishing a transform from the viewbox to the display.
  245. self.docWidth = float( DEFAULT_WIDTH )
  246. self.docHeight = float( DEFAULT_HEIGHT )
  247. self.docTransform = [[1.0, 0.0, 0.0], [0.0, 1.0, 0.0]]
  248. # Dictionary of warnings issued. This to prevent from warning
  249. # multiple times about the same problem
  250. self.warnings = {}
  251. def getLength( self, name, default ):
  252. '''
  253. Get the <svg> attribute with name "name" and default value "default"
  254. Parse the attribute into a value and associated units. Then, accept
  255. units of cm, ft, in, m, mm, pc, or pt. Convert to pixels.
  256. Note that SVG defines 90 px = 1 in = 25.4 mm.
  257. '''
  258. str = self.document.getroot().get( name )
  259. if str:
  260. v, u = parseLengthWithUnits( str )
  261. if not v:
  262. # Couldn't parse the value
  263. return None
  264. elif ( u == 'mm' ):
  265. return float( v ) * ( 90.0 / 25.4 )
  266. elif ( u == 'cm' ):
  267. return float( v ) * ( 90.0 * 10.0 / 25.4 )
  268. elif ( u == 'm' ):
  269. return float( v ) * ( 90.0 * 1000.0 / 25.4 )
  270. elif ( u == 'in' ):
  271. return float( v ) * 90.0
  272. elif ( u == 'ft' ):
  273. return float( v ) * 12.0 * 90.0
  274. elif ( u == 'pt' ):
  275. # Use modern "Postscript" points of 72 pt = 1 in instead
  276. # of the traditional 72.27 pt = 1 in
  277. return float( v ) * ( 90.0 / 72.0 )
  278. elif ( u == 'pc' ):
  279. return float( v ) * ( 90.0 / 6.0 )
  280. elif ( u == 'px' ):
  281. return float( v )
  282. else:
  283. # Unsupported units
  284. return None
  285. else:
  286. # No width specified; assume the default value
  287. return float( default )
  288. def getDocProps( self ):
  289. '''
  290. Get the document's height and width attributes from the <svg> tag.
  291. Use a default value in case the property is not present or is
  292. expressed in units of percentages.
  293. '''
  294. self.docHeight = self.getLength( 'height', DEFAULT_HEIGHT )
  295. self.docWidth = self.getLength( 'width', DEFAULT_WIDTH )
  296. if ( self.docHeight == None ) or ( self.docWidth == None ):
  297. return False
  298. else:
  299. return True
  300. def handleViewBox( self ):
  301. '''
  302. Set up the document-wide transform in the event that the document has an SVG viewbox
  303. '''
  304. if self.getDocProps():
  305. viewbox = self.document.getroot().get( 'viewBox' )
  306. if viewbox:
  307. vinfo = viewbox.strip().replace( ',', ' ' ).split( ' ' )
  308. if ( vinfo[2] != 0 ) and ( vinfo[3] != 0 ):
  309. sx = self.docWidth / float( vinfo[2] )
  310. sy = self.docHeight / float( vinfo[3] )
  311. self.docTransform = simpletransform.parseTransform( 'scale(%f,%f)' % (sx, sy) )
  312. def getPathVertices( self, path, node=None, transform=None ):
  313. '''
  314. Decompose the path data from an SVG element into individual
  315. subpaths, each subpath consisting of absolute move to and line
  316. to coordinates. Place these coordinates into a list of polygon
  317. vertices.
  318. '''
  319. if ( not path ) or ( len( path ) == 0 ):
  320. # Nothing to do
  321. return None
  322. # parsePath() may raise an exception. This is okay
  323. sp = simplepath.parsePath( path )
  324. if ( not sp ) or ( len( sp ) == 0 ):
  325. # Path must have been devoid of any real content
  326. return None
  327. # Get a cubic super path
  328. p = cubicsuperpath.CubicSuperPath( sp )
  329. if ( not p ) or ( len( p ) == 0 ):
  330. # Probably never happens, but...
  331. return None
  332. if transform:
  333. simpletransform.applyTransformToPath( transform, p )
  334. # Now traverse the cubic super path
  335. subpath_list = []
  336. subpath_vertices = []
  337. for sp in p:
  338. # We've started a new subpath
  339. # See if there is a prior subpath and whether we should keep it
  340. if len( subpath_vertices ):
  341. subpath_list.append( [ subpath_vertices, [ sp_xmin, sp_xmax, sp_ymin, sp_ymax ] ] )
  342. subpath_vertices = []
  343. subdivideCubicPath( sp, float( self.options.smoothness ) )
  344. # Note the first point of the subpath
  345. first_point = sp[0][1]
  346. subpath_vertices.append( first_point )
  347. sp_xmin = first_point[0]
  348. sp_xmax = first_point[0]
  349. sp_ymin = first_point[1]
  350. sp_ymax = first_point[1]
  351. # See if the first and last points are identical
  352. # OpenSCAD doesn't mind if we duplicate the first and last
  353. # vertex, but our polygon in polygon algorithm may
  354. n = len( sp )
  355. last_point = sp[n-1][1]
  356. #if ( first_point[0] == last_point[0] ) and ( first_point[1] == last_point[1] ):
  357. # n = n - 1
  358. # Traverse each point of the subpath
  359. for csp in sp[1:n]:
  360. # Append the vertex to our list of vertices
  361. pt = csp[1]
  362. subpath_vertices.append( pt )
  363. # Track the bounding box of this subpath
  364. if pt[0] < sp_xmin:
  365. sp_xmin = pt[0]
  366. elif pt[0] > sp_xmax:
  367. sp_xmax = pt[0]
  368. if pt[1] < sp_ymin:
  369. sp_ymin = pt[1]
  370. elif pt[1] > sp_ymax:
  371. sp_ymax = pt[1]
  372. # Track the bounding box of the overall drawing
  373. # This is used for centering the polygons in OpenSCAD around the (x,y) origin
  374. if sp_xmin < self.xmin:
  375. self.xmin = sp_xmin
  376. if sp_xmax > self.xmax:
  377. self.xmax = sp_xmax
  378. if sp_ymin < self.ymin:
  379. self.ymin = sp_ymin
  380. if sp_ymax > self.ymax:
  381. self.ymax = sp_ymax
  382. # Handle the final subpath
  383. if len( subpath_vertices ):
  384. subpath_list.append( [ subpath_vertices, [ sp_xmin, sp_xmax, sp_ymin, sp_ymax ] ] )
  385. if len( subpath_list ) > 0:
  386. self.paths[node] = subpath_list
  387. def convertPath( self, node ):
  388. path = self.paths[node]
  389. if ( path is None ) or ( len( path ) == 0 ):
  390. return
  391. # Determine which polys contain which
  392. contains = [ [] for i in xrange( len( path ) ) ]
  393. contained_by = [ [] for i in xrange( len( path ) ) ]
  394. for i in range( 0, len( path ) ):
  395. for j in range( i + 1, len( path ) ):
  396. if polyInPoly( path[j][0], path[j][1], path[i][0], path[i][1] ):
  397. # subpath i contains subpath j
  398. contains[i].append( j )
  399. # subpath j is contained in subpath i
  400. contained_by[j].append( i )
  401. elif polyInPoly( path[i][0], path[i][1], path[j][0], path[j][1] ):
  402. # subpath j contains subpath i
  403. contains[j].append( i )
  404. # subpath i is containd in subpath j
  405. contained_by[i].append( j )
  406. #NEW
  407. # create identifier
  408. id = node.get ( 'id', '' )
  409. if ( id is None ) or ( id == '' ):
  410. id = str( self.pathid ) + 'x'
  411. self.pathid += 1
  412. else:
  413. id = re.sub( '[^A-Za-z0-9_]+', '', id )
  414. # fold all subpaths into a single list of points and a number of paths by index.
  415. prefix = 0
  416. for i in range( 0, len( path ) ):
  417. # Skip this subpath if it is contained by another one
  418. if len( contained_by[i] ) != 0:
  419. continue
  420. subpath = path[i][0]
  421. bbox = path[i][1]
  422. #
  423. poly = id + '_' + str(prefix) + '_points = ['
  424. polypaths = id + '_' + str(prefix) + '_paths = [['
  425. if len( contains[i] ) == 0:
  426. # This subpath does not contain any subpaths
  427. for point in subpath:
  428. poly += '[%f,%f],' % ( ( point[0] - self.cx ), ( point[1] - self.cy ) )
  429. poly = poly[:-1]
  430. poly += '];\n'
  431. self.f.write( poly )
  432. prefix += 1
  433. else:
  434. # This subpath contains other subpaths
  435. # collect all points into poly
  436. # also collect the indices into polypaths
  437. for point in subpath:
  438. poly += '[%f,%f],' % ( ( point[0] - self.cx ), ( point[1] - self.cy ) )
  439. count = len(subpath)
  440. for k in range(0, count):
  441. polypaths += '%d,' % (k)
  442. polypaths = polypaths[:-1] + '],\n\t\t\t\t['
  443. # The nested paths
  444. for j in contains[i]:
  445. for point in path[j][0]:
  446. poly += '[%f,%f],' % ( ( point[0] - self.cx ), ( point[1] - self.cy ) )
  447. for k in range(count, count + len(path[j][0])):
  448. polypaths += '%d,' % k
  449. count += len(path[j][0])
  450. polypaths = polypaths[:-1] + '],\n\t\t\t\t['
  451. poly = poly[:-1]
  452. poly += '];\n'
  453. polypaths = polypaths[:-7] + '];\n'
  454. # write the polys and paths
  455. self.f.write( poly )
  456. self.f.write( polypaths )
  457. prefix += 1
  458. # Generate an OpenSCAD module for this path
  459. self.f.write( '\nmodule poly_' + id + '(h, w, res=4) {\n')
  460. self.f.write( ' scale([profile_scale, -profile_scale, 1])' )
  461. self.f.write( '\n union()' ) # !!This line optional and can be removed
  462. self.f.write(' {\n' )
  463. # And add the call to the call list
  464. self.call_list.append( 'poly_%s(height, width);\n' % ( id ) )
  465. prefix = 0
  466. for i in range( 0, len( path ) ):
  467. # Skip this subpath if it is contained by another one
  468. if len( contained_by[i] ) != 0:
  469. continue
  470. #self.f.write( '//%s\n' % path[i][0])
  471. if closed_p(path[i]):
  472. #self.f.write( '//closed\n')
  473. if len( contains[i] ) == 0:
  474. # This subpath does not contain any subpaths
  475. if self.options.force_line:
  476. msg = msg_extrude_by_hull(id, prefix)
  477. else:
  478. msg = msg_linear_extrude(id, prefix)
  479. self.f.write( msg )
  480. prefix += 1
  481. else:
  482. # This subpath contains other subpaths
  483. msg = msg_linear_extrude_by_paths(id, prefix)
  484. self.f.write( msg )
  485. prefix += 1
  486. else:
  487. #self.f.write( '//open\n')
  488. if len( contains[i] ) == 0:
  489. # This subpath does not contain any subpaths
  490. msg = msg_extrude_by_hull(id, prefix)
  491. self.f.write( msg )
  492. prefix += 1
  493. else:
  494. # This subpath contains other subpaths
  495. msg = msg_linear_extrude_by_paths(id, prefix)
  496. self.f.write( msg )
  497. prefix += 1
  498. # End the module
  499. self.f.write( ' }\n' )
  500. self.f.write( '}\n' )
  501. def recursivelyTraverseSvg( self, aNodeList,
  502. matCurrent=[[1.0, 0.0, 0.0], [0.0, 1.0, 0.0]],
  503. parent_visibility='visible' ):
  504. '''
  505. [ This too is largely lifted from eggbot.py ]
  506. Recursively walk the SVG document, building polygon vertex lists
  507. for each graphical element we support.
  508. Rendered SVG elements:
  509. <circle>, <ellipse>, <line>, <path>, <polygon>, <polyline>, <rect>
  510. Supported SVG elements:
  511. <group>, <use>
  512. Ignored SVG elements:
  513. <defs>, <eggbot>, <metadata>, <namedview>, <pattern>,
  514. processing directives
  515. All other SVG elements trigger an error (including <text>)
  516. '''
  517. for node in aNodeList:
  518. # Ignore invisible nodes
  519. v = node.get( 'visibility', parent_visibility )
  520. if v == 'inherit':
  521. v = parent_visibility
  522. if v == 'hidden' or v == 'collapse':
  523. pass
  524. # First apply the current matrix transform to this node's tranform
  525. matNew = simpletransform.composeTransform( matCurrent, simpletransform.parseTransform( node.get( "transform" ) ) )
  526. if node.tag == inkex.addNS( 'g', 'svg' ) or node.tag == 'g':
  527. self.recursivelyTraverseSvg( node, matNew, v )
  528. elif node.tag == inkex.addNS( 'use', 'svg' ) or node.tag == 'use':
  529. # A <use> element refers to another SVG element via an xlink:href="#blah"
  530. # attribute. We will handle the element by doing an XPath search through
  531. # the document, looking for the element with the matching id="blah"
  532. # attribute. We then recursively process that element after applying
  533. # any necessary (x,y) translation.
  534. #
  535. # Notes:
  536. # 1. We ignore the height and width attributes as they do not apply to
  537. # path-like elements, and
  538. # 2. Even if the use element has visibility="hidden", SVG still calls
  539. # for processing the referenced element. The referenced element is
  540. # hidden only if its visibility is "inherit" or "hidden".
  541. refid = node.get( inkex.addNS( 'href', 'xlink' ) )
  542. if not refid:
  543. pass
  544. # [1:] to ignore leading '#' in reference
  545. path = '//*[@id="%s"]' % refid[1:]
  546. refnode = node.xpath( path )
  547. if refnode:
  548. x = float( node.get( 'x', '0' ) )
  549. y = float( node.get( 'y', '0' ) )
  550. # Note: the transform has already been applied
  551. if ( x != 0 ) or (y != 0 ):
  552. matNew2 = composeTransform( matNew, parseTransform( 'translate(%f,%f)' % (x,y) ) )
  553. else:
  554. matNew2 = matNew
  555. v = node.get( 'visibility', v )
  556. self.recursivelyTraverseSvg( refnode, matNew2, v )
  557. elif node.tag == inkex.addNS( 'path', 'svg' ):
  558. path_data = node.get( 'd')
  559. if path_data:
  560. self.getPathVertices( path_data, node, matNew )
  561. elif node.tag == inkex.addNS( 'rect', 'svg' ) or node.tag == 'rect':
  562. # Manually transform
  563. #
  564. # <rect x="X" y="Y" width="W" height="H"/>
  565. #
  566. # into
  567. #
  568. # <path d="MX,Y lW,0 l0,H l-W,0 z"/>
  569. #
  570. # I.e., explicitly draw three sides of the rectangle and the
  571. # fourth side implicitly
  572. # Create a path with the outline of the rectangle
  573. x = float( node.get( 'x' ) )
  574. y = float( node.get( 'y' ) )
  575. if ( not x ) or ( not y ):
  576. pass
  577. w = float( node.get( 'width', '0' ) )
  578. h = float( node.get( 'height', '0' ) )
  579. a = []
  580. a.append( ['M ', [x, y]] )
  581. a.append( [' l ', [w, 0]] )
  582. a.append( [' l ', [0, h]] )
  583. a.append( [' l ', [-w, 0]] )
  584. a.append( [' Z', []] )
  585. self.getPathVertices( simplepath.formatPath( a ), node, matNew )
  586. elif node.tag == inkex.addNS( 'line', 'svg' ) or node.tag == 'line':
  587. # Convert
  588. #
  589. # <line x1="X1" y1="Y1" x2="X2" y2="Y2/>
  590. #
  591. # to
  592. #
  593. # <path d="MX1,Y1 LX2,Y2"/>
  594. x1 = float( node.get( 'x1' ) )
  595. y1 = float( node.get( 'y1' ) )
  596. x2 = float( node.get( 'x2' ) )
  597. y2 = float( node.get( 'y2' ) )
  598. if ( not x1 ) or ( not y1 ) or ( not x2 ) or ( not y2 ):
  599. pass
  600. a = []
  601. a.append( ['M ', [x1, y1]] )
  602. a.append( [' L ', [x2, y2]] )
  603. self.getPathVertices( simplepath.formatPath( a ), node, matNew )
  604. elif node.tag == inkex.addNS( 'polyline', 'svg' ) or node.tag == 'polyline':
  605. # Convert
  606. #
  607. # <polyline points="x1,y1 x2,y2 x3,y3 [...]"/>
  608. #
  609. # to
  610. #
  611. # <path d="Mx1,y1 Lx2,y2 Lx3,y3 [...]"/>
  612. #
  613. # Note: we ignore polylines with no points
  614. pl = node.get( 'points', '' ).strip()
  615. if pl == '':
  616. pass
  617. pa = pl.split()
  618. d = "".join( ["M " + pa[i] if i == 0 else " L " + pa[i] for i in range( 0, len( pa ) )] )
  619. self.getPathVertices( d, node, matNew )
  620. elif node.tag == inkex.addNS( 'polygon', 'svg' ) or node.tag == 'polygon':
  621. # Convert
  622. #
  623. # <polygon points="x1,y1 x2,y2 x3,y3 [...]"/>
  624. #
  625. # to
  626. #
  627. # <path d="Mx1,y1 Lx2,y2 Lx3,y3 [...] Z"/>
  628. #
  629. # Note: we ignore polygons with no points
  630. pl = node.get( 'points', '' ).strip()
  631. if pl == '':
  632. pass
  633. pa = pl.split()
  634. d = "".join( ["M " + pa[i] if i == 0 else " L " + pa[i] for i in range( 0, len( pa ) )] )
  635. d += " Z"
  636. self.getPathVertices( d, node, matNew )
  637. elif node.tag == inkex.addNS( 'ellipse', 'svg' ) or \
  638. node.tag == 'ellipse' or \
  639. node.tag == inkex.addNS( 'circle', 'svg' ) or \
  640. node.tag == 'circle':
  641. # Convert circles and ellipses to a path with two 180 degree arcs.
  642. # In general (an ellipse), we convert
  643. #
  644. # <ellipse rx="RX" ry="RY" cx="X" cy="Y"/>
  645. #
  646. # to
  647. #
  648. # <path d="MX1,CY A RX,RY 0 1 0 X2,CY A RX,RY 0 1 0 X1,CY"/>
  649. #
  650. # where
  651. #
  652. # X1 = CX - RX
  653. # X2 = CX + RX
  654. #
  655. # Note: ellipses or circles with a radius attribute of value 0 are ignored
  656. if node.tag == inkex.addNS( 'ellipse', 'svg' ) or node.tag == 'ellipse':
  657. rx = float( node.get( 'rx', '0' ) )
  658. ry = float( node.get( 'ry', '0' ) )
  659. else:
  660. rx = float( node.get( 'r', '0' ) )
  661. ry = rx
  662. if rx == 0 or ry == 0:
  663. pass
  664. cx = float( node.get( 'cx', '0' ) )
  665. cy = float( node.get( 'cy', '0' ) )
  666. x1 = cx - rx
  667. x2 = cx + rx
  668. d = 'M %f,%f ' % ( x1, cy ) + \
  669. 'A %f,%f ' % ( rx, ry ) + \
  670. '0 1 0 %f,%f ' % ( x2, cy ) + \
  671. 'A %f,%f ' % ( rx, ry ) + \
  672. '0 1 0 %f,%f' % ( x1, cy )
  673. self.getPathVertices( d, node, matNew )
  674. elif node.tag == inkex.addNS( 'pattern', 'svg' ) or node.tag == 'pattern':
  675. pass
  676. elif node.tag == inkex.addNS( 'metadata', 'svg' ) or node.tag == 'metadata':
  677. pass
  678. elif node.tag == inkex.addNS( 'defs', 'svg' ) or node.tag == 'defs':
  679. pass
  680. elif node.tag == inkex.addNS( 'desc', 'svg' ) or node.tag == 'desc':
  681. pass
  682. elif node.tag == inkex.addNS( 'namedview', 'sodipodi' ) or node.tag == 'namedview':
  683. pass
  684. elif node.tag == inkex.addNS( 'eggbot', 'svg' ) or node.tag == 'eggbot':
  685. pass
  686. elif node.tag == inkex.addNS( 'text', 'svg' ) or node.tag == 'text':
  687. inkex.errormsg( 'Warning: unable to draw text, please convert it to a path first.' )
  688. pass
  689. elif node.tag == inkex.addNS( 'title', 'svg' ) or node.tag == 'title':
  690. pass
  691. elif node.tag == inkex.addNS( 'image', 'svg' ) or node.tag == 'image':
  692. if not self.warnings.has_key( 'image' ):
  693. inkex.errormsg( gettext.gettext( 'Warning: unable to draw bitmap images; ' +
  694. 'please convert them to line art first. Consider using the "Trace bitmap..." ' +
  695. 'tool of the "Path" menu. Mac users please note that some X11 settings may ' +
  696. 'cause cut-and-paste operations to paste in bitmap copies.' ) )
  697. self.warnings['image'] = 1
  698. pass
  699. elif node.tag == inkex.addNS( 'pattern', 'svg' ) or node.tag == 'pattern':
  700. pass
  701. elif node.tag == inkex.addNS( 'radialGradient', 'svg' ) or node.tag == 'radialGradient':
  702. # Similar to pattern
  703. pass
  704. elif node.tag == inkex.addNS( 'linearGradient', 'svg' ) or node.tag == 'linearGradient':
  705. # Similar in pattern
  706. pass
  707. elif node.tag == inkex.addNS( 'style', 'svg' ) or node.tag == 'style':
  708. # This is a reference to an external style sheet and not the value
  709. # of a style attribute to be inherited by child elements
  710. pass
  711. elif node.tag == inkex.addNS( 'cursor', 'svg' ) or node.tag == 'cursor':
  712. pass
  713. elif node.tag == inkex.addNS( 'color-profile', 'svg' ) or node.tag == 'color-profile':
  714. # Gamma curves, color temp, etc. are not relevant to single color output
  715. pass
  716. elif not isinstance( node.tag, basestring ):
  717. # This is likely an XML processing instruction such as an XML
  718. # comment. lxml uses a function reference for such node tags
  719. # and as such the node tag is likely not a printable string.
  720. # Further, converting it to a printable string likely won't
  721. # be very useful.
  722. pass
  723. else:
  724. inkex.errormsg( 'Warning: unable to draw object <%s>, please convert it to a path first.' % node.tag )
  725. pass
  726. def recursivelyGetEnclosingTransform( self, node ):
  727. '''
  728. Determine the cumulative transform which node inherits from
  729. its chain of ancestors.
  730. '''
  731. node = node.getparent()
  732. if node is not None:
  733. parent_transform = self.recursivelyGetEnclosingTransform( node )
  734. node_transform = node.get( 'transform', None )
  735. if node_transform is None:
  736. return parent_transform
  737. else:
  738. tr = simpletransform.parseTransform( node_transform )
  739. if parent_transform is None:
  740. return tr
  741. else:
  742. return simpletransform.composeTransform( parent_transform, tr )
  743. else:
  744. return self.docTransform
  745. def effect( self ):
  746. # Viewbox handling
  747. self.handleViewBox()
  748. # First traverse the document (or selected items), reducing
  749. # everything to line segments. If working on a selection,
  750. # then determine the selection's bounding box in the process.
  751. # (Actually, we just need to know it's extrema on the x-axis.)
  752. if self.options.ids:
  753. # Traverse the selected objects
  754. for id in self.options.ids:
  755. transform = self.recursivelyGetEnclosingTransform( self.selected[id] )
  756. self.recursivelyTraverseSvg( [self.selected[id]], transform )
  757. else:
  758. # Traverse the entire document building new, transformed paths
  759. self.recursivelyTraverseSvg( self.document.getroot(), self.docTransform )
  760. # Determine the center of the drawing's bounding box
  761. self.cx = self.xmin + ( self.xmax - self.xmin ) / 2.0
  762. self.cy = self.ymin + ( self.ymax - self.ymin ) / 2.0
  763. # Determine which polygons lie entirely within other polygons
  764. try:
  765. if '/' == os.sep:
  766. self.f = open( os.path.expanduser( self.options.fname ), 'w')
  767. else:
  768. self.f = open( os.path.expanduser( self.options.fname ).replace('/', os.sep), 'w')
  769. self.f.write('''
  770. // Module names are of the form poly_<inkscape-path-id>().
  771. // As a result you can associate a polygon in this OpenSCAD program with the
  772. // corresponding SVG element in the Inkscape document by looking for
  773. // the XML element with the attribute id=\"inkscape-path-id\".
  774. // Paths have their own variables so they can be imported and used
  775. // in polygon(points) structures in other programs.
  776. // The NN_points is the list of all polygon XY vertices.
  777. // There may be an NN_paths variable as well. If it exists then it
  778. // defines the nested paths. Both must be used in the
  779. // polygon(points, paths) variant of the command.
  780. profile_scale = 25.4/90; //made in inkscape in mm
  781. // helper functions to determine the X,Y dimensions of the profiles
  782. function min_x(shape_points) = min([ for (x = shape_points) min(x[0])]);
  783. function max_x(shape_points) = max([ for (x = shape_points) max(x[0])]);
  784. function min_y(shape_points) = min([ for (x = shape_points) min(x[1])]);
  785. function max_y(shape_points) = max([ for (x = shape_points) max(x[1])]);
  786. ''' )
  787. # writeout width and height
  788. self.f.write( 'height = %s;\n' % ( self.options.height ) )
  789. self.f.write( 'width = %s;\n\n' % ( self.options.line_width ) )
  790. for key in self.paths:
  791. self.f.write( '\n' )
  792. self.convertPath( key )
  793. # Now output the list of modules to call
  794. self.f.write( '\n// The shapes\n' )
  795. for call in self.call_list:
  796. self.f.write( call )
  797. except:
  798. inkex.errormsg( 'Unable to open the file ' + self.options.fname )
  799. if __name__ == '__main__':
  800. e = OpenSCAD()
  801. e.affect()