iLogic to set Appearances from a Dialog Box for all components in that active View Rep

iLogic to set Appearances from a Dialog Box for all components in that active View Rep

steveh3
Advisor Advisor
372 Views
3 Replies
Message 1 of 4

iLogic to set Appearances from a Dialog Box for all components in that active View Rep

steveh3
Advisor
Advisor

Gang...

Piecing together some iLogic that will prompt the user to choose an Appearance and then apply this appearance to all components and welds in that assembly.

 

I get it to work once and awhile, but mostly it doesn't work.

 

I get it to work thru the pop up message, but after that it errors.

 

Thoughts?

 

' This iLogic shows a list from our colors and allows you to pick each component that you want to change to.
Dim oAsset As Asset
Dim oAsset_Array As New ArrayList
For Each oAsset_Array_X In ThisApplication.ActiveAppearanceLibrary.AppearanceAssets
oAsset_Array.Add(oAsset_Array_X.DisplayName)
oAsset_Array.Sort()
Next
'present the user with the list to choose from
oAsset_Array_Selected = InputListBox("CHOOSE TEXTURE FROM ABOVE LIST", oAsset_Array, oAsset_Array.Item(0), "TEXTURE SELECTION", "LIST OF TEXTURES")

MessageBox.Show(oAsset_Array_Selected, " All components will change to this color...")


Dim doc As AssemblyDocument = ThisApplication.ActiveDocument
' set your colors/assets here
Dim assetOccurrences As Asset = doc.Assets.Item(oAsset_Array_Selected)
Dim assetWelds As Asset = doc.Assets.Item(oAsset_Array_Selected)

For Each occ As ComponentOccurrence In doc.ComponentDefinition.Occurrences
    occ.Appearance = assetOccurrences
Next

Dim weldDef As WeldmentComponentDefinition = doc.ComponentDefinition
weldDef.WeldBeadAppearance = assetWelds

Thanks in advance,

 

Steve H.

 

Steve Hilvers
Inventor Certified User / Vault Professional Influencer
0 Likes
Accepted solutions (1)
373 Views
3 Replies
Replies (3)
Message 2 of 4

WCrihfield
Mentor
Mentor
Accepted solution

Hi @steveh3.  This may be another case of not working with a 'local copy' of the appearance asset.  You are getting the list from the application library, so it is not guaranteed that all of them will already be available as a local copy within the document.  Then, as I mentioned in the other post, not every component you encounter in a weldment type assembly will be a normal component that you can readily apply an appearance to, without encountering potential errors.  As you can see in my example below, I am filtering first of all for any suppressed components, because you can't do much of anything with a suppressed component.  Then I am filtering for virtual components, and the 'welds' component.  Then I am still using a Try...Catch block for even more thorough error handling, just to be safe.

See if this example code works better for you:

If ThisDoc.Document.DocumentType <> DocumentTypeEnum.kAssemblyDocumentObject Then
	MsgBox("An Assembly Document must be active for this rule to work. Exiting.", vbCritical, "")
	Exit Sub
End If
Dim oADoc As AssemblyDocument = ThisDoc.Document
If Not TypeOf oADoc.ComponentDefinition Is WeldmentComponentDefinition Then Exit Sub
Dim oWDef As WeldmentComponentDefinition = oADoc.ComponentDefinition
Dim oAppearanceNames As New List(Of String)
Dim oLibAppAssets As AssetsEnumerator = ThisApplication.ActiveAppearanceLibrary.AppearanceAssets
For Each oAppAsset As Asset In oLibAppAssets
	oAppearanceNames.Add(oAppAsset.DisplayName)
Next
oAppearanceNames.Sort
Dim oAssetName As String = InputListBox("CHOOSE TEXTURE FROM ABOVE LIST", _
oAppearanceNames, oAppearanceNames.Item(0), "TEXTURE SELECTION", "LIST OF TEXTURES")
Dim oSelectedAppAsset As Asset = oLibAppAssets.Item(oAssetName)
'make sure we are working with a local copy (saved within the assembly)
Dim oFound As Boolean = False
For Each oApAsset As Asset In oADoc.AppearanceAssets
	If oApAsset.Name = oSelectedAppAsset.Name Or _
		oApAsset.DisplayName = oSelectedAppAsset.DisplayName Then
		oSelectedAppAsset = oApAsset
		oFound = True
		Exit For
	End If
Next
If Not oFound Then oSelectedAppAsset = oSelectedAppAsset.CopyTo(oADoc)
'now apply this appearance to components and welds in assembly
oWDef.WeldBeadAppearance = oSelectedAppAsset
oWDef.WeldEndFillAppearance = oSelectedAppAsset
Dim oOccs As ComponentOccurrences = oWDef.Occurrences
For Each oOcc As ComponentOccurrence In oOccs
	If oOcc.Suppressed Then Continue For
	If TypeOf oOcc.Definition Is VirtualComponentDefinition Then Continue For
	If TypeOf oOcc.Definition Is WeldsComponentDefinition Then Continue For
	Try
		oOcc.Appearance = oSelectedAppAsset
	Catch
		Logger.Error("Could not change Appearance of Component named:  " & oOcc.Name)
	End Try
Next

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 4

WCrihfield
Mentor
Mentor

On a side note...I noticed something odd within my UNDO list after running this rule.  There was something listed labeled "Create Protein Style".  I'm sure it is simply referring to when it created a local copy of the global appearance asset, but that is certainly an odd label for that action.  I would think it created an 'Appearance' style, not a 'Protein' style. 🤔

WCrihfield_0-1668095060651.png

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 4 of 4

steveh3
Advisor
Advisor

Awesome...that did the trick 😁 

Steve Hilvers
Inventor Certified User / Vault Professional Influencer
0 Likes