AnsweredAssumed Answered

Insert JPG on Part Surface Macro

Question asked by Shawn Leenerts on Aug 26, 2019

I'm trying to create a VBA macro that will insert a JPEG image onto the face of a part and then align it properly through mapping. This is similar to dropping an image onto the part and changing the mapping settings manually...


I have spend a couple of days searching and testing tons of code. Some applied images as decals that I couldn't get to work, like this link: 2016 SOLIDWORKS API Help - Add Decal Example (VBA). It set the Fit width and height to selection like I wanted, but I couldn't get the decal to show on my part.


I found that the example here: 2019 SOLIDWORKS API Help - Change Texture on Face in Specified Configuration Example (VBA) was the closest thing I was looking for. I tweaked the code and got the image onto the face of the part like I wanted, but I cannot find any details on how to check the Fit width and height to selection through code.


Here is what I've got so far. Currently, I need the part open and the surface selected to run this, but it has gotten me closer to what I was looking for.


Option Explicit

Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swSelMgr As SldWorks.SelectionMgr
Dim swModelDocExt As SldWorks.ModelDocExtension
Dim face As SldWorks.Face2
Dim texture As SldWorks.texture
Dim boolstatus As Boolean
Dim namStr As String
Dim configName As String


Sub main()
Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
Set swSelMgr = swModel.SelectionManager
Set swModelDocExt = swModel.Extension

configName = "Default"

Set face = swSelMgr.GetSelectedObject6(1, -1)

'Get existing texture on this face
Set texture = face.GetTexture(configName)

namStr = "\\MASTERSVR2016\Design\CAD Library\3D Design Library\_Solidworks Templates\Automation (Do Not Change)\Generic\DIR\_Testing\DUKE-DIR-042-NL-DF-060H-X.jpg"
Set texture = swModelDocExt.CreateTexture(namStr, 1, 0, False)
boolstatus = face.SetTexture(configName, texture)
End Sub


Any help that you can give will be greatly appreciated! Thank you for your time.