• Login
sketchucation logo sketchucation
  • Login
πŸ€‘ SketchPlus 1.3 | 44 Tools for $15 until June 20th Buy Now

Guidelines for adding a method to existing classes?

Scheduled Pinned Locked Moved Developers' Forum
12 Posts 8 Posters 422 Views 8 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.
  • C Offline
    Chris Fullmer
    last edited by Chris Fullmer 13 May 2009, 03:51

    WARNING from 2014: Extending the SketchUp classes is virtually always a bad idea and will make your plugin not be allowed onto the Extension Warehouse. Please find a different way to do things, other than by extending existing SketchUp classes, modules, methods, etc.

    I've written a bezier tool. I know there are lots of them already, but I needed to understand how it works for my script, and I wanted to make sure I had all the functionality I needed from it.

    So now I'm thinking about adding a .add_bezier method to Sketchup::Entities class. Is that ok to do? I know there is an offset.rb that adds an offset method to the Face class. But I didn't know if it is generally not ok to do? Or if it is acceptable? Should I consider a less obvious name for the method to reduce any possible conflicts from in case someone else does the same? My standard would be something like .clf_add_bezier

    Any thoughts? Thanks,

    Chris

    Lately you've been tan, suspicious for the winter.
    All my Plugins I've written

    1 Reply Last reply Reply Quote 0
    • C Offline
      coldsteel
      last edited by 13 May 2009, 06:18

      thanks for your works

      1 Reply Last reply Reply Quote 0
      • R Offline
        remus
        last edited by 13 May 2009, 06:40

        From my newbie standpoint i cant see a problem with it. If it doesnt exist already it's unlikely anyones going to be calling it mistakenly.

        I suppose the only problem would be if someone else decides to add the same method to the class in a later script.

        waits for people who know what theyre talking about

        http://remusrendering.wordpress.com/

        1 Reply Last reply Reply Quote 0
        • C Offline
          Chris Fullmer
          last edited by 13 May 2009, 06:44

          well you're welcome. This however won't be a plugin in the traditional sense. It will be available for other script writers to incorporate into their scripts. But hopefully that is still helpful!

          Chris

          Lately you've been tan, suspicious for the winter.
          All my Plugins I've written

          1 Reply Last reply Reply Quote 0
          • T Offline
            thomthom
            last edited by 13 May 2009, 06:47

            I'm not sure about this extending SU and Ruby classes.
            It's convenient, but I've avoided it so far as I'm worried that it might conflict. I've noted when I've been inspecting classes that with a few plugins installed there's quite a few additional methods, even to the private ones.

            I've just added it to a separate function, to be 100% sure.

            But I don't really know what I'm talking about, so I'll join you in the wait and see what people says.

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

            1 Reply Last reply Reply Quote 0
            • C Offline
              Chris Fullmer
              last edited by 13 May 2009, 06:47

              Yeah, that is my thinking too remus. I think I'll go ahead and name it with the initials in the front just to be safe.

              Chris

              waits along with Remus and Thom

              Lately you've been tan, suspicious for the winter.
              All my Plugins I've written

              1 Reply Last reply Reply Quote 0
              • T Offline
                thomthom
                last edited by 13 May 2009, 06:51

                Ohh, look at that tumbleweed! 😎

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

                1 Reply Last reply Reply Quote 0
                • J Offline
                  Juantxo
                  last edited by 5 Feb 2014, 17:46

                  I would like to use a method like

                  3.1415.watts_to_kwatts

                  SketchUp warehouse does not let me to add the method to Float class:

                  class Float
                  def watts_to_kwatts()
                  ...
                  end
                  end

                  Is there a solution to make sentence work storing method in a subclass?

                  1 Reply Last reply Reply Quote 0
                  • D Offline
                    Dan Rathbun
                    last edited by 5 Feb 2014, 18:51

                    You can try a subclass.. BUT it must be defined within YOUR or your company's) namespace.
                    Do not define it at the toplevel within Object !

                    class Juantxo;;Watts < Float
                    
                      def to_kw
                        self / 1000.0
                      end
                    
                    end # custom class
                    
                    

                    Your subclass should inherit a to_f method.

                    w = Juantxo::Watts.new(3.1415) kw = w.to_kw()

                    ❓

                    I'm not here much anymore.

                    1 Reply Last reply Reply Quote 0
                    • tt_suT Offline
                      tt_su
                      last edited by 5 Feb 2014, 19:16

                      You can also use .extend() on single instances:
                      http://www.ruby-doc.org/core-1.8.6/Object.html#method-i-extend

                      Note that extending Entity instances would not be allowed by EW because the instances are global to everyone using the SketchUp environment. But for other types such as Point3d, Vector3d, String etc that would work.

                      1 Reply Last reply Reply Quote 0
                      • C Offline
                        Chris Fullmer
                        last edited by 5 Feb 2014, 20:02

                        WARNING from 2014: Extending the SketchUp classes is virtually always a bad idea and will make your plugin not be allowed onto the Extension Warehouse. Please find a different way to do things, other than by extending existing SketchUp classes, modules, methods, etc.

                        class Sketchup;;Entities
                        
                        	# Adds a clf_bezier_points method to the Entities class.  Arguments are the starting point of the bezier, first handle, the endpoint of the bezier, the endpoints handle.  
                        	# All are either Point3d objects, or an array of [x,y,z] values.  The last argument is the number of segments the bezier should have.  
                        	# This method returns an array of [x,y,z] points, starting with the given starting point and ending with the given endoing point.
                        	def clf_bezier_points( p0, p1, p3, p2, segs )
                        		point_array = []
                        		np = [0,0,0]
                        		cx = (3 * (p1[0] - p0[0]))
                        		bx = (3 * (p2[0] - p1[0])) - cx
                        		ax = (p3[0] - p0[0]) - cx - bx
                        		cy = (3 * (p1[1] - p0[1]))
                        		by = (3 * (p2[1] - p1[1])) - cy
                        		ay = (p3[1] - p0[1]) - cy - by
                        		cz = (3 * (p1[2] - p0[2]))
                        		bz = (3 * (p2[2] - p1[2])) - cz
                        		az = (p3[2] - p0[2]) - cz - bz
                        		(segs.to_i+1).times do |e|
                        			t = e/segs.to_f
                        			np[0] = (ax*(t**3)) + (bx*(t**2)) + (cx*t) + p0[0]
                        			np[1] = (ay*(t**3)) + (by*(t**2)) + (cy*t) + p0[1]
                        			np[2] = (az*(t**3)) + (bz*(t**2)) + (cz*t) + p0[2]
                        			point_array << np.clone
                        		end
                        		point_array
                        	end # clf_bezier_points
                        	
                        	# Adds a clf_add_bezier method to the Entities class.  Arguments are the starting point of the bezier, first handle, the endpoint of the bezier, the endpoints handle.  
                        	# All are either Point3d objects, or an array of [x,y,z] values.  The last argument is the number of segments the bezier should have.
                        	# This method returns an array that contains an arc object and an array of the edges, in order, in the bezier curve.
                        	def clf_add_bezier( p0, p1, p3, p2, segs )
                        		point_array = clf_bezier_points( p0, p1, p3, p2, segs )
                        		z = parent.active_entities.add_curve point_array
                        		bez_curve = z[0].curve
                        		[bez_curve,z]
                        	end # clf_add_bezier
                        	
                        end # Sketchup;;Entities class
                        

                        This is the code I was planning on releasing as a developer's tool to add quick bezier curve ability.

                        Is writing it as its own .rb file that needs to be downloaded and installed really the best way? Or would it just made more sense to post is as standalone methods that would be easy for other scripter's to just copy and paste from here into their code?

                        And don't get me wrong, I fully expect that about 0 people will ever actually use this code. It was a bit of an exercise in figuring out how to extend SU's classes. But theoretically, what does everyone think the most efficient way to provide code bits like this is?

                        Chris

                        EDIT: Also, I was hoping that someone with some experience in these things could look over the code quickly and check if I have done anything majorly wrong that would quickly cause problems? Thanks in advance (Jim, Todd, Rick, TBD, TIG, Fredo, Tomasz, Whaat, the other Chris, and the other one, Wehby, and everyone that I missed, or didn't include who feels like they have answers! πŸ˜„ )

                        Lately you've been tan, suspicious for the winter.
                        All my Plugins I've written

                        1 Reply Last reply Reply Quote 0
                        • danielbowringD Offline
                          danielbowring
                          last edited by 7 Feb 2014, 01:26

                          @tt_su said:

                          Note that extending Entity instances would not be allowed by EW because the instances are global to everyone using the SketchUp environment. But for other types such as Point3d, Vector3d, String etc that would work.

                          Here delegators can be useful.

                          
                          require 'delegate'
                          
                          module DanielB
                            class SampleFace < DelegateClass(Sketchup;;Face)
                              def inner_loops
                                return loops.find_all { |l| l != outer_loop }
                              end
                            end
                          
                            def self.demo
                              face = Sketchup.active_model.selection.find { |e| e.is_a?(Sketchup;;Face) }
                              return if face.nil?
                              face = SampleFace.new(face)
                              puts face.inner_loops
                            end
                          end
                          
                          
                          1 Reply Last reply Reply Quote 0
                          • 1 / 1
                          • First post
                            Last post
                          Buy SketchPlus
                          Buy SUbD
                          Buy WrapR
                          Buy eBook
                          Buy Modelur
                          Buy Vertex Tools
                          Buy SketchCuisine
                          Buy FormFonts

                          Advertisement