How to subclass a drawing element
-
Hi all,
I am starting with SketchUp Development, so please be patient with me
.
First of all: Is there any good literature that you recommend?
Second: My overall plan is to use SketchUp as visualization and configuration tool for an external software I am currently developing. I tried around with ruby and managed to have the essential communication working.
The next step is, that I receiver centerpoint and radius of a sphere, that I want to visualize. I managed to do so by creating a group where I have two circles, where one "follows" the other. However, I would like to have this sphere (and later other objects) as an own Entity, such that I can to specified stuff with it later.
I am quite familiar with C++, where I would create a subclass "Sketchup::Sphere" from "Sketchup::Drawingelement". The constructor would contain center and radius as input parameter. Furthermore, I would add a function "add_sphere" to "Sketchup::Entities".
So I guess my file should look something like
` require "sketchup.rb"class Sketchup::Sphere< Sketchup::Drawingelement
def Sphere(i_Pos) #constructor?
end
end
class Sketchup::Entities
def add_sphere(i_Pos)
Sphere = Sketchup::Sphere(i_Pos)# like this?
# how to add Sphere to entities?
end
end
....
Sketchup.active_model.entities.add_source [1, 2, 3]`Besides that, I have no idea where to start.
Can anyone help me?
-
You cannot inherit or sub-class the DrawingElement type of classes we provide. They are defined in C in such a way that it's not possible.
Further more, we don't condone this - nor do we allow extensions that modify the Ruby or our Ruby API classes to be submitted to the Extension Warehouse.
The reason for this is that if people modified existing methods they would break the behaviour we promise with our API and it would be impossible to develop for the Ruby API as all extensions share the same environment.
We also don't allow people to add methods to our classes or modules because:
- It'd be the same as adding methods to the global namespace. What if someone else implement a Entities.add_sphere? Then there'd be a conflict.
- We reserve the namespace for us. Maybe we would like to implement an add_sphere method - in which case we wouldn't want it overridden by third party extensions.
So you should instead be writing it with an alternative syntax. Make a method that accepts the Entities object where the sphere will be added. It's not as pretty, but for the sake of integrity and the sanity of the developers that use the API it's the way to go.
-
@curator said:
However, I would like to have this sphere (and later other objects) as an own Entity, such that I can to specified stuff with it later.
You cannot create custom types of entities either. It's something that would be awesome, but it cannot be done as things are now. The alternative would be to create it as a Group, then perhaps add some attributes that add some meta data if you want to access some of the parameters you used to initially create it.
-
Hi, thanks for your replies. All your arguments are resonable. Actually, I am currently working with a group
` def self.drawSphere(i_Point, i_Radius, i_Entities)
p_Circle1 = i_Entities.add_circle i_Point, [0,1.m,0], i_Radius, 16
p_Circle2 = i_Entities.add_circle i_Point+[0.m,0.m,-2.0*i_Radius], [0.m,0.m,1.m], 2.0 * i_Radius, 16
p_Face = i_Entities.add_face p_Circle1p_Face.followme p_Circle2 i_Entities.erase_entities p_Circle2 end`
but that SEEMED to me to be not the best choice. But as I said, I am very new to this.
So lets say a sphere is a group like that. If I "rightklick" on such a sphere (group) it shows a menu starting with "Entity info". How can I add, sets say, an option like "change radius" or "change position". How do I add such an option only to "sphere", and not to "cube" or whatever.
Thanks again,
curator -
Look for the 'parametric.rb' plugin and the example 3d 'Shapes' script in the Extension-Warehouse...
https://groups.google.com/forum/#!topic/sketchupruby/wE2APdEC82k
http://extensions.sketchup.com/en/content/shapes
These do much of what you want [create and then context-menu Edit later etc].
The example Shapes cover most basic 3d forms as parametric groups...
A dome is the most complex... but a sphere is readily added... -
You cannot modify Entity Info itself. But you can create your own WebDialog for your custom UI.
Note that there is a few issues with WebDialog that might bite you, check out the Wiki at this GitHub repo: https://github.com/thomthom/sketchup-webdialogs-the-lost-manual
If you just want to do a quick start with some simple UI elements you might want to use SKUI: https://github.com/thomthom/SKUI
Note that it's not 100% complete yet - but for simple textbox editing it's ready. -
@curator said:
How can I add, sets say, an option like "change radius" or "change position". How do I add such an option only to "sphere", and not to "cube" or whatever.
IF you use a group:
UI.add_context_menu_handler {|menu| sel = Sketchup.active_model.selection if (not sel.empty?) && sel.single_object? && sel.first.is_a?(Sketchup;;Group) && sel.first.entities.parent.name == "Curator_Sphere" menu.add_item("Change Radius") { # do code here, or call a method like; Curator;;SphereWorx;;set_radius(sel.first) } end }
IF you use a component instead:
UI.add_context_menu_handler {|menu| sel = Sketchup.active_model.selection if (not sel.empty?) && sel.single_object? && sel.first.is_a?(Sketchup;;ComponentInstance) && sel.first.definition.name == "Curator_Sphere" menu.add_item("Change Radius") { # do code here, or call a method like; Curator;;SphereWorx;;set_radius(sel.first) } end }
Advertisement