Trying create dynamic component

I’m trying to create a dynamic component with instances of other dynamic components. Could someone help me understand the error?

CaixaMDF2.rb (1.4 KB)
Cubo.rb (2.4 KB)

  1. The code you provided is nothing to do with Dynamic Components.
    There is no proper way to interact with Dynamic Components from another extension. Dynamic Components does not have a public API and all interactions with it is basically just a hacks and risky…
    .
    However your methods can be called as a way to create you component dynamically.
  2. We do not know what error you got?
  3. We do not know with which parameters and how the CaixaMDF were called?
  4. If the last parameter "nome" passed as String, then the verificaParametrosNumericos method will return false.
  5. There is no add_to_model method exist in the SketchUp Ruby API for the entities and I did not see if you delfineden it somewhere… (it is line 20 in CaixaMDF2.rb)
1 Like

“Cubo.rb”

(1) Do not define custom classes at the top level ObjectSpace. Only Ruby and the API should do this.
All of you code should be within your unique top-level namespace module. Each of your extenstions should be within a separate submodule of your namespace module.

(2) The last statement of Cubo#initialize (ie, return instancia) is worthless. A class’ #initialize method ALWAYS returns the new instance of it’s class. (In this case the new instance of a Cubo.)

  • Your code cannot return something else, so this return statement would be ignored.
  • What you might not understand, is that by the time a class’ #initialize method gets called, it’s instance object is already created and the #initialize method is being evaluated within this new instance object.

(3) It is not normal procedure to do all the “work” within an initialize method. Usually this is done in other methods of a class and called from the initialize method. Generally, the initialize method defines internal instance variables that will hold state throughout the life if the class’ instance objects.

That said, …
(4) What you show in “Cubo.rb” should not be a class as it does not have any state (properties) to hold. Really what you’ve shown is simply a module method, so Cubo should really be module as there is nothing to instantiate. It just contains one “factory method” that producs a SketchUp API object and adds it to the model’s active entities collection.

The main error is the misuse of classes. Ie, neither Cubo nor CaixaMDF should be a class.

In CaixaMDF, the calls to Cubo.new does not return what the coder thinks it does. The Cubo objects are basically “empty” Ruby Object subclasses that have no state or functionality (beyond what is inherited from Object.)

So, they cannot be later added to any entities collection using #add_instance.


The solution is for both files to be combined into a single module with module methods.

Your answer was helpful and clarified things for me. Do you have any sample code that instantiates an object with other objects, splitting their functions? It’s quite hard to find examples like this on the web.