diff options
Diffstat (limited to 'share')
| -rw-r--r-- | share/extensions/dpiswitcher.inx | 12 | ||||
| -rw-r--r-- | share/extensions/dpiswitcher.py | 184 | ||||
| -rw-r--r-- | share/extensions/dxf_input.inx | 12 | ||||
| -rwxr-xr-x | share/extensions/dxf_input.py | 16 |
4 files changed, 167 insertions, 57 deletions
diff --git a/share/extensions/dpiswitcher.inx b/share/extensions/dpiswitcher.inx index 145fd9357..a4f487c97 100644 --- a/share/extensions/dpiswitcher.inx +++ b/share/extensions/dpiswitcher.inx @@ -5,10 +5,18 @@ <id>org.inkscape.dpiswitcher</id> <dependency type="executable" location="extensions">dpiswitcher.py</dependency> <dependency type="executable" location="extensions">inkex.py</dependency> - <param name="switcher" type="enum" _gui-text="Type of convert:"> + <param name="action" type="notebook"> + <page name="dpi_swicher" _gui-text="Switch DPI"> + <param name="switcher" type="enum" _gui-text="Type of convert:"> <_item value="0">DPI Switch from 90 to 96</_item> <_item value="1">DPI Switch from 96 to 90</_item> - </param> + </param> + </page> + <page name="page_info" _gui-text="Show page info"> + <_param name="d" type="description">Choose this tab if you would like to see page info previously to apply DPI Switcher.</_param> + </page> + </param> + <effect needs-live-preview="false" needs-document="no"> <object-type>all</object-type> <effects-menu> diff --git a/share/extensions/dpiswitcher.py b/share/extensions/dpiswitcher.py index b4630ae4c..b110c259f 100644 --- a/share/extensions/dpiswitcher.py +++ b/share/extensions/dpiswitcher.py @@ -34,8 +34,12 @@ class DPISwitcher(inkex.Effect): self.OptionParser.add_option("--switcher", action="store", type="string", dest="switcher", default="0", help="Select the DPI switch you want") + self.OptionParser.add_option("--action", action="store", + type="string", dest="action", + default=None, help="") self.factor = 90.0/96.0 - self.unit = "px" + self.units = "px" + self.unitExponent = 1.0 def scaleRoot(self, svg): widthNumber = re.sub("[a-zA-Z]", "", svg.get('width')) @@ -43,13 +47,17 @@ class DPISwitcher(inkex.Effect): if svg.get('viewBox'): widthNumber = svg.get('viewBox').split(" ")[2] heightNumber = svg.get('viewBox').split(" ")[3] - widthDoc = str(float(widthNumber) * self.factor) - heightDoc = str(float(heightNumber) * self.factor) + widthDoc = str(float(widthNumber) * self.factor * self.unitExponent) + heightDoc = str(float(heightNumber) * self.factor * self.unitExponent) if svg.get('height'): svg.set('height', heightDoc) if svg.get('width'): svg.set('width', widthDoc) svg.set('viewBox',"0 0 " + widthDoc + " " + heightDoc) + self.scaleGuides(svg) + self.scaleGrid(svg) + if self.options.switcher == "1": + self.factor = self.factor * self.unitExponent xpathStr = '//svg:rect | //svg:image | //svg:path | //svg:circle | //svg:ellipse | //svg:text' elements = svg.xpath(xpathStr, namespaces=inkex.NSS) for element in elements: @@ -58,10 +66,16 @@ class DPISwitcher(inkex.Effect): continue if element.get('transform'): if "matrix" in str(element.get('transform')): - result = re.sub(r".*?((matrix).*?\))", self.scaleMatrixElement, str(element.get('transform'))) + result = re.sub(r".*?matrix( \(|\()(.*?)\)", self.matrixElement, str(element.get('transform'))) element.set('transform', result) if "scale" in str(element.get('transform')): - result = re.sub(r".*?((scale).*?\))", self.scaleElement, str(element.get('transform'))) + result = re.sub(r".*?scale( \(|\()(.*?)\)", self.scaleElement, str(element.get('transform'))) + element.set('transform', result) + if "translate" in str(element.get('transform')): + result = re.sub(r".*?translate( \(|\()(.*?)\)", self.translateElement, str(element.get('transform'))) + element.set('transform', result) + if "skew" in str(element.get('transform')): + result = re.sub(r".*?skew( \(|\()(.*?)\)", self.skewElement, str(element.get('transform'))) element.set('transform', result) if "scale" not in str(element.get('transform')) and "matrix" not in str(element.get('transform')): element.set('transform', str(element.get('transform')) + "scale(" + str(self.factor) + ", " + str(self.factor) + ")") @@ -72,35 +86,42 @@ class DPISwitcher(inkex.Effect): for element in elements: if element.get('transform'): if "matrix" in str(element.get('transform')): - result = re.sub(r".*?((matrix).*?\))", self.translateMatrixElement, str(element.get('transform'))) + result = re.sub(r".*?matrix( \(|\()(.*?)\)", self.matrixGroupElement, str(element.get('transform'))) element.set('transform', result) if "translate" in str(element.get('transform')): - result = re.sub(r".*?((translate).*?\))", self.translateElement, str(element.get('transform'))) + result = re.sub(r".*?translate( \(|\()(.*?)\)", self.translateElement, str(element.get('transform'))) element.set('transform', result) - self.scaleGuides(svg) - self.scaleGrid(svg) - - def scaleGrid(self, svg): - xpathStr = '//inkscape:grid' - grids = svg.xpath(xpathStr, namespaces=inkex.NSS) - for grid in grids: - empspacingNumber = float(grid.get("empspacing")) * self.factor - spacingxNumber = float(grid.get("spacingx").replace("px", "")) * self.factor - spacingyNumber = float(grid.get("spacingy").replace("px", "")) * self.factor - originxNumber = float(grid.get("originx").replace("px", "")) * self.factor - originyNumber = float(grid.get("originy").replace("px", "")) * self.factor - grid.set("empspacing", str(empspacingNumber)) - grid.set("spacingx", str(spacingxNumber) + "px") - grid.set("spacingy", str(spacingyNumber) + "px") - grid.set("originx", str(originxNumber) + "px") - grid.set("originy", str(originyNumber) + "px") - + if "skew" in str(element.get('transform')): + result = re.sub(r".*?skew( \(|\()(.*?)\)", self.skewElement, str(element.get('transform'))) + element.set('transform', result) + def scaleGuides(self, svg): xpathStr = '//sodipodi:guide' guides = svg.xpath(xpathStr, namespaces=inkex.NSS) for guide in guides: point = string.split(guide.get("position"), ",") - guide.set("position", str(float(point[0].strip()) * self.factor) + "," + str(float(point[1].strip()) * self.factor)) + guide.set("position", str(float(point[0].strip()) * self.factor ) + "," + str(float(point[1].strip()) * self.factor )) + + def scaleGrid(self, svg): + xpathStr = '//inkscape:grid' + grids = svg.xpath(xpathStr, namespaces=inkex.NSS) + for grid in grids: + if self.options.switcher == "0": + self.unitExponent = 1.0/(self.factor/self.__uuconv[grid.get("units")]) + self.factor = self.factor * self.unitExponent + grid.set("units", "px") + if grid.get("spacingx"): + spacingx = str(float(re.sub("[a-zA-Z]", "", grid.get("spacingx"))) * self.factor) + "px" + grid.set("spacingx", str(spacingx)) + if grid.get("spacingy"): + spacingy = str(float(re.sub("[a-zA-Z]", "", grid.get("spacingy"))) * self.factor) + "px" + grid.set("spacingy", str(spacingy)) + if grid.get("originx"): + originx = str(float(re.sub("[a-zA-Z]", "", grid.get("originx"))) * self.factor) + "px" + grid.set("originx", str(originx)) + if grid.get("originy"): + originy = str(float(re.sub("[a-zA-Z]", "", grid.get("originy"))) * self.factor) + "px" + grid.set("originy", str(originy)) #a dictionary of unit to user unit conversion factors __uuconv = {'in':96.0, 'pt':1.33333333333, 'px':1.0, 'mm':3.77952755913, 'cm':37.7952755913, @@ -110,38 +131,105 @@ class DPISwitcher(inkex.Effect): 'km':3543307.0866, 'pc':15.0, 'yd':3240 , 'ft':1080} def scaleElement(self, m): - scaleVal = m.group(1).replace("scale","").replace(" ","").replace("(","").replace(")","").split(",") - return "scale(" + str(float(scaleVal[0]) * self.factor) + "," + str(float(scaleVal[1]) * self.factor) + ")" + scaleVal = m.group(2).replace(" ","") + total = scaleVal.count(',') + if total == 1: + scaleVal = scaleVal.split(",") + return "scale(" + str(float(scaleVal[0]) * self.factor) + "," + str(float(scaleVal[1]) * self.factor) + ")" + else: + return "scale(" + str(float(scaleVal) * self.factor) + ")" - def scaleMatrixElement(self, m): - scaleMatrixVal = m.group(1).replace("matrix","").replace(" ","").replace("(","").replace(")","").split(",") - return "matrix(" + str(float(scaleMatrixVal[0]) * self.factor) + "," + scaleMatrixVal[1] + "," + scaleMatrixVal[2] + "," + str(float(scaleMatrixVal[3]) * self.factor) + "," + scaleMatrixVal[4] + "," + scaleMatrixVal[5] + ")" def translateElement(self, m): - translateVal = m.group(1).replace("translate","").replace(" ","").replace("(","").replace(")","").split(",") - return "translate(" + str(float(translateVal[0]) * self.factor) + "," + str(float(translateVal[1]) * self.factor) + ")" - - def translateMatrixElement(self, m): - translateMatrixVal = m.group(1).replace("matrix","").replace(" ","").replace("(","").replace(")","").split(",") - return "matrix(" + translateMatrixVal[0] + "," + translateMatrixVal[1] + "," + translateMatrixVal[2] + "," + translateMatrixVal[3] + "," + str(float(translateMatrixVal[4]) * self.factor) + "," + str(float(translateMatrixVal[5]) * self.factor) + ")" + translateVal = m.group(2).replace(" ","") + total = translateVal.count(',') + if total == 1: + translateVal = translateVal.split(",") + return "translate(" + str(float(translateVal[0]) * self.factor) + "," + str(float(translateVal[1]) * self.factor) + ")" + else: + return "translate(" + str(float(translateVal) * self.factor) + ")" + + def skewElement(self, m): + skeweVal = m.group(2).replace(" ","") + total = skewVal.count(',') + if total == 1: + skeweVal = skewVal.split(",") + return "skew(" + str(float(skewVal[0]) * self.factor) + "," + str(float(skewVal[1]) * self.factor) + ")" + else: + return "skew(" + str(float(skewVal) * self.factor) + ")" + + def matrixElement(self, m): + matrixVal = m.group(2).replace(" ","") + total = matrixVal.count(',') + matrixVal = matrixVal.split(",") + if total > 2: + return "matrix(" + str(float(matrixVal[0]) * self.factor) + "," + str(float(matrixVal[1]) * self.factor) + "," + str(float(matrixVal[2]) * self.factor) + "," + str(float(matrixVal[3]) * self.factor) + "," + str(float(matrixVal[4]) * self.factor) + "," + str(float(matrixVal[5]) * self.factor) + ")" + else: + return "matrix(" + str(float(matrixVal[0]) * self.factor) + "," + str(float(matrixVal[1]) * self.factor) + "," + str(float(matrixVal[2]) * self.factor) + ")" + + def matrixGroupElement(self, m): + matrixVal = m.group(2).replace(" ","") + total = matrixVal.count(',') + matrixVal = matrixVal.split(",") + if total > 2: + return "matrix(" + matrixVal[0] + "," + str(float(matrixVal[1]) * self.factor) + "," + matrixVal[2] + "," + str(float(matrixVal[3]) * self.factor) + "," + str(float(matrixVal[4]) * self.factor) + "," + str(float(matrixVal[5]) * self.factor) + ")" + else: + return "matrix(" + matrixVal[0] + "," + str(float(matrixVal[1]) * self.factor) + "," + str(float(matrixVal[2]) * self.factor) + ")" def effect(self): - if self.options.switcher == "0": - self.factor = 96.0/90.0 + action = self.options.action.strip("\"") # TODO Is this a bug? (Extra " characters) saveout = sys.stdout sys.stdout = sys.stderr svg = self.document.getroot() - namedview = svg.find(inkex.addNS('namedview', 'sodipodi')) - self.unit = namedview.get(inkex.addNS('document-units', 'inkscape')) - if self.unit and self.unit <> "px": - unitExponent = 0.0 + if action == "page_info": + print ":::SVG document related info:::" + width = svg.get('width') + if width: + print "width: " + width + height = svg.get('height') + if height: + print "height: " + height + viewBox = svg.get('viewBox') + if viewBox: + print "viewBox: " + viewBox + namedview = svg.find(inkex.addNS('namedview', 'sodipodi')) + docunits= namedview.get(inkex.addNS('document-units', 'inkscape')) + if docunits: + print "document-units: " + docunits + units = namedview.get('units') + if units: + print "units: " + units + xpathStr = '//sodipodi:guide' + guides = svg.xpath(xpathStr, namespaces=inkex.NSS) + xpathStr = '//inkscape:grid' + if guides: + numberGuides = len(guides) + print "Document has " + str(numberGuides) + " guides" + grids = svg.xpath(xpathStr, namespaces=inkex.NSS) + i = 1 + for grid in grids: + print "Grid number " + str(i) + ": Units: " + grid.get("units") + i = i+1 + else: if self.options.switcher == "0": - unitExponent = 1.0/(self.factor/self.__uuconv[self.unit]) - else: - unitExponent = 1.0/(self.factor/self.__uuconvLegazy[self.unit]) + self.factor = 96.0/90.0 + namedview = svg.find(inkex.addNS('namedview', 'sodipodi')) namedview.set(inkex.addNS('document-units', 'inkscape'), "px") - self.factor = self.factor * unitExponent - self.scaleRoot(svg); + self.units = re.sub("[0-9]*\.?[0-9]", "", svg.get('width')) + if self.units and self.units <> "px": + if self.options.switcher == "0": + self.unitExponent = 1.0/(self.factor/self.__uuconv[self.units]) + else: + self.unitExponent = 1.0/(self.factor/self.__uuconvLegazy[self.units]) + ''' + else: + self.scaleGuides(svg) + self.unitExponent = 1.0 + self.scaleGrid(svg) + sys.stdout = saveout + return + ''' + self.scaleRoot(svg); sys.stdout = saveout effect = DPISwitcher() diff --git a/share/extensions/dxf_input.inx b/share/extensions/dxf_input.inx index d51a07c39..4488774a3 100644 --- a/share/extensions/dxf_input.inx +++ b/share/extensions/dxf_input.inx @@ -6,8 +6,12 @@ <dependency type="executable" location="extensions">inkex.py</dependency> <param name="tab" type="notebook"> <page name="options" _gui-text="Options"> - <param name="auto" type="boolean" _gui-text="Use automatic scaling to size A4">true</param> - <param name="scale" type="string" _gui-text="Or, use manual scale factor:">1.0</param> + <param name="scalemethod" type="optiongroup" _gui-text="Method of Scaling:"> + <option value="manual">Manual scale</option> + <option value="auto">Automatic scaling to size A4</option> + <option value="file">Read from file</option> + </param> + <param name="scale" type="string" _gui-text="Manual scale factor:">1.0</param> <param name="xmin" type="string" _gui-text="Manual x-axis origin (mm):">0.0</param> <param name="ymin" type="string" _gui-text="Manual y-axis origin (mm):">0.0</param> <param name="gcodetoolspoints" type="boolean" _gui-text="Gcodetools compatible point import">false</param> @@ -22,9 +26,11 @@ </page> <page name="help" _gui-text="Help"> <_param name="inputhelp" type="description" xml:space="preserve">- AutoCAD Release 13 and newer. -- assume dxf drawing is in mm. +- for manual scaling, assume dxf drawing is in mm. - assume svg drawing is in pixels, at 96 dpi. - scale factor and origin apply only to manual scaling. +- 'Automatic scaling' will fit the width of an A4 page. +- 'Read from file' uses the variable $MEASUREMENT. - layers are preserved only on File->Open, not Import. - limited support for BLOCKS, use AutoCAD Explode Blocks instead, if needed.</_param> </page> diff --git a/share/extensions/dxf_input.py b/share/extensions/dxf_input.py index 972895cb8..f0a1296f1 100755 --- a/share/extensions/dxf_input.py +++ b/share/extensions/dxf_input.py @@ -344,7 +344,7 @@ colors = { 1: '#FF0000', 2: '#FFFF00', 3: '#00FF00', 4: '#00FFFF', 5: ' 250: '#333333', 251: '#505050', 252: '#696969', 253: '#828282', 254: '#BEBEBE', 255: '#FFFFFF'} parser = inkex.optparse.OptionParser(usage="usage: %prog [options] SVGfile", option_class=inkex.InkOption) -parser.add_option("--auto", action="store", type="inkbool", dest="auto", default=True) +parser.add_option("--scalemethod", action="store", type="string", dest="scalemethod", default="manual") parser.add_option("--scale", action="store", type="string", dest="scale", default="1.0") parser.add_option("--xmin", action="store", type="string", dest="xmin", default="0.0") parser.add_option("--ymin", action="store", type="string", dest="ymin", default="0.0") @@ -366,6 +366,7 @@ inkex.etree.SubElement(pattern, 'path', {'d': 'M4 0 l-4,4', 'stroke': '#000000', stream = open(args[0], 'r') xmax = xmin = ymin = 0.0 height = 297.0*96.0/25.4 # default A4 height in pixels +measurement = 0 # default inches line = get_line() polylines = 0 flag = 0 # (0, 1, 2, 3) = (none, LAYER, LTYPE, DIMTXT) @@ -376,7 +377,10 @@ DIMTXT = {} # store DIMENSION text sizes while line[0] and line[1] != 'BLOCKS': line = get_line() - if options.auto: + if options.scalemethod == 'file': + if line[1] == '$MEASUREMENT': + measurement = get_group('70') + elif options.scalemethod == 'auto': if line[1] == '$EXTMIN': xmin = get_group('10') ymin = get_group('20') @@ -406,7 +410,11 @@ while line[0] and line[1] != 'BLOCKS': if line[0] == '0' and line[1] == 'ENDTAB': flag = 0 -if options.auto: +if options.scalemethod == 'file': + scale = 25.4 # default inches + if measurement == 1.0: + scale = 1.0 # use mm +elif options.scalemethod == 'auto': scale = 1.0 if xmax > xmin: scale = 210.0/(xmax - xmin) # scale to A4 width @@ -414,7 +422,7 @@ else: scale = float(options.scale) # manual scale factor xmin = float(options.xmin) ymin = float(options.ymin) -desc.text = '%s - scale = %f, origin = (%f, %f), auto = %s' % (unicode(args[0], options.input_encode), scale, xmin, ymin, options.auto) +desc.text = '%s - scale = %f, origin = (%f, %f), method = %s' % (unicode(args[0], options.input_encode), scale, xmin, ymin, options.scalemethod) scale *= 96.0/25.4 # convert from mm to pixels if not layer_nodes.has_key('0'): |
