tutorials:create-groimp-plugin
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
tutorials:create-groimp-plugin [2024/12/06 06:37] – [New file filter] gaetan | tutorials:create-groimp-plugin [2024/12/06 08:54] (current) – gaetan | ||
---|---|---|---|
Line 1: | Line 1: | ||
This tutorial aims at showing how to get started on GroIMP plugin development. It is assumed that you are able to compile the plugin using Maven (either the plugin alone, or the whole project). | This tutorial aims at showing how to get started on GroIMP plugin development. It is assumed that you are able to compile the plugin using Maven (either the plugin alone, or the whole project). | ||
- | In this tutorial we are going to create a new plugin (from the empty template), add a new MimeType (format of file accepted by GroIMP) for both import | + | In this tutorial we are going to create a new plugin (from the empty template), add a new MimeType (format of file accepted by GroIMP) for import, and add a menu item that run a specific command. |
See: | See: | ||
Line 212: | Line 212: | ||
{{: | {{: | ||
+ | === Actual content === | ||
+ | |||
+ | While the filter exists and is recognized by GroIMP, it is not doing anything because the java file is " | ||
+ | |||
+ | Open the file '' | ||
+ | <code java> | ||
+ | @Override | ||
+ | public Object getObject() throws IOException { | ||
+ | BufferedReader reader = new BufferedReader(((ReaderSource) source).getReader ()); | ||
+ | |||
+ | Node n = new Node(); | ||
+ | Node last = n; | ||
+ | String line; | ||
+ | while((line = reader.readLine()) != null) { | ||
+ | | ||
+ | float length = Float.valueOf( l[0]); | ||
+ | float radius = Float.valueOf( l[1]); | ||
+ | | ||
+ | | ||
+ | | ||
+ | } | ||
+ | |||
+ | return n; | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | And add the dependencies: | ||
+ | * Add the following imports in the file. | ||
+ | <code java> | ||
+ | import java.io.BufferedReader; | ||
+ | |||
+ | import de.grogra.graph.Graph; | ||
+ | import de.grogra.graph.impl.Node; | ||
+ | import de.grogra.imp3d.objects.Cylinder; | ||
+ | import de.grogra.pf.io.ReaderSource; | ||
+ | </ | ||
+ | |||
+ | * Add the module dependency in '' | ||
+ | <code java> | ||
+ | requires graph; | ||
+ | requires imp3d; | ||
+ | requires imp; | ||
+ | requires utilities; | ||
+ | </ | ||
+ | |||
+ | * In the '' | ||
+ | <code xml> | ||
+ | <import plugin=" | ||
+ | </ | ||
+ | |||
+ | * And the maven dependecy in the '' | ||
+ | <code xml> | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | </ | ||
+ | </ | ||
+ | |||
+ | Compile the project again and replace the old version by the newly created one. | ||
+ | GroIMP is now able to load .example file of the format: each line define a cylinder, with two values: length radius. | ||
+ | |||
+ | Let's create a file called '' | ||
+ | < | ||
+ | 1 0.2 | ||
+ | 0.6 0.1 | ||
+ | 2 0.5 | ||
+ | </ | ||
+ | |||
+ | This should create a chain of three cylinders if loaded in GroIMP. | ||
+ | |||
+ | Open GroIMP, create a new project, import the file and look at the result. | ||
+ | |||
+ | ==== New command ==== | ||
+ | |||
+ | In this section we will create a GroIMP command callable from a menu item that will create a random chain of Cylinders and adds it to the graph. | ||
+ | |||
+ | === Create the command === | ||
+ | |||
+ | In the java directory create a new file called '' | ||
+ | <code java> | ||
+ | package de.grogra.template.tutorial; | ||
+ | |||
+ | import de.grogra.graph.Graph; | ||
+ | import de.grogra.graph.impl.Node; | ||
+ | import de.grogra.imp3d.objects.Cylinder; | ||
+ | import de.grogra.pf.registry.Item; | ||
+ | import de.grogra.pf.ui.Context; | ||
+ | |||
+ | public class Commands { | ||
+ | |||
+ | public static void randomCylinders(Item item, Object info, Context ctx) { | ||
+ | Node n = new Node(); | ||
+ | Node last = n; | ||
+ | for(int i=0; | ||
+ | float length = (float) (0.5f + Math.random() * (2 - 0.5)); | ||
+ | float radius = (float) (0.1f + Math.random() * (0.4 - 0.1)); | ||
+ | Cylinder c = new Cylinder(length, | ||
+ | last.addEdgeBitsTo(c, | ||
+ | last = c; | ||
+ | } | ||
+ | IMP.addNode(null, | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | To be callable from the GUI, GroIMP commands needs to be //public//, //static//, and as their return value is not used //void//. | ||
+ | The parameters //Item, Object, Context// are the default and most used ones. | ||
+ | |||
+ | === Create a menu item === | ||
+ | |||
+ | Now that the command exists in java, it needs to be referenced in the registry to be accessible from GroIMP GUI. | ||
+ | |||
+ | Open the '' | ||
+ | <code xml> | ||
+ | < | ||
+ | | ||
+ | < | ||
+ | | ||
+ | </ | ||
+ | |||
+ | <ref name=" | ||
+ | <ref name=" | ||
+ | <ref name=" | ||
+ | < | ||
+ | < | ||
+ | </ | ||
+ | </ | ||
+ | </ | ||
+ | </ | ||
+ | </ | ||
+ | |||
+ | The tag '' | ||
+ | |||
+ | Then, the hooks > complete > project defines what happens when a GroIMP project is open. In this case at ''/ | ||
+ | |||
+ | === Use from GroIMP === | ||
+ | |||
+ | Now that the command has been implemented and added to the menu bar, compile the plugin and replace the old one. | ||
+ | Open GroIMP and a new project. | ||
+ | |||
+ | {{: | ||
+ | |||
+ | === Add an option === | ||
+ | |||
+ | The previous command is creating a chain of five cylinders. Instead of using a fixed number we can add a GroIMP option to let the user define the number. | ||
+ | |||
+ | Options are defined in the registry. In the '' | ||
+ | <code xml> | ||
+ | < | ||
+ | | ||
+ | < | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | </ | ||
+ | </ | ||
+ | |||
+ | The directory is now using the parameter // | ||
+ | |||
+ | {{: | ||
+ | |||
+ | Now we change the java code that was creating the Cylinders to use the value of this option. In the file '' | ||
+ | |||
+ | <code java> | ||
+ | |||
+ | import de.grogra.util.Utils; | ||
+ | |||
+ | public class Commands { | ||
+ | |||
+ | public static void randomCylinders(Item item, Object info, Context ctx) { | ||
+ | Node n = new Node(); | ||
+ | Node last = n; | ||
+ | |||
+ | Item ite = Item.resolveItem(item.getRegistry(), | ||
+ | int nbOfCylinders = (int) Utils.get(ite, | ||
+ | |||
+ | for(int i=0; | ||
+ | float length = (float) (0.5f + Math.random() * (2 - 0.5)); | ||
+ | float radius = (float) (0.1f + Math.random() * (0.4 - 0.1)); | ||
+ | Cylinder c = new Cylinder(length, | ||
+ | last.addEdgeBitsTo(c, | ||
+ | last = c; | ||
+ | } | ||
+ | IMP.addNode(null, | ||
+ | } | ||
+ | } | ||
+ | </ |
tutorials/create-groimp-plugin.1733463442.txt.gz · Last modified: 2024/12/06 06:37 by gaetan