Program format
-
I have a plugin that I wrote that operates on a component to place it a layer and makes a new page view for the dimensional drawing. This is an outline of the program.
Description : Place the Component/Board on a new layer named from the component
Layer name includes prefix A##- where ## is 2 digit no (01...99)
A "Assembly" page will be created if it doesn't exist
Existing Components will be placed on new hidden layer
The "Assembly" page will be up dated
A new Drawing Page named A-## will be created.
A copy of the Board will be added and moved to 200,0,200 for the Drawing Page
The copy will be rotated so long dim is on red axis
The view will be changed to zoom in on the Board @ 200,0,200
If user has MakeOrthoViews.rb installed then ortho views are created
All drawing instances of the component will be placed on the new layer
The drawing page will be updated
The question I have should this program be written as a Class with def for each step, Module with def for each step, or single def as I currently have written it? I am convinced the long single def is not the proper format.
Keith
-
ALL of your plugins need to be INSIDE your "author" module (aka toplevel namespace.)
see this sample tool post: [ Code ] Scarpino's SphereTool Sample
-
Following the Ruby object oriented paradigm, I write all my Tool plugins as classes within a top level module.
-
So namespaces aside... lets talk about methods.
Using methods, breaks up your code into smaller, more easily debugged chunks.
It makes it more readable, also.
And it makes it much more flexible (you can vary how the plugin works, based upon user selected options.)
Program types: Linear and Event-Driven
Linear is the old way. (They still can be used in Ruby, using the
load
method.)If you really want to create an interactive GUI
Tool
, you must write Event-Driven code.
This NEEDS what are termed "callback methods" that get called by the system, when an event occurs, such as the user clicking a button, or picking a point in the drawing area, or moving the mouse with a certain button held down. -
I will re read the listed docs and use that to format my program. From the outline you can see that the only user interaction after selecting the program is to provide a component name and there is the option to add the drawing scene or not. I have written it as a context right click on a component so tool icons and toolbar parts of the program are not needed. Thanks for the help.
Keith
-
Question: If you use a authors name to wrap the plugin in as additional plugins are written if I understand the principle the new plugins will be added within the authors name space. If this is the case as an extension it seems that all the authors plugins will display as a single extension so the user only has the option of loading all the authors plugins or not any. For the few things I may write this is not a problem but for the more prolific authors I would think this would not be a good thing?
Keith
-
NO. Each of your plugins would be wrapped in a nested module, within the Author module.
That way each plugin is separated from one another.
But because they are all submodules of the Author top module, they are all separated from everything else.
Also module and class definitions, do not have to be all in one single file.
You can break amodule
/class
up into several files. (For instance, to make it easier to write and maintain.)When Ruby reads:
module AuthorName
it's NOT an exclusive "begin of defintion."It means "open for editing," which can add things (methods, constants, submodules, etc.) OR modify things (like overriding existing or inherited methods, resetting variables, etc.)
The latter is very important. Modifying program constructs, throughout the runtime cycle, is called a dynamic language. It's what makes Ruby so powerful and flexible.
So a user can load only the plugins from an author, that they wish to. And if they are each within a different submodule, of the Author's toplevel module (each in it's file,) .. when Ruby reads each file, it knows to put the submodules, inside the author's top module. The files do not have to be loaded in any certain order.
Other author's files can be loaded in between loading yours. But only yours, will go inside your top modules, because you are the only one using that module name (hopefully, there is no one else programming Ruby for Sketchup, that chooses your top level namespace.)
So choose one that's bound to be unique...KtkOh
is likely better thanKeith
, for example.
But you can also use a company name, if you have one, or are writing plugins for the company you work at. Just don't use trademark or copyrighted names, that belong to some other company. -
Thanks Dan for the info and help. Its a lot to digest but I will work on getting my plugins in the desired format. I am sure I'll have questions along the way. I am sure this discussion has also helped other beginning SU programers.
Keith
I call my shop K2 (pronounced k squared) Wood Shop so the grand-kids don't ask for too many curves in their furniture designs. So I will use the K2WS as my name space as that is the prefix I already used for the joint tools.
-
Thanks Dan I will post a file when I have the first one working. I am working on the grandson's Ar-moire so coding time is limited.
Keith
-
The namespace
K2WS
sounds good to me.Using a squared symbol (
178.chr
) in the module name can be done. It works fine in the Console because, it's a Windows dialog using the system's character set, to display a UTF-8 char.Using Notepad++, you must save the source file in "UTF-8 without BOM" Encoding (aka "ANSI as UTF8", will show up in the status bar on the lower right.)
With the file open, "Encoding" (menu) > "Encode in UTF-8 without BOM"
.. and also end of line setting:
"Edit" (menu) > "EOL Conversion" (submenu) > "UNIX Format"
(Current EOL setting shows on status bar, lower right, just left of the current encoding setting.)Here's a template/test file for your toplevel module definition, but using
K²WS
.
You can put constanst and methods that you may wish to use in all your submodules and classes, in THIS file, and then just require THIS script at the top of any of your files. Then define the sub-namespace, using full qualification (saving indents.):require('K2WS/k2ws_module.rb') module K²WS;;FancyPlugin # sub module code here end
or:
require('K2WS/k2ws_module.rb') class K²WS;;LumberClass # namespace protected class code here end
-
Keith.. updated the template example.
Moved the definition of
WIN
andMAC
constants up to theObject
level.
I had an inline comment, that said they'd be inherited by all nested namespaces.
They would not have been. I've been working with mixin modules and subclasses lately, and got some braincells crossed.We've asked Google to define these when Sketchup starts but they have not, as yet.
Advertisement