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

    Download to Sketchup button

    Scheduled Pinned Locked Moved Developers' Forum
    29 Posts 4 Posters 4.4k Views 4 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.
    • Dan RathbunD Offline
      Dan Rathbun
      last edited by

      @unknownuser said:

      Where do I place the code?
      Sketchup Ruby is client side. (It does NOT go on the website.)

      @unknownuser said:

      So that Ruby code sample you gave me seems to be exactly what I need, according to the description.
      Actually I did notice a few errors in that sample code. It could be cleaned up and better implemented.

      @unknownuser said:

      Here's the deal though, I am REALLY Ruby illiterate...
      Well you'll need to take some time and get literate, or run the risk of having your code collide with other plugins, possibly making users angry.
      or..
      Check the script libraries for something similar, that you can tweak and customize for your own use, (which is one way to learn more quickly.)
      or..
      If you don't have the time, you can hire someone experienced, to write it for you, or cleanup your code.

      I'm not here much anymore.

      1 Reply Last reply Reply Quote 0
      • E Offline
        ericschimel
        last edited by

        Well, I think I may hire someone to do this for me, I am just trying to get a "Proof of concept" going so I can test out its functionality, and then pass it on to the real programmers to make perfect.

        I have found plenty of examples of the web dialog being implemented, thats how I was able to pull that off. I haven't been able to find any examples of where someone has opened a web dialog and sent a model into Sketchup from a website.

        You saying that the example is buggy makes me not feel so warm a fuzzy about learning off of that example, I'd rather see one that works.

        -Eric
        http://plugin.sketchthis.net
        Sketchup Kitchen Design Plugin
        Custom Models

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

          @unknownuser said:

          You saying that the example is buggy makes me not feel so warm a fuzzy about learning off of that example, I'd rather see one that works.

          Eric, The 'bugs' are not that bad:

          read this thread, it is an example of one of the 'issues' with the API
          sample (it's not module wrapped.)
          http://groups.google.com/group/sketchupruby/browse_frm/thread/4f22a3a...

          below 'class LoadHandler', add this line:
          attr_reader(:error)

          in 'method onFailure', make this change:
          rename $last_error to @error so the line reads:
          @error = error_message

          now the if statement will work correctly.

          The whole thing (for YOUR plugin, plus any other code) needs to be wrapped in a module SketchTHIS block. You MAY have nested modules and classes inside your Top Level Namespace (SketchTHIS.) If you plan more than one plugin, that works with your site, OR is 'site-branded' each should be (or end up in,) in it's own submodule, beneath the outer namespace SketchTHIS. Read the post at Google Groups to see how to use file spanning. (It means even if you don't know what the next plugin will be named, or how many you may end up with; each will be protected from each other in their own namespace; all will be protected from the rest of the Ruby plugin world.)
          NOTE, that for module and class names, Ruby may not allow the use of a '!' character; it's kind of reserved for method names and a form of the not operator, (as in !=) 'not equal to'.

          **You might have missed the fact that the use of a LoadHandler class object is optional.

          You can always just use (for testing):
          Sketchup.active_model.definitions.load_from_url("myurl/comp.skp")
          within the WebDialog callback method, that executes when the user clicks the "download to model" button. (The filename part of the url string changes of course, depending on the file.)

          The component is added to the InModel Component List, but an instance is NOT added to the actual model. (The user would need to drag&drop into the model, in the normal way of adding a component instance to
          the model.)

          I think that's what you are after?

          I'm not here much anymore.

          1 Reply Last reply Reply Quote 0
          • E Offline
            ericschimel
            last edited by

            I think this is what I am after...

            I am good with the Ruby code, where I am confused is how to implement this on the server side. If I have a hyperlink to a file, what has to happen for Sketchup to insert, rather then download the model?

            -Eric
            http://plugin.sketchthis.net
            Sketchup Kitchen Design Plugin
            Custom Models

            1 Reply Last reply Reply Quote 0
            • J Offline
              Jim
              last edited by

              using load_from_url puts the download in the Component browser. You then need to place an instance in the model using either Model.place_component or Entities.add_instance.

              Hi

              1 Reply Last reply Reply Quote 0
              • E Offline
                ericschimel
                last edited by

                I understand that, I did find those commands in the Ruby docs, my question is this:

                Those commands, from what I understand, need to be generated when the user clicks on a model. How do I make that happen? A standard hyperlink won't do it....

                -Eric
                http://plugin.sketchthis.net
                Sketchup Kitchen Design Plugin
                Custom Models

                1 Reply Last reply Reply Quote 0
                • J Offline
                  Jim
                  last edited by

                  For that to work, your user needs to be viewing your web page in a WebDialog opened from SketchUp.

                  The hyperlink does not href the model, but rather it might look something like the following, although it could be any element not just an anchor.

                  
                  <a href="#" onclick="skp;download_model@modelname.skp">Download Model</a>
                  or
                  <span onclick="skp;download_model@model_url>Download This</span>
                  
                  

                  which calls the WebDialog callback named "download_model" and passes in the filename.

                  # Then in the Ruby plug-in;
                  @dialog.add_action_callback("download_model") { |dlg, args|
                    Sketchup.active_model.definitions.load_from_url(args)
                  }
                  

                  In a nutshell, the html code sends the model name or url to the Ruby plug-in, which then uses load_from_url to download the model.

                  Hi

                  1 Reply Last reply Reply Quote 0
                  • E Offline
                    ericschimel
                    last edited by

                    Aha! That was the thing I was looking for! Now that I know how to send commands down to Sketchup through the web dialog, I can start experimenting!

                    I'll let you know how it works for me...

                    Thanks so much!

                    -Eric
                    http://plugin.sketchthis.net
                    Sketchup Kitchen Design Plugin
                    Custom Models

                    1 Reply Last reply Reply Quote 0
                    • E Offline
                      ericschimel
                      last edited by

                      Ok Jim, I used your code exactly as you posted it. The method I used in my webpage was your "href" method. The only thing I changed was switching "modelname.skp" for "test.skp" which is a Sketchup model of the same name that I have in the same directory as the web page.

                      I am getting a script error whenever I load the web page in Sketchup. The error, near as I can tell, points to the ":" in the command. Why? I have no clue... Perhaps there needs to be some additional code in the HTML of my web page? Right now the only code in it at all is the "href" command.

                      See attached file for the screenshot of the error.

                      By the way, I added the code exactly as you posted it into my Ruby code. Sketchup seems to like it so far, I suspect that the script error I am getting has nothing to do with the Ruby code, as I don't think the Ruby has even been used yet, because of the error in the web page.

                      @jim said:

                      For that to work, your user needs to be viewing your web page in a WebDialog opened from SketchUp.

                      The hyperlink does not href the model, but rather it might look something like the following, although it could be any element not just an anchor.

                      
                      > <a href="#" onclick="skp;download_model@modelname.skp">Download Model</a>
                      > or
                      > <span onclick="skp;download_model@model_url>Download This</span>
                      > 
                      

                      which calls the WebDialog callback named "download_model" and passes in the filename.

                      # Then in the Ruby plug-in;
                      > @dialog.add_action_callback("download_model") { |dlg, args|
                      >   Sketchup.active_model.definitions.load_from_url(args)
                      > }
                      

                      In a nutshell, the html code sends the model name or url to the Ruby plug-in, which then uses load_from_url to download the model.


                      Script error.PNG

                      -Eric
                      http://plugin.sketchthis.net
                      Sketchup Kitchen Design Plugin
                      Custom Models

                      1 Reply Last reply Reply Quote 0
                      • J Offline
                        Jim
                        last edited by

                        Because I made a mistake! 😳

                        
                        onclick="window.location='skp;download_model@modelname.skp'"
                        
                        

                        Hi

                        1 Reply Last reply Reply Quote 0
                        • E Offline
                          ericschimel
                          last edited by

                          Thanks Jim for correcting it... Now I can load the page with no script errors.

                          I am having an issue with the Ruby code I think... I haven't seen any Ruby code with an "@" symbol in front of it, so I tried it with, and without, and I still not getting any results.

                          I checked with the Ruby console, and there seems to be an error with the "add action callback" line. Should it matter where in the Ruby I place the code? and what other code is in there? Currently, all my Ruby does is open a web dialog first, then the "Add action callback line is in there" and then there is the code to place an icon in its own toolbar, and in the "plugins" menu. That's it.

                          See the screenshot for the Ruby console...

                          Once I see this working one time, I should be able to get things moving on my own....


                          ruby console.PNG

                          -Eric
                          http://plugin.sketchthis.net
                          Sketchup Kitchen Design Plugin
                          Custom Models

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

                            @unknownuser said:

                            I am having an issue with the Ruby code I think... I haven't seen any Ruby code with an "@" symbol in front of it, so I tried it with, and without, and I still not getting any results.

                            Well, Jim doesn't realize how much your a spankin'-newbie at Ruby.

                            In Jim's example the '@dialog' refers to YOUR WebDialog instance reference name, whatever it is you chose to name it in the constructor statement (example):
                            @dialog = UI::WebDialog.new( ..*parameters*.. )

                            IF you named it "SketchTHISdlg", then your constructor statement should look something like ( with proper parameters, of course.):
                            SketchTHISdlg = UI::WebDialog.new( ..*parameters*.. )
                            then the block to create the callback method, would be (like):
                            # Then in the SketchTHIS Ruby plug-in: SketchTHISdlg.add_action_callback("download_model") { |dlg, args| Sketchup.active_model.definitions.load_from_url(args) }

                            Both Jim and I are hoping you have wrapped your plugin within a module, and this is why Jim used an instance type reference (' @dialog'); my example uses a module constant (' SketchTHISdlg'); both can work, or you can use a module/class reference that begins with @@, as in ' @@mydialogname'.

                            I'm not here much anymore.

                            1 Reply Last reply Reply Quote 0
                            • E Offline
                              ericschimel
                              last edited by

                              Ok, so if I understand this correctly, the "add action callback" command needs to know what window the commands are coming from. So, when you create a web window, you name it. Naturally, the "add action callback" would need to have the same name as your window. That makes sense to me, if I am reading everything correctly.

                              So, I went ahead and switched the name that Jim gave me as a filler name, and put my dialog name in, and it still doesn't work. In the Ruby console I am getting a "uninitialized constant" error.

                              As far as wrapping stuff up in modules, I think I understand the concept of that, but at this point, I think its way over my head... πŸ˜„

                              Here is the entire ruby script I am using:

                              #-----------------------------------------------------------------------------
                              
                              require 'sketchup.rb'
                              
                              #-----------------------------------------------------------------------------
                              
                              def create_dialog
                              
                              	dlg = UI;;WebDialog.new("SketchThisNET", true, "", 1200, 800, 150, 150, true);
                              	dlg.set_url "file;///C;/Users/Eric/Documents/SketchThis/TEST2.html"
                              	dlg.show
                              
                              
                              	
                              	SketchThisNET.add_action_callback("download_model") { |dlg, args|
                                Sketchup.active_model.definitions.load_from_url(args)
                              }
                              
                              end
                              
                              if( not file_loaded?("SketchThisNET.rb") )
                                  add_separator_to_menu("Plugin")
                                  UI.menu("Plugin").add_item("SketchThis.NET") { create_dialog }
                              
                                toolbar = UI;;Toolbar.new "www.SketchThis.NET"
                                   # This toolbar icon simply displays Hello World on the screen
                                   cmd = UI;;Command.new("www.SketchThis.NET") { 
                                     create_dialog
                                   }
                                   cmd.small_icon = "SketchThisSmallIcon.png"
                                   cmd.large_icon = "SketchThisLargeIcon.png"
                                   cmd.tooltip = "www.SketchThis.NET"
                                   cmd.status_bar_text = "www.SketchThis.NET Toolbar"
                                   cmd.menu_text = "SketchThis"
                                   toolbar = toolbar.add_item cmd
                                   toolbar.show
                              
                              end
                              
                              #-----------------------------------------------------------------------------
                              file_loaded("SketchThisNET.rb")
                              
                              

                              -Eric
                              http://plugin.sketchthis.net
                              Sketchup Kitchen Design Plugin
                              Custom Models

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

                                Eric, on you on a PC or a Mac?

                                I'm not here much anymore.

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

                                  @unknownuser said:

                                  As far as wrapping stuff up in modules, I think I understand the concept of that, but at this point, I think its way over my head...

                                  Ok.. homework assignment time. Your homework is to go and read this tutorial:
                                  Ruby User's Guide

                                  I'm not here much anymore.

                                  1 Reply Last reply Reply Quote 0
                                  • E Offline
                                    ericschimel
                                    last edited by

                                    I am on both the PC and the Mac. I am mostly working on a PC though.

                                    So did I miss something obvious on my current situation, or will I find the answers in the users guide? πŸ˜„

                                    -Eric
                                    http://plugin.sketchthis.net
                                    Sketchup Kitchen Design Plugin
                                    Custom Models

                                    1 Reply Last reply Reply Quote 0
                                    • thomthomT Offline
                                      thomthom
                                      last edited by

                                      
                                      def create_dialog
                                      
                                         dlg = UI;;WebDialog.new("SketchThisNET", true, "", 1200, 800, 150, 150, true);
                                         dlg.set_url "file;///C;/Users/Eric/Documents/SketchThis/TEST2.html"
                                         dlg.show
                                      
                                      
                                         
                                         SketchThisNET.add_action_callback("download_model") { |dlg, args|
                                        Sketchup.active_model.definitions.load_from_url(args)
                                      }
                                      
                                      end
                                      
                                      

                                      When you create your webdialog
                                      dlg = UI::WebDialog.new("SketchThisNET", true, "", 1200, 800, 150, 150, true);

                                      here, the variable dlg is your reference to the webdialog. This is what you must use to refer to the dialog.

                                      Later on when you do SketchThisNET.add_action_callback("download_model")
                                      This is incorrect. It should be dlg.add_action_callback("download_model")

                                      I take it that you tried to refer to the webdialog by using the title you assigned the webdialog window when you created it?

                                      Thomas Thomassen β€” SketchUp Monkey & Coding addict
                                      List of my plugins and link to the CookieWare fund

                                      1 Reply Last reply Reply Quote 0
                                      • thomthomT Offline
                                        thomthom
                                        last edited by

                                        @dan rathbun said:

                                        Well I asked because your path in this statement:
                                        dlg.set_url "file:///C:/Users/Eric/Documents/SketchThis/TEST2.html"
                                        is a mixture of a Mac path and a Windows path !

                                        ? That's a valid URI to a local file.
                                        When I have a file in my Documents folder and open it in Firefox, the path in the location bar is: file:///C:/Users/Thomas/Documents/test.html.

                                        Also, localhost is not required.

                                        Link Preview Image
                                        File URI scheme - Wikipedia

                                        favicon

                                        (en.wikipedia.org)

                                        Thomas Thomassen β€” SketchUp Monkey & Coding addict
                                        List of my plugins and link to the CookieWare fund

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

                                          @thomthom said:

                                          here, the variable dlg is your reference to the webdialog. This is what you must use to refer to the dialog.

                                          True what Thomas says, but dlg is a local reference so when the method ends, Ruby may dispose of the object.

                                          Eric, wrap your code in a module block as I showed you, AND rename all occurances of dlg to @dlg as Jim suggested. Then the object will persist, because it's an instance reference of module SketchTHIS.

                                          The other issue you have is each time the menu or toolbar is used your calling SketchTHIS.create_dialog. You can add a nil test:
                                          Inside the module:

                                          def dlg
                                            (defined? @dlg).nil? ? nil ; @dlg
                                          end
                                          

                                          Then at the bottom of your code, where you define your UI::Command object, change the code between the curly braces from:
                                          create_dialog
                                          to:

                                          if SketchTHIS.dlg.nil?
                                            SketchTHIS.create_dialog
                                          else
                                            SketchTHIS.dlg.show
                                          end
                                          

                                          Then move the menu separator and menu add item statements down below the statements for the toolbar (ie: after your UI::Command is defined,) and change the statement in the menu_add_item curly braces to cmd
                                          That way both toolbar and menu item use the same code in the UI::Command object.

                                          I'm not here much anymore.

                                          1 Reply Last reply Reply Quote 0
                                          • E Offline
                                            ericschimel
                                            last edited by

                                            Ok, so here's what I've got (I think I've made some progress)

                                            I followed Thomthoms suggestions, and of course they didn't work. (I am sure I am 100% at fault!) HOWEVER... I checked with the ruby console, and I found an error for "invalid model url"

                                            ruby console.PNG

                                            I had the model in the same directory as the web page, and I assumed that is where it would be looked for. However, it also occured to me that it might need to go in the same folder as the plugin, so I tried that, and still no dice...

                                            The good news here (I think) is that I've got Sketchup and my web dialog talking to each other!

                                            Here is the link I am using to try to send my model into Sketchup (the one that generates the "invalid model URL error)

                                            <a href="#" onclick="window.location='skp:download_model@test.skp'">Download Model</a>

                                            To Dan... Yes, I did copy the file locations right out of Firefox. Those URL's are going to acutlaly be on my webserver. They do work, why, I am not sure, but they do.

                                            Also, about the module situation, after doing some more reading, I think I am starting to understand the benefit of making this into a module. I am not going to go there just yet because I just need to get this thing working, then I can worry about making it "neater".

                                            As far as putting the plugin in a special directory, I want to do that, I am just trying to tackle one thing at a time. Great suggestions, and I WILL be following them, I am just trying not to let my head explode over what is probably a simple task for you Ruby pros! πŸ˜„

                                            Thanks for all the help so far guys, I am really learning a lot!

                                            -Eric
                                            http://plugin.sketchthis.net
                                            Sketchup Kitchen Design Plugin
                                            Custom Models

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

                                            Advertisement