///<summary> This gets called when when the user runs this command.</summary>
        public override IRhinoCommand.result RunCommand(IRhinoCommandContext context)
        {
            // Verify that we have materials to remap textures
              MRhinoMaterialTable materialTable = context.m_doc.m_material_table;
              int materialCount = materialTable.MaterialCount();
              if (0 == materialCount)
              {
            RhUtil.RhinoApp().Print("No material found.\n");
            return IRhinoCommand.result.nothing;
              }

              // Prompt the user for the new texture folder
              FolderBrowserDialog folderBrowser = new System.Windows.Forms.FolderBrowserDialog();
              folderBrowser.Description = "Select new texture folder";
              folderBrowser.RootFolder = System.Environment.SpecialFolder.Desktop;
              DialogResult result = folderBrowser.ShowDialog();
              if (result != DialogResult.OK)
            return IRhinoCommand.result.cancel;

              // The folder to search fo textures
              string searchFolder = folderBrowser.SelectedPath;

              // We will cache the search results (for performance)
              Dictionary<string, string> textureMap = new Dictionary<string, string>();

              // Used for reporting the results of the remap
              int numMaterials = 0;
              int numTextures = 0;
              int numRemapped = 0;

              // Iterate through the material table
              for (int i = 0; i < materialCount; i++)
              {
            // Get a material
            IRhinoMaterial oldMaterial = materialTable[i];

            // Validate...
            if (null == oldMaterial)
              continue;

            // .. and validate...
            if (oldMaterial.IsDeleted() || oldMaterial.IsReference())
              continue;

            numMaterials++;

            // .. and validate.
            int textureCount = oldMaterial.m_textures.Count();
            if (0 == textureCount)
              continue;

            numTextures += textureCount;

            // Copy the material
            OnMaterial newMaterial = new OnMaterial(oldMaterial);
            bool bModify = false;

            for (int j = 0; j < textureCount; j++)
            {
              string texturePath = newMaterial.m_textures[j].m_filename;
              string textureName = Path.GetFileName(texturePath).ToLower();

              // See if the material to remap has already been found
              string mapValue;
              if (textureMap.TryGetValue(textureName, out mapValue))
              {
            // Do the remap
            newMaterial.m_textures[j].m_filename = mapValue;
            numRemapped++;
            bModify = true;
              }
              else
              {
            // The material to remap has not been found (already), so go searching
            string[] searchResults = Directory.GetFiles(searchFolder, textureName, SearchOption.AllDirectories);
            if (null != searchResults && searchResults.Length > 0)
            {
              // Do the remap (use the first one found...)
              newMaterial.m_textures[j].m_filename = searchResults[0];
              // Add the results of the search to the dictionary
              textureMap.Add(textureName, searchResults[0]);
              numRemapped++;
              bModify = true;
            }
            else
            {
              // Oops...
              RhUtil.RhinoApp().Print(string.Format("Unable to remap {0}\n", texturePath));
            }
              }
            }

            // Modify the existing material by replacing with with the new one
            if (bModify)
              materialTable.ModifyMaterial(newMaterial, oldMaterial.m_material_index);
              }

              // Remapping textures requries a full redraw
              if (numRemapped > 0)
            context.m_doc.Regen();

              // Report the results
              if (1 == numMaterials)
            RhUtil.RhinoApp().Print(string.Format("1 material found\n", numMaterials));
              else
            RhUtil.RhinoApp().Print(string.Format("{0} materials found\n", numMaterials));

              if (1 == numTextures)
            RhUtil.RhinoApp().Print(string.Format("1 texture found\n", numTextures));
              else
            RhUtil.RhinoApp().Print(string.Format("{0} textures found\n", numTextures));

              if (1 == numRemapped)
            RhUtil.RhinoApp().Print(string.Format("1 texture remapped\n", numRemapped));
              else
            RhUtil.RhinoApp().Print(string.Format("{0} textures remapped\n", numRemapped));

              return IRhinoCommand.result.success;
        }
        ///<summary> This gets called when when the user runs this command.</summary>
        public override IRhinoCommand.result RunCommand(IRhinoCommandContext context)
        {
            // Verify that we have materials to remap textures
            MRhinoMaterialTable materialTable = context.m_doc.m_material_table;
            int materialCount = materialTable.MaterialCount();

            if (0 == materialCount)
            {
                RhUtil.RhinoApp().Print("No material found.\n");
                return(IRhinoCommand.result.nothing);
            }

            // Prompt the user for the new texture folder
            FolderBrowserDialog folderBrowser = new System.Windows.Forms.FolderBrowserDialog();

            folderBrowser.Description = "Select new texture folder";
            folderBrowser.RootFolder  = System.Environment.SpecialFolder.Desktop;
            DialogResult result = folderBrowser.ShowDialog();

            if (result != DialogResult.OK)
            {
                return(IRhinoCommand.result.cancel);
            }

            // The folder to search fo textures
            string searchFolder = folderBrowser.SelectedPath;

            // We will cache the search results (for performance)
            Dictionary <string, string> textureMap = new Dictionary <string, string>();

            // Used for reporting the results of the remap
            int numMaterials = 0;
            int numTextures  = 0;
            int numRemapped  = 0;

            // Iterate through the material table
            for (int i = 0; i < materialCount; i++)
            {
                // Get a material
                IRhinoMaterial oldMaterial = materialTable[i];

                // Validate...
                if (null == oldMaterial)
                {
                    continue;
                }

                // .. and validate...
                if (oldMaterial.IsDeleted() || oldMaterial.IsReference())
                {
                    continue;
                }

                numMaterials++;

                // .. and validate.
                int textureCount = oldMaterial.m_textures.Count();
                if (0 == textureCount)
                {
                    continue;
                }

                numTextures += textureCount;

                // Copy the material
                OnMaterial newMaterial = new OnMaterial(oldMaterial);
                bool       bModify     = false;

                for (int j = 0; j < textureCount; j++)
                {
                    string texturePath = newMaterial.m_textures[j].m_filename;
                    string textureName = Path.GetFileName(texturePath).ToLower();

                    // See if the material to remap has already been found
                    string mapValue;
                    if (textureMap.TryGetValue(textureName, out mapValue))
                    {
                        // Do the remap
                        newMaterial.m_textures[j].m_filename = mapValue;
                        numRemapped++;
                        bModify = true;
                    }
                    else
                    {
                        // The material to remap has not been found (already), so go searching
                        string[] searchResults = Directory.GetFiles(searchFolder, textureName, SearchOption.AllDirectories);
                        if (null != searchResults && searchResults.Length > 0)
                        {
                            // Do the remap (use the first one found...)
                            newMaterial.m_textures[j].m_filename = searchResults[0];
                            // Add the results of the search to the dictionary
                            textureMap.Add(textureName, searchResults[0]);
                            numRemapped++;
                            bModify = true;
                        }
                        else
                        {
                            // Oops...
                            RhUtil.RhinoApp().Print(string.Format("Unable to remap {0}\n", texturePath));
                        }
                    }
                }

                // Modify the existing material by replacing with with the new one
                if (bModify)
                {
                    materialTable.ModifyMaterial(newMaterial, oldMaterial.m_material_index);
                }
            }

            // Remapping textures requries a full redraw
            if (numRemapped > 0)
            {
                context.m_doc.Regen();
            }

            // Report the results
            if (1 == numMaterials)
            {
                RhUtil.RhinoApp().Print(string.Format("1 material found\n", numMaterials));
            }
            else
            {
                RhUtil.RhinoApp().Print(string.Format("{0} materials found\n", numMaterials));
            }

            if (1 == numTextures)
            {
                RhUtil.RhinoApp().Print(string.Format("1 texture found\n", numTextures));
            }
            else
            {
                RhUtil.RhinoApp().Print(string.Format("{0} textures found\n", numTextures));
            }

            if (1 == numRemapped)
            {
                RhUtil.RhinoApp().Print(string.Format("1 texture remapped\n", numRemapped));
            }
            else
            {
                RhUtil.RhinoApp().Print(string.Format("{0} textures remapped\n", numRemapped));
            }

            return(IRhinoCommand.result.success);
        }
Esempio n. 3
0
 //public override bool EnableAssignMaterialButton() { return true; }
 public override bool EnableEditMaterialButton(ref OnMaterial material)
 {
     return true;
 }
Esempio n. 4
0
 //public override bool EnableCreateMaterialButton() { return true; }
 /*public override bool OnAssignMaterial(ref System.Windows.Forms.IWin32Window hwndParent, OnMaterial material)
 {
     return true;
 }*/
 /* public override bool OnAssignMaterial(System.Windows.Forms.IWin32Window hwndParent, ref OnMaterial material)
 {
     ymateditor mateditor = new ymateditor();
     mateditor.ShowDialog(hwndParent);
     //return IRhinoCommand.result.success;
     //material.m_plugin_id
     return true;
     //return base.OnAssignMaterial(hwndParent, ref material);
 }*/
 public override bool OnEditMaterial(System.Windows.Forms.IWin32Window hwndParent, ref OnMaterial rhinoMaterial)
 {
     ymateditor mateditor = new ymateditor(ref rhinoMaterial, ref this.materials);
     mateditor.ShowDialog(hwndParent);
     return true;
 }
Esempio n. 5
0
 public ymateditor(ref OnMaterial material, ref MaterialManager materials)
 {
     this.rhinoMaterial = material;
     this.materials = materials;
     InitializeComponent();
 }