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.

--------------------
Code: Select all
# 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
Last edited by michaelwhksg on Tue Apr 04, 2017 7:26 am, edited 1 time in total.

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
 

Re: Dynamic Component DCFunctionsV1 within in Module

Postby michaelwhksg » Sat Mar 25, 2017 11:52 pm

Hi TIG, I have been trying for a few days with DCFunctionsV1.instance_methods.sort.

However, it does not really work.

Are you able to help enlighten me further?
0

michaelwhksg 
 

Re: Dynamic Component DCFunctionsV1 within in Module

Postby TIG » Mon Mar 27, 2017 3:01 pm

Using
DCFunctionsV1.instance_methods.sort
in the Ruby Console simply returns a list of all of the methods available.
If you have added your own method it will also be listed there.

The example I gave for adding 'atan2' will work if you then used it as directed in your DC code...
0
TIG
User avatar
TIG 
Global Moderator
 

Re: Dynamic Component DCFunctionsV1 within in Module

Postby michaelwhksg » Tue Mar 28, 2017 3:22 am

If i am using the code which you have shared, and keying DCFunctionsV1.instance_methods.sort in the Ruby Console, I do see atan2 appearing in the console.

However, if I tried inserting the DCFunctionsV1.instance_methods.sort into the code, an error message appeared.

Below is the code which I have tried...

---------------------------------------------------------
Code: Select all
# add_funcs.rb
# extends DCs functions
require('sketchup')

module ABC
module ABC123

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.instance_methods.sort #(did I do this correctly?)
    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

end #module ABC123
end #module ABC
-------------------------------------------------------

This generated an error message.


"Error Loading File arctan-test2.rb
Error: #<SyntaxError: C:/Users/wongh/AppData/Roaming/SketchUp/SketchUp 2017/SketchUp/Plugins/arctan-test2.rb:16: syntax error, unexpected '\n', expecting :: or '[' or '.'
C:/Users/wongh/AppData/Roaming/SketchUp/SketchUp 2017/SketchUp/Plugins/arctan-test2.rb:30: syntax error, unexpected keyword_end, expecting end-of-input
end #module ABC
^>"

Is there anything I have done wrongly?
0
Last edited by michaelwhksg on Tue Apr 04, 2017 7:31 am, edited 2 times in total.

michaelwhksg 
 

Re: Dynamic Component DCFunctionsV1 within in Module

Postby TIG » Tue Mar 28, 2017 11:08 am

You have misunderstood the purpose of the ...methods.sort snippet.
It is for use in the Ruby Console - pasted manually - so that you can see what's available.
The main code that defines the new function should NOT have that added into it !
As you have found it breaks it !

You have a script creating the atan2 function.
Do not do anything more to that.
Do not place it within your own name spaces [for now at least].
Use it as my example...
Use it to add the extra function directly into the DC code.

In the DC you then use: =atan2(y,x)
as the function...
0
TIG
User avatar
TIG 
Global Moderator
 

Re: Dynamic Component DCFunctionsV1 within in Module

Postby michaelwhksg » Wed Mar 29, 2017 6:56 am

Understood your message, TIG.

Does this mean that at this point in time, we are unable to introduce a new function in our name spaces?
0

michaelwhksg 
 

Re: Dynamic Component DCFunctionsV1 within in Module

Postby TIG » Wed Mar 29, 2017 1:17 pm

There are ways to use an existing class and add to it or 'refine' it etc within your own module[s].
But that will provide the new class/method when called within your module - but not outside of it.

Since the DC code is calling its class/method directly, then your own method-wrapped additions will not be seen by it.

Since you are only using this code for yourself [?] I see little wrong in adding directly to the DC class and then making your own separate code within your own module[s] to do other things.
The method I provided only adds the new method it it doesn't exist [for example some other code is adding it or in the future it's added to the main shipped class.

It is bad form to mess with shipped classes etc, but sometimes you must do it - e.g. this DC class methods are used directly in the DC functions...
0
TIG
User avatar
TIG 
Global Moderator
 

Re: Dynamic Component DCFunctionsV1 within in Module

Postby michaelwhksg » Wed Mar 29, 2017 11:56 pm

Thanks for the advice, TIG. Appreciate your input.

At this moment, I am using the code for myself. However, I am also looking at opportunity to release the dynamic components I have created into the Extension Store.

I have seen the recommendation that you have shared with others in the forum. One way of getting the DC component to be dependent on the plugin is to give the DC component a function defined in the code. The DC component will not work without the plugin.

How can we use the class DCFunctionV1 within the name space (our own module) so that it fulfills the requirement of a good practice of a plugin? This is the question I am trying to get around. Any recommendation on this?
0

michaelwhksg 
 

Re: Dynamic Component DCFunctionsV1 within in Module

Postby TIG » Thu Mar 30, 2017 11:44 am

I don't think you can do it wrapped inside your module.
It needs to add the function-method to the DC class directly, so that it's then seen by the DC function calling that class.
If you define it within your module it's your module's class and will not be seen.
Even if you 'refine' it or use it in other ways it's new methods are only seen in your class.
You need to change the DC class directly !

Changing a shipped class might get you into a mess on the EWH.
You could of course sell it separately...
0
TIG
User avatar
TIG 
Global Moderator
 

Re: Dynamic Component DCFunctionsV1 within in Module

Postby michaelwhksg » Sat Apr 01, 2017 10:01 am

Since we are unable to wrap the class DCFunctionsV1 in the name space, if we just submit the script with 2 parts, one of which is the DCFunctionsV1 and letting the formula be defined as my own unique name, and the second part which is wrapped within my name space, will this work? Will EWH team accept this?

For example,...

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?(:XXXXXX)
      # access to Ruby's arctangent method
      # Usage: =ABC123
      def (we give this definition a unique name)
        return XXXXXX (my own unique formula)
      end
    end#unless
    protected:ABC123
  end#class
end#if

module ABC
module ABC123

codes to be in the name space

end #ABC123
end #ABC
0
Last edited by michaelwhksg on Tue Apr 04, 2017 7:44 am, edited 3 times in total.

michaelwhksg 
 

Re: Dynamic Component DCFunctionsV1 within in Module

Postby TIG » Sat Apr 01, 2017 2:03 pm

Can you please format your code using the 'code' tags and indent it.
It's very hard to read as it is...

Your example is still changing the shipped class, so it will probably fail EWH checking...
You can but submit it, and wait and see.

You do not need the convolutions you added - they do nothing to a DC and still change the shipped class.

I can see no way you can do this and distribute it via the EWH, unless they agree to your class additions which technically break their strict rules...
:?
0
TIG
User avatar
TIG 
Global Moderator
 

Re: Dynamic Component DCFunctionsV1 within in Module

Postby michaelwhksg » Mon Apr 03, 2017 3:57 am

Hi TIG. Sorry, will use the "code" tags in future.

Will try to submit once I have cleaned up the code.
Fingers crossed, but I have a feeling that you are right. It might get rejected.

It will be a pity though, because I believe there are many members who are able to build up the dynamic components (which can speed up and simplify the entire modelling process), but there's no way to prevent the DC from being distributed without the authors' agreement.
0

michaelwhksg 
 

Re: Dynamic Component DCFunctionsV1 within in Module

Postby Dan Rathbun » Tue Apr 04, 2017 1:04 am

michaelwhksg wrote:Hi TIG. Sorry, will use the "code" tags in future.


(1) A better idea is to edit your previous post, and insert the code tags.

(2) The example is STILL wrong in that the check for method definition still uses the name atan2.

(3) Yes, please use a unique method name, as your implementation of the atan2 method (specifically the parameter list) is not how the rest of the world would define it for use by ALL coders.
0
    I'm not here much anymore. But a PM will fire email notifications.
    User avatar
    Dan Rathbun 
    PluginStore Author
    PluginStore Author
     

    Re: Dynamic Component DCFunctionsV1 within in Module

    Postby michaelwhksg » Wed Apr 05, 2017 6:33 am

    Hi Dan,
    Thanks for the feedback.

    1. Have already edited the previous posts with the code tags.
    2. Have changed the example slightly (removal of the name atan2).
    3. Will use my unique name for the formula in the DCFunctionsV1 class.

    Will try to submit the extension after I clean up my code.
    Thanks.
    1

    michaelwhksg 
     

    SketchUcation One-Liner Adverts

    by Ad Machine » 5 minutes ago



    Ad Machine 
    Robot
     



     

    Return to Developers' Forum

    Who is online

    Users browsing this forum: grahamkirby01 and 3 guests

    Visit our sponsors: