Hi @tkddud711. I played around with the iLogic rule code for changing the LightingStyles in all components in an assembly for a while, and could not seem to get it to work in my test assembly for a long time. It was really frustrating, because my much simpler original code, which was designed to work on a single 'specific' type of model file, which was the active document, worked just fine every time. Then I finally figured out that the variable I was using within my Sub routine needed to be defined as the more specific document Type (either AssemblyDocument or PartDocument, not just Document) to be used in the last part of the code, where it sets the ActiveLightingStyle's Value. If the variable was just defined as a generic Document, it would not work, and always threw an error. It is pretty strange, because I can use the regular Document type variable for all the rest of the code, and that part works just fine. I just had some code for updating & purging those styles before, which worked OK, but never needed to change LightingStyles in a bunch of referenced documents by code before. I added in a bunch of feedback messages throughout the code while I was in the testing phase, just so I could better see what all is going on in all those documents as the code runs, but they did not really help solve the problem in the end. So, I just left all those feedback messages in there for right now. You can either comment those lines of code out, or delete them completely if you want. I wasn't sure if you preferred using MsgBox or Logger, so I had lines for both in there. More info is almost always better than less, as long as its not slowing things down too much.
Here is the code that I finally got working in my tests:
Sub Main
Dim oDoc As Document = ThisDoc.Document
Dim oAllRefDocs As DocumentsEnumerator = oDoc.AllReferencedDocuments
'specify which LightingStyle to set all referenced documents to
Dim oLStyleName As String = "One Light" '<<< CHANGE THIS >>>
ChangeLightingStyle(oDoc, oLStyleName)
For Each oRefDoc As Document In oAllRefDocs
ChangeLightingStyle(oRefDoc, oLStyleName)
Next
If oDoc.RequiresUpdate Then oDoc.Update2(True)
End Sub
Sub ChangeLightingStyle(oModelDoc As Document, oLightingStyleName As String)
Dim oADoc As AssemblyDocument = Nothing
Dim oPDoc As PartDocument = Nothing
If oModelDoc.DocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then
oADoc = oModelDoc
ElseIf oModelDoc.DocumentType = DocumentTypeEnum.kPartDocumentObject Then
oPDoc = oModelDoc
Else
Exit Sub 'would also work for a presentation document, if needed
End If
If Not oModelDoc.IsModifiable Then
MsgBox(oModelDoc.DisplayName & " is Not Modifiable.", vbExclamation, "")
'Logger.Debug(oModelDoc.DisplayName & " is Not Modifiable.")
Exit Sub
End If
Dim oLStyle As LightingStyle = Nothing
'check for specified LightingStyle within supplied document
For Each oLS As LightingStyle In oModelDoc.LightingStyles
If oLS.Name = oLightingStyleName Then
'MsgBox("oLS.InternalName = " & oLS.InternalName, vbInformation, "")
oLStyle = oLS
Exit For
End If
Next
If IsNothing(oLStyle) Then
MsgBox("Specified LightingStyle was not found in " & oModelDoc.DisplayName, vbCritical, "")
'Logger.Debug("Specified LightingStyle was not found in " & oModelDoc.DisplayName)
Exit Sub
End If
If oLStyle.StyleLocation = StyleLocationEnum.kLibraryStyleLocation And _
oLStyle.StyleLocation <> StyleLocationEnum.kBothStyleLocation Then
Try
oLStyle = oLStyle.ConvertToLocal
MsgBox("Just converted Library style to Local style in " & oModelDoc.DisplayName, vbInformation, "")
'Logger.Info("Just converted Library style to Local style in " & oModelDoc.DisplayName)
Catch
MsgBox("Error converting library style to local style in " & oModelDoc.DisplayName, vbExclamation, "")
'Logger.Error("Error converting library style to local style in " & oModelDoc.DisplayName)
End Try
End If
'If Not oLStyle.UpToDate Then oLStyle.UpdateFromGlobal
Dim oOpened As Boolean = False
Dim oALS As LightingStyle = oModelDoc.ActiveLightingStyle
If oALS IsNot oLStyle Then
If Not IsOpenVisibly(oModelDoc) Then
oModelDoc = ThisApplication.Documents.Open(oModelDoc.FullDocumentName, True)
oOpened = True
If oADoc IsNot Nothing Then
Try
oADoc.ActiveLightingStyle = oLStyle
MsgBox("Successfully changed LightingStyle in " & oADoc.DisplayName & ".", , "")
'Logger.Info("Successfully changed LightingStyle in " & oADoc.DisplayName & ".")
Catch
MsgBox("Failed to change 'active' LightingStyle in " & oADoc.DisplayName & ".", vbExclamation, "")
'Logger.Error("Failed to change LightingStyle in " & oADoc.DisplayName & ".")
End Try
ElseIf oPDoc IsNot Nothing Then
Try
oPDoc.ActiveLightingStyle = oLStyle
MsgBox("Successfully changed LightingStyle in " & oPDoc.DisplayName & ".", , "")
'Logger.Info("Successfully changed LightingStyle in " & oPDoc.DisplayName & ".")
Catch
MsgBox("Failed to change LightingStyle in " & oPDoc.DisplayName & ".", vbExclamation, "")
'Logger.Error("Failed to change LightingStyle in " & oPDoc.DisplayName & ".")
End Try
End If
End If
End If
'may have to use oModelDoc.Rebuild or oModelDoc.Rebuild2(True) instead of Update to see the change
If oModelDoc.RequiresUpdate Then oModelDoc.Update2(True)
If oModelDoc.Dirty Then oModelDoc.Save2(False)
If oOpened Then oModelDoc.Close(True)
End Sub
Function IsOpenVisibly(oDoc As Document) As Boolean
If IsNothing(oDoc) Then Return False
Dim oVisDocs As DocumentsEnumerator = ThisApplication.Documents.VisibleDocuments
For Each oVisDoc As Document In oVisDocs
If oVisDoc Is oDoc Then Return True
Next
Return False 'it was not found
End Function
By the way, I mentioned this in a comment near the end of the Sub routine, but you may have to use Rebuild instead of Update to see the changes in the LightingStyle of the visibly open document.
Wesley Crihfield

(Not an Autodesk Employee)