kie27 / draw_ratchet

inkscape extension (for drawing ratchets)
GNU General Public License v3.0
2 stars 1 forks source link

python version #1

Open Neon22 opened 8 years ago

Neon22 commented 8 years ago

just in case someone doesn't have perl. I thought I'd contribute a python version. Cheers.

#!/usr/bin/python
#
# Copyright (C) 2016 Kie Brooks
# Distributed under GPLv3 or later.
# draw_ratchet
# version 0.1
#
#   This file is part of draw_ratchet
#
#   draw_ratchet is free software: you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation, either version 3 of the License, or
#   (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
#
# description :
# draw ratchet gears to arbitrary sizes and precision
#
# I am not an engineer, I am sure this little program could be improved or made much more complex,
# it may contain horrible flaws but it fills my current need and should be easy to improve on.
# Alternatively pay me some money and I might do it for you ;P
#
# This program is used by inkscape.
# It can be found in the menu :  extensions -> render -> ratchet
# see file draw_ratch.inx
# (directory ~/.config/inkscape/extensions/)

import inkex, simplestyle
import math

Line_style =   { 'stroke'        : '#000000',
                 'stroke-width'  : '0.5px',
                 'fill'          : 'none' }

### SVG shape helpers
def draw_SVG_circle(parent, r, cx, cy, name, style):
    " structure an SVG circle entity under parent "
    circ_attribs = {'style': simplestyle.formatStyle(style),
                    'cx': str(cx), 'cy': str(cy), 
                    'r': str(r),
                    inkex.addNS('label','inkscape'): name}
    circle = inkex.etree.SubElement(parent, inkex.addNS('circle','svg'), circ_attribs )

###
class Ratchet(inkex.Effect):

    def __init__(self):
        inkex.Effect.__init__(self)
        self.OptionParser.add_option("--centre_hole",      action="store", type='inkbool',
                                  dest="centre_hole",      default=True, help="Show or not")
        self.OptionParser.add_option("--teeth",            action="store", type="int",
                                  dest="teeth",            default=12, help="Number of teeth around outside")
        self.OptionParser.add_option("--centre_hole_diam", action="store", type="float",
                                  dest="centre_hole_diam", default=1, help="Dia of central hole")
        self.OptionParser.add_option("--diam_in",          action="store", type="float",
                                  dest="diam_in",          default=28, help="Inner diamter of the Ratchet")
        self.OptionParser.add_option("--diam_out",         action="store", type="float",
                                  dest="diam_out",         default=30, help="Outer diamter of the Ratchet")
        self.OptionParser.add_option('--vtooth_shape',     action='store', type='string',
                                  dest='vtooth_shape',     default='straight', help="Shape of tooth")
        self.OptionParser.add_option('--htooth_shape',     action='store', type='string',
                                  dest='htooth_shape',     default='curve', help="Shape of tooth")

    def effect(self):
        # sort out the options
        teeth = self.options.teeth
        diam_in = self.options.diam_in
        diam_out = self.options.diam_out
        vtooth_shape = self.options.vtooth_shape
        htooth_shape = self.options.htooth_shape
        # Create group center of view
        t = 'translate(%s,%s)' % (self.view_center[0], self.view_center[1])
        grp_attribs = {inkex.addNS('label','inkscape'):'Ratchet', 'transform':t}
        grp = inkex.etree.SubElement(self.current_layer, 'g', grp_attribs)
        #
        # Central hole (at origin)
        if self.options.centre_hole:
            draw_SVG_circle(grp, self.options.centre_hole_diam, 0,0, 'Central_hole', Line_style)
        #Polygon drawing
        #    <path d="M50,50 A30,50 0 0,1 100,100"
        #          style="stroke:#660000; fill:none;"/>
        # L - line : x posn, y posn
        # A - arc : radius x, radius y, x-axis rotation, large arc flag, sweep flag
        # Points on the ratchet
        #    b
        #    |\
        #    |  \    |\
        # \  |    \  |  \
        #  \ |      \|    \
        #    a       c      \
        path = "M%s %s " %(diam_in,0)
        for i in range(teeth):
            angle = math.radians(i * 360/ teeth)
            x_a = diam_in * math.cos(angle)
            y_a = diam_in * math.sin(angle)
            x_b = diam_out * math.cos(angle)
            y_b = diam_out * math.sin(angle)
            if vtooth_shape == "straight":
                path += ' L %s %s ' %(x_b, y_b)
            else:
                path += " A %s,%s 0 0 0 %s %s" % (diam_in, diam_in, x_b, y_b)
            #
            angle_next = math.radians((i+1) * 360 / teeth)
            x_c = diam_in * math.cos(angle_next)
            y_c = diam_in * math.sin(angle_next)
            if htooth_shape == "straight":
                path += ' L %s %s ' %(x_c, y_c)
            else: # Arc
                path += " A %s,%s 0 0 1 %s %s" % (diam_in, diam_in, x_c, y_c)
        # close path
        path += 'z'
        # draw it
        line_attribs = {'style' : simplestyle.formatStyle(Line_style), inkex.addNS('label','inkscape') : 'Cone' }
        line_attribs['d'] = path
        inkex.etree.SubElement(grp, inkex.addNS('path','svg'), line_attribs )

# Create effect instance and apply it.
if __name__ == '__main__':
    Ratchet().affect()

To get the inx file adjusted you could change the following (I uses for < substitution): ''' dependency type="executable" location="extensions">draw_ratchet.py effect needs-live-preview="true"> object-type>path __command reldir="extensions" interpreter="python">draw_ratchet.py '''

vmario89 commented 4 years ago

Hi, i made this work with small modifications for InkScape 0.92.4 grafik

#!/usr/bin/python
#
# Copyright (C) 2016 Kie Brooks
# Distributed under GPLv3 or later.
# draw_ratchet
# version 0.1
#
#   This file is part of draw_ratchet
#
#   draw_ratchet is free software: you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation, either version 3 of the License, or
#   (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
#
# description :
# draw ratchet gears to arbitrary sizes and precision
#
# I am not an engineer, I am sure this little program could be improved or made much more complex,
# it may contain horrible flaws but it fills my current need and should be easy to improve on.
# Alternatively pay me some money and I might do it for you ;P
#
# This program is used by inkscape.
# It can be found in the menu :  extensions -> render -> ratchet
# see file draw_ratch.inx
# (directory ~/.config/inkscape/extensions/)

import inkex, simplestyle
import math

Line_style =   { 'stroke'        : '#000000',
                 'stroke-width'  : '0.5px',
                 'fill'          : 'none' }

### SVG shape helpers
def draw_SVG_circle(parent, r, cx, cy, name, style):
    " structure an SVG circle entity under parent "
    circ_attribs = {'style': simplestyle.formatStyle(style),
                    'cx': str(cx), 'cy': str(cy), 
                    'r': str(r),
                    inkex.addNS('label','inkscape'): name}
    circle = inkex.etree.SubElement(parent, inkex.addNS('circle','svg'), circ_attribs )

###
class Ratchet(inkex.Effect):

    def __init__(self):
        inkex.Effect.__init__(self)
        self.OptionParser.add_option("--centre_hole",      action="store", type='inkbool',
                                  dest="centre_hole",      default=True, help="Show or not")
        self.OptionParser.add_option("--teeth",            action="store", type="int",
                                  dest="teeth",            default=12, help="Number of teeth around outside")
        self.OptionParser.add_option("--centre_hole_diam", action="store", type="float",
                                  dest="centre_hole_diam", default=1, help="Dia of central hole")
        self.OptionParser.add_option("--diam_in",          action="store", type="float",
                                  dest="diam_in",          default=28, help="Inner diamter of the Ratchet")
        self.OptionParser.add_option("--diam_out",         action="store", type="float",
                                  dest="diam_out",         default=30, help="Outer diamter of the Ratchet")
        self.OptionParser.add_option('--vtooth_shape',     action='store', type='string',
                                  dest='vtooth_shape',     default='straight', help="Shape of tooth")
        self.OptionParser.add_option('--htooth_shape',     action='store', type='string',
                                  dest='htooth_shape',     default='curve', help="Shape of tooth")

    def effect(self):
        # sort out the options
        teeth = self.options.teeth
        diam_in = self.options.diam_in
        diam_out = self.options.diam_out
        vtooth_shape = self.options.vtooth_shape
        htooth_shape = self.options.htooth_shape
        # Create group center of view
        t = 'translate(%s,%s)' % (self.view_center[0], self.view_center[1])
        grp_attribs = {inkex.addNS('label','inkscape'):'Ratchet', 'transform':t}
        grp = inkex.etree.SubElement(self.current_layer, 'g', grp_attribs)
        #
        # Central hole (at origin)
        if self.options.centre_hole:
            draw_SVG_circle(grp, self.options.centre_hole_diam, 0,0, 'Central_hole', Line_style)
        #Polygon drawing
        #    <path d="M50,50 A30,50 0 0,1 100,100"
        #          style="stroke:#660000; fill:none;"/>
        # L - line : x posn, y posn
        # A - arc : radius x, radius y, x-axis rotation, large arc flag, sweep flag
        # Points on the ratchet
        #    b
        #    |\
        #    |  \    |\
        # \  |    \  |  \
        #  \ |      \|    \
        #    a       c      \
        path = "M%s %s " %(diam_in,0)
        for i in range(teeth):
            angle = math.radians(i * 360/ teeth)
            x_a = diam_in * math.cos(angle)
            y_a = diam_in * math.sin(angle)
            x_b = diam_out * math.cos(angle)
            y_b = diam_out * math.sin(angle)
            if vtooth_shape == "straight":
                path += ' L %s %s ' %(x_b, y_b)
            else:
                path += " A %s,%s 0 0 0 %s %s" % (diam_in, diam_in, x_b, y_b)
            #
            angle_next = math.radians((i+1) * 360 / teeth)
            x_c = diam_in * math.cos(angle_next)
            y_c = diam_in * math.sin(angle_next)
            if htooth_shape == "straight":
                path += ' L %s %s ' %(x_c, y_c)
            else: # Arc
                path += " A %s,%s 0 0 1 %s %s" % (diam_in, diam_in, x_c, y_c)
        # close path
        path += 'z'
        # draw it
        line_attribs = {'style' : simplestyle.formatStyle(Line_style), inkex.addNS('label','inkscape') : 'Cone' }
        line_attribs['d'] = path
        inkex.etree.SubElement(grp, inkex.addNS('path','svg'), line_attribs )

effect = Ratchet()
effect.affect() 
<?xml version="1.0" encoding="UTF-8"?>
<!--
    Copyright (C) 2016 Kie Brooks
    Distributed under GPLv3 or later
    This file is part of draw_ratchet
    version 0.1

    draw_ratchet is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    See the GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
-->
<inkscape-extension xmlns="http://www.inkscape.org/namespace/inkscape/extension">
    <_name>Ratchet</_name>
    <id>ratchet</id>
    <dependency type="executable" location="extensions">eridur-ratchet.py</dependency>
    <param name="teeth" type="int" min="2" max="300" _gui-text="Number of teeth">12</param>
    <param name="centre_hole" type="boolean" _gui-text="Centre hole">true</param>
    <param name="centre_hole_diam" type="float" precision="3" min="0" max="50" _gui-text="Centre hole size">1.000</param>
    <param name="diam_in" type="float" precision="3" min="0" max="200" _gui-text="Outside diameter">30.000</param>
    <param name="diam_out" type="float" precision="3" min="0" max="200" _gui-text="Inside diameter">28.000</param>
    <param name="vtooth_shape" type="optiongroup" _gui-text="Vertical tooth line:">
        <option value="straight">straight</option>
        <option value="curve">curve</option>
    </param>
    <param name="htooth_shape" type="optiongroup" _gui-text="Horizontal tooth line:">
        <option value="straight">straight</option>
        <option value="curve">curve</option>
    </param>
    <_param name="header_description" type="description" appearance="header">Author / License</_param>
    <_param name="some_description" type="description" xml:space="preserve">  written by Kie (inkscape@kitamura-studios.com)
  Draw Ratchet Copyright (C) 2016 Kie Brooks
  Distributed under GPLv3 or later

  This is a simple extension to save time drawing ratchets.
  </_param>
    <!-- classify which menu it appears under - can be new name -->
    <effect>
        <object-type>path</object-type>
        <effects-menu>
            <submenu _name="eridur Generators" />
            <!-- which menu under Extensions -->
        </effects-menu>
    </effect>
    <script>
        <command reldir="extensions" interpreter="python">eridur-ratchet.py</command>
    </script>
</inkscape-extension>
Neon22 commented 4 years ago

Nice job Kie. Have you considered adding it to the Extensions section:

You just need a thumbnail style mage and to zip it up.