Hide Table of Contents

Set Material Example (VB.NET)

This example shows how to get the names of the material schema, material databases, and bodies in a part document. This example also shows how to apply a SolidWorks Material to all of the bodies in a  part document.


' Preconditions: Specified document exists.


' Postconditions: The material ABS PC from the

' SolidWorks Material database is applied to all

' bodies in the open part document.


' NOTE: Because the part document is used elsewhere,

' do not save any changes when closing it.


Imports SolidWorks.Interop.sldworks

Imports SolidWorks.Interop.swconst

Imports System

Imports System.Diagnostics


Partial Class SolidWorksMacro


    Public Sub Main()


        Dim swModel As ModelDoc2

        Dim swPart As PartDoc

        Dim swBody As Body2

        Dim errors As Long

        Dim warnings As Long

        Dim vMatDBarr As Object

        Dim vMatDB As Object

        Dim Bodies As Object

        Dim BodyMaterialError As Long

        Dim sMatName As String = ""

        Dim sMatDB As String = ""

        Dim itr As Long

        Dim boolstat as Boolean


        ' Open the document

        swModel = swApp.OpenDoc6("c:\Program Files\SolidWorks Corp\SolidWorks\samples\tutorial\multibody\multi_inter.sldprt", swDocumentTypes_e.swDocPART, swOpenDocOptions_e.swOpenDocOptions_Silent, "", errors, warnings)

        swPart = swModel


        ' Get the material schema and names

        ' of available materials databases

        vMatDBarr = swApp.GetMaterialDatabases

        Debug.Print("Material schema pathname = " & swApp.GetMaterialSchemaPathName)

        For Each vMatDB In vMatDBarr

            Debug.Print("  Material database: " & vMatDB)





        Bodies = swPart.GetBodies2(swBodyType_e.swAllBodies, False)

        For itr = 0 To UBound(Bodies)

            swBody = Bodies(itr)

            ' Get the name of the body



            boolstat = swBody.Select2(False, Nothing)


            ' Set the SolidWorks material for that body

            BodyMaterialError = swBody.SetMaterialProperty("Default", "solidworks materials.sldmat", "ABS PC")

            ' Comment out previous statement and uncomment following statement to use custom material

            'BodyMaterialError = swBody.SetMaterialProperty("Default", "custom materials.sldmat", "Custom Plastic")


            ' Get the names of the body's material and the

            ' database to which it belongs

            sMatName = swBody.GetMaterialPropertyName("", sMatDB)

            If sMatName = "" Then

                Debug.Print("Body " & itr & "'s material name: No material applied")


                Debug.Print("Body " & itr & "'s material name: " & sMatName)

                Debug.Print("Body " & itr & "'s material database: " & sMatDB)

                Debug.Print(" ")

            End If

        Next itr


    End Sub


    ''' <summary>

    ''' The SldWorks swApp variable is pre-assigned for you.

    ''' </summary>

    Public swApp As SldWorks


End Class

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

Subject:   Feedback on Help Topics
Page:   Set Material Example (VB.NET)
*   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:


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

Web Help Content Version: API Help (English only) 2012 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.