Scale Texture

11Jan07

SolidWorks Macro to adjust texture scale to configurations sizes

If you need to adjust the material texture to many different sizes (configurations) of a part, here is a macro for you:

'ScaleTexture : Solidworks macro to scale material texture on each config of current part
'so that it looks the same for different sizes of the part
'Author : Ph. Guglielmetti, e-Systems Switzerland, all rights reserved
'Licence: you may use this macro for free, as long as this header is kept untouched.
'License: no support is provided, except for e-Systems customers
'Licence: it is forbidden to sell this code or publish it elsewhere without our permission
'Revision : PGu 2007/01/11 Written for my colleague Arnaud who needed it for gem stones
Option Explicit
Dim swApp As SldWorks.SldWorks
Dim part As SldWorks.PartDoc
Sub ScaleTexture ()
    Set swApp = Application.SldWorks
    Set part = swApp.ActiveDoc
    ' find texture scale factor for current config
    Dim texture As SldWorks.texture
    Set texture = part.Extension.GetTexture(part.GetActiveConfiguration.Name)
    Dim s As Double: s = texture.ScaleFactor * boxsize
    ' apply it to all configs
    Dim confnames As Variant
    confnames = part.GetConfigurationNames
    Dim conf As Variant
    For Each conf In confnames
        Call part.ShowConfiguration(conf)
        Set texture = part.Extension.GetTexture(conf)
        If Not texture Is Nothing Then
            Dim d As Double: d = boxsize
            texture.ScaleFactor = s / d
        End If
        Debug.Assert part.Extension.SetTexture(conf, texture)
        part.ViewZoomtofit2
        DoEvents
continue:
    Next conf
End Sub
' returns the part's size (diagonal of the bounding box) in current configuration
Function boxsize(Optional body_index As Integer = 0) As Double
    Dim bodies As Variant
    bodies = part.GetBodies(swSolidBody)
    If IsEmpty(bodies) Then Exit Function ' no solid body …
    Dim box As Variant
    box = bodies(body_index).GetBodyBox
    boxsize = Sqr((box(3) - box(0)) * (box(3) - box(0)) + _
                  (box(4) - box(1))* (box(4) - box(1)) + _
                  (box(5) - box(2)) * (box(5) - box(2)))
End Function
Advertisements


No Responses Yet to “Scale Texture”

  1. Leave a Comment

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: