Doesn't work: GetAmbientLightProperties or SetAmbientLightProperties

Programming and macros
User avatar
loeb
Posts: 47
Joined: Sun Jan 16, 2022 5:55 pm
Answers: 1
x 35
x 8

Doesn't work: GetAmbientLightProperties or SetAmbientLightProperties

Unread post by loeb »

Does anyone have any VBA examples that use GetAmbientLightProperties or SetAmbientLightProperties. I can't get anything but 0 or FALSE values from GettAmbientLightProperties. I also had to initialize/dim "Colour" to Long (instead of Integer as the API help defines it) to get it to complile.

Thank You
User avatar
josh
Posts: 261
Joined: Thu Mar 11, 2021 1:05 pm
Answers: 11
x 19
x 450

Re: Doesn't work: GetAmbientLightProperties or SetAmbientLightProperties

Unread post by josh »

What are you expecting to get from the function? It returns true or false. Do you understand what ByRef means? You have to pass variables into the function to receive all the values.
User avatar
loeb
Posts: 47
Joined: Sun Jan 16, 2022 5:55 pm
Answers: 1
x 35
x 8

Re: Doesn't work: GetAmbientLightProperties or SetAmbientLightProperties

Unread post by loeb »

josh wrote: Sat Sep 10, 2022 7:00 pm What are you expecting to get from the function? It returns true or false. Do you understand what ByRef means? You have to pass variables into the function to receive all the values.
It must be the byref thingy. Guess I'll have to figure that out. Never rant into that that before.
User avatar
loeb
Posts: 47
Joined: Sun Jan 16, 2022 5:55 pm
Answers: 1
x 35
x 8

Re: Doesn't work: GetAmbientLightProperties or SetAmbientLightProperties

Unread post by loeb »

josh wrote: Sat Sep 10, 2022 7:00 pm What are you expecting to get from the function? It returns true or false. Do you understand what ByRef means? You have to pass variables into the function to receive all the values.
That's how I'm using it and when I inspect the values after calling GetAmbienLight Properties, they are all zero or FALSE. Also, calling SetAmbientLightProperties with non-zero/non-False values has no affects on the properties of the Ambient light.
User avatar
loeb
Posts: 47
Joined: Sun Jan 16, 2022 5:55 pm
Answers: 1
x 35
x 8

Re: Doesn't work: GetAmbientLightProperties or SetAmbientLightProperties

Unread post by loeb »

Here's a sample of code:

Code: Select all

Option Explicit
Sub main()
    Dim swApp                       As SldWorks.SldWorks
    Dim swModel                     As SldWorks.ModelDoc2
    Dim ModelDocExtension           As ModelDocExtension
    Dim BoolStatus                  As Boolean
    Dim i                           As Integer
    
    Dim LightSourceCount            As Integer
    Dim LightSourcename             As String
    Dim LightSourceUserName         As String
    
    Dim Name                        As String
    Dim Ambient                     As Double
    Dim Diffuse                     As Double
    Dim Specular                    As Double
'    Dim Colour                      As Integer
    Dim Colour                      As Long
    Dim Enabled                     As Boolean
    Dim Fixed                       As Boolean
    
    Set swApp = Application.SldWorks
    Set swModel = swApp.ActiveDoc
    
    LightSourceCount = swModel.GetLightSourceCount
    Debug.Print "LightSourceCount: " & LightSourceCount
    
    For i = 0 To LightSourceCount - 1
        LightSourcename = swModel.GetLightSourceName(i)
        LightSourceUserName = swModel.LightSourceUserName(i)
        Debug.Print i & ": " & LightSourcename & " - " & LightSourceUserName
    Next i
    
    BoolStatus = swModel.GetAmbientLightProperties(Name, Ambient, Diffuse, Specular, Colour, Enabled, Fixed)
    Debug.Print "Name: " & Name
    Debug.Print "Ambient: " & Ambient
    Debug.Print "Diffuse: " & Diffuse
    Debug.Print "Specular: " & Specular
    Debug.Print "Colour: " & Colour
    Debug.Print "Enabled: " & Enabled
    Debug.Print "Fixed: " & Fixed
    
End Sub
and the output

Code: Select all

LightSourceCount: 7
0: Ambient - Ambient
1: Direction1 - Directional1
2: SW#2 - Point1
3: SW#3 - Directional2
4: SW#4 - Spot1
5: SW#5 - Point2
6: SW#6 - Spot2
Name: 
Ambient: 0
Diffuse: 0
Specular: 0
Colour: 0
Enabled: False
Fixed: False

User avatar
josh
Posts: 261
Joined: Thu Mar 11, 2021 1:05 pm
Answers: 11
x 19
x 450

Re: Doesn't work: GetAmbientLightProperties or SetAmbientLightProperties

Unread post by josh »

The Name is an input to the function. You have 7 light sources in your model. You have to specify the name of the light source you want. You will need to run GetAmbientLightProperties 7 times, each time with "Name" having the value of one of the names you retrieved using GetLightSourceName.
User avatar
loeb
Posts: 47
Joined: Sun Jan 16, 2022 5:55 pm
Answers: 1
x 35
x 8

Re: Doesn't work: GetAmbientLightProperties or SetAmbientLightProperties

Unread post by loeb »

Josh,

This helps. There is only one Ambient light and this function only works on Ambient lights. Specifying the light name did do the trick, though.
Post Reply