Eneroth3 / inkscape-purge-short-lines

Inskcape extension for purging short lines. Useful for cleaning up SketchUp 2D exports.
MIT License
6 stars 2 forks source link

PlugIn does not work for me #1

Open vmario89 opened 5 years ago

vmario89 commented 5 years ago

Dear Julia,

i am interested to get the plugin working but i had no luck. Neither with InkScape 0.91, 092.0, 0.92.3 or 0.92.4. I tried with simple triangle as well as huge graphics.

I guess your plugin maybe worked/works for you because the "d" from node might not have more than 2 points each node/path (=pdf export from SketchUp?)

grafik

The stacktrace thrown is always

Traceback (most recent call last):
  File "eneroth_purge_short_edges.py", line 64, in <module>
    EnerothPurgeShortEdges().affect()
  File "C:\Users\mario\Downloads\Inkscape-0.91-1-win64\inkscape\share\extensions\inkex.py", line 268, in affect
    self.effect()
  File "eneroth_purge_short_edges.py", line 30, in effect
    self.iterate_node(node)
  File "eneroth_purge_short_edges.py", line 33, in iterate_node
    self.do_node(node)
  File "eneroth_purge_short_edges.py", line 51, in do_node
    coords[0] += prev_coords[0]
TypeError: 'NoneType' object is unsubscriptable

I just did some experiments with it but it won't work

#!/usr/bin/env python

import sys
sys.path.append('/usr/share/inkscape/extensions')

# Purge short lines
# This script is designed to be used to clean up/simplify 2D vector exports from
# SketchUp. It ignores everything but paths between exactly 2 points.

import inkex
import math
from simplestyle import *

debug = False

class EnerothPurgeShortEdges(inkex.Effect):
  def __init__(self):
    inkex.Effect.__init__(self)
    self.OptionParser.add_option('-w', '--length', action = 'store',
      type = 'float', dest = 'length', default = 10.0)

  def effect(self):
    length = self.options.length
    svg = self.document.getroot()

    if len(self.selected)==0:
      self.iterate_node(self.document.getroot())
    else:
      for id, node in self.selected.iteritems():
        self.iterate_node(node)

  def iterate_node(self, node):
    self.do_node(node)
    for child in node:
      self.iterate_node(child)

  def do_node(self, node):
    if node.attrib.has_key('d'):
      points = []

      instruction = None
      prev_coords = [0,0]

      words = node.get('d').split(' ')
      for i, word in enumerate(words):
        if len(word) == 1:
          instruction = word
          # inkex.debug(word)         
        else:
          # Sometimes there is the case that "coords" returns only an array like [-1.29] (only one coordinate) instead of XY coordinates. Reason is the type "map"
          coords = map(lambda c: float(c), word.split(','))
          # inkex.debug(coords)
          if instruction.lower() == instruction:
            # inkex.debug("coords len=" + str(len(coords)) + "prev_coords len=" + str(len(prev_coords)))
            coords[0] += prev_coords[0]
            coords[1] += prev_coords[1]
            # if len(coords) == 2:
            # coords[1] += prev_coords[1]
          prev_coords = coords
          # Assume all coordinates are points of straight lines (instructor being M, m, L or l)
          inkex.debug("X=" + str(coords[0]) + "Y=" + str(coords[1]))  
          points.append(coords)
          inkex.debug("pointsCount=" + str(len(points)))  

      if len(points) == 2:
        length = math.sqrt((points[0][0]-points[1][0])**2 + (points[0][1]-points[1][1])**2)
        inkex.debug("length=" + str(length))
        if debug:
          inkex.debug(length)
        if length < self.options.length:
          inkex.debug("delete")
          node.getparent().remove(node)

EnerothPurgeShortEdges().affect()

Do you have some idea on how to fix it?

regards and thanks for your work, Mario

Eneroth3 commented 5 years ago

Hello Mario,

This extension is only designed to function on straight lines between 2 points. I made it to lower the weight of PDF output from SketchUp, which doesn't contain any polylines or other shapes.

Other paths should just be ignored but i haven't used this extension for about 2 years myself so I don't know if there has been any breaking changes in Inkscape since.

vmario89 commented 5 years ago

Hey. Thanks for your reply. I gave another try. Now i understand. Plugin is still good for some more special use cases!

made it working with the upper mentioned code (except that i reverted the debugging output): grafik

grafik

I think this could be enhanced

but i know: there might be no time to do this :-(

regards, Mario