• Login
sketchucation logo sketchucation
  • Login
⚠️ Libfredo 15.4b | Minor release with bugfixes and improvements Update

Help Understanding Classes and Class objects

Scheduled Pinned Locked Moved Developers' Forum
30 Posts 4 Posters 276 Views 4 Watching
Loading More Posts
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    pmagans
    last edited by pmagans 3 Oct 2012, 20:46

    Hello all,

    I would like to make a class that is a collection of other class objects. I am having a tough time determining exactly how to do this.
    st_ep and end_ep end up as class objects but I can't add them to a group in the the class Conveyor draw method. I can't access the objects at all it seems for grouping, transformations etc. Any help where my thought process is wrong would be greatly appreciated. I have been searching all over for an answer.

    <span class="syntaxdefault"></span><span class="syntaxkeyword">class&nbsp;</span><span class="syntaxdefault">Conveyor<br />&nbsp;&nbsp;&nbsp;&nbsp;def&nbsp;initialize</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">st_point</span><span class="syntaxkeyword">,&nbsp;</span><span class="syntaxdefault">end_point</span><span class="syntaxkeyword">)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@</span><span class="syntaxdefault">st_point&nbsp;</span><span class="syntaxkeyword">=&nbsp;</span><span class="syntaxdefault">st_point<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="syntaxkeyword">@</span><span class="syntaxdefault">end_point&nbsp;</span><span class="syntaxkeyword">=&nbsp;</span><span class="syntaxdefault">end_point<br />&nbsp;&nbsp;&nbsp;&nbsp;end&nbsp;</span><span class="syntaxcomment">#of&nbsp;initialize()<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="syntaxdefault">def&nbsp;draw</span><span class="syntaxkeyword">()<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="syntaxdefault">ents&nbsp;</span><span class="syntaxkeyword">=&nbsp;</span><span class="syntaxdefault">Sketchup</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">active_model</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">entities<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;st_ep&nbsp;</span><span class="syntaxkeyword">=&nbsp;</span><span class="syntaxdefault">EndPulley</span><span class="syntaxkeyword">.new(@</span><span class="syntaxdefault">st_point</span><span class="syntaxkeyword">)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="syntaxdefault">st_ep</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">draw<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end_ep&nbsp;</span><span class="syntaxkeyword">=&nbsp;</span><span class="syntaxdefault">EndPulley</span><span class="syntaxkeyword">.new(@</span><span class="syntaxdefault">end_point</span><span class="syntaxkeyword">)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="syntaxdefault">end_ep</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">draw<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;conv_group&nbsp;</span><span class="syntaxkeyword">=&nbsp;</span><span class="syntaxdefault">ents</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">add_group&nbsp;st_ep</span><span class="syntaxkeyword">,&nbsp;</span><span class="syntaxdefault">en_ep<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;conv_group</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">name&nbsp;</span><span class="syntaxkeyword">=&nbsp;</span><span class="syntaxstring">"CONVEYOR&nbsp;ASSY"<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="syntaxdefault">end&nbsp;</span><span class="syntaxcomment">#of&nbsp;draw()<br /><br /></span><span class="syntaxdefault">end&nbsp;</span><span class="syntaxcomment">#of&nbsp;Conveyor&nbsp;class<br /><br /></span><span class="syntaxkeyword">class&nbsp;</span><span class="syntaxdefault">EndPulley<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;def&nbsp;initialize</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">ins_pt</span><span class="syntaxkeyword">)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@</span><span class="syntaxdefault">ins_pt&nbsp;</span><span class="syntaxkeyword">=&nbsp;</span><span class="syntaxdefault">ins_pt<br />&nbsp;&nbsp;&nbsp;&nbsp;end&nbsp;</span><span class="syntaxcomment">#of&nbsp;initialize()<br />&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="syntaxdefault">def&nbsp;draw</span><span class="syntaxkeyword">()<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="syntaxdefault">pts&nbsp;</span><span class="syntaxkeyword">=&nbsp;@</span><span class="syntaxdefault">ins_pt<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;width&nbsp;</span><span class="syntaxkeyword">=&nbsp;</span><span class="syntaxdefault">36<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;length&nbsp;</span><span class="syntaxkeyword">=&nbsp;</span><span class="syntaxdefault">18<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ents&nbsp;</span><span class="syntaxkeyword">=&nbsp;</span><span class="syntaxdefault">Sketchup</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">active_model</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">entities<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pt1&nbsp;</span><span class="syntaxkeyword">=[</span><span class="syntaxdefault">pts</span><span class="syntaxkeyword">[</span><span class="syntaxdefault">0</span><span class="syntaxkeyword">]&nbsp;-&nbsp;(</span><span class="syntaxdefault">width</span><span class="syntaxkeyword">/</span><span class="syntaxdefault">2</span><span class="syntaxkeyword">),</span><span class="syntaxdefault">pts</span><span class="syntaxkeyword">[</span><span class="syntaxdefault">1</span><span class="syntaxkeyword">],</span><span class="syntaxdefault">pts</span><span class="syntaxkeyword">[</span><span class="syntaxdefault">2</span><span class="syntaxkeyword">]]<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="syntaxdefault">pt2&nbsp;</span><span class="syntaxkeyword">=[</span><span class="syntaxdefault">pts</span><span class="syntaxkeyword">[</span><span class="syntaxdefault">0</span><span class="syntaxkeyword">]&nbsp;-&nbsp;(</span><span class="syntaxdefault">width</span><span class="syntaxkeyword">/</span><span class="syntaxdefault">2</span><span class="syntaxkeyword">),</span><span class="syntaxdefault">pts</span><span class="syntaxkeyword">[</span><span class="syntaxdefault">1</span><span class="syntaxkeyword">]+</span><span class="syntaxdefault">length</span><span class="syntaxkeyword">,</span><span class="syntaxdefault">pts</span><span class="syntaxkeyword">[</span><span class="syntaxdefault">2</span><span class="syntaxkeyword">]]<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="syntaxdefault">pt3&nbsp;</span><span class="syntaxkeyword">=[</span><span class="syntaxdefault">pts</span><span class="syntaxkeyword">[</span><span class="syntaxdefault">0</span><span class="syntaxkeyword">]&nbsp;+&nbsp;(</span><span class="syntaxdefault">width</span><span class="syntaxkeyword">/</span><span class="syntaxdefault">2</span><span class="syntaxkeyword">),</span><span class="syntaxdefault">pts</span><span class="syntaxkeyword">[</span><span class="syntaxdefault">1</span><span class="syntaxkeyword">]+</span><span class="syntaxdefault">length</span><span class="syntaxkeyword">,</span><span class="syntaxdefault">pts</span><span class="syntaxkeyword">[</span><span class="syntaxdefault">2</span><span class="syntaxkeyword">]]<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="syntaxdefault">pt4&nbsp;</span><span class="syntaxkeyword">=[</span><span class="syntaxdefault">pts</span><span class="syntaxkeyword">[</span><span class="syntaxdefault">0</span><span class="syntaxkeyword">]&nbsp;+&nbsp;(</span><span class="syntaxdefault">width</span><span class="syntaxkeyword">/</span><span class="syntaxdefault">2</span><span class="syntaxkeyword">),</span><span class="syntaxdefault">pts</span><span class="syntaxkeyword">[</span><span class="syntaxdefault">1</span><span class="syntaxkeyword">],</span><span class="syntaxdefault">pts</span><span class="syntaxkeyword">[</span><span class="syntaxdefault">2</span><span class="syntaxkeyword">]]<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="syntaxdefault">face&nbsp;</span><span class="syntaxkeyword">=&nbsp;</span><span class="syntaxdefault">ents</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">add_face&nbsp;pt1</span><span class="syntaxkeyword">,</span><span class="syntaxdefault">pt2</span><span class="syntaxkeyword">,</span><span class="syntaxdefault">pt3</span><span class="syntaxkeyword">,</span><span class="syntaxdefault">pt4<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;group&nbsp;</span><span class="syntaxkeyword">=&nbsp;</span><span class="syntaxdefault">ents</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">add_group&nbsp;face<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;group</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">name&nbsp;</span><span class="syntaxkeyword">=&nbsp;</span><span class="syntaxstring">"END&nbsp;PULLEY"<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="syntaxdefault">face</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">reverse</span><span class="syntaxkeyword">!<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="syntaxdefault">face</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">pushpull&nbsp;5<br />&nbsp;&nbsp;&nbsp;&nbsp;end&nbsp;</span><span class="syntaxcomment">#of&nbsp;draw()<br /></span><span class="syntaxdefault">end&nbsp;</span><span class="syntaxcomment">#of&nbsp;EndPulley&nbsp;class&nbsp;</span><span class="syntaxdefault"></span>
    
    1 Reply Last reply Reply Quote 0
    • D Offline
      Dan Rathbun
      last edited by 4 Oct 2012, 06:56

      (please wrap code in phpBB code tags, with space indenting.)

      Sorry ...

      (1) We cannot create custom Sketchup::Drawingelement subclasses. (The app would not know how to render them, .. nor how to export them to other file formats.)

      (2) The draw method is used only for a Tool class to draw temporary geometry, guidelines etc. as the user moves the mouse, before they commit the 2nd point. (Notice how the rectangle tool works. The temporary stretchy rectangle after the first point would be drawn with the draw method.)

      (3) What you want to do, is create a Group or Component, that is named "Pulley" and "Conveyor". Perhaps even DynamicComponents.

      But you dont really need to create Ruby class objects to do this.

      I'm not here much anymore.

      1 Reply Last reply Reply Quote 0
      • T Offline
        TIG Moderator
        last edited by 4 Oct 2012, 08:48

        Let's gloss over the misuse of the 'draw' method, and assume you rename it 'draw_geometry' or some such...
        There is still nothing in your code to run that method.

        Why not add something at the end of both class's initialize code
        like self.draw_geometry()
        So when you call EndPulley.new(@st_point) etc the initialize runs and then it gets to do the next step of making geometry without recourse to any additional steps or code... 😕

        TIG

        1 Reply Last reply Reply Quote 0
        • T Offline
          thomthom
          last edited by 4 Oct 2012, 09:13

          conv_group = ents.add_group st_ep, en_ep

          st_ep and en_ep are instance of EndPulley - you can't feed that to Entities.add_group.

          You seem to be confusing Ruby classes with SketchUp entities.

          Thomas Thomassen — SketchUp Monkey & Coding addict
          List of my plugins and link to the CookieWare fund

          1 Reply Last reply Reply Quote 0
          • D Offline
            Dan Rathbun
            last edited by 4 Oct 2012, 09:39

            @dan rathbun said:

            (3) What you want to do, is create a Group or Component, that is named "Pulley" and "Conveyor". Perhaps even DynamicComponents.

            OK, I see that you ARE actually doing (3), which IS the correct thing to do. (I see that your draw method has nothing to do with the View.draw() method(s), it is just a naming similarity which is best avoided. Methods that create geometry, by convention, we usually prefix with "create_", as in create_end_pully(), etc.)

            (A) You need to understand that the actual objects you create will be Sketchup::Group class instance objects. So they already HAVE a class identity.

            You cannot really (or should not actually,) expect that you can assign a class identity to a Sketchup::Drawingelement subclass instance object, (or any Sketchup::Entity subclass instance object,) ... and expect the reference to it to be persistent. Sometimes SketchUp reindexes things on the C++ after a save, or purge of layers, etc.
            If you close the model, and reopen it, your custom class instances would not be valid.

            (B) What you attempted to do, is a certain kind of a class implementation, called a "wrapper class." They do have their place, and come in handy sometimes. In this scenario, the custom wrapper class creates an instance of another class, and holds an internal reference the the "wrapped" object, assigned to an instance variable.

            Your Conveyor class written as a wrapper class, (module wrapped as Magans::Conveyor.)

            [Semantic Example ONLY. Still needs changes to work properly!]

            module Magans
            
              class Conveyor
            
                # make @group var and .group() instance method;
                attr_reader(;group)
            
                private
            
                def initialize(st_point, end_point)
            
                    @st_point = st_point
                    @end_point = end_point
            
                    ents = Sketchup.active_model.active_entities
            
                    pully1 = create_end_pully(ents,@st_point,36,18)
            
                    pully2 = create_end_pully(ents,@end_point,36,18)
                    
                    # hold reference to group;
                    @group = ents.add_group( pully1, pully2 )
                    @group.name = "CONVEYOR ASSY"
                    
                end #of initialize()
            
                def create_end_pully(ents,pts,width,length)
                    
                    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]]
                    
                    face = ents.add_face( pt1,pt2,pt3,pt4 )
                    group = ents.add_group( face )
                    group.name = "END PULLEY"
                    face.reverse!
                    face.pushpull( 5 )
            
                    # return the pully's group reference;
                    return group
            
                end #of create_end_pully()
            
                public
            
                # Example "wrapper" methods
            
                def name()
                   return @group.name
                end #of name()
            
                def valid?()
                   return @group.valid?
                end #of name()
            
              end #of Conveyor class
            
            end # module Magans
            
            

            (C) try to use Sketchup.active_model.active_entities so you can add things into the current editing context (which can be the model, or some nesting level within a group or componet instance.)

            (D) Choose a toplevel namespace module name. (The Magans name above is an example.) All your code should run within that namespace. You'll likely be wanted to wrap each of your plugin projects within a submodule of you namespace.
            You decide on a unique toplevel module (your company name, or your SCF member "screen name".)

            I'm not here much anymore.

            1 Reply Last reply Reply Quote 0
            • D Offline
              Dan Rathbun
              last edited by 4 Oct 2012, 09:52

              There is still a basic grouping protocol steps that are out of sequence in your original example, and in mine above.

              Can you figure out what the proper sequence is ???

              (If not... say so, we'll show you what the proper sequence is.)

              ❓

              I'm not here much anymore.

              1 Reply Last reply Reply Quote 0
              • T Offline
                thomthom
                last edited by 4 Oct 2012, 10:45

                Also - when creating Group with the API you create the group first - then add the entities directly into the group. As oppose to when you model in SketchUp where you draw the geometry - then group.

                Thomas Thomassen — SketchUp Monkey & Coding addict
                List of my plugins and link to the CookieWare fund

                1 Reply Last reply Reply Quote 0
                • T Offline
                  TIG Moderator
                  last edited by 4 Oct 2012, 11:02

                  You CAN make a group from existing entities gp=entities.add_group(array_of_entities)... BUT if those entities are not all in the same context OR if their context is NOT the active_entities then you will get a Bugsplat.
                  If you are making new entities then it is best to make them inside the desired context in the first place - using a new [initially] empty group gp=entities.add_group();gp.entities.add_face(points) to get the face etc.
                  Doing this also removes the risk of your new geometry clashing with some existing geometry in the active_context - e.g. overlapping faces, which will intersect unexpectedly if not separated from the very creation of your new face.
                  The only time I would suggest you want even to think about making a group from existing objects... is when you are using a user's [premade] selection - because they can only select objects in the 'active_entities' context then you can safely group those objects [or a filtered sub-set of them - e.g. just faces and edges] into a new group that is made in the same context, thus: gp=model.active_entities.add_group(model.selection.to_a) will make a group of the current selection.

                  TIG

                  1 Reply Last reply Reply Quote 0
                  • D Offline
                    Dan Rathbun
                    last edited by 4 Oct 2012, 12:09

                    @dan rathbun said:

                    There is still a basic grouping protocol steps that are out of sequence in your original example, and in mine above.

                    Can you figure out what the proper sequence is ???

                    @thomthom said:

                    ... when creating Group with the API you create the group first - then add the entities directly into the group. As oppose to when you model in SketchUp where you draw the geometry - then group.

                    Thomas gave away the answer.

                    So ... lets see the my example, using this protocol.

                    We will first create the conveyor group, add a temporary construction point to it, so SU does not clean it up, then past that parent group's entities reference into the other method, and have the subordinate method add it's subgroup to the parent group. Finally we will add pully primitives to the subgroup and return it's reference.

                    module Magans
                     
                      class Conveyor
                     
                        # make @group var and .group() instance method;
                        attr_reader(;group)
                     
                        private
                     
                        def initialize(st_point, end_point)
                     
                            @st_point = st_point
                            @end_point = end_point
                     
                            ents = Sketchup.active_model.active_entities
                     
                            # hold reference to group;
                            @group = ents.add_group()
                            cpt = @group.entities.add_cpoint(ORIGIN)
                            @group.name = "CONVEYOR ASSY"
                           
                            # pully1
                            create_end_pully(@group.entities,@st_point,36,18)
                     
                            # pully2
                            create_end_pully(@group.entities,@end_point,36,18)
                            
                            cpt.erase!
                           
                        end #of initialize()
                     
                        def create_end_pully(ents,pts,width,length)
                           
                            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]]
                           
                            subgroup = ents.add_group()
                            face = subgroup.entities.add_face( pt1,pt2,pt3,pt4 )
                            subgroup.name = "END PULLEY"
                    
                            face.reverse!
                            face.pushpull( 5 )
                     
                            # return the pully's group reference;
                            return subgroup
                     
                        end #of create_end_pully()
                     
                        public
                     
                        # Example "wrapper" methods
                     
                        def name()
                           return @group.name
                        end #of name()
                     
                        def valid?()
                           return @group.valid?
                        end #of name()
                     
                      end #of Conveyor class
                     
                    end # module Magans
                    

                    I'm not here much anymore.

                    1 Reply Last reply Reply Quote 0
                    • P Offline
                      pmagans
                      last edited by 4 Oct 2012, 16:01

                      First of all I would like to thank you guys for all of the help. My thought process was skewed in the wrong direction and I don't think I would've come up with the passing the group object. Also starting with the group and then adding the entities is now clear in my head. I had read that before in other posts but could never quite understand the "how". That was where I was hopelessly stuck for the past day. I have re-written the previous code and module-ized it and it works perfectly.

                      I kept the classes for Conveyor and EndPulley seperate so that I can use both seperately for inheritance in the future.

                      I still need to check into "Wrapper" classes further for how I can implement them effectively.

                      The only snag was I had to remove the private designation for the methods under the Conveyor class to work. I'm not really certain why.

                      module Pmagans
                      	# Type in Console
                      	# load 'Conveyor/CONVEYOR2.rb'
                      	# conv = Pmagans;;Conveyor.new([0,0,0], [0,120,0])
                      	
                      	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
                      			
                      			@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"
                      						
                      			self.create_geometry
                      			cpt.erase!
                      			
                      		end #of initialize()
                      	
                      		def create_geometry()
                      			ents = Sketchup.active_model.entities
                      			puts ents.length
                      			
                      			#pulley 1
                      			st_ep = EndPulley.new(@group, @st_point)
                      			
                      			#pulley 2
                      			end_ep = EndPulley.new(@group, @end_point)
                      			
                      			puts ents.length
                      			
                      		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"
                      					
                      		end #of create_geometry()
                      		
                      	end #of EndPulley class 
                      	
                      end #of module Pmagans
                      
                      1 Reply Last reply Reply Quote 0
                      • P Offline
                        pmagans
                        last edited by 4 Oct 2012, 19:27

                        I'm not sure if my last post went through. I wanted to thank everyone though. Also I got everything working and kept the two classes seperate for future inheritence. I had to remove the "private" so that the methods for the Conveyor class could be accessed. I'm not really sure why?

                        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
                        			puts ents.length
                        			
                        			#pulley 1
                        			st_ep = EndPulley.new(@group, @st_point)
                        			
                        			#pulley 2
                        			end_ep = EndPulley.new(@group, @end_point)
                        			
                        			puts ents.length
                        			
                        		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"
                        					
                        		end #of create_geometry()
                        		
                        	end #of EndPulley class 
                        	
                        end #of module CONVEYOR
                        
                        1 Reply Last reply Reply Quote 0
                        • T Offline
                          thomthom
                          last edited by 4 Oct 2012, 20:05

                          @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.

                          Thomas Thomassen — SketchUp Monkey & Coding addict
                          List of my plugins and link to the CookieWare fund

                          1 Reply Last reply Reply Quote 0
                          • T Offline
                            TIG Moderator
                            last edited by 5 Oct 2012, 10:07

                            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...

                            TIG

                            1 Reply Last reply Reply Quote 0
                            • P Offline
                              pmagans
                              last edited by 5 Oct 2012, 11:42

                              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
                              
                              1 Reply Last reply Reply Quote 0
                              • T Offline
                                TIG Moderator
                                last edited by 5 Oct 2012, 18:30

                                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...

                                TIG

                                1 Reply Last reply Reply Quote 0
                                • P Offline
                                  pmagans
                                  last edited by 5 Oct 2012, 18:47

                                  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
                                  
                                  1 Reply Last reply Reply Quote 0
                                  • T Offline
                                    TIG Moderator
                                    last edited by 5 Oct 2012, 18:50

                                    Try this change: in the EndPulley code after
                                    self.create_geometry
                                    ###ADD
                                    return @group

                                    as the last code before the ' end' of the initialize...

                                    REMOVE the equivalent line from the create_geometry method...

                                    Run with the ' puts' to see what's what now...

                                    TIG

                                    1 Reply Last reply Reply Quote 0
                                    • P Offline
                                      pmagans
                                      last edited by 5 Oct 2012, 18:58

                                      Same message No change - its dying on the puts statements?

                                      1 Reply Last reply Reply Quote 0
                                      • D Offline
                                        Dan Rathbun
                                        last edited by 5 Oct 2012, 19:16

                                        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, whose new() constructor returns the wrapper instance NOT a Sketchup::Group instance.

                                        SO.. therefore... he needs to call the EndPully instance's group() wrapper method (created via the attr_reader(:group) call,) which will return the actual group reference:
                                        st_ep.group.transform! t1

                                        I'm not here much anymore.

                                        1 Reply Last reply Reply Quote 0
                                        • T Offline
                                          TIG Moderator
                                          last edited by 5 Oct 2012, 19:18

                                          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...

                                          TIG

                                          1 Reply Last reply Reply Quote 0
                                          • 1
                                          • 2
                                          • 1 / 2
                                          1 / 2
                                          • First post
                                            1/30
                                            Last post
                                          Buy SketchPlus
                                          Buy SUbD
                                          Buy WrapR
                                          Buy eBook
                                          Buy Modelur
                                          Buy Vertex Tools
                                          Buy SketchCuisine
                                          Buy FormFonts

                                          Advertisement