I want to change the lighting style of parts throughout the assembly.

I want to change the lighting style of parts throughout the assembly.

tkddud711
Advocate Advocate
659 Views
8 Replies
Message 1 of 9

I want to change the lighting style of parts throughout the assembly.

tkddud711
Advocate
Advocate

hello?

I want to change the lighting style of all parts and assemblies of the entire assembly at once.
Can you make an ilogic for this?

0 Likes
660 Views
8 Replies
Replies (8)
Message 2 of 9

WCrihfield
Mentor
Mentor

Hi @tkddud711.  I think this is likely doable.  I created a fairly simple iLogic rule you can try out.  There are a certain number of pre-existing LightingStyles that are usually available within Inventor's documents, which you can normally choose from in the drop-down list on the View tab.  In this example, I just chose the first one in that list called "Cool Light" to work with.  You can change that if you want.  Also, I am not sure if its name would be different in different language installations of Inventor, so you may need to edit/change its name.  I only tested this on a single document, not an entire large assembly, so let me know how this works out for you.  All referenced documents should already be open in the background, so you shouldn't really need to open them.  However, they may need to be open 'visibly' (instead of 'invisibly', in the background), for the code to work, but I am not 100% sure at the moment.  If that is needed, the code can be adjusted to make that happen, but of course adding that step in there will most likely greatly reduce processing time, and may bog down inventor for a bit.

Here is the code I came up with.

 

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 = "Cool Light" '<<< CHANGE THIS >>>
	For Each oRefDoc As Document In oAllRefDocs
		ChangeLightingStyle(oRefDoc, oLStyleName)
	Next
End Sub

Sub ChangeLightingStyle(oModelDoc As Document, oLightingStyleName As String)
	If oModelDoc.DocumentType <> DocumentTypeEnum.kAssemblyDocumentObject And _
		oModelDoc.DocumentType <> DocumentTypeEnum.kPartDocumentObject Then
		Exit Sub 'would also work for a presentation document, if needed
	End If
	Dim oLStyle As LightingStyle = Nothing
	Try
		oLStyle = oModelDoc.LightingStyles.Item(oLStyleName)
	Catch
		Logger.Debug("Specified LightingStyle was not found.")
		Exit Sub
	End Try
	If oModelDoc.ActiveLightingStyle IsNot oLStyle Then
		Try
			oModelDoc.ActiveLightingStyle = oLStyle
		Catch
			Logger.Error("Error trying to change the 'active' LightingStyle to the specified one.")
		End Try
	End If
End Sub

 

If this solved your problem, or answered your question, please click ACCEPT SOLUTION .
Or, if this helped you, please click (LIKE or KUDOS) 👍.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 3 of 9

tkddud711
Advocate
Advocate

Neither the part nor the assembly does anything. I speak Korean, but neither Korean nor English has changed. What's the problem?

0 Likes
Message 4 of 9

tkddud711
Advocate
Advocate

Neither the part nor the assembly does anything. I speak Korean, but neither Korean nor English has changed. What's the problem?

0 Likes
Message 5 of 9

WCrihfield
Mentor
Mentor

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

EESignature

(Not an Autodesk Employee)

Message 6 of 9

tkddud711
Advocate
Advocate

hello.
First of all, thank you for your reply.
It works so well.
However, I want to receive only a confirmation message that the conversion has been applied after batch application without the text below, is it possible?

 

 

" Just converted Library style to Local style in XXXX.iam

   Successfully changed LightingStyle in XXXX.ipt "

0 Likes
Message 7 of 9

WCrihfield
Mentor
Mentor

Sure.  In the version of the code below, I have removed all of the MsgBox lines, and made sure all the Logger lines are commented out (so they won't run), but left them in place, because they may be valuable in the future, if you encounter problems.  Then I added one MsgBox at the end of the Sub Main block of code, which will only show after all referenced documents have been processed, and after the main assembly has possibly been updated.  I hope this is what you wanted.

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)
	MsgBox("All referenced model documents have been processed for LightingStyle changes.", _
	vbInformation, "LightingStyles Updates Finished")
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
			oLStyle = oLS
			Exit For
		End If
	Next
	If IsNothing(oLStyle) Then
		'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
			'Logger.Info("Just converted Library style to Local style in " & oModelDoc.DisplayName)
		Catch
			'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
					'Logger.Info("Successfully changed LightingStyle in " & oADoc.DisplayName & ".")
				Catch
					'Logger.Error("Failed to change LightingStyle in " & oADoc.DisplayName & ".")
				End Try
			ElseIf oPDoc IsNot Nothing Then
				Try
					oPDoc.ActiveLightingStyle = oLStyle
					'Logger.Info("Successfully changed LightingStyle in " & oPDoc.DisplayName & ".")
				Catch
					'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

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 8 of 9

tkddud711
Advocate
Advocate

Stop at the sketch.ipt or read-only library you created with the frame generator in the assembly.
Can I ignore these and run the code?

0 Likes
Message 9 of 9

WCrihfield
Mentor
Mentor

Hi @tkddud711.  I am honestly not that familiar with the Frame Generator, because even though it is available to me, I never use it.  However, I have learned about how to detect documents generated by the Frame Generator AddIn, by some code similar to the following:

If oModelDoc.DocumentInterests.HasInterest("{AC211AE0-A7A5-4589-916D-81C529DA6D17}") Then Exit Sub

The 'DocumentInterests' of a document seems to record which AddIn(s) may have been involved in creating the document or any of its features.  That long, unreadable String is obtained directly from the ClassIdString property of the Frame Generator ApplicationAddIn object itself.

Try adding the above line of code within the 'ChangeLightingStyle' Sub's code, just below that first If...Then...End If statement, and see if that fixes the problem for you.

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes