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

    How to call method added to SU?

    Scheduled Pinned Locked Moved Developers' Forum
    9 Posts 3 Posters 508 Views 3 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.
    • honoluludesktopH Offline
      honoluludesktop
      last edited by

      I have included TIG's "ComponentInstance-add_entities.rb" in my plugin folder, and called it from my_script in its simplest form:
      componentinstance.add_entities(my_entities)

      The ruby console reports:
      Error: #<NameError: undefined local variable or method `componentinstance' for #Object:0xcbcf9ec>

      Any advice?

      1 Reply Last reply Reply Quote 0
      • TIGT Offline
        TIG Moderator
        last edited by

        The componentinstance must be a reference to an instance πŸ˜• Is it ❓
        Here's the usage notes from the script's file...

        Usage : Put this file into Plugins folder. Call in other scripts.
        It adds a method to Sketchup's ComponentInstance Class.

                      componentinstance.add_entities(entities, copy, unique)
                    
                    entities =  entities to add into the component-instance's definition.
                                
                    The other arguments are optional, and can be given as true or false.
                    If they are not given 'false' is assumed.
                    
                    copy   = true  - the entities are copied into the component-instance's definition,
                                     the originals remain as they are.
                           = false - [default] the entities are moved into the component-instance's
                                     definition, the originals are erased from the model.
                              
                    unique = true  - the component-instance's definition is 'made unique', 
                                     any other instances of that definition are therefore unchanged.
                           = false - [default] the component-instance's definition is updated, 
                                     and all other instances of that definition are also changed, 
                                     the '~SCRAP~' version of the former definition is deleted...
                                        
                    Thus the simplest form is:                
                      componentinstance.add_entities(entities)
                    which moves the entities into the component-instance's definition, 
                    changes all instances of that definition globally and then purges the 
                    original '~SCRAP~' definition.
                                    
                    The entities are added to the component-instance's definition using the same 
                    orientation and location, even if the instance had been manipulated in 3D etc.
                    An instance cannot be in its own 'add_entities' list, BUT another instance of 
                    that instance's definition can - therefore if the instance is in the list it 
                    is removed and a 'duplicate' added.
                    An 'add_entities' list that contains another instance of the instance's 
                    definition recursively could Bugsplat - if so then any of these instances are 
                    'made unique' to avoid this.
                    It can sometimes BugSplat if the Outliner window is open when it runs...
                    so it can now be forced to rollup first - using Jim's code ideas - you'll need 
                    'toggleWindows.rb' and 'Win32API.so' - sorry it's for Windows only - Mac users ???
                    If you are on a PC and don't install toggleWindows.rb/Win32API.so then 
                    it'll still work BUT please close the Outliner to avoid BugSplats !
        

        TIG

        1 Reply Last reply Reply Quote 0
        • Dan RathbunD Offline
          Dan Rathbun
          last edited by

          @honoluludesktop said:

          The ruby console reports:
          Error: #<NameError: undefined local variable or method `componentinstance' for #Object:0xcbcf9ec>

          Any advice?

          Yes.. wrap your code in a module. See the topic on Using Ruby Modules.

          The error shows you are executing your code within Object and therefore your code is unwrapped. (Executing code in the Ruby Console, runs it within Object.)

          Also, you are not actually calling TIG's method against an instance object of Sketchup::ComponentInstance.
          ~

          I'm not here much anymore.

          1 Reply Last reply Reply Quote 0
          • honoluludesktopH Offline
            honoluludesktop
            last edited by

            OK, thanks fellows. I looked at the api, and I believe that components are currently "over my head",>_< so I will back up, and work with groups first:-) I can easily make those with:
            a_group = entities.add_group my_entities

            Sigh....guess as far as ruby and the SU api, I am still in the "Hello World" catagory.

            1 Reply Last reply Reply Quote 0
            • Dan RathbunD Offline
              Dan Rathbun
              last edited by

              See my linkslist :
              RUBY RESOURCES
              http://forums.sketchucation.com/viewtopic.php?f=180&t=10142#p269709

              Read the tutorials at the top of the list, then download all the offline PDF books in the next section.

              You need a basic understanding of standard Ruby, before you can be successful applying the modules and classes of the Sketchup API.
              .

              I'm not here much anymore.

              1 Reply Last reply Reply Quote 0
              • honoluludesktopH Offline
                honoluludesktop
                last edited by

                Well, a_group = entities.add_group my_entities only seems worked in a limited way. I can create a group of one entity at a time, but apparently not an array of entities.>_<.

                I found a script "Clf_loosegroups" that suggest how it is done, but includes a method to_group = ents[0].*all_connected* and code that I do not understand.

                1 Reply Last reply Reply Quote 0
                • TIGT Offline
                  TIG Moderator
                  last edited by

                  We are in danger of getting tangled up in several different problems here... πŸ˜•
                  Usage: grp=entities.add_group(xxx)
                  where xxx must be an 'entity', OR a list of 'entities' separated by commas, OR an array of 'entities': i.e. the items under xxx could be just one 'entity' or groups/instance or a collection of other raw geometry like edges/faces, or indeed most other 'drawing_elements' [e.g. cpoints] etc...
                  BUT xxx cannot be a reference to an 'entities-object' - but you can always convert an 'entities-object' into an array using entities_object.to_a [Note that this is also a wise thing to do if you are going to change (e.g. .erase! or ' .add_...') entities within an entities-set, using loop iterating the list - because and entities-object is a dynamic list of the current entities and you will change the list as you loop and erase/add and cause unexpected results - whereas using entities.to_a.each{|e|...} will 'freeze' the list at the start and so it won't lead to confusing results...].
                  ALSO the entities you list that are to be added to the group must be in the same 'context' as the group itself [i.e. they must all share the same 'parent' - which is effectively the model's or a group's or a component-definition's 'entities-object'] - so do not try to add entities from one group into another group unless you want a Bugsplat! If you want to combine the entities of two groups into one group then you need to add these two groups into a new group in the same context, and then explode them so that their entities are now inside that new group...
                  grp=entities.add_group() simply makes an empty group 'grp' you can then add entities to it in the form
                  grp.entities.add_...(...)
                  For example grp.entities.add_cline([0,0,0],[0,0,1]) adds a guide-line [cline] 1" high into the [previously empty] group 'grp; at the origin...
                  Then grp.entities.add_cpoint([0,0,1]) places a guide-point [cpoint] at its end... etc etc...
                  Hope this helps.......

                  TIG

                  1 Reply Last reply Reply Quote 0
                  • honoluludesktopH Offline
                    honoluludesktop
                    last edited by

                    TIG, Yes, didn't know about "It cannot be a reference to an 'entities' object..". OK that gives me something to work with. I will have to read the API more carefully. Thanks.

                    Addenda: Well, I did it. Not sure that I understand everything as I had to borrow code from others. Couple things to fix, features to add, and I will be able to post my file:-)

                    1 Reply Last reply Reply Quote 0
                    • honoluludesktopH Offline
                      honoluludesktop
                      last edited by

                      For other beginning ruby writers like myself:

                      model = Sketchup.active_model
                      @entities = model.active_entities
                      @selection = model.selection
                      .
                      .
                      .
                      # Group entities inserted.
                      sel = @selection
                      sel_ents = sel.to_a
                      first_entity = sel_ents[0]
                      while sel_ents.length > 0
                        if ((sel_ents[0].is_a? Sketchup;;Edge) or (sel_ents[0].is_a? Sketchup;;Face)) and sel_ents[0] == first_entity
                          new_group = sel_ents[0].all_connected
                          sel_ents = sel_ents - new_group
                          group = @entities.add_group(new_group)
                        else
                          sel_ents = sel_ents - [sel_ents[0]]
                        end
                      end
                      model.commit_operation
                      

                      C&C is welcomed:-)

                      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