Set Virtual Component Material - INV 2015

Set Virtual Component Material - INV 2015

Anonymous
Not applicable
325 Views
1 Reply
Message 1 of 2

Set Virtual Component Material - INV 2015

Anonymous
Not applicable

I'm creating some virtual components and trying to set the material property for each one. It either doesn't work, the property value doesn't change, or it crashes with an "unspecifed error". The interesting part is; I can set any iProperty and even create custom iProperties but, cannot set the material. What can I do to resolve this issue.

 

Thanks,

 

Eric....

 

 

public bool SetVirtualCompMaterial(ComponentOccurrence _occ, string _matlName)
        {
            // Validate the occurrence is virtual.
            if (!(_occ.Definition is VirtualComponentDefinition)) return false;

            // Set a reference to the virtual component definition   
            VirtualComponentDefinition _vCompDef = (VirtualComponentDefinition) _occ.Definition;

            // Return if the virtual component definition is null.
            if (_vCompDef == null) return false;

            // Set a reference to the material asset.
            Asset _materialAsset = _getMaterialAsset(_matlName);

            // Return if the material asset cannot be set.
            if (_materialAsset != null)
            {
                try
                {
                    _vCompDef.Material.Name = _materialAsset.DisplayName;
                    return true;
                }
                catch (Exception)
                { return false; }
            }

            // Assign the material from the provided string 
            // because a material in the assets wasn't found.
            try
            {   _vCompDef.Material.Name = _matlName;}
            catch (Exception)
            { return false; }
            
            return true;
        }

 

0 Likes
326 Views
1 Reply
Reply (1)
Message 2 of 2

xiaodong_liang
Autodesk Support
Autodesk Support

Hi,

 

firstly, the usage of setting display name of a material is NOT a correct way to change a material. You need to change the object 'material'. 

 

But, Document.Material has been retired since 2014. Currently it is a hidden methods for compatibility. Normally, we use MaterialAsset or AppearanceAsset to update the material/appearance. While it looks these new methods are not working well with virtual component. So I have to turn back to recommend the old way below.

 

 

 

Sub changeVCompM()

 Dim asmDoc As AssemblyDocument
    Set asmDoc = ThisApplication.ActiveDocument
   
    Dim oAssDef As AssemblyComponentDefinition
    Set oAssDef = ThisApplication.ActiveDocument.ComponentDefinition
     
    Dim oM As Matrix
    Set oM = ThisApplication.TransientGeometry.CreateMatrix
    
    Dim oV1 As ComponentOccurrence
    Set oV1 = oAssDef.Occurrences.AddVirtual("111", oM)
    oV1.Definition.Material = asmDoc.Materials(4)
    
    Dim oV2 As ComponentOccurrence
    Set oV2 = oAssDef.Occurrences.AddVirtual("222", oM)
    oV2.Definition.Material = asmDoc.Materials(5)
    


End Sub

2016-1-14 14-54-16.png

 

 

 

0 Likes