Reset Light Sources

Library for macros
User avatar
Stefan Sterk
Posts: 47
Joined: Tue Aug 10, 2021 2:40 am
Answers: 5
x 73
x 88

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: 2177
Joined: Wed Mar 17, 2021 4:24 pm
Answers: 14
x 2382
x 1990
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: 91
Joined: Sun Jan 16, 2022 5:55 pm
Answers: 1
x 43
x 14

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: 2177
Joined: Wed Mar 17, 2021 4:24 pm
Answers: 14
x 2382
x 1990
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
User avatar
Dwight
Posts: 298
Joined: Thu Mar 18, 2021 7:02 am
Answers: 2
x 2
x 230

Re: Reset Light Sources

Unread post by Dwight »

Stefan

Your Reset Light Sources works well, is just what I need. I tried to add some lines to turn the background to "none", but clearly I don't know what I'm doing. Could you suggess some lines to add, maybe also set the rest of the "scene" basics?

Thanks

Dwight

What doesn't work:

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.5
AddDirectionalLight swModel, "Directional1", True, RGB(255, 255, 255), 0, 0.5, 0.3, False, -20, 30
AddDirectionalLight swModel, "Directional2", True, RGB(255, 255, 255), 0, 0.1, 0.3, False, -180, 45

' Set Background to none
Set Scene.BackgroundType = "none"


' Rebuild model to show light
swModelDocExt.EditRebuildAll

End Sub
User avatar
gupta9665
Posts: 459
Joined: Thu Mar 11, 2021 10:20 am
Answers: 28
Location: India
x 466
x 504

Re: Reset Light Sources

Unread post by gupta9665 »

Replace
Set Scene.BackgroundType = "none"
with
Dim swConfig As SldWorks.Configuration
Set swConfig = swModel.GetActiveConfiguration

Dim Scene As SldWorks.SWScene
Set Scene = swConfig.GetScene

Scene.BackgroundType = 0
Deepak Gupta
SOLIDWORKS Consultant/Blogger
User avatar
Dwight
Posts: 298
Joined: Thu Mar 18, 2021 7:02 am
Answers: 2
x 2
x 230

Re: Reset Light Sources

Unread post by Dwight »

Thanks very much, Deepak, that worked just right.

Dwight
User avatar
Dwight
Posts: 298
Joined: Thu Mar 18, 2021 7:02 am
Answers: 2
x 2
x 230

Re: Reset Light Sources

Unread post by Dwight »

Dwight wrote: Wed Feb 19, 2025 2:02 pm Thanks very much, Deepak, that worked just right.
Actually, not quite. All works except that the . . .

Scene.BackgroundType = 0

. . . does work but doesn't stick. If I change configurations, the background will revert to its previous state. I searched a little, got a clue, and found that . . .

Scene.BackgroundType = swSceneBackgroundType_e.swBackgroundType_None

. . . works and does stick.

Dwight
User avatar
Dwight
Posts: 298
Joined: Thu Mar 18, 2021 7:02 am
Answers: 2
x 2
x 230

Re: Reset Light Sources

Unread post by Dwight »

But . . . you have to do that for each configuration. That seems odd because when you open the scene properties window and edit it there, the setting applies to all the configurations. I didn't know you could even have different scenes for different configurations.

Dwight
User avatar
Stefan Sterk
Posts: 47
Joined: Tue Aug 10, 2021 2:40 am
Answers: 5
x 73
x 88

Re: Reset Light Sources

Unread post by Stefan Sterk »

Dwight wrote: Wed Mar 05, 2025 1:25 pm But . . . you have to do that for each configuration. That seems odd because when you open the scene properties window and edit it there, the setting applies to all the configurations. I didn't know you could even have different scenes for different configurations.

Dwight
Hi Dwight, use this code instead. It will set the Scene background type to none for each configuration.

Code: Select all

    Dim vConfigName As Variant
    For Each vConfigName In swModel.GetConfigurationNames
        swModel.GetConfigurationByName(vConfigName).GetScene.BackgroundType = 0
    Next vConfigName
It's bit strange indeed, because display states can also have different scene settings, but via the API there are no scene option under the display states.
changing the scene via the configuration seems to override all linked display states.
User avatar
Dwight
Posts: 298
Joined: Thu Mar 18, 2021 7:02 am
Answers: 2
x 2
x 230

Re: Reset Light Sources

Unread post by Dwight »

Stefan

Thanks, that does it, works efficiently.

Dwight
Post Reply