Real Time Rendering

Switching on and off All the Lights' Shadows of a Product


This macro shows you how to switch off the shadows of a the current document, and how to switch them on again.

 

switch_LightsShadows is launched in CATIA. An existing document called "TestLight.CATProduct" must be found in the CATDocView. It needs "Back.CATPart" and "Hole.CATPart".

switch_LightsShadows.CATScript is located in the CAAScdRscUseCases module. Execute macro (windows only).

 

switch_LightsShadows includes three steps:

  1. Prolog
  2. Switching off Shadows
  3. Switching on Shadows

Prolog

	' Get the documents collection
	Dim oCollection As Documents
	Set oCollection = CATIA.Documents

	' test if no document is open
	If 0=oCollection.Count Then 
		msgbox "A product document must be active to execute this macro.", vbOKOnly, "Switch On Lights"
		Exit Sub
	End If

	 ' Get material library
	Dim oProductDocument As Document
	Set oProductDocument = CATIA.ActiveDocument

	' test if the active document is a material library (CATMaterial)
	If 0=InStr(oProductDocument.Name, ".CATProduct") Then
		msgbox "A product document must be active to execute this macro.", vbOKOnly, "Switch Off Shadows"
		Exit Sub
	End If

	' Accessing the Root Product
	Dim oRootProduct As Document
	Set oRootProduct = oProductDocument.Product

	' Accessing the collection of rendering lights
	Dim oLights As RenderingLights
	Set oLights = oRootProduct.GetItem("CATRscRenderingLightVBExt")

	' Declarations
	Dim I As Int
	Dim oLight As RenderingLight
	Dim oTab(3) As CATSafeArrayVariant

	' Create the parameter
	Dim oParams As Parameters
	Dim oReadParam As Parameter
	Dim oParam As Parameter
	Dim sParamValue As String
	Set oParams = oProductDocument.Product.Parameters

	On Error Resume Next
	Set oParam = oParams.Item("LightsShadowsStatus")
	...

At first, this macro test if a document is active in CATIA and if this document is a CATProduct. Then it access to the valid root document and to the collection of rendering lights. After that the macro tests if the knowledge parameter exists. This parameter is used in order to remember which shadow was active.

[Top]

Switching off Shadows

	...
	If Err <> 0 Then ''''''''' switch OFF '''''''''

		' Lights loop
		For I=1 To oLights.Count
			Set oLight = oLights.Item(I)

			' Create sParamValue
			If I>1 Then
				sParamValue = sParamValue & "&"
			End If
			sParamValue = sParamValue & oLight.Name & "="
			
			If oLight.HardwareShadowStatus = 1 Then
				oLight.HardwareShadowStatus = 0
				sParamValue = sParamValue & "1"
			Else
				sParamValue = sParamValue & "0"
			End If

			sParamValue = sParamValue & "/"

			If oLight.ShadowObjectStatus = 1 Then
				oLight.ShadowObjectStatus = 0
				sParamValue = sParamValue & "1"
			Else
				sParamValue = sParamValue & "0"
			End If
		Next

		' Create the parameter
		oParams.CreateString "LightsShadowsStatus", sParamValue
		oParams.Item("LightsShadowsStatus").Hidden = True
		...

The macro deactivates lights' shadows on environments and on objects. And it creates the string to be saved by the parameter.

[Top]

Switching on Shadows

	...
	Else ''''''''' switch ON '''''''''

		' Parse the parameter value
		Dim aLights As Array
		Dim aLight As Array
		sParamValue = oParam.ValueAsString ' read the parameter value
		aLights = Split(sParamValue, "&")

		' Lights loop
		For I=0 To UBound(aLights)

			aLight = Split(aLights(I), "=")
			Err.Clear
			Set oLight = oLights.Item(aLight(0))
			If Err <> 0 Then ' light not exist
				msgbox "Impossible to find the light '" & aLight(0) & "'", vbOKOnly, "Switch On Lights' Shadows"
			Else
				If oLight.Type = 3 Then ' 1=directional light
					oLight.HardwareShadowStatus = Mid(aLight(1), 1, 1)
				End If
				If oLight.Type = 1 Then ' 1=spot light
					oLight.ShadowObjectStatus = Mid(aLight(1), 3, 1)
				End If
			End If
		Next

		' Remove parameter
		oParams.Remove "LightsShadowsStatus"

	End If
	...

The macro reactivates lights' shadows on environments and on objects. And it destroys the parameter.

[Top]


In Short

This use case presents a macro which can be usefull for the demos with CATIA. It gives the possibility to deactivate the lights' shadows on environments and on objects, and the ability to reactivate only thoses which where ON.

[Top]


Copyright © 1994-2003, Dassault Systèmes. All rights reserved.