Max Kluge

How to properly Apply Texture on a surface using a C# API

Discussion created by Max Kluge on Dec 14, 2020

Basicly, what i'm trying to do is, to select an Surface in SolidWorks, click a Button in a C# WPF .net Application and set a Texture on that selected face. I used this SolidWorks example as a Template (2017 SOLIDWORKS API Help - Change Texture on Face in Specified Configuration Example (C#)). Below is the Method, that runs without errors.
The Application gets the Face (if no face is selectet -> error), but it does not apply any texture. 
I tried to run the code line by line (F11) and the Bold text is where it supposed to apply the texture.
Does anyone know why it doesnt work?
Maybe SolidWorks denies the access for external files?


Using SolidWorks 2019 and VisualStudios 2019

 

 

################## START CODE #####################

 

private void swTexture()
        {
            //var            
            ModelDoc2 swModel;
            SelectionMgr swSelMgr;
            SldWorks.ModelDocExtension swModelDocExt;
            SldWorks.Face2 face;
            SldWorks.Texture texture;
            bool boolstatus;           
            string configName;                    
            string texturePath = null;
            double Scal;
            double Angl;

 

            //selected Texture (ComboBox)
            if (TextureList.SelectedItem.ToString() == "Halbkugel")
            {
                texturePath = "G:\\Uni\\00_Diplom\\89_Texturen\test\\Halbkugel_Textur.bmp";
            }

 

            else if (TextureList.SelectedItem.ToString() == "Rippe")
            {
                texturePath = @"G:\Uni\00_Diplom\89_Texturen\test\Rippe_Textur.bmp";
            }

 

            else if (TextureList.SelectedItem.ToString() == "Spike sechseckig")
            {
                texturePath = @"G:\Uni\00_Diplom\89_Texturen\test\Spike_sechseckig_Textur.bmp";
            }
            else
            {
                Console.WriteLine("Bitte Oberflächenstruktur auswählen");
            }

 

            //Scaling
            if (string.IsNullOrWhiteSpace(txtScal.Text))
            {
                txtScal.Text = Convert.ToString(1);
            }
            Scal  = double.Parse(txtScal.Text);

 

            //Angle
            if (string.IsNullOrWhiteSpace(txtAngle.Text))
            {
                txtScal.Text = Convert.ToString(0);
            }
            Angl = double.Parse(txtAngle.Text);

 


            swModel = (ModelDoc2)swApp.ActiveDoc;
            swSelMgr = (SelectionMgr)swModel.SelectionManager;
            swModelDocExt = swModel.Extension;

 

            configName = "Default";

 

            face = (SldWorks.Face2)swSelMgr.GetSelectedObject6(1, -1);

 

            //Get existing texture on this face
            texture = face.GetTexture(configName);

 

           

 

            if ((texture != null))
            {
                Debug.Print("Texture before:");
                Debug.Print("Material: " + texture.MaterialName);
                Debug.Print("Granularity: " + texture.ScaleFactor);
                Debug.Print("Angle of rotation: " + texture.Angle);

 

                //Change texture on this face
                texture.Angle = Angl;
                texture.ScaleFactor = Scal;
                texture.MaterialName = texturePath;
                boolstatus = face.SetTexture(configName, texture);

 

                Debug.Print("");
                Debug.Print("Texture after:");
                Debug.Print("Material: " + texture.MaterialName);
                Debug.Print("Granularity: " + texture.ScaleFactor);
                Debug.Print("Angle of rotation: " + texture.Angle);

 

            }
            else
            {
                //If no texture exists on this face, then apply a texture to it        
                

 

               
                texture = swModelDocExt.CreateTexture(texturePath, Scal, Angl, false);  // CreateTexture(Path, Scaling, Angle, bool);
                boolstatus = face.SetTexture(configName, texture);

 

                Debug.Print("New texture:");
                Debug.Print("Material: " + texture.MaterialName);
                Debug.Print("Granularity: " + texture.ScaleFactor);
                Debug.Print("Angle of rotation: " + texture.Angle);

 

            }
            Debug.Print(texture.GetSystemTextureName(texture.MaterialName.ToString(), out boolstatus));
        }

 

################## END CODE #####################

Outcomes