sketchucation logo sketchucation
    • 登入
    Oops, your profile's looking a bit empty! To help us tailor your experience, please fill in key details like your SketchUp version, skill level, operating system, and more. Update and save your info on your profile page today!
    🔌 Smart Spline | Fluid way to handle splines for furniture design and complex structures. Download

    Texture from url?

    已排程 已置頂 已鎖定 已移動 Developers' Forum
    39 貼文 10 Posters 6.2k 瀏覽 10 Watching
    正在載入更多貼文
    • 從舊到新
    • 從新到舊
    • 最多點贊
    回覆
    • 在新貼文中回覆
    登入後回覆
    此主題已被刪除。只有擁有主題管理權限的使用者可以查看。
    • J 離線
      Jim
      最後由 編輯

      @tig said:

      A pure Javascript version can't work [?]

      I think it can. There is no save or save as in JavaScript. You can save to disk using JavaScript, but there isn't a cross-platform solution. On Windows, you would use a FileSystem activex object. The same scripting host that rtuns the .vbs can run .js also.

      But, the XHR is available on all (important) browser platforms. So a cross-platform solution is to use a WebDialog, fetch the file using the XHR, then pass it to the ruby plugin for saving to disk.

      This small library is a good example of using a XHR cross-platform:
      http://code.google.com/p/microajax/

      
      if (window.ActiveXObject)
      return new ActiveXObject('Microsoft.XMLHTTP');
      else if (window.XMLHttpRequest)
      return new XMLHttpRequest();
      return false;
      
      

      Hi

      1 條回覆 最後回覆 回覆 引用 0
      • TIGT 離線
        TIG Moderator
        最後由 編輯

        What's this then ?
        <a href="java script&#058;void(0);" onclick="document.execCommand('SaveAs',true,'http://fileden.com/somefolder/some file.mp3');">download</a>
        There's just not a 'Save' version ???

        TIG

        1 條回覆 最後回覆 回覆 引用 0
        • J 離線
          Jim
          最後由 編輯

          I've never heard of "execCommand" before, but I'd be surprised if it were cross-platform. If it existed, we probably wouldn't be having this discussion.

          Hi

          1 條回覆 最後回覆 回覆 引用 0
          • TIGT 離線
            TIG Moderator
            最後由 編輯

            Perhaps a MAC users could confirm...

            TIG

            1 條回覆 最後回覆 回覆 引用 0
            • B 離線
              ben.doherty
              最後由 編輯

              While the JS debate rages, I've been playing with what can be done with ruby (being single-minded/stubborn/lazy)

              I've updated my !loadpaths file to match dan's latest http://forums.sketchucation.com/viewtopic.php?f=180&t=29412&hilit=load+path#p257058
              and when I try a couple of things I get this response:
              require 'net/http' Error: #<LoadError: C:/ruby186/lib/ruby/1.8/net/protocol.rb:21:inrequire': No such file to load -- socket>
              C:/ruby186/lib/ruby/1.8/net/protocol.rb:21
              require 'date/format'
              true`

              Any ideas on why net/http fails, but date/format works?

              I'm sure it will take some clever file management, and refresh cycle control etc, but it'd be cool to be able to specify a url to get a texture from. That'd open up a load of opportunities for data vis etc.

              1 條回覆 最後回覆 回覆 引用 0
              • J 離線
                Jim
                最後由 編輯

                That means it couldn't find the file (socket.so) in the load path.

                For a short-term solution, you could try to copy socket.so to the Plugins folder.

                Hi

                1 條回覆 最後回覆 回覆 引用 0
                • Dan RathbunD 離線
                  Dan Rathbun
                  最後由 編輯

                  @jim said:

                  I've never heard of "execCommand" before, but I'd be surprised if it were cross-platform. If it existed, we probably wouldn't be having this discussion.

                  It's HTML 5 me thinks. And seems Microsoft may have added a bunch of standard CommandIDs without getting them approved by the web community (as usual.) The spec implies browser specific IDs should have a prefix like "IE-", but MS has been doing this 'arm-twisting' for years.

                  See: http://www.w3.org/TR/html5/author/dom.html#execCommand

                  MSDN: http://msdn.microsoft.com/en-us/library/ms536419(v=VS.85).aspx
                  and MS' command IDS: http://msdn.microsoft.com/en-us/library/ms533049(v=VS.85).aspx
                  ~

                  I'm not here much anymore.

                  1 條回覆 最後回覆 回覆 引用 0
                  • B 離線
                    Ben_M
                    最後由 編輯

                    Hello!

                    Has anyone made any progress on a simple, cross platform solution to this in the past few months?

                    Thanks,

                    Ben

                    1 條回覆 最後回覆 回覆 引用 0
                    • V 離線
                      Viskiz
                      最後由 編輯

                      I solved in on Windows platform.
                      In my script installation I included ruby lib files needed for http, so users will have them after installing script.
                      The code followed in script:

                      $LOAD_PATH << File.join(MY_PLUGIN_HOME, "bin", "rblib")
                      Sketchup::require 'uri'
                      Sketchup::require 'net/http'

                      Installer for MAC OS should have different ruby library files I think. I haven't MAC yet, so I can't tell any more. Let me know if you solve it.

                      1 條回覆 最後回覆 回覆 引用 0
                      • J 離線
                        Jim
                        最後由 編輯

                        You may be interested in this plugin effort - downlod files plugin using the cUrl library.

                        Hi

                        1 條回覆 最後回覆 回覆 引用 0
                        • K 離線
                          kaas
                          最後由 編輯

                          Digging up this old thread because I'm trying to do almost the same as the OP. Since the last post in this topic, Ruby has been updated so I'm hoping its more simple now.

                          I have been trying several methods. This code saves the image in the local documents folder so I could pick it up there etc.

                          require 'net/http'
                          Net;;HTTP.start("www.google.nl") do |http|
                              resp = http.get("/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png")
                              open("googlelogo_color_272x92dp.png", "wb") do |file|
                                  file.write(resp.body)
                              end
                          end
                          puts "Done"
                          

                          Problem is, my images need a different type of url. Something like:
                          https://url/coordinates/?key=1234abc&index=0
                          This sort of link doesn't work in the code above.

                          Another method I'm trying is loading the image in a webdialog and then grabbing it from Ruby.

                          The test html code (the url could easily replaced with the actual https code) is:

                          <!DOCTYPE html>
                          <html><head></head><body>
                          <img id ="webimage" src = "https://www.google.nl/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png">
                          </body></html>
                          

                          And to grab the image in ruby:
                          img = @wd.get_element_value('webimage')

                          Of course this doesn't work because the image isn't a value. I'm stuck.

                          Anyone has a suggestion? That would be very welcome.

                          1 條回覆 最後回覆 回覆 引用 0
                          • TIGT 離線
                            TIG Moderator
                            最後由 編輯

                            Something like this should work:

                            require('open-uri')
                            fail = false
                            url = "http://www.google.nl/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png"
                            file = File.join(Dir.pwd, File.basename(url))
                            begin
                            	download = open(url)
                            rescue Exception => error
                            	puts "#{error}"
                            	fail = true
                            end
                            unless fail
                            	begin
                            		File.delete(file) ### in case it exists
                            	rescue
                            		###
                            	end
                            	begin
                            		IO.copy_stream(download, file)
                            	rescue Exception => error
                            		puts "#{error}"
                            	end
                            end
                            
                            

                            It should work with a 'php' formatted URL, but remember that then you need to parse out the file name another way. Perhaps [assuming 'key=' gives the file name] split the URL at '?', then take [1], then split at '&', then find the array element starting "key=", then split that at '=' and then [1] is the file name given by 'key=' ?
                            I also saved the downloaded image file in the 'current directory', but you can of course choose any other folder you want...

                            TIG

                            1 條回覆 最後回覆 回覆 引用 0
                            • K 離線
                              kaas
                              最後由 編輯

                              Thanks for the reply TIG. Your code does download a file but when trying to open it I get:
                              Decode Error! Invalid or unsupported PNG file

                              Edit:
                              I was also trying with net/http. It does work with simple files but the actual https request still fails.

                              1 條回覆 最後回覆 回覆 引用 0
                              • K 離線
                                kaas
                                最後由 編輯

                                In case someone runs into the same question. It seems to work now.

                                require 'net/http'
                                require 'tempfile'
                                
                                uri = URI('https://www.google.nl/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png')
                                
                                model = Sketchup.active_model
                                materials=model.materials
                                m = materials.add "Test Color"
                                loc = ''
                                
                                begin
                                	request = Net;;HTTP;;Get.new uri
                                	Net;;HTTP.start(uri.host, uri.port,
                                  ;use_ssl => uri.scheme == 'https') do |http|
                                	  http.request(request)
                                	  response = http.request request # Net;;HTTPResponse object
                                		file = Tempfile.new(['foo','.png'], Dir.tmpdir, 'wb+')
                                	  begin
                                			file.binmode
                                			file.write(response.body)
                                			file.flush
                                			loc = file.path.gsub("/", '\\\\\\')
                                	  ensure
                                			file.close
                                	  end
                                	end
                                rescue Exception => e  
                                  puts e.message 
                                end
                                
                                m.texture = loc
                                m.texture.size = [m.texture.image_width.to_m, m.texture.image_height.to_m]
                                
                                1 條回覆 最後回覆 回覆 引用 0
                                • D 離線
                                  driven
                                  最後由 編輯

                                  for completeness you need a conditional for osx

                                  
                                   loc = file.path if Sketchup.platform == ;platform_osx 
                                  

                                  john

                                  learn from the mistakes of others, you may not live long enough to make them all yourself...

                                  1 條回覆 最後回覆 回覆 引用 0
                                  • Dan RathbunD 離線
                                    Dan Rathbun
                                    最後由 編輯

                                    When a image is loaded as a web page resource, it is present in the "Temporary Internet files" folder. But some security settings might prevent the copying of a file from there to a trusted local folder.

                                    Anyway:
                                    require "FileUtils"

                                    then try the copy method.

                                    See: http://ruby-doc.org/stdlib-2.0.0/libdoc/fileutils/rdoc/index.html

                                    I'm not here much anymore.

                                    1 條回覆 最後回覆 回覆 引用 0
                                    • K 離線
                                      kaas
                                      最後由 編輯

                                      John and Dan, good suggestions. Thanks

                                      1 條回覆 最後回覆 回覆 引用 0
                                      • thomthomT 離線
                                        thomthom
                                        最後由 編輯

                                        When you say different type of URL, what do you mean? HTTPS?

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

                                        1 條回覆 最後回覆 回覆 引用 0
                                        • K 離線
                                          kaas
                                          最後由 編輯

                                          @thomthom said:

                                          When you say different type of URL, what do you mean? HTTPS?

                                          Yes, something like https://url/coordinates/?key=1234abc&index=0

                                          1 條回覆 最後回覆 回覆 引用 0
                                          • thomthomT 離線
                                            thomthom
                                            最後由 編輯

                                            For HTTPS connections you need to enable SSL when you create the HTTP connection.
                                            Note that there's a bug in Ruby that shipped with SU2014-2016 that might cause problems under Windows where the shipped SSL certificates might not work.

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

                                            1 條回覆 最後回覆 回覆 引用 0
                                            • 1
                                            • 2
                                            • 2 / 2
                                            • 第一個貼文
                                              最後的貼文
                                            Buy SketchPlus
                                            Buy SUbD
                                            Buy WrapR
                                            Buy eBook
                                            Buy Modelur
                                            Buy Vertex Tools
                                            Buy SketchCuisine
                                            Buy FormFonts

                                            Advertisement