Hide Table of Contents

Get Surface-finish Image Path and File Name Example (C#)

This example shows how to set and get the path and file name of the image used for an appearance's surface finish.

//----------------------------------------------------------------
// Preconditions:
// 1. Verify that the specified model document exists.
// 2. Verify that the specified materials and surface-finish image
//    files exist.
// 3. Open the Immediate window.
//
// Postconditions:
// 1. Opens the specified model document.
// 2. Applies the specified appearance and surface finish to the model.
// 3. Gets the file names of the first appearance and surface finish
//    applied to the model.
// 4. Examine the graphics area and the Immediate window.
//
// NOTE: Because the model is used elsewhere, do not save changes.
//----------------------------------------------------------------
using SolidWorks.Interop.sldworks;
using SolidWorks.Interop.swconst;
using System.Runtime.InteropServices;
using System;
using System.Diagnostics;
 
namespace FileNameCSharp.csproj
{
    public partial class SolidWorksMacro
    {
 
        public void Main()
        {
            ModelDoc2 swModel = default(ModelDoc2);
            ModelDocExtension swModelDocExt = default(ModelDocExtension);
            SelectionMgr swSelectionMgr = default(SelectionMgr);
            Component2 swComponent = default(Component2);
            Configuration swConfiguration = default(Configuration);
            RenderMaterial swRenderMaterial = default(RenderMaterial);
            int nbrRenderMaterials = 0;
            string fileName = null;
            int warnings = 0;
            int errors = 0;
            bool status = false;
            string materialName = null;
            string[] displayStateNames = null;
            int materialID1 = 0;
            int materialID2 = 0;
 
            //Open model document
            fileName = "C:\\Program Files\\SolidWorks Corp\\SolidWorks\\samples\\tutorial\\tolanalyst\\offset\\bushing.sldprt";
            swModel = (ModelDoc2)swApp.OpenDoc6(fileName, (int)swDocumentTypes_e.swDocPART, (int)swOpenDocOptions_e.swOpenDocOptions_Silent, ""ref errors, ref warnings);
 
            //Create material for the appearance
            materialName = "C:\\Program Files\\SolidWorks Corp\\SolidWorks\\data\\graphics\\Materials\\metal\\iron\\sandblasted iron.p2m";
            swModelDocExt = (ModelDocExtension)swModel.Extension;
            swRenderMaterial = (RenderMaterial)swModelDocExt.CreateRenderMaterial(materialName);
 
            //Verify that RealView Graphics is set
            Debug.Print("RealView Graphics set? " + swModelDocExt.ViewDisplayRealView);
 
            //Add an appearance
            status = swModelDocExt.SelectByID2("bushing.SLDPRT""COMPONENT", 0, 0, 0, false, 0, null, 0);
            swSelectionMgr = (SelectionMgr)swModel.SelectionManager;
            swComponent = (Component2)swSelectionMgr.GetSelectedObject6(1, -1);
            status = swRenderMaterial.AddEntity(swComponent);
            Debug.Print("Appearance added to model? " + status);
 
            //Add surface finish
            fileName = "C:\\Program Files\\SolidWorks Corp\\SolidWorks\\data\\Images\\textures\\metal\\cast\\cast_bump.jpg";
            swRenderMaterial.BumpTextureFilename = fileName;
 
            swModel.ClearSelection2(true);
 
            //Get the names of display states
            //Add the appearance to all display states
            swConfiguration = (Configuration)swModel.GetActiveConfiguration();
            displayStateNames = (string[])swConfiguration.GetDisplayStates();
            status = swModelDocExt.AddDisplayStateSpecificRenderMaterial(swRenderMaterial, (int)swDisplayStateOpts_e.swAllDisplayState, displayStateNames, out materialID1, out materialID2);
 
            //Get the number of appearances
            nbrRenderMaterials = swModelDocExt.GetRenderMaterialsCount2((int)swDisplayStateOpts_e.swAllDisplayState, displayStateNames);
 
            //If one or more appearances are applied to the model,
            //then get the file names of the first appearance and surface finish applied
            if (nbrRenderMaterials > 0)
            {
                Debug.Print("First appearance's file name: " + swRenderMaterial.FileName);
                Debug.Print("  Surface finish's file name: " + swRenderMaterial.BumpTextureFilename);
                Debug.Print("Fixed aspect ratio? " + swRenderMaterial.FixedAspectRatio);
                Debug.Print( " Width: " + swRenderMaterial.Width * 1000);
                Debug.Print( " Height: " + swRenderMaterial.Height * 1000);
            }
            else
            {
                Debug.Print("No appearances applied to this model.");
            }
 
        }
 
        /// <summary>
        ///  The SldWorks swApp variable is pre-assigned for you.
        /// </summary>
        public SldWorks swApp;
    }
}


Provide feedback on this topic

SOLIDWORKS welcomes your feedback concerning the presentation, accuracy, and thoroughness of the documentation. Use the form below to send your comments and suggestions about this topic directly to our documentation team. The documentation team cannot answer technical support questions. Click here for information about technical support.

* Required

 
*Email:  
Subject:   Feedback on Help Topics
Page:   Get Surface-finish Image Path and File Name Example (C#)
*Comment:  
*   I acknowledge I have read and I hereby accept the privacy policy under which my Personal Data will be used by Dassault Systèmes

Print Topic

Select the scope of content to print:

x

We have detected you are using a browser version older than Internet Explorer 7. For optimized display, we suggest upgrading your browser to Internet Explorer 7 or newer.

 Never show this message again
x

Web Help Content Version: API Help (English only) 2015 SP05

To disable Web help from within SOLIDWORKS and use local help instead, click Help > Use SOLIDWORKS Web Help.

To report problems encountered with the Web help interface and search, contact your local support representative. To provide feedback on individual help topics, use the “Feedback on this topic” link on the individual topic page.