Update style from Library

Update style from Library

Anonymous
Not applicable
2,205 Views
6 Replies
Message 1 of 7

Update style from Library

Anonymous
Not applicable

Hi.
 
I've some old assembly that I want to update.

Actually I update the styles manualy, but I want to make it with C#.
 
I do :
1. Update styles, check all, yes for all, OK.

styles.png

 



Can I do it with some C# code ?
 
Best regards.

0 Likes
Accepted solutions (1)
2,206 Views
6 Replies
Replies (6)
Message 2 of 7

wayne.brill
Collaborator
Collaborator

Hi Marcelo,

 

I do not have an example that does this but I believe your code would need to traverse through the components of the assembly and call UpdateFromGlobal for the styles and materials. This post has C# examples.

http://modthemachine.typepad.com/my_weblog/2012/10/c-help-examples-for-assemblies-occurences.html

 

The example function in that project named AssemblyCount tranverses an assembly.  (does not call UpdateFromGlobal however).

 

Here is a VBA procedure that can be used for a quick test for UpdateFromGlobal with a sheet metal PartDocument.

 

Sub SheetMetal_Sytle_Update_Test()
   
    Dim oPartDoc As PartDocument
    Set oPartDoc = ThisApplication.ActiveDocument
     
    Dim oSheetMetalCompDef As SheetMetalComponentDefinition
    Set oSheetMetalCompDef = oPartDoc.ComponentDefinition
  
    Dim oSheetMetalStyle As SheetMetalStyle
    Set oSheetMetalStyle = oSheetMetalCompDef.ActiveSheetMetalStyle
    oSheetMetalStyle.UpdateFromGlobal
 
End Sub

 

 

Here is a VBA example that calls UpdateFromGlobal for materials in a PartDocument.

 

Sub Material_Update_Test()
  
    Dim oPartDoc As PartDocument
    Set oPartDoc = ThisApplication.ActiveDocument
 
    Dim mat As Material
    For Each mat In oPartDoc.Materials
        If mat.UpToDate = False Then
            Call mat.UpdateFromGlobal
            'Debug.Print mat.Name
        End If
    Next
End Sub

 

 

This post has information about appearance assets that may help with what you are trying to do:

http://forums.autodesk.com/t5/inventor-customization/updatefrom-global-for-colors-appearances/m-p/64...

 

 

Thanks,

Wayne

 

 



Wayne Brill
Developer Technical Services
Autodesk Developer Network

0 Likes
Message 3 of 7

Anonymous
Not applicable

Thank you Wayne for the feedback.

From what I understand, the example updates the part material.

Is it possible to recursively update all parts within an assembly?

Best regards.

0 Likes
Message 4 of 7

wayne.brill
Collaborator
Collaborator

Hi Marcelo,

 

Yes it is possible to recursively update all parts in the assembly. The post I mentioned in my previous reply has C# examples. You could start with the AssemblyCount example and update it so that it gets the document for each occurrence and updates the part material. The AsseblyCount example is also in the Programming help so you could do a prototype using VBA and then move it to C#.

 

Thanks,

Wayne

 

 

 

 



Wayne Brill
Developer Technical Services
Autodesk Developer Network

0 Likes
Message 5 of 7

Anonymous
Not applicable
Accepted solution

Hi Wayne.

 

With your tips and another example I found here on this site http://modthemachine.typepad.com/my_weblog/2009/03/accessing-assembly-components.html, I was able to do what I needed.

 

public void UpdateStyle(Inventor.Application invApp) {
	try {
		AssemblyDocument oAsmDoc = (AssemblyDocument)invApp.ActiveDocument;

		foreach (Document oDoc in oAsmDoc.AllReferencedDocuments) {

			PartDocument oPartDoc = (PartDocument)oDoc;

			Material oMat = oPartDoc.ComponentDefinition.Material;

			if (!oMat.UpToDate) {
				oPartDoc.ComponentDefinition.Material.UpdateFromGlobal();
			}
		}

		MessageBox.Show("OK");
	}
	catch (Exception ex) {
		MessageBox.Show(ex.Message);
	}
}



Best regards.

0 Likes
Message 6 of 7

freesbee
Collaborator
Collaborator

Hi Wayne,

I am struggling with this issue: I also would like to update local material styles form the library, but I cannot find the "Material" in the API: only the "MaterialAsset" appears to be available, but unfortunately I am not able to find any "UpdateFromGlobal" method for the MaterialAsset.

What am I doing wrong?

Sub updateMaterial()
Dim oPartDoc As PartDocument
Set oPartDoc = ThisApplication.ActiveDocument

Dim mat As MaterialAsset
For Each mat In oPartDoc.MaterialAssets
  MsgBox mat.DisplayName
Next
End Sub
Massimo Frison
CAD R&D // PDM Admin · Hekuma GmbH
0 Likes
Message 7 of 7

freesbee
Collaborator
Collaborator

...well, as I wrote before I am not sure that the "material" member exists.

Luckly in the meantime I have found the right solution to this issue, nicely documented by @VGonsalves here:

UpdateFromGlobal for Materials

Thank you so much!

Massimo Frison
CAD R&D // PDM Admin · Hekuma GmbH
0 Likes