sketchucation logo sketchucation
    • Login
    ℹ️ Licensed Extensions | FredoBatch, ElevationProfile, FredoSketch, LayOps, MatSim and Pic2Shape will require license from Sept 1st More Info

    Automatically load a component.

    Scheduled Pinned Locked Moved Developers' Forum
    70 Posts 5 Posters 5.6k Views 5 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.
    • TNTDAVIDT Offline
      TNTDAVID
      last edited by

      Hello everyone. ☀

      As my last objective seems difficult, I return to my first idea.

      Instead of automatically import the "cube.skp" on each new SketchUp scene.

      The "cube" will be imported along with the first "Dynamic Component".

      Here is the modified code and that works:

      module TesteCube
      
      
        module CubeImport
        
            path_to_components = File.join(File.dirname(__FILE__), 'Components')
            @@path_to_00 = File.join(path_to_components, 'Dynamic Component.skp')
            @@path_to_resources = File.join(File.dirname(__FILE__), 'Resources')
      	  
          def self.methode1
      
            path = Sketchup.find_support_file("cube.skp", 
            "Plugins/TesteCube/Components/")
            model = Sketchup.active_model
            definitions = model.definitions
            component_definition = definitions.load(path)
            point = Geom;;Point3d.new(0, 0, 0)
            transformation = Geom;;Transformation.new(point)
            instance = model.entities.add_instance(component_definition, transformation)
            status = instance.locked = true 
            layers = model.layers
            new_layer = layers.add "cube"
            new_layer.visible = false
            instance.layer = new_layer
            instance.locked = true
            instance.hidden = true
            Sketchup.active_model.import(@@path_to_00)
      
          end
      	
      
          if !file_loaded?(__FILE__)
      	
      	
            su_menu = UI.menu("Plugins")
      
            cc_menu = su_menu.add_submenu(%Q(Teste_Cube))
      		
            tb = UI;;Toolbar.new(%Q(Teste-Cube))
      		
            command1 = UI;;Command.new("add Dynamic Component"){ CubeImport.methode1 }
      
            command1.small_icon = File.join(@@path_to_resources,"add_00.png")
      
            command1.large_icon = File.join(@@path_to_resources,"add_00.png")
      
            command1.tooltip = %Q(add Dynamic Component)
      
            command1.status_bar_text = %Q(add Dynamic Component)
      
            command1.menu_text = %Q(add Dynamic Component)
      
            cc_menu.add_item(command1)
      
            tb.add_item(command1)
      		
            tb.restore
       
            file_loaded(__FILE__) 
      
            end
        
         end
      
      end	 
      

      Notice that "method1" to one more line to import "Dynamique. Component.skp"
      at the same time that the "cube.skp".

      The line of code with the more interest to me is:

      command1 = UI::Command.new("add cube") { CubeImport.methode1 }

      She created a new icon { that triggers methode1 }.

      If I click once on the icon:

      methode1 will import "1 cube" + "1 component Dynamic".

      If I click ten times on the icon:

      method1 will import "10 cubes" + "10 Dynamic Components".

      That's exactly my problem ❗

      I do need a single "cube", with no restrictions on the number of "Dynamic Components".

      How to limit the amount of cube imported at "1" ?

      Through links provided by DanRathbun and especially "Ruby in twenty minutes",
      I understand better now the ruby. 😍

      But this is not sufficient to finalize my code as a great.

      Thank you in advance for your help.

      Cordially

      David

      [b:8wt9py2i]* Nouveau !!![/b:8wt9py2i] Découvrez notre nouveau Plugin [url=http://www.composant-dynamique.com/:8wt9py2i][color=#40FF00:8wt9py2i]C[/color:8wt9py2i][color=#77FF00:8wt9py2i]l[/color:8wt9py2i][color=#ADFF00:8wt9py2i]i[/color:8wt9py2i][color=#E4FF00:8wt9py2i]c[/color:8wt9py2i][color=#FFDB09:8wt9py2i]k[/color:8wt9py2i][color=#FF921B:8wt9py2i]-[/color:8wt9py2i][color=#FF492E:8wt9py2i]C[/color:8wt9py2i][color=#FF0040:8wt9py2i]u[/color:8wt9py2i][color=#ED2577:8wt9py2i]i[/color:8wt9py2i][color=#DA49AD:8wt9py2i]s[/color:8wt9py2i][color=#C86EE4:8wt9py2i]i[/color:8wt9py2i][color=#AD77FF:8wt9py2i]n[/color:8wt9py2i][color=#8965FF:8wt9py2i]e[/color:8wt9py2i] [color=#4040FF:8wt9py2i]2[/color:8wt9py2i][/url:8wt9py2i], pour créer vos cuisines 3D !

      1 Reply Last reply Reply Quote 0
      • TNTDAVIDT Offline
        TNTDAVID
        last edited by

        To limit the amount of cube to import, I found this method:

        ComponentInstance.manifold?

        This is the method that can meet my expectations?

        I tried many things and it does not work:

        instance.componentinstance.manifold instance = false
        
        componentinstance.manifold instance = false
        
        instance = componentinstance.manifold?
        instance.componentinstance.manifold = false
        
        status = istance.componentinstance.manifold?
        status.componentinstance.manifold = false
        
        componentinstance.manifold status = false
        
        instance.componentinstance.manifold status = false
        
        instance.componentinstance.manifold = false
        

        Nothing works !

        And all methods that are following this method are not executed.

        I need your help more than ever.

        Thank you

        David

        [b:8wt9py2i]* Nouveau !!![/b:8wt9py2i] Découvrez notre nouveau Plugin [url=http://www.composant-dynamique.com/:8wt9py2i][color=#40FF00:8wt9py2i]C[/color:8wt9py2i][color=#77FF00:8wt9py2i]l[/color:8wt9py2i][color=#ADFF00:8wt9py2i]i[/color:8wt9py2i][color=#E4FF00:8wt9py2i]c[/color:8wt9py2i][color=#FFDB09:8wt9py2i]k[/color:8wt9py2i][color=#FF921B:8wt9py2i]-[/color:8wt9py2i][color=#FF492E:8wt9py2i]C[/color:8wt9py2i][color=#FF0040:8wt9py2i]u[/color:8wt9py2i][color=#ED2577:8wt9py2i]i[/color:8wt9py2i][color=#DA49AD:8wt9py2i]s[/color:8wt9py2i][color=#C86EE4:8wt9py2i]i[/color:8wt9py2i][color=#AD77FF:8wt9py2i]n[/color:8wt9py2i][color=#8965FF:8wt9py2i]e[/color:8wt9py2i] [color=#4040FF:8wt9py2i]2[/color:8wt9py2i][/url:8wt9py2i], pour créer vos cuisines 3D !

        1 Reply Last reply Reply Quote 0
        • K Offline
          kaas
          last edited by

          Manifold is used for testing if its a solid or not. Not the right tool for your problem.

          So you just need 1 cube in the model: you have linked the 'cube' to 'path' and you refer to it as 'component_definition'. You insert the cube using:
          instance = model.entities.add_instance(component_definition, transformation)

          Why not create a small test and see if an instance is already in the model and if so - don't load it again. Replace the above line with these lines:

          in_scene=false
          definitions.each{|d|
          	if d.name == 'cube'
          		c = d.count_used_instances
          		in_scene = true if c > 0
          	end
          }
          if !in_scene
          	instance = model.entities.add_instance(component_definition, transformation)
          end
          
          1 Reply Last reply Reply Quote 0
          • TIGT Offline
            TIG Moderator
            last edited by

            You seem to be misunderstanding how some things [many things!] work.
            !!! .manifold? is NOT what you need it's for testing if a group or instance is a a 'solid' - e.g. for 3d printing.

            When your extension starts to load the FIRST DC it initially adds 'definition'.
            AND it then adds an 'instance' of that.
            Subsequent DC loads should also check in case the definition or instance are not present...

            if definition.valid?
              if instance.valid? ###[or perhaps || definition.instances[0] ???]
                ### You are already set to do the DC load...
              else
                ### You need to add an instance - perhaps it's been deleted by the user ???
                ### NOW you are set to do the DC load...
              end
            else ### definition is NOT valid - it's been deleted - so the instance has too.
              ### You load the definition AND add the instance again - seems like the user has deleted both !
              ### NOW you are set to do the DC load...
            end
            

            A certain logic is needed in thinking through the coded steps...

            TIG

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

              Following @Kaas's useful input...
              I am unclear as to whether you want one 'cube' instance in each model or in each scene or in each entities-context ???
              If you only ever need one then it's easily done, otherwise it gets more convoluted [but still do-able]...

              TIG

              1 Reply Last reply Reply Quote 0
              • TNTDAVIDT Offline
                TNTDAVID
                last edited by

                @kaas said:

                Pourquoi ne pas créer un petit test et voir si une instance est déjà dans le modèle et si oui - ne pas le charger à nouveau. Remplacer la ligne ci - dessus avec ces lignes:

                CODE PROMO: SÉLECTIONNER TOUT
                in_scene=false
                definitions.each{|d|
                   if d.name == 'cube'
                      c = d.count_used_instances
                      in_scene = true if c > 0
                   end
                }
                if !in_scene
                   instance = model.entities.add_instance(component_definition, transformation)
                end


                Like this?

                module TesteCube
                
                
                  module CubeImport
                  
                      path_to_components = File.join(File.dirname(__FILE__), 'Components')
                      @@path_to_00 = File.join(path_to_components, 'Dynamic Component.skp')
                      @@path_to_resources = File.join(File.dirname(__FILE__), 'Resources')
                	  
                    def self.methode1
                      path = Sketchup.find_support_file("cube.skp", 
                      "Plugins/TesteCube/Components/")
                      model = Sketchup.active_model
                      definitions = model.definitions
                      component_definition = definitions.load(path)
                      point = Geom;;Point3d.new(0, 0, 0)
                      transformation = Geom;;Transformation.new(point) 
                      in_scene=false
                      definitions.each{|d|
                      if d.name == 'cube'
                      c = d.count_used_instances
                      in_scene = true if c > 0
                      end
                      }
                      if !in_scene
                      instance = model.entities.add_instance(component_definition, transformation)
                      end
                      status = instance.locked = true
                      layers = model.layers
                      new_layer = layers.add "cube"
                      new_layer.visible = false
                      instance.layer = new_layer
                      instance.locked = true
                      instance.hidden = true
                      Sketchup.active_model.import(@@path_to_00)
                      end
                	 
                      if !file_loaded?(__FILE__)
                
                      su_menu = UI.menu("Plugins")
                
                      cc_menu = su_menu.add_submenu(%Q(Teste_Cube))
                		
                      tb = UI;;Toolbar.new(%Q(Teste-Cube))
                		
                      command1 = UI;;Command.new("add Dynamic Component"){ CubeImport.methode1 }
                
                      command1.small_icon = File.join(@@path_to_resources,"add_00.png")
                
                      command1.large_icon = File.join(@@path_to_resources,"add_00.png")
                
                      command1.tooltip = %Q(add Sliding Window)
                
                      command1.status_bar_text = %Q(add Sliding Window)
                
                      command1.menu_text = %Q(add Sliding Window)
                
                      cc_menu.add_item(command1)
                
                      tb.add_item(command1)
                		
                      tb.restore
                 
                      file_loaded(__FILE__) 
                
                      end
                  
                   end
                
                end	 
                

                When I click on my icon, nothing happens.

                I made a mistake ?

                @tig said:

                I am unclear as to whether you want one 'cube' instance in each model or in each scene or in each entities-context ???

                I want one cube for each SketchUp scene!

                Thank you for your help.

                [b:8wt9py2i]* Nouveau !!![/b:8wt9py2i] Découvrez notre nouveau Plugin [url=http://www.composant-dynamique.com/:8wt9py2i][color=#40FF00:8wt9py2i]C[/color:8wt9py2i][color=#77FF00:8wt9py2i]l[/color:8wt9py2i][color=#ADFF00:8wt9py2i]i[/color:8wt9py2i][color=#E4FF00:8wt9py2i]c[/color:8wt9py2i][color=#FFDB09:8wt9py2i]k[/color:8wt9py2i][color=#FF921B:8wt9py2i]-[/color:8wt9py2i][color=#FF492E:8wt9py2i]C[/color:8wt9py2i][color=#FF0040:8wt9py2i]u[/color:8wt9py2i][color=#ED2577:8wt9py2i]i[/color:8wt9py2i][color=#DA49AD:8wt9py2i]s[/color:8wt9py2i][color=#C86EE4:8wt9py2i]i[/color:8wt9py2i][color=#AD77FF:8wt9py2i]n[/color:8wt9py2i][color=#8965FF:8wt9py2i]e[/color:8wt9py2i] [color=#4040FF:8wt9py2i]2[/color:8wt9py2i][/url:8wt9py2i], pour créer vos cuisines 3D !

                1 Reply Last reply Reply Quote 0
                • pilouP Offline
                  pilou
                  last edited by

                  Maybe this concept will more clear ! 😄

                  B_hidden.jpg

                  The mysterious thing for me is how to avoid the list of components (even hidden)
                  on the Onliner ? 😉

                  Bon courage!

                  Frenchy Pilou
                  Is beautiful that please without concept!
                  My Little site :)

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

                    My response is why use a hidden geometric construct to "protect" other components ?
                    I would instead use an AttributeDictionary.

                    I'm not here much anymore.

                    1 Reply Last reply Reply Quote 0
                    • K Offline
                      kaas
                      last edited by

                      @tntdavid said:

                      ...When I click on my icon, nothing happens....

                      Not sure. Maybe you already have an instance of the component present in your model? Its easy to find out by debugging. Add just one line in this loop:

                      
                      definitions.each{|d|
                          if d.name == 'cube'
                      		c = d.count_used_instances
                      		puts %(there are #{c} instances in the model of cube)
                      		in_scene = true if c > 0
                      	end
                      }
                      

                      This code won't help you though if you want to have just 1 cube per scene. That would make the code more complex.

                      Edit: Dan's suggestion might be just what you need. Its the only way to really hide data.

                      1 Reply Last reply Reply Quote 0
                      • TNTDAVIDT Offline
                        TNTDAVID
                        last edited by

                        Pilou thank you for your clearer picture that my writings. 😛

                        Because of my English very bad, I think I lacked clarity.

                        I want a single cube by Sketchup file.

                        In Sketchup file, you can have a complete project with many dynamic components, but you have a single cube!

                        @kaas said:

                        Not sure. Maybe you already have an instance of the component present in your model? Its easy to find out by debugging. Add just one line in this loop:

                        No instance is present in my file. 😕

                        To test the code, you can download the extension HERE.

                        @kaas said:

                        This code won't help you though if you want to have just 1 cube per scene. That would make the code more complex.

                        It's complicated to automatically remove a "cube", when there are 2?

                        @dan rathbun said:

                        My response is why use a hidden geometric construct to "protect" other components ?
                        I would instead use an AttributeDictionary.

                        Thank you for this information, I must annalyser this "class" with attention. 👍

                        With the methods provided by this class, is it possible to make a dynamic component unusable without the presence of the extension?

                        For example :

                        If someone shares one of my dynamic components with a user without my extension, it must be impossible to use these dynamic features!

                        I want to go at the end of my research, because my final interest is not only the security of my dynamic components.

                        Thank you for your support

                        David

                        [b:8wt9py2i]* Nouveau !!![/b:8wt9py2i] Découvrez notre nouveau Plugin [url=http://www.composant-dynamique.com/:8wt9py2i][color=#40FF00:8wt9py2i]C[/color:8wt9py2i][color=#77FF00:8wt9py2i]l[/color:8wt9py2i][color=#ADFF00:8wt9py2i]i[/color:8wt9py2i][color=#E4FF00:8wt9py2i]c[/color:8wt9py2i][color=#FFDB09:8wt9py2i]k[/color:8wt9py2i][color=#FF921B:8wt9py2i]-[/color:8wt9py2i][color=#FF492E:8wt9py2i]C[/color:8wt9py2i][color=#FF0040:8wt9py2i]u[/color:8wt9py2i][color=#ED2577:8wt9py2i]i[/color:8wt9py2i][color=#DA49AD:8wt9py2i]s[/color:8wt9py2i][color=#C86EE4:8wt9py2i]i[/color:8wt9py2i][color=#AD77FF:8wt9py2i]n[/color:8wt9py2i][color=#8965FF:8wt9py2i]e[/color:8wt9py2i] [color=#4040FF:8wt9py2i]2[/color:8wt9py2i][/url:8wt9py2i], pour créer vos cuisines 3D !

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

                          So to paraphrase...

                          You want to have your cube component loaded into the model's definitions.
                          You want to have ONE instance of it placed in the model.
                          That instance should be 'masked' - e.g. assigned a special OFF layer, be hidden and be locked.
                          [Aside: You could also make it so tiny that if was hard to find !]

                          All this is readily done.
                          My example test code, checks if the definition is loaded and if not it loads it.
                          If it id loaded and there is an instance there is nothing more to do, otherwise it adds the instance...


                          I agree with Dan...
                          All of the messing on so far could be achieved by more subtle and less complex means.
                          It probably needs a separate thread...
                          However, here's my outline.
                          Your plugin code loads DC components.
                          It's possible to give a DC component a function which stops it working if the function fails.
                          Your RB code could add a new function to the DC functions-code in the model, as it loads.
                          Thereby your RB makes your special DCs usable.
                          If a recipient of your DC does not have your licensed RB loading, then your DC does not work for them.
                          The absence of your extra DC function to validate your DC could also be triggered when a users license expires.
                          Here's an example of an extra DC function your RB could add...

                          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.
                            ### BUT only if DC extension is active...
                          	class DCFunctionsV1
                          		protected
                          		unless DCFunctionsV1.method_defined?(;tnt_checker)
                          			def tnt_checker()
                          				return true
                          			end
                          			protected;tnt_checker
                          			### Usage; =tnt_checker()
                          		end
                          	end#class
                          end#if
                          

                          Your main RB adds this extra function.
                          Within each of your DCs you use the extra function to set up a reference etc.
                          Usage: =tnt_checker()
                          It returns true if the RB has added the function of false/or fails otherwise...

                          Unfortunately even that could be circumvented by a Pro user editing the DCs attributes, BUT if your DC code is convoluted enough and it relies on the =tnt_checker() function's value being true to perform its later functions, then it at least makes the DC much more difficult to 'pirate'...
                          They could also write their own code RB snippet to set up that function and always return true ?

                          TIG

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

                            @tntdavid said:

                            If someone shares one of my dynamic components with a user without my extension, it must be impossible to use these dynamic features!

                            There is no way to make it impossible.

                            If you must use a DC function try to use a refinement for Ruby 2.0+.
                            Code example withdrawn. A refinement will not work, because the DC code files would themselves need to use the refinement.

                            • Modifying the DCFunctionsV1 class may not pass inspection by the Trimple Developer extension signing page.

                            I'm not here much anymore.

                            1 Reply Last reply Reply Quote 0
                            • TNTDAVIDT Offline
                              TNTDAVID
                              last edited by

                              @tig said:

                              You could also make it so tiny that if was hard to find !

                              Very good idea. 👍

                              We can also hide the edges of a model?

                              @tig said:

                              My example test code, checks if the definition is loaded and if not it loads it.
                              If it id loaded and there is an instance there is nothing more to do, otherwise it adds the instance...

                              You talk about this example?

                              @tig said:

                              if definition.valid?
                                if instance.valid? ###[or perhaps || definition.instances[0] ???]
                                  ### You are already set to do the DC load...
                                else
                                  ### You need to add an instance - perhaps it's been deleted by the user ???
                                  ### NOW you are set to do the DC load...
                                end
                              else ### definition is NOT valid - it's been deleted - so the instance has too.
                                ### You load the definition AND add the instance again - seems like the user has deleted both !
                                ### NOW you are set to do the DC load...
                              end


                              @tig said:

                              I agree with Dan...
                              All of the messing on so far could be achieved by more subtle and less complex means.
                              It probably needs a separate thread...

                              I do not doubt for a moment of your words.

                              As my specialty are dynamic components, create a protection directly into the models is much easier for me.

                              Although my protection is not foolproof, it will be very difficult to circumvent.


                              @dan rathbun said:

                              There is no way to make it impossible.

                              If you must use a DC function try to use a refinement for Ruby 2.0+.
                              Code example withdrawn. A refinement will not work, because the DC code files would themselves need to use the refinement.

                              Thanks for these informations ❗

                              [b:8wt9py2i]* Nouveau !!![/b:8wt9py2i] Découvrez notre nouveau Plugin [url=http://www.composant-dynamique.com/:8wt9py2i][color=#40FF00:8wt9py2i]C[/color:8wt9py2i][color=#77FF00:8wt9py2i]l[/color:8wt9py2i][color=#ADFF00:8wt9py2i]i[/color:8wt9py2i][color=#E4FF00:8wt9py2i]c[/color:8wt9py2i][color=#FFDB09:8wt9py2i]k[/color:8wt9py2i][color=#FF921B:8wt9py2i]-[/color:8wt9py2i][color=#FF492E:8wt9py2i]C[/color:8wt9py2i][color=#FF0040:8wt9py2i]u[/color:8wt9py2i][color=#ED2577:8wt9py2i]i[/color:8wt9py2i][color=#DA49AD:8wt9py2i]s[/color:8wt9py2i][color=#C86EE4:8wt9py2i]i[/color:8wt9py2i][color=#AD77FF:8wt9py2i]n[/color:8wt9py2i][color=#8965FF:8wt9py2i]e[/color:8wt9py2i] [color=#4040FF:8wt9py2i]2[/color:8wt9py2i][/url:8wt9py2i], pour créer vos cuisines 3D !

                              1 Reply Last reply Reply Quote 0
                              • TNTDAVIDT Offline
                                TNTDAVID
                                last edited by

                                Hello,

                                I still can not limit the number of cube to 1. 😕

                                If someone can give me more information on writing code, I would be very recognize him.

                                Thank you

                                David

                                [b:8wt9py2i]* Nouveau !!![/b:8wt9py2i] Découvrez notre nouveau Plugin [url=http://www.composant-dynamique.com/:8wt9py2i][color=#40FF00:8wt9py2i]C[/color:8wt9py2i][color=#77FF00:8wt9py2i]l[/color:8wt9py2i][color=#ADFF00:8wt9py2i]i[/color:8wt9py2i][color=#E4FF00:8wt9py2i]c[/color:8wt9py2i][color=#FFDB09:8wt9py2i]k[/color:8wt9py2i][color=#FF921B:8wt9py2i]-[/color:8wt9py2i][color=#FF492E:8wt9py2i]C[/color:8wt9py2i][color=#FF0040:8wt9py2i]u[/color:8wt9py2i][color=#ED2577:8wt9py2i]i[/color:8wt9py2i][color=#DA49AD:8wt9py2i]s[/color:8wt9py2i][color=#C86EE4:8wt9py2i]i[/color:8wt9py2i][color=#AD77FF:8wt9py2i]n[/color:8wt9py2i][color=#8965FF:8wt9py2i]e[/color:8wt9py2i] [color=#4040FF:8wt9py2i]2[/color:8wt9py2i][/url:8wt9py2i], pour créer vos cuisines 3D !

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

                                  Think through the logic...

                                  Here set out in words - not code...

                                  If there is no loaded definition, either it's a fresh start or the user has managed to delete it from the model: therefore you load it - otherwise you have got it already and you go to the next step.
                                  Next.
                                  If there is no instance of that definition, either because it's a fresh start or the user has managed to delete it from the model: therefore you need to add it - otherwise you already have it and do nothing.
                                  If there are multiple instances delete all but the first one.

                                  S'il n'y a pas de la definition chargée, soit il est un nouveau départ, ou l'utilisateur a réussi à le supprimer du modèle: donc vous le chargez [load] - sinon vous l'avez, déjà vous allez à l'étape suivante.
                                  Prochain.
                                  S'il n'y a pas d'instance de cette definition, soit parce qu'il est un nouveau départ, ou l'utilisateur a réussi à le supprimer du modèle: par conséquent, vous devez l'ajouter - sinon, vous l'avez déjà et ne rien faire ...
                                  S'il y a plusieurs instances supprimer tout sauf la première.

                                  ###
                                  model = Sketchup.active_model
                                  definitions = model.definitions
                                  if definition = definitions['cube'] ### or whatever it's named
                                    ### do nothing as we already have it
                                  else
                                    ### we load it because it's missing
                                    definition = definitions.load(path_to_cube.skp) ### you need to work that out
                                  end #if
                                  ### now check instance exists
                                  if definition.instances.length == 1
                                    ### we have it
                                    instance = definition.instances[0]
                                  elsif definition.instances.length > 1
                                    ### somehow we have duplicates, erase all but the first one [0]
                                    definition.instances[1..-1].each{|i| i.erase! }
                                    instance = definition.instances[0]
                                  else
                                    ### there is NO instance so we add it
                                    instance = model.entities.add_instance(definition, ORIGIN)
                                  end #if
                                  ###
                                  ### Now change instance to have an OFF layer, hide it, lock it etc etc
                                  ### ALWAYS do this in case the user has messed with it...
                                  ### 
                                  

                                  [mod=:1961t99s]Fixed my stupid typo ' definition.instances[1..-1]**.each**{|i| i.erase! }'[/mod:1961t99s]

                                  TIG

                                  1 Reply Last reply Reply Quote 0
                                  • TNTDAVIDT Offline
                                    TNTDAVID
                                    last edited by

                                    Thanks TIG,

                                    I followed your method, which is very clear and detailed:

                                    def self.methode1
                                          path = Sketchup.find_support_file("cube.skp", 
                                          "Plugins/TesteCube/Components/")
                                          model = Sketchup.active_model
                                          definitions = model.definitions
                                          component_definition = definitions.load(path)
                                          point = Geom;;Point3d.new(0, 0, 0)
                                          transformation = Geom;;Transformation.new(point) 
                                    	  
                                          if definition = definitions['cube']
                                    	  
                                          else
                                          definition = definitions.load(path_to_cube.skp)
                                          end
                                    	  
                                          if definition.instances.length == 1
                                          instance = definition.instances[0]
                                    
                                          elsif definition.instances.length > 1
                                          definition.instances[1..-1]{|i| i.erase! }
                                          instance = definition.instances[0]
                                    	  
                                          else
                                          instance = model.entities.add_instance(component_definition, transformation)
                                    	  
                                          status = instance.locked = true
                                          layers = model.layers
                                          new_layer = layers.add "cube"
                                          new_layer.visible = false
                                          instance.layer = new_layer
                                          instance.locked = true
                                          instance.hidden = true
                                          Sketchup.active_model.import(@@path_to_00)
                                          end
                                    

                                    I get this error message at the launch of SketchUp:

                                    @unknownuser said:

                                    Error Loading File C:/Users/DAVID/AppData/Roaming/SketchUp/SketchUp 2015/SketchUp/Plugins/TesteCube/logic.rb
                                    Error: #<SyntaxError: C:/Users/DAVID/AppData/Roaming/SketchUp/SketchUp 2015/SketchUp/Plugins/TesteCube/logic.rb:30: syntax error, unexpected { arg, expecting keyword_end
                                    definition.instances[1..-1]{|i| i.erase! }

                                                                ^
                                    

                                    C:/Users/DAVID/AppData/Roaming/SketchUp/SketchUp 2015/SketchUp/Plugins/TesteCube/logic.rb:30: syntax error, unexpected '}', expecting keyword_end
                                    definition.instances[1..-1]{|i| i.erase! }

                                                                              ^
                                    

                                    C:/Users/DAVID/AppData/Roaming/SketchUp/SketchUp 2015/SketchUp/Plugins/TesteCube/logic.rb:102: syntax error, unexpected end-of-input, expecting keyword_end>
                                    C:/Program Files/SketchUp/SketchUp 2015/Tools/extensions.rb:197:in require' C:/Program Files/SketchUp/SketchUp 2015/Tools/extensions.rb:197:in load'
                                    C:/Users/DAVID/AppData/Roaming/SketchUp/SketchUp 2015/SketchUp/Plugins/TesteCube.rb:28:in register_extension' C:/Users/DAVID/AppData/Roaming/SketchUp/SketchUp 2015/SketchUp/Plugins/TesteCube.rb:28:in module:TesteCube'
                                    C:/Users/DAVID/AppData/Roaming/SketchUp/SketchUp 2015/SketchUp/Plugins/TesteCube.rb:18:in `<top (required)>'

                                    I made a mistake somewhere?

                                    Thank you

                                    [b:8wt9py2i]* Nouveau !!![/b:8wt9py2i] Découvrez notre nouveau Plugin [url=http://www.composant-dynamique.com/:8wt9py2i][color=#40FF00:8wt9py2i]C[/color:8wt9py2i][color=#77FF00:8wt9py2i]l[/color:8wt9py2i][color=#ADFF00:8wt9py2i]i[/color:8wt9py2i][color=#E4FF00:8wt9py2i]c[/color:8wt9py2i][color=#FFDB09:8wt9py2i]k[/color:8wt9py2i][color=#FF921B:8wt9py2i]-[/color:8wt9py2i][color=#FF492E:8wt9py2i]C[/color:8wt9py2i][color=#FF0040:8wt9py2i]u[/color:8wt9py2i][color=#ED2577:8wt9py2i]i[/color:8wt9py2i][color=#DA49AD:8wt9py2i]s[/color:8wt9py2i][color=#C86EE4:8wt9py2i]i[/color:8wt9py2i][color=#AD77FF:8wt9py2i]n[/color:8wt9py2i][color=#8965FF:8wt9py2i]e[/color:8wt9py2i] [color=#4040FF:8wt9py2i]2[/color:8wt9py2i][/url:8wt9py2i], pour créer vos cuisines 3D !

                                    1 Reply Last reply Reply Quote 0
                                    • TNTDAVIDT Offline
                                      TNTDAVID
                                      last edited by

                                      Wonderful 😍 😍 😍

                                      If I delete this line of code:

                                      elsif definition.instances.length > 1
                                            definition.instances[1..-1]{|i| i.erase! }
                                            instance = definition.instances[0]
                                      
                                      

                                      Your method works great TIG! 👍

                                      Do you know why the line of code that is intended to deleted "cube", does not?

                                      Many thanks for your help.

                                      David

                                      [b:8wt9py2i]* Nouveau !!![/b:8wt9py2i] Découvrez notre nouveau Plugin [url=http://www.composant-dynamique.com/:8wt9py2i][color=#40FF00:8wt9py2i]C[/color:8wt9py2i][color=#77FF00:8wt9py2i]l[/color:8wt9py2i][color=#ADFF00:8wt9py2i]i[/color:8wt9py2i][color=#E4FF00:8wt9py2i]c[/color:8wt9py2i][color=#FFDB09:8wt9py2i]k[/color:8wt9py2i][color=#FF921B:8wt9py2i]-[/color:8wt9py2i][color=#FF492E:8wt9py2i]C[/color:8wt9py2i][color=#FF0040:8wt9py2i]u[/color:8wt9py2i][color=#ED2577:8wt9py2i]i[/color:8wt9py2i][color=#DA49AD:8wt9py2i]s[/color:8wt9py2i][color=#C86EE4:8wt9py2i]i[/color:8wt9py2i][color=#AD77FF:8wt9py2i]n[/color:8wt9py2i][color=#8965FF:8wt9py2i]e[/color:8wt9py2i] [color=#4040FF:8wt9py2i]2[/color:8wt9py2i][/url:8wt9py2i], pour créer vos cuisines 3D !

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

                                        Use:
                                        definition.instances[1..-1].each {|i| i.erase! }

                                        I'm not here much anymore.

                                        1 Reply Last reply Reply Quote 0
                                        • K Offline
                                          kaas
                                          last edited by

                                          Also have a look at this section in the picture.

                                          My suggestion is: add more puts statements into your code to quickly find the problems (yourself).

                                          Edit: where are you declaring definitionanyway??


                                          code.jpg

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

                                            Sorry about the typo - substitute Dan's .each{ correction.
                                            I have edited the original post to suit !

                                            Also I used the reference ' definition' BUT you then used ' component_definition'
                                            Please try to match such references when you are given 'generic' code...

                                            Why did you not follow my example more closely [except for the stupid typo!] ???

                                            PS: @Kaas is actually wrong it IS = NOT == because
                                            if definition = definitions[...
                                            With the = test you are trying to set the reference ' definition' to be pointing to something 'by name' in definitions - if that fails then you get nil returned, so then the else part of the test runs...
                                            A == test is for 'equality' of two items, and not for testing if setting a reference works...

                                            TIG

                                            1 Reply Last reply Reply Quote 0
                                            • 1
                                            • 2
                                            • 3
                                            • 4
                                            • 2 / 4
                                            • First post
                                              Last post
                                            Buy SketchPlus
                                            Buy SUbD
                                            Buy WrapR
                                            Buy eBook
                                            Buy Modelur
                                            Buy Vertex Tools
                                            Buy SketchCuisine
                                            Buy FormFonts

                                            Advertisement