sketchucation logo sketchucation
    • Login
    ℹ️ GoFundMe | Our friend Gus Robatto needs some help in a challenging time Learn More

    [Code] New SKM class and methods

    Scheduled Pinned Locked Moved Developers' Forum
    2 Posts 1 Posters 1.7k Views
    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.
    • TIGT Offline
      TIG Moderator
      last edited by

      See here for download http://forums.sketchucation.com/viewtopic.php?p=293677#p293677
      ###########################################################################
      TIG (c) 2010
      All Rights Reserved.
      THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
      WARRANTIES,INCLUDING,WITHOUT LIMITATION,THE IMPLIED WARRANTIES OF
      MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
      ###########################################################################
      skm_class.rb
      ###########################################################################
      Needs 'ZipDirectory.jar' and 'UnZipDirectory.jar',
      and 'Thumbnailer.jar' and 'ThumbnailerRGB.jar' in the same sub-folder.
      You will need Java installed on your PC/MAC - it usually is...
      Also needs 'material-save_texture.rb' to export a SKM's texture.
      [Related files are 'material-save_thumbnail.rb' & 'material-export_skm.rb'.]
      Temporary .ini files are written to the ../Plugins/SKMtools/ folder and
      are deleted by the various methods to pass values to the .jar executable.
      Loaded by 'SKMtools.rb' in the ../Plugins/ folder.
      ###########################################################################
      Adds a new class 'SKM' - with methods to import, export, save_texture and
      save_thumbnail from SKM files.
      ###########################################################################

      SKM.import(path_skm)
      It returns an error if the file 'path_skm' doesn't exist or have a
      '.skm' suffix.
      It makes a temporary SKM folder with the extracted SKM files contents
      [overwriting any existing one], e.g.
      '../Materials/My Material.skm'
      makes a temporary SKM folder as
      '../Plugins/SKMtools/Temp/My_Material/'
      [note the SKM name using [A-Za-z0-9_-] characters only]
      The required values are read from the files and a new material is added
      to the model based on these, named.
      e.g. 'My_Material'
      If that material name exists then it increments with a numerical suffix,
      e.g. 'My_Material1'
      The temporary SKM folder/files are deleted [unloaded].
      Returns a new 'Sketchup::Material' if successful, or 'nil'.
      Note that any attribute-dictionaries in the SKM xml file ARE imported
      with the new Material.

      SKM.export(material, folder)
      Takes the given 'material' [as a 'Sketchup::Material' Object] and the
      destination 'folder' to receive the exported SKM file [as a 'String'.]
      It return errors if 'material' or 'folder' do not exist.
      e.g.
      For the 'material'
      'My Material'
      with 'folder'
      '../Materials/'
      it gives the SKM file as
      '../Materials/My_Material.skm'
      [note the SKM name using [A-Za-z0-9_-] characters only]
      It makes a temporary SKM folder with the default SKM files in it called
      '../Plugins/SKMtools/Temp/My_Material/'
      The required values are read from the 'material' and added to these
      files in the temporary SKM folder.
      The temporary SKM folder is compressed into a SKM file in the
      destination 'folder'.
      If the SKM exists in that folder it is overwritten.
      The temporary SKM folder/files are deleted [unloaded] on completion.
      Returns the path to a new 'SKM' file if successful, or 'nil'.
      Any attribute-dictionaries in the 'material' ARE put into the SKM xml.

      SKM.save_texture(path_skm, folder)
      If the file 'path_skm' or 'folder' doesn't exist there's an error.
      It makes a temporary SKM folder with the extracted SKM files
      [overwriting any existing one], e.g.
      '../Materials/My Material.skm'
      makes a temporary folder as
      '../Plugins/SKMtools/Temp/My_Material/'
      [using [A-Za-z0-9_-] characters in the name only]
      If the SKM has no 'texture' file it returns 'nil'.
      Otherwise the 'texture' file - e.g. 'My_Materials_1.jpg' - is moved to
      the destination 'folder'.
      The temporary SKM folder/files are deleted [unloaded].
      Returns the 'path to the texture file, or 'nil' if failed or the SKM
      has ho texture file.

      SKM.save_thumbnail(path_skm, file_png)
      If the 'file' specified by 'file_png' is actually an existing
      'directory' then the thumbnail is extracted into that folder using its
      original name - 'doc_thumbnail.png'.
      If the file 'file_png' is doesn't have a '.png' suffix it is added.
      If the destination file 'file_png' exists it will be overwritten.
      If the file 'path_skm' or the 'parent-folder' for the 'file_png' file
      doesn't exist there's an error.
      It makes a temporary SKM folder with the extracted SKM files
      [overwriting any existing one], e.g.
      '../Materials/My Material.skm'
      makes a temporary folder as
      '../Plugins/SKMtools/Temp/My_Material/'
      [using [A-Za-z0-9_-] characters in the name only]
      If the SKM has no 'thumbnail' file it returns 'nil' - but this is very
      unlikely as every SKM should contain a file called 'doc_thumbnail.png'
      [64x64pxs].
      The 'thumbnail' file
      - e.g. 'My_Materials_thumbnail.jpg' or 'doc_thumbnail.png' -
      is moved to the specified destination 'parent-folder'.
      The temporary SKM folder/files are deleted [unloaded].
      Returns the 'path to the thumbnail .png file, or 'nil' if failed.

      **SKM.extract(path_skm, [folder])
      This extracts all of the data from the SKM file specifed in 'path_skm'.
      The SKM's data is returned as a 'hash' [see below for contents...]
      If the optional 'folder' is specified the extracted SKM data is
      retained in a sub-folder named after the SKM file, e.g. 'MY Mat.skm' >>>
      a new folder called 'My_Mat'. This sub-folder contains all of the SKM's
      files as follows: The SKM's thumbnail called 'doc_thumbnail.png',
      three files containing assorted data relating to the SKM [document.xml,
      documentProperties.xml and references.xml], and if the SKM has a
      'texture' then there is also the 'image-file' is in a sub-folder called
      'ref', e.g. /ref/'MyMat.jpg'. If there is a 'folder' specified then it
      returns additional entries in the 'hash' [see below for contents...]
      The 'data' hash contains the following:
      :path >>> the path to the original SKM file, e.g. 'C:\Temp\My Mat.skm'.
      :name >>> the SKM's name as it will SKM.import(), e.g. 'My_Mat'.
      :color >>> the SKM material's color as an array [r, g, b] - e.g. [225, 0, 0].
      :alpha >>> the SKM material's alpha [transparency/opacity] - e.g. 0.65.
      :texture >>> the SKM material's texture name, e.g. 'Brick666.jpg' or 'nil' if no texture
      : xscale >>> the SKM material's texture's xscale or 'nil' if no texture
      : yscale >>> the SKM material's texture's yscale or 'nil' if no texture
      :avgcolor >>> the SKM material's texture's average-color, e.g. '2837892', or 'nil' if no 'texture'
      :attributes >>> the SKM material's attributes as an array, e.g. [[dict,key,value], [dict,key.value],...] or [] if none.
      The following are all set to 'nil' if the optional 'folder' is not specified
      :extracted_path >>> the path to the extracted SKM 'data', e.g. 'C:\Temp\My_Mat'.
      :extracted_thumbnail >>> the extracted SKM's thumnail path, e.g. 'C:\Temp\My_Mat\doc_thumbnail.png'.
      :extracted_texture >>> the extracted SKM's texture's path, e.g. 'C:\Temp\My_Mat\Brick666.jpg', or 'nil' if no 'texture'.
      So set the SKM data thus: data=SKM.extract('C:\Temp\My Mat.skm', 'C:\Temp')
      Then to find the SKM's 'color' you use color=data[:color] >>> [255, 0, 0]
      or it's 'alpha' you use alpha=data[:alpha] >>> 0.65 etc.

      SKM.save_texture_and_thumbnail(path_skm, folder)
      It returns the 'sub-folder' if successful or 'nil'.
      e.g. SKM.save_texture("C:Temp\My Mat.skm", "C:\Temp\XXX")
      >>> C:\Temp\XXX\My_Mat
      If the 'folder' does not exist there is an error message.
      The 'sub-folder' is named after the SKM [with some characters made
      into '_']. The 'sub-folder' contains the SKM's 'texture file' - e.g.
      'Brick666.jpg' AND the equivalent thumbnail 'doc_thumbnail.png', if the
      SKM has NO texture then there is only the 'doc_thumbnail.png' file in
      the 'sub-folder'.
      It returns the 'sub-folder' - e.g. C:\Temp\XXX\My_Mat**

      ###########################################################################
      Donations:
      By Paypal.com to info @ revitrev.org
      ###########################################################################
      Version:
      1.0 20101129 First issue
      1.1 20101202 Now uses .jar and .ini files.
      1.2 20101210 General update.
      1.3 20101212 SKM.extract and SKM.save_texture_and_thumbnail added.
      ###########################################################################

      TIG

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

        New update here with additional methods... http://forums.sketchucation.com/viewtopic.php?p=293700#p293700

        TIG

        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