Skip to content

Tutorials: How to create a plugin

SteveMoto edited this page Jun 29, 2018 · 2 revisions

Here is a short example with exhaustive comments on how to create a simple and useless plugin:

You can copy everything and put it into a text file with .py extension in the "plugins" directory. It's nice to give the file the same name as your plugin. In this case myplugin.py
Start bCNC and test. Take inspiration from all others plugins and share.

bCNC uses tab for indentation, beware when using copy and paste

#!/usr/bin/python
# -*- coding: ascii -*-
# $Id$
#
# Author:  Put your name here 
# Date:	   

__author__ = "Name Surname"
__email__  = "my.mail@gmail.com"  #<<< here put an email where plugins users can contact you

#Here import the libraries you need, these are necessary to modify the code  
from CNC import CNC,Block
from ToolsPage import Plugin

#==============================================================================
# My plugin
#==============================================================================
class Tool(Plugin):
	# WARNING the __doc__ is needed to allow the string to be internationalized
	__doc__ = _("""This is my plugin""")			#<<< This comment will be show as tooltip for the ribbon button
	def __init__(self, master):
		Plugin.__init__(self, master,"MyPlugin")
		#MYPlugin: is the name of the plugin show in the tool ribbon button
		self.icon = "lamp"			#<<< This is the name of gif file used as icon for the ribbon button. It will be search in the "icons" subfolder
		self.group = "Generator"	#<<< This is the name of group that plugin belongs
		#Here we are creating the widgets presented to the user inside the plugin
		#Name, Type , Default value, Description
		self.variables = [			#<<< Define a list of components for the GUI
			("name"    ,    "db" ,    "", _("Name")),							#used to store plugin settings in the internal database
			("Size"    ,    "mm" ,   100.0, _("Size description")),				#a variable that will be converted in mm/inch based on bCNC settting
			("Int"     ,   "int" ,    2222, _("Integer description")),			#an integer variable
			("Float"   , "float" ,    33.3, _("Float description")),			#a float value variable
			("Text"    , "text" ,    "Free Text", _("Text description")),		#a text input box
			("CheckBox", "bool" ,  False, _("CheckBox description")),			#a true/false check box
			("OpenFile", "file" ,     "", _("OpenFile description")),			#a file chooser widget
			("ComboBox", "Item1,Item2,Item3" , "Item1", _("ComboBox description"))	#a multiple choice combo box 
		]
		self.buttons.append("exe")  #<<< This is the button added at bottom to call the execute method below

	# ----------------------------------------------------------------------
	# This method is executed when user presses the plugin execute button 
	# ----------------------------------------------------------------------
	def execute(self, app):
		name = self["name"]
		if not name or name=="default": name="Default Name"
		
		#Retrive data from user imput
		userSize = self["Size"]
		
		#Initialize blocks that will contain our gCode
		blocks = []
		block = Block(name)
		
		#use some useful bCNC functions to generate gCode movement, see CNC.py for more
		x = 0
		y = 0
		block.append(CNC.zsafe()) 			#<<< Move rapid Z axis to the safe height in Stock Material
		block.append(CNC.grapid(x,y))		#<<< Move rapid to X and Y coordinate
		block.append(CNC.zenter(-1)) 		#<<< Enter in the material with Plunge Feed for current material
		cutFeed = CNC.vars["cutfeed"]		#<<< Get cut feed for the current material
		block.append(CNC.fmt("f",cutFeed))	#<<< Set cut feed
 
		#Add interpolated line to varius x,y coordinates
		i = userSize / 20.0
		while (x < userSize):
			x += i
			block.append(CNC.gline(x,0))
			block.append(CNC.gline(0,userSize - y))
			y += i
			block.append(CNC.gline(0,userSize - y))

		#
		blocks.append(block)
		active = app.activeBlock()
		app.gcode.insBlocks(active, blocks, "MyPlugins inserted")	#<<< insert blocks over active block in the editor
		app.refresh()												#<<< refresh editor
		app.setStatus(_("Generated: MyPlugin Result"))				#<<< feed back result

Result

Clone this wiki locally