Dynamic Component DCFunctionsV1 within in Module

Dynamic Component DCFunctionsV1 within in Module

Postby michaelwhksg » Sun Mar 19, 2017 4:47 am

Hi I am a newbie to ruby. Can we place the DCFunctionsV1 within Module?
I am unable to get the customized formula in the dynamic component.
Please kindly advise.
Thanks.

--------------------
# add_funcs.rb

require 'sketchup'

module M123
module M123abc

# Open SketchUp's Dynamic Component Functions (V1) class.

class M123::M123abc::DCFunctionsV1
protected

# provide access to Ruby's arctangent method
#
# Usage: atan2(y,x)

def atan2(a)
return Math::atan2(a[0].to_f, a[1].to_f).radians
end

end
end
end
0

michaelwhksg 
 

Re: Dynamic Component DCFunctionsV1 within in Module

Postby slbaumgartner » Sun Mar 19, 2017 9:11 pm

Two thoughts:

First, the code you show isn't modifying SU's DCFunctionsV1, it is creating a new class of that name within the M123::M123abc module namespace.

Second, what makes you think that just defining atan2 within the DCFunctionsV1 class will make that formula available for use in DC's? That assumes a lot about how the DC class is programmed (which isn't documented).
0

slbaumgartner 
PluginStore Author
PluginStore Author
 

Re: Dynamic Component DCFunctionsV1 within in Module

Postby TIG » Sun Mar 19, 2017 10:04 pm

This code will work outside of your own module.
Code: Select all
# add_funcs.rb
# extends DCs functions
require('sketchup')
if Sketchup.version.to_i <= 8
  require('dynamiccomponents.rb')
else
  require('su_dynamiccomponents.rb')
end
if defined?($dc_observers)
  # Open SketchUp's Dynamic Component Functions (V1) class.
  # only if DC extension is active
  class DCFunctionsV1
    protected
    unless DCFunctionsV1.method_defined?(:atan2)
      # access to Ruby's arctangent method
      # Usage: =atan2(y,x)
      def atan2(a)
        return Math::atan2(a[0],a[1]).radians
      end
    end#unless
    protected:atan2
  end#class
end#if
0
TIG
User avatar
TIG 
Global Moderator
 

Re: Dynamic Component DCFunctionsV1 within in Module

Postby michaelwhksg » Mon Mar 20, 2017 7:06 am

Thanks a lot for the advice, slbaumgartner and TIG. :) Will try the codes later.

Another question, if you don't mind. Assuming we do not confine the entire code within it's own namespace (module), will there be a chance that name of the customised formula clashes with someone's else?

If we have created another unique formula for the dynamic component, for example, def qwer(a) and qwer(a)=a[0]+a[1]+a[2]. Is there any way from preventing others from using the same name as qwer(a)?

Thanks.
0

michaelwhksg 
 

Re: Dynamic Component DCFunctionsV1 within in Module

Postby TIG » Mon Mar 20, 2017 12:05 pm

Yes you should keep your own code within your own modules.

The example I gave is adding methods into SketchUp's DCFunctionsV1 class directly.
I did that to show how to construct the new method etc...

You could do it within your module with suitable extends etc:
search.php?keywords=extend+class&fid%5B0%5D=180
with refine:
viewtopic.php?p=573770#p573770
etc
BUT I'm unsure that DCFunctionsV1 will respect those added methods outside of your code - i.e. with DC code...
Test it and see...
DCFunctionsV1.instance_methods.sort
0
TIG
User avatar
TIG 
Global Moderator
 

SketchUcation One-Liner Adverts

by Ad Machine » 5 minutes ago



Ad Machine 
Robot
 



 

Return to Developers' Forum

Who is online

Users browsing this forum: No registered users and 4 guests

Visit our sponsors: