Reset Light Sources

Library for macros
User avatar
Stefan Sterk
Posts: 37
Joined: Tue Aug 10, 2021 2:40 am
Answers: 3
x 51
x 77

Reset Light Sources

Unread post by Stefan Sterk »

Here is a simple macro which gives you the power to easily reset your light sources within a SOLIDWORKS Part or Assembly model by deleting the current light sources and adding back the 'default' (user specified) light sources.

Code: Select all

Option Explicit
Sub main()
 
    Dim swApp As SldWorks.SldWorks
    Dim swModel As SldWorks.ModelDoc2
    Dim swModelDocExt As SldWorks.ModelDocExtension
 
    ' Get SolidWorks Application
    Set swApp = Application.SldWorks
    ' Get active model document
    Set swModel = swApp.ActiveDoc
 
    ' Check for active document and if document is a Assembly or Part
    If swModel Is Nothing Then End
    If swModel.GetType <> swDocASSEMBLY And swModel.GetType <> swDocPART Then End
    Set swModelDocExt = swModel.Extension
 
    ' Delete current lights
    DeleleteLights swModel

    ' Add a directional light source
    SetAmbiantLightSource swModel, True, RGB(255, 255, 255), 0.3
    AddDirectionalLight swModel, "Directional1", True, RGB(255, 255, 255), 0.1, 0.3, 0.3, True, 90, 45
    AddDirectionalLight swModel, "Directional2", True, RGB(255, 255, 255), 0, 0.1, 0.3, True, -154.41, 37.77
    AddDirectionalLight swModel, "Directional3", True, RGB(255, 255, 255), 0, 0.2, 0.3, True, -101.07, 19.94
 
    ' Rebuild model to show light
    swModelDocExt.EditRebuildAll
 
End Sub

Function DeleleteLights(swModel As SldWorks.ModelDoc2)
    Dim i As Integer
    For i = swModel.GetLightSourceCount - 1 To 0 Step -1
        swModel.DeleteLightSource i
    Next i
End Function

Function AddDirectionalLight( _
    swModel As SldWorks.ModelDoc2, _
    LightName As String, _
    OnInSolidWorks As Boolean, _
    Color As Long, _
    Ambient As Double, _
    Brightness As Double, _
    Specularity As Double, _
    LockToModel As Boolean, _
    Longitude As Double, _
    Latitude As Double _
    )
    Dim X As Double, Y As Double, Z As Double
                             
    GetCartesianCoordinates Longitude, Latitude, X, Y, Z
 
    swModel.AddLightSource LightName, 4, LightName
    swModel.SetLightSourcePropertyValuesVB LightName, 4, Brightness, Color, 1, X, Y, Z, 0, 0, 0, 0, 0, 0, 0, Ambient, Specularity, 0, Not OnInSolidWorks
    swModel.LockLightToModel swModel.GetLightSourceCount - 1, LockToModel

End Function

Function GetCartesianCoordinates(ByVal Longitude As Double, ByVal Latitude As Double, ByRef X As Double, ByRef Y As Double, ByRef Z As Double)
    Longitude = (Longitude * (3.14159265359 / 180))
    Latitude = (Latitude * (3.14159265359 / 180))
    X = Cos(Latitude) * Sin(Longitude)
    Z = Cos(Latitude) * Cos(Longitude)
    Y = Sin(Latitude)
End Function

Function SetAmbiantLightSource(swModel As SldWorks.ModelDoc2, OnInSolidWorks As Boolean, Color As Long, Ambient As Double)
    swModel.SetLightSourcePropertyValuesVB swModel.GetLightSourceName(0), 1, 0, Color, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, Ambient, 0, 0, Not OnInSolidWorks
End Function


User avatar
SPerman
Posts: 2055
Joined: Wed Mar 17, 2021 4:24 pm
Answers: 14
x 2225
x 1876
Contact:

Re: Reset Light Sources

Unread post by SPerman »

Thank you. I was just going to start asking about / looking for such a macro. oa
-
I may not have gone where I intended to go, but I think I have ended up where I needed to be. -Douglas Adams
User avatar
loeb
Posts: 68
Joined: Sun Jan 16, 2022 5:55 pm
Answers: 1
x 39
x 10

Re: Reset Light Sources

Unread post by loeb »

I wrote a similar one that deletes all lights and sets ambient to 1. Now all our parts that are the same material look the same.
User avatar
SPerman
Posts: 2055
Joined: Wed Mar 17, 2021 4:24 pm
Answers: 14
x 2225
x 1876
Contact:

Re: Reset Light Sources

Unread post by SPerman »

I find 1 to be a bit too bright, but I am doing the same thing.
-
I may not have gone where I intended to go, but I think I have ended up where I needed to be. -Douglas Adams
Post Reply