Inkspace1.0 插件(2) 添加圖層畫線 例子

第二個插件實現畫路路徑, 添加圖層

配置文件 D:\Inkscape\share\inkscape\extensions\mj_draw_path_example.inx

<?xml version="1.0" encoding="UTF-8"?>
<inkscape-extension xmlns="http://www.inkscape.org/namespace/inkscape/extension">
    <name>Draw Path Example</name>
    <id>org.inkscape.filter.mj_draw_path_example</id>

    <effect>
        <object-type>path</object-type>
            <effects-menu>
                <submenu name="AAA Tools"/>
            </effects-menu>
    </effect>
    <script>
        <command location="inx" interpreter="python">mj_draw_path_example.py</command>
    </script>
</inkscape-extension>

程序文件 D:\Inkscape\share\inkscape\extensions\mj_draw_path_example.py

#!/usr/bin/env python
# coding=utf-8

import inkex
from inkex.elements import Group, PathElement

class MyExtension(inkex.EffectExtension):
    # ...
    def effect(self):
        # Create a new layer. 增加一個叫MyNewlayer的圖層, 注意1.0用插件可以產生同名圖層,可能是bug
        layer = self.svg.add(inkex.Layer.new(u'MyNewlayer'))
        layer.set('style', 'display:true')  # initially visible     
        layer.append(self.make_a_shape())       # 添加一個路徑
        return layer

    def make_a_shape(self):
        my_shape = PathElement()
        # Lists of numbers, pythonic objects, Cubic curves etc.
        my_shape.style =  {'stroke': 'red', 'stroke-width': '2', 'fill': 'none'}
        my_shape.path = "M 10 10 L 15 250 C 150 70 10 10 10 20"        #svg 代碼  
        # Transform can be modified in many ways too
        my_shape.transform.add_translate(self.svg.namedview.center)     #移動到中間
        my_shape.transform.add_scale(2.0,2.0)                           #放大2倍
        return my_shape

if __name__ == '__main__':
    MyExtension().run()

 實現結果爲紅線部分

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章