Help Understanding Classes and Class objects
-
@pmagans said:
I'm not sure if my last post went through.
The first few posts of new members must be approved by a moderator before it appear on the forum - this is an anti-spam measure.
-
You still have this
st_ep = EndPulley.new(@group, @st_point)
which won't return the new object [group] unless you add
return @group
as the final code in the EndPulley method... -
So I added the return @group per your recommendation TIG however I am having some confusion as to how Sketchup sees the newly created groups. It doesn't seem like you can manipulate the returned groups as per the example code below returns undefined method transform!
It seems as though Sketchup is treating the groups st_ep and end_ep as objects not groups. I have been creating "dummy-container groups to bypass this like Conveyor but I'm guessing from your comment there is an easier way to access the groups that I am creating in the EndPulley class?module CONVEYOR #CONVEYOR.rb # Type in Console # load 'Conveyor/CONVEYOR2.rb' # ex. conv = CONVEYOR;;Conveyor.new([0,0,0], [0,120,0]) require 'sketchup.rb' class Conveyor #make the @group var and .group()instance attr_reader(;group) #private #why does this need to be private? def initialize(st_point, end_point) ents = Sketchup.active_model.entities #t1 = Geom;;Transformation.translation [0,0,-5] @st_point = st_point @end_point = end_point #hold reference to group; @group = ents.add_group() cpt = @group.entities.add_cpoint(ORIGIN) @group.name = "CONVEYOR ASSY" #@group.transform! t1 self.create_geometry cpt.erase! end #of initialize() def create_geometry() ents = Sketchup.active_model.entities t1 = Geom;;Transformation.translation [0,0,-5] #pulley 1 st_ep = EndPulley.new(@group, @st_point) #puts st_ep st_ep.transform! t1 #pulley 2 end_ep = EndPulley.new(@group, @end_point) end_ep.transform! t1 end #of create_geometry() end #of Conveyor class class EndPulley attr_reader(;group) def initialize(conv_group, ins_pt) #TODO; add *args if a group not passed to EndPulley just "create_geometry" @conv_group = conv_group @ins_pt = ins_pt self.create_geometry end #of initialize() def create_geometry() pts = @ins_pt width = 36 length = 18 ents = Sketchup.active_model.entities pt1 =[pts[0] - (width/2),pts[1],pts[2]] pt2 =[pts[0] - (width/2),pts[1]+length,pts[2]] pt3 =[pts[0] + (width/2),pts[1]+length,pts[2]] pt4 =[pts[0] + (width/2),pts[1],pts[2]] @group = @conv_group.entities.add_group() face = @group.entities.add_face pt1,pt2,pt3,pt4 face.reverse! face.pushpull 5 @group.name = "END PULLEY" return @group end #of create_geometry() end #of EndPulley class end #of module CONVEYOR
-
end_ep = EndPulley.new(@group, @end_point)
NOW ADD
puts 'end_tp details...' puts end_tp puts end_tp.class puts end_tp.name puts 'END'
THEN
end_ep.transform!(t1)
FAIL ? IF end_tp is NOT a group or component_instance ???
Report back what the 'puts' show in the Ruby Console...
-
This is the output - Doesn't get to the puts statements
load 'Conveyor/CONVEYOR2.rb' true conv = CONVEYOR;;Conveyor.new([0,0,0], [0,120,0]) end_tp details... Error; #<NameError; undefined local variable or method `end_tp' for #<CONVEYOR;;Conveyor;0xb8a563c>> C;/PROGRA~2/Google/GOOGLE~1/Plugins/Conveyor/CONVEYOR2.rb;43 C;/PROGRA~2/Google/GOOGLE~1/Plugins/Conveyor/CONVEYOR2.rb;27;in `initialize' (eval);435;in `new' (eval);435
-
Try this change: in the EndPulley code after
self.create_geometry
###ADD
return @group
as the last code before the '
end
' of theinitialize
...REMOVE the equivalent line from the
create_geometry
method...Run with the '
puts
' to see what's what now... -
Same message No change - its dying on the puts statements?
-
WAIT !!
Earlier on I attempted to explain that his custom classes were WRAPPER CLASSES. (And questioned why anyone would want to actually do this. Subsequent edits by a user such as boolean operations can cause the references held by the wrapper class instances to become invalid and point at
Sketchup::DeletedEntity
objects.)I showed an example which did NOT have a
EndPully
class, because it would ALSO be a wrapper class, whosenew()
constructor returns the wrapper instance NOT aSketchup::Group
instance.SO.. therefore... he needs to call the
EndPully
instance'sgroup()
wrapper method (created via theattr_reader(:group)
call,) which will return the actual group reference:
st_ep.group.transform! t1
-
Can you also add the equivalent 'puts' just after the
st_ep = EndPulley.new(@group, @st_point)
puts 'st_tp details...' puts st_tp puts st_tp.class puts st_tp.name puts 'END'
so we can see what the first part does...
-
Sorry ... I got busy and did not get back to this thread,
I was going to continue the discussion on why wrapper classes are extremely problematic for
Sketchup::Drawingelement
subclasses. (One issue is on Mac multiple models could be open, but the API does not yet have a good way to track them, and we know of some bugs on the PC, that causes issues.)
Anyway... the alternative you REALLY should be using is to create a
Sketchup::ComponentDefinition
for your pully and conveyor, and then place instances of these (rather than Groups.)The definition's instances collection will keep track of them for you.
The model's
DefintionList
collection will keep track of your definition(s) for each model that they are inserted into.When a model is opened, your plugin can search through it's
DefintionList
to find those definitions that are "known" to the plugin. And then grab an array of their instances, and iterate it to get references. -
Dan you are correct. Adding the .group gave me access to the "group" objects that were created.
As for my thought process I am not sure that it is valid so let me describe my thoughts.
I don't want to code "like" things so I assumed that using classes would allow me to inherit class attributes things without having to copy a bunch of code it multiple places. For example classes would be:
- FUNCTIONAL AREAS
[list:11tc2ay1]1. CONVEYOR
-
CONVEYOR_COMPONENTS
-
END_PULLEY
-
DRIVE
-
INTERMEDIATE_BED
-
PAN_GUARD
-
BEARINGS
-
ETC.
-
BUILDING_MODSETC.[/*11tc2ay1][/list11tc2ay1]Later I could write Customer specific code for each of the classes and inherit all of the functionality that I wrote in the "General" Classes
One customer's end_pulley may contain different bearings, pulleys, profiles etc. Also there are many types of end pulleys. That being said they do have some similarities especially attribute definitions, layer structure, basic functionality.
Am I on the right track or am I way off the rails?
- FUNCTIONAL AREAS
-
Simply put:
A class constructor returns an instance of IT's class.
CONVEYOR::EndPully.new()
returns an instance ofCONVEYOR::EndPully
not an instanceSketchup::Group
This is one of the MAJOR drawbacks of a wrapper class, you have to write wrapper methods for all the methods of the wrapped object that you wish to access "as if" the wrapper "were" the object itself.
There are some ruby tricks to get a list of only the API methods, and define wrapper methods via an eval loop. But .. it's still can get complicated in other nitty-gritty ways.
I just do not recommend it.
-
Dynamic Component definitions can give you a lot of flexibility. A style of pully, were the diameter and width are settable via the Component Options dialog.
Your code classes and modules... are geometry creators, but not the actual model objects themselves.
You can also inherit (share) code functionality via mixin module libraries.
Given a mixin module named
BasicPully
...module FancyPully include BasicPully # now overrride inherited methods if desired # now add additional methods if desired end
.. and later ...
module NiftyPully include FancyPully # now overrride inherited methods if desired # now add additional methods if desired end
-
doh!
-
I will check into the mixin module approach also the dynamic components. Are dynamic components "create-able" through Ruby code or only through the Sketchup interface?
The only info/tutorial I could find was:
http://sketchucation.com/forums/viewtopic.php?f=180&t=17888&p=145120&hilit=dynamic+component+tutorial#p145120 -
Both.
But of course the coding means is not documented because the Free license users are not supposed to do it. (... or at least use a wizard interface.)
A lot has been written about them.
-
@dan rathbun said:
But of course the coding means is not documented because the Free license users are not supposed to do it. (... or at least use a wizard interface.)
That's a bummer. I like the concept of dynamic tools for locking up variables and documenting the components, but I hate the user interface! It would just take me too long through traditional means to create a workable library. And of course if I wanted to change something I would have to do it in multiple places. I will do a more active search for creating/modifying dynamic components through Ruby. So far what I have found has not been that helpful besides holding/setting attributes. Any tips on where to start?
Thanks! -
In SU Pro.. open the "Component Attributes" wizard.
Switch to the "Functions" pane.
Click on the "more >>" link, ... a bowser window will open to the DC online reference.
Bookmark that page.
Notice also the links in the left navigation column.
Happy Reading.
Basically scripting them is first understanding how to create and access Attribute Dictionary objects.
-
@dan rathbun said:
Bookmark that page.
Done, thanks for the link.
I remember doing the tutorials when DC's first came out but have forgotton the majority of what I learned so I guess I will be spending some time there first.
Advertisement