Hello
In order to modify the lights and scene on old parts, I remove the existing directional lights and create them via macro.
For the dataset no worries, except for the check mark " Keep the light when the scene changes ".
I found this function:
https://help.solidworks.com/2020/English/api/sldworksapi/SolidWorks.Interop.sldworks~SolidWorks.Interop.sldworks.IModelDocExtension~SetKeepLightInRenderScene.html?verRedirect=1
But I can't get it to work in my code.
A little idea?
Option Explicit
'https://help.solidworks.com/2019/english/api/sldworksapi/SolidWorks.Interop.sldworks~SolidWorks.Interop.sldworks.IModelDoc2~GetLightSourceIdFromName.html
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swConfig As SldWorks.Configuration
Dim Scene As SldWorks.SWScene
Dim boolstatus As Boolean
Dim swPoint As SldWorks.MathPoint
Dim swVector As SldWorks.MathVector
Dim point As Variant
Dim vect As Variant
Sub main()
Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
If swModel Is Nothing Then Exit Sub
'On modifie les lumières
ModifiyLightSourceProps swModel
Dim réponse As Integer
réponse = MsgBox("Voulez-vous modifier la scène?", vbQuestion + vbYesNo)
If réponse = vbYes Then
'On modifie la scène
ModifiyScene swModel
End If
boolstatus = swModel.EditRebuild3()
End Sub
Private Sub ModifiyLightSourceProps(swModel As SldWorks.ModelDoc2)
Dim i As Integer
For i = 0 To swModel.GetLightSourceCount - 1
'Debug.Print "Light type:" & swModel.GetLightSourceName(i) & " - i=" & i
If InStr(swModel.GetLightSourceName(i), "Ambient") > 0 Then
boolstatus = swModel.SetLightSourcePropertyValuesVB(swModel.GetLightSourceName(i), 1, 1, 16777215, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.29, 0, 0, False)
End If
Next i
'Supprime toutes les lumières directionnelles
Do While swModel.GetLightSourceCount > 1
swModel.DeleteLightSource 1
Loop
'Création lumière directionnelle1
boolstatus = swModel.AddLightSource("Directionnelle1", 4, "Directionnelle1")
boolstatus = swModel.SetLightSourcePropertyValuesVB(swModel.GetLightSourceName(i), 4, 0.45, 16777215, 1, -2.5, 2.5, 7.5, 0, 0, 0, 0, 0, 0, 0, 0.2, 0.69, 0, 0)
boolstatus = swModel.LockLightToModel(1, False)
'Code en echec (3 lignes)
Dim swModelDocExt As SldWorks.ModelDocExtension
Set swModelDocExt = swModel.Extension
boolstatus = swModelDocExt.SetKeepLightInRenderScene(1, True)
'boolstatus = swModelDoc.SetKeepLightInRenderScene(1, True)
'swModelDoc.SetKeepLightInRenderScene(1, True)
'Fin des essai de code
'Création lumière directionnelle2
boolstatus = swModel.AddLightSource("Directionnelle2", 4, "Directionnelle2")
boolstatus = swModel.SetLightSourcePropertyValuesVB(swModel.GetLightSourceName(i), 4, 0.88, 12615808, 1, 1.70132, -0.270355, -0.180017, 0, 0, 0, 0, 0, 0, 0, 0.13, 0.85, 0, 0)
boolstatus = swModel.LockLightToModel(2, False)
boolstatus = swModel.SetKeepLightInRenderScene(2, True)
End Sub
Private Sub ModifiyScene(swModel As SldWorks.ModelDoc2)
Set swConfig = swModel.GetActiveConfiguration
Set Scene = swConfig.GetScene
Dim P2SFilename As String
Scene.GetP2SFileName P2SFilename
Debug.Print "Scene file: " & P2SFilename
Debug.Print "Scene background top gradient color: " & Scene.BackgroundTopGradientColor
Debug.Print "Scene background bottom gradient color: " & Scene.BackgroundBottomGradientColor
Scene.GetFloorNormal swPoint, swVector
point = swPoint.ArrayData
Debug.Print "Scene floor normal point: " & point(0) & ", " & point(1) & ", " & point(2)
vect = swVector.ArrayData
Debug.Print "Scene floor normal vector: " & vect(0) & ", " & vect(1) & ", " & vect(2)
'Arrière plan changer le Type (Aucun, couleur, Image, Environnement)
Scene.BackgroundType = swSceneBackgroundType_e.swBackgroundType_Image
Debug.Print "Type of scene background as defined in swSceneBackgroundType_e: " & Scene.BackgroundType
'Arrière plan chemin de l'image
Scene.BackgroundImage = "C:\Program Files\SOLIDWORKS Corp\SOLIDWORKS\data\images\textures\background\softbox.png"
Debug.Print "Scene background environment image file: " & Scene.BackgroundEnvImage
Scene.BackgroundEnvImage = "C:\Program Files\SOLIDWORKS Corp\SOLIDWORKS\data\images\textures\background\3 point beige.hdr"
Debug.Print "Scene background image file: " & Scene.BackgroundImage
Debug.Print "Scene environment rotation: " & Scene.EnvironmentRotation
Scene.FitToSWWindow = True
Debug.Print "Stretch to fit in SOLIDWORKS window? " & Scene.FitToSWWindow
Debug.Print "Scale the scene floor uniformly? " & Scene.FixedAspectRatio
Debug.Print "Flip the scene floor direction? " & Scene.FloorDirection
Debug.Print "Automatically resize the scene floor based on the model bounding box? " & Scene.FloorAutoSize
Debug.Print "Distance between scene floor and model: " & Scene.FloorOffset
Debug.Print "Flip the scene floor offset direction? " & Scene.FloorOffsetDirection
Scene.FloorReflections = True
Debug.Print "Show model reflections on the scene floor? " & Scene.FloorReflections
Debug.Print "Scene floor rotation: " & Scene.FloorRotation
Debug.Print "Show model shadows on the scene floor? " & Scene.FloorShadows
Debug.Print "Keep the scene background when changing the scene? " & Scene.KeepBackground
Scene.FlattenFloor = True
Debug.Print "Flatten the scene floor of the spherical environment? " & Scene.FlattenFloor
Debug.Print "Horizon height: " & Scene.HorizonHeight
Debug.Print "Environment size: " & Scene.EnvironmentSize
End Sub