Autodesk Inventor iLogic to set active lighting style

Autodesk Inventor iLogic to set active lighting style

PaulMunford
Autodesk Autodesk
1,930 Views
7 Replies
Message 1 of 8

Autodesk Inventor iLogic to set active lighting style

PaulMunford
Autodesk
Autodesk

Please could you help me to set the active lighting style with iLogic?

 

This code will get the active lighting style name, but I can't set the active lighting style - what am I missing?

 

Dim oAssy As AssemblyDocument = ThisApplication.ActiveDocument

Dim oLightStyleName As String 

oLightStyleName = oAssy.ActiveLightingStyle.Name

MessageBox.Show(oLightStyleName, "Title")

'This line doesn't work
oAssy.ActiveLightingStyle.Name = "Grid Light"

 

P.S. I found this post on the manufacturing dev blog. I have converted it to iLogic, but it doesn't work for me 😞
http://adndevblog.typepad.com/manufacturing/2015/07/change-activelightingstyle.html

Sub ChangeLightingStyle()
  Dim doc As Document
  Set doc = ThisApplication.ActiveDocument
  
  Dim lss As LightingStyles
  Set lss = doc.LightingStyles
  
  ' Let's just use the first style
  ' InternalName: "1:Cool Light"
  doc.ActiveLightingStyle = lss("1:Cool Light")
End Sub

 



Paul Munford
Technical Onboarding Architect
Linkedin 

Accepted solutions (2)
1,931 Views
7 Replies
Replies (7)
Message 2 of 8

AlexFielder
Advisor
Advisor
Accepted solution

Hi @PaulMunford

 

Long time no see around these parts 😉

 

I ran your code and it worked the first time, but not after that in a newly created assembly file.

 

The reason for this is because you were changing the name (of the active lightingstyle) to the string you desired with this line, not changing the active lightingstyle to it:

 

oAssy.ActiveLightingStyle.Name = "Grid Light"

I've knocked up this simple rule:

 

Dim oAssy As AssemblyDocument = ThisApplication.ActiveDocument

Dim oLightStyleName As String 

oLightStyleName = oAssy.ActiveLightingStyle.Name

MessageBox.Show(oLightStyleName, "Title")

Dim lightingstyletochangeto As LightingStyle = oAssy.LightingStyles.Item("Grid Light")
If Not oAssy.ActiveLightingStyle.Name = lightingstyletochangeto.Name Then
	oAssy.ActiveLightingStyle = lightingstyletochangeto
	InventorVb.DocumentUpdate()
Else
	MessageBox.Show("We're already using the desired lighting style!")
End If

Which appears on the surface to answer your question. When it runs however, the active lightingstyle on the "View" tab of the Inventor interface doesn't seem to update so there must either be:

  1. a bug OR
  2. a missing update function I haven't used to update the Inventor interface

I'm not sure which without further investigation.

 

Cheers,

 

Alex.

Message 3 of 8

chandra.shekar.g
Autodesk Support
Autodesk Support
Accepted solution

@PaulMunford,

 

Try below iLogic code to set lightstyle.

 


Dim doc As Document
doc = ThisApplication.ActiveDocument

Dim ls As LightingStyle
If doc.DocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then

Dim oAssy As AssemblyDocument
oAssy = doc

ls = oAssy.LightingStyles.Item("Grid Light")
oAssy.ActiveLightingStyle = ls

Else If doc.DocumentType = DocumentTypeEnum.kPartDocumentObject Then
Dim oPart As PartDocument
oPart = doc

ls = oPart.LightingStyles.Item("Grid Light")
oPart.ActiveLightingStyle = ls

End If

Thanks and regards,


CHANDRA SHEKAR G
Developer Advocate
Autodesk Developer Network



Message 4 of 8

PaulMunford
Autodesk
Autodesk

It was so much easier when I could just stop by your desk 😉

 

Thanks very much for that - it's ideal. I understand now that I need to call it up as an item, not just change it's name. I need to get better at browsing the object model and understanding hat I can do with it 🙂

 

I will be back with my next question shortly - i'm sure!

 

Paul



Paul Munford
Technical Onboarding Architect
Linkedin 

0 Likes
Message 5 of 8

PaulMunford
Autodesk
Autodesk

Awesome!

 

Thanks @chandra.shekar.g you anticipated my next task:)

 

My ultimate goal is to create an iLogic rule that will step through all files in an assembly, open the file, change the lighting style and save the file - to update the thumbnail.

 

Paul



Paul Munford
Technical Onboarding Architect
Linkedin 

0 Likes
Message 6 of 8

AlexFielder
Advisor
Advisor

psuedo-code for that would be something like:

 

 

Sub Main()
dim stylename as string = "My Chosen Stylename"

For each doc as Document in ThisAssembly.AllReferencedDocuments
   If typeof doc is PartDocument then
      setPartLighting(stylename)
   else 'assembly
      setAssemblyLighting(stylename)
   end if
Next

End Sub

Sub SetPartLighting(Byval stylename as string)
 'copy in the answers from above and hope the magic smoke doesn't escape!
End Sub

Sub SetAssemblyLighting(Byval stylename as string) 'copy in the answers from above and hope the magic smoke doesn't escape! End Sub

 

🙂

0 Likes
Message 7 of 8

PaulMunford
Autodesk
Autodesk

That looks encouraging!

I had the error:

'AllReferencedDocments' is not a member of 'Autodesk.iLogic.Interfaces.iManagedAssembly' ?

 

So I changed it to:
ThisAssembly.Document.AllReferencedDocuments

 

The code runs - but it doesn't seem to work 😕

 

An additional question, would the part file actually need to be opened and saved to get the thumbnail to update?

 



Paul Munford
Technical Onboarding Architect
Linkedin 

0 Likes
Message 8 of 8

AlexFielder
Advisor
Advisor

You saw the bit in my reply that said "pseudo-code" right? Smiley Wink Smiley LOL

 

The best thing you can do is share what code you have for me/others to see.

0 Likes