Opcja światła kierunkowego (światło sceniczne i kamery)

Witam
Aby zmodyfikować światła i scenę na starych częściach, usuwam istniejące światła kierunkowe i tworzę je za pomocą makro.
W przypadku zestawu danych nie ma obaw, z wyjątkiem znacznika wyboru " Zachowaj światło, gdy scena się zmienia ".


Znalazłem tę funkcję:
https://help.solidworks.com/2020/English/api/sldworksapi/SolidWorks.Interop.sldworks~SolidWorks.Interop.sldworks.IModelDocExtension~SetKeepLightInRenderScene.html?verRedirect=1
Ale nie mogę go zmusić do pracy w moim kodzie.
Mały pomysł?

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

… Nie rozumiem logiki Solidworks w tej kwestii:
Proszę wejść:

Debug.Print "Keep the scene background when changing the scene? " & Scene.KeepBackground

i
instancja. SetKeepLightInRenderScene(ID, Val)

Spróbuj z:
wartość = wystąpienie. GetKeepLightInRenderScene(ID)
, aby sprawdzić zwracaną wartość.
https://help.solidworks.com/2020/english/api/sldworksapi/solidworks.interop.sldworks~solidworks.interop.sldworks.imodeldocextension~getkeeplightinrenderscene.html

Ale tak naprawdę nie widzę związku między " Light ID " a polem wyboru...

1 polubienie

Witam @Maclane

Pole wyboru zostanie zaznaczone tylko dla światła, którego ustawienia zostały otwarte, w tym przypadku " Kierunkowe1 ".

1 polubienie

Witam

Testowałem po swojej stronie, wartość zwracana jest modyfikowana, ale nie jest stosowana.
Albo zły parametr, albo błąd (miałem inne z aplikacją do kolorów, w których musiałem dodać " cofnij "/" ponów ", aby uwzględnić zmiany).
Na światłach nie znalazłem obejścia i nic nie znalazłem na różnych forach (myślę, że niewiele osób bawi się światłami)
Edycja: Z drugiej strony musisz usunąć boolstatus dla zestawu. Musisz napisać w odpowiedni sposób: swModelDocExt.SetKeepLightInRenderScene 1, True

3 polubienia

Testowane również z Solidworks 2022... wartość zdefiniowana, ale również nie przypisana.
Czy może to mieć coś wspólnego z " nową " zasadą Solidworks (2020, jeśli się nie mylę) dotyczącą resetowania świateł za każdym razem, gdy zmieniają się sceny?

Muszę przeszukać moje archiwa, ale muszę mieć makro do przypisywania wartości zdefiniowanych na światłach. (co do @sbadenis to ja go stworzyłem aby "ponownie oświetlić bardzo stare pomieszczenia")

1 polubienie

Powiedzmy, że nagrywając makro, nie podejmuje też działania na " znaczniku wyboru ". Wiem, że generalnie rejestrator jest częściowy, ale inne parametry wziął.

2 polubienia