This code I cobbled together from a number of sources (1, 2) should get you started:
Private Sub Main UpdateAssyBrowser(ThisDoc.Document) End Sub Private Sub UpdateAssyBrowser(ByVal oDoc As Inventor.Document) Dim oAssy As Inventor.AssemblyDocument Dim oComp As Inventor.ComponentOccurrence Dim oSubDoc As Inventor.Document Dim NodeName() As String Dim InstNum As String If oDoc.DocumentType = Inventor.DocumentTypeEnum.kAssemblyDocumentObject Then oAssy = CType(oDoc, AssemblyDocument) For Each oComp In oAssy.ComponentDefinition.Occurrences oSubDoc = CType(oComp.Definition.Document, Document) If Left(oComp.Name,7) = "PlyWood" Then If oSubDoc.DocumentType = Inventor.DocumentTypeEnum.kPartDocumentObject Then 'MessageBox.show("Matching Part file found!") Component.Color(oComp.Name) = "Cyan" End If End If If oSubDoc.DocumentType = Inventor.DocumentTypeEnum.kAssemblyDocumentObject Then Call UpdateAssyBrowser(oSubDoc) End If Next oComp End If End Sub
It is currently based on the name of the part occurence but could easily be changed to suit your needs.
**Please bear in mind that it only affects the top level assembly. The parts themselves don't change colour. I wish to change this fact, but have yet to figure it out.**
🙂
HI AlexF1980
Thank you for your response to this thread however can you help me tweak?
It works a treat if you where looking for all browser names that have been named plywood,however we have a very strict numbering system and all broweser names never get overidden unless it is Ilogic parts Example:CAM-01-0401-000 and so on each additional part will take the next number.
One of the reasons i asked for it to look a iproperties within a part is to find all parts down all levels with material type "PLYWOOD" then when found give me an overide at assembly level just as your code does and change colour for example "Cyan".
it would be great if we can get this to work it would save me lots of ime on doing examples of lets say for example find me all mdf doors in assembly which could be hundreds and show me them in alternate colour?
Many thanks
Dave
Chris is correct, running this code as an external rule will provide a better result. You would just need to add the relevant iLogic code to look for the material property.
@chris: would you mind posting the code here for others (myself included) to see?
🙂
Try this from the Assembly :
Dim oCurrentDoc As Inventor.Document Dim oDoc As Inventor.Document Dim oPartDoc As PartDocument Dim oRefDoc As DocumentsEnumerator Dim oTargetColor As RenderStyle Dim oNewColor As RenderStyle oCurrentDoc = ThisApplication.ActiveDocument If oCurrentDoc.DocumentType = kAssemblyDocumentObject Then For Each oDoc In oCurrentDoc.AllReferencedDocuments If oDoc.DocumentType = kPartDocumentObject Then oPartDoc = oDoc ''' Side note : I do not have Plywood or Clear - Light ''' as Render Styles that I can choose from. The colors used ''' are some default colors from 2011 / 2012 and should ''' serve as reasonable place holders. oTargetColor = oPartDoc.RenderStyles.Item("Wood") oNewColor = oPartDoc.RenderStyles.Item("Blue (Clear)") If oPartDoc.ActiveRenderStyle Is oTargetColor Then ''' Use this if you want to change the ''' color currently being used, without ''' changing the properties of the material. ''' This is like using the drop down box from ''' inside the Part environment. oPartDoc.ActiveRenderStyle = oNewColor ''' Use this line of code if you want to alter ''' the actual Render Style of the Material. ''' This will alter the material so that when it's ''' applied to the part, it will maintain whatever ''' color change you made to it. 'oPartDoc.ComponentDefinition.Material.RenderStyle = oNewColor End If End If Next End If
Hi
Thank you for code correct me if im wrong but your code is looking for certain type of color style assigned to part and hen found a match its changing it to desired color of our choosing.
I have pasted song code below and it kind of does what I want it to do but with few tweaks can you help,its easy if I bullet list below to explain steps
Any help would be great
( code below just randomizes all parts to random colors but it does not change core color of part and this is exactly what I need but in the instance of finding material then changing color )
Public Sub SetOccurrenceOverrideColors()
Dim oAsmDoc As AssemblyDocument
Set oAsmDoc = ThisApplication.ActiveDocument
' Initialize the random number generator and get
' the number of render styles defined.
Randomize
Dim iColorCount As Long
iColorCount = oAsmDoc.RenderStyles.Count
' Iterate through the top-level occurrences in the assembly.
Dim oOcc As ComponentOccurrence
For Each oOcc In oAsmDoc.ComponentDefinition.Occurrences
' Set the color to a random color.
Call oOcc.SetRenderStyle(kOverrideRenderStyle, _
oAsmDoc.RenderStyles.Item(Int((iColorCount * Rnd) + 1)))
Next
End Sub
So, something like this?
Dim oCurrentDoc As Inventor.Document Dim oDoc As Inventor.Document Dim oPartDoc As PartDocument Dim oRefDoc As DocumentsEnumerator Dim oTargetColor As RenderStyle Dim oNewColor As RenderStyle oCurrentDoc = ThisApplication.ActiveDocument If oCurrentDoc.DocumentType = kAssemblyDocumentObject Then For Each oDoc In oCurrentDoc.AllReferencedDocuments If oDoc.DocumentType = kPartDocumentObject Then oPartDoc = oDoc If oPartDoc.ComponentDefinition.Material.Name = "Plywood" oNewColor = oPartDoc.RenderStyles.Item("Clear") If Not oPartDoc.ActiveRenderStyle Is oNewColor Then oPartDoc.ActiveRenderStyle = oNewColor End If End If End If Next End If
This one searches for a Material called "Plywood" and will change the active render style to "Clear". The other code would have done the same thing, but would have looked for the color of the Material rather than the Material itself (though it did require you to swap out the two text names for the RednerStyles for the ones that you were using / wanted to change).
Out of curiosity, Instead of making a color Clear, why not just turn off the visibility of the part?
Also, if you're messing with the visibility of a certain group of parts, why not just create a view representation of the assembly with all of those items turned off? You could even put a Custom iProperty on each part that identifies which View Rep it should go to, and have code go through each part setting it to where it needs to go. This would give you automation plus, possibly, sexier assemblies.
Any who, I hope that this runs for you correctly.
HI
Thanks again for the code but when it runs nothing happens? i dont kno wahts wrong checked spelling on material type and color and everything is ok but it does not change anything unlike the previos code you posted.
As for you earlier question we are make holisay hames which are made into massive top level assemblys containing any thing upto 10.000 parts at times.And the reason for the code is to turn all our our plywood to a more see threw color to check for all internall fixings clashes with electrical points and etc. really just a clear visual check to make and ensure hat we have drawn is fit for purpose when it goes to manufacturer.
hope this makes sense.
Just to note i used to be able to what is required by using the find button on your browser panel looking for .ipt and with material "PLYWOOD" at hit find it worked really well it found all my parts hhighlights them all so i just went to color styles and changed to clear (job done)
However there is something wrong with my machine and cad reseller cannot give solution ,because after running this search when i press close to finish this task i get a serious memory leak and pc just builds up until it reaches my 36gb of ram then just crashes my workstation.
thanks for your time looking at this for me
Could you make a small assembly of parts and attach them to a post? I am not sure if you are not changing the string values to what they should be, or if I am simply misinterpreting what you're trying to tell us. If you could attach a small assembly file (with parts) that have some variety, IE: 1~2 plywood parts, and 1~2 non-plywood parts, I can get better create something that would be suitable for your situation.
HI MegaJerk
For some reason i cannot attach assembly to post will upload to Autodesk 360 if you can send me email address
All we are after is to find any .ipt with material of plywood and change at assembly level to color clear ,so we can see via fixing points.(Parts are not to change permanent just need color override)
We run this at its uppermost level and upto 50 assemblies to deal with?
Thanks for your help
Are you using 2013? Perhaps that's the reason why I can't seem to crack this. I know that they changed a lot of the material / color stuffs in 2013, and I'm still running 2012.
If you're running something below 2013, try zipping up the files (or pack -and- go) first before posting them here.
I am interested in this. It is very useful if I could get the color assigned to the part (at the part level). Attached here to is my assmbly (this is inprogress).
I hope you would not mind it I request you to reply (for changing color of parts).
Thanks.
Autodesk Inventor - 2014 & Vault Basic 2014.
Hi
I give color to all components in the assembly. Assembly Component Color = Part Color. For each part in the assembly (for each component). How can I do that? Assembly all components Color Red = all Parts color Red
After open part and it has a color Red
I find the solution here:
https://modthemachine.typepad.com/my_weblog/2009/02/controlling-part-colors.html
Public Sub RemoveOccurrenceOverrideColors()
Dim oAsmDoc As AssemblyDocument
Set oAsmDoc = ThisApplication.ActiveDocument
' Iterate through the top-level occurrences in the assembly.
Dim oOcc As ComponentOccurrence
For Each oOcc In oAsmDoc.ComponentDefinition.Occurrences
' Set each occurrence to use the part defined color.
Call oOcc.SetRenderStyle(kPartRenderStyle)
Next
End Sub
Can't find what you're looking for? Ask the community or share your knowledge.