1 Reply Latest reply on May 6, 2015 4:39 PM by Keith Rice

    [API] How to apply appearances to specific features

    Ross Hepburn

      I'm looking at designing a part of a macro that will enable me to change the appearance of a specific feature in my feature tree. So far i can change the whole appearance with:


      strName = "c:\Program Files\solidworks corp\solidworks\data\graphics\materials\organic\wood\maple\polished maple 2d.p2m"

      Set swAppearance = swModelDocExt.CreateRenderMaterial(strName)

      boolstatus = swAppearance.AddEntity(swModel)

      boolstatus = swComp.AddRenderMaterial(swAppearance, nDecalID)


      This changes the whole of my part, but i wish to only apply the appearance to say "Boss-Extrude18"

      I have tried a few things like SelectbyID2, getcomponentbyname etc but no luck atm.


      Anyone got any help on how I can apply that appearance to a specific feature ?


        • Re: [API] How to apply appearances to specific features
          Keith Rice

          Select a feature in the feature tree and run this code:


          'Written by Keith Rice
          'CADSharp LLC
          Option Explicit
          Const APPEARANCE_PATH As String = _
              "c:\Program Files\solidworks corp\solidworks\data\graphics\materials\organic\wood\maple\polished maple 2d.p2m"
          Sub main()
              Dim swApp As SldWorks.SldWorks
              Dim swModel As SldWorks.ModelDoc2
              Dim swSelMgr As SldWorks.SelectionMgr
              Dim swObj As Object
              Dim swRenderMat As SldWorks.RenderMaterial
              Set swApp = Application.SldWorks
              Set swModel = swApp.ActiveDoc
              Set swSelMgr = swModel.SelectionManager
              Set swObj = swSelMgr.GetSelectedObject6(1, -1)
              If swModel.GetType = swDocPART Then
                  If swObj Is Nothing Then Set swObj = swModel
              ElseIf swModel.GetType = swDocASSEMBLY Then
                  Dim swAssy As SldWorks.AssemblyDoc
                  Dim swComp As SldWorks.Component2
                  Dim lngInfo As Long
                  Set swAssy = swModel
                  If TypeOf swObj Is SldWorks.Face2 Or _
                      TypeOf swObj Is SldWorks.Feature Or _
                      TypeOf swObj Is SldWorks.Body2 Then
                      Set swComp = swSelMgr.GetSelectedObjectsComponent3(1, -1)
                      swComp.Select4 False, Nothing, False
                      swAssy.EditPart2 False, True, lngInfo
                      If lngInfo = -1 Then
                          swApp.SendMsgToUser "Failed to edit component."
                          Exit Sub
                      End If
                  End If
                  Exit Sub
              End If
              Set swRenderMat = swModel.Extension.CreateRenderMaterial(APPEARANCE_PATH)
              If swRenderMat.AddEntity(swObj) = False Then
                  swApp.SendMsgToUser "Failed to add entity."
                  Exit Sub
              End If
              If swModel.Extension.AddDisplayStateSpecificRenderMaterial( _
                  swRenderMat, swAllDisplayState, Empty, Empty, Empty) = False Then
                  swApp.SendMsgToUser "Failed to add appearance."
                  Exit Sub
              End If
              If Not swComp Is Nothing Then
                  swComp.Select4 False, Nothing, False
              End If
          End Sub


          SolidWorks API Tutorials