Setting the color of a part in conjunction with its material

By Vladimir Ananyev

Issue

How to set the color of a part in conjunction with a desired material?

Solution

You can specify both the color (using the RenderStyle object) and material (using the Material object) of a part using the Inventor API. A newly created part is created with the default color and material both of which can be overridden. Assigning a color to the part would mean that it retains its default material but, a new color is assigned. This would be equivalent to keeping the default material of the part but, coloring the outside of the part.

Next, if you were to assign a material to the part, since the material has its own default color, the previously assigned color will now be overridden. But, the Inventor API also allows you to override the default color of a material. Hence, before assigning a material to a part, you can reset the color of the material, and then assign the material to the part. Another method would be, to assign the desired material to a part (without customizing its color) and then assign the desired color to the part itself. But the Material (also like other styles: RenderStyle, Drawing style etc.) can be edited when it is a local one(or both in library and local document).

 

VBA

 

Private Sub RenderStyle()

 

  Dim oDoc As PartDocument

  Set oDoc = ThisApplication.ActiveDocument

 

  ‘get the render styles(colors) collection

  Dim oRenderStyles As RenderStyles

  Set oRenderStyles = oDoc.RenderStyles

 

  ‘get the desired render style (color) from the collection

  Dim oMyPartStyle As RenderStyle

  Set oMyPartStyle = oRenderStyles("Yellow")

 

  ‘color the part with the style

  oDoc.ActiveRenderStyle = oMyPartStyle

 

  ‘you can also specify the material of the part

  ‘get the materials collection

  Dim oMaterials As Materials

  Set oMaterials = oDoc.Materials

 

  ‘get the desired material from the collection

  Dim oMyMaterial As Material

  Set oMyMaterial = oMaterials("Copper")

 

  ‘the material has its own default color (i.e. render style)

&#16
0;
‘you can override it as follows:

  ‘get the material’s default render style

  Dim oDefaultStyle As RenderStyle

  Set oDefaultStyle = oMyMaterial.RenderStyle

 

  ‘apply a new style to the material

  Dim oMyMaterialStyle As RenderStyle

  Set oMyMaterialStyle = oRenderStyles("Blue")

 

  Dim oLocalMaterial As Material

  ‘library one cannot be edited. so convert it to local

  If oMyMaterial.StyleLocation = StyleLocationEnum.kLibraryStyleLocation Then

    Set oLocalMaterial = oMyMaterial.ConvertToLocal

  Else

    Set oLocalMaterial = oMyMaterialStyle

  End If

 

  oLocalMaterial.RenderStyle = oMyMaterialStyle

 

  ‘finally,set the material for the part

  oDoc.ComponentDefinition.Material = oLocalMaterial

 

  ‘ Save this to library

  ‘oLocalMaterial.SaveToGlobal

 

  oDoc.Update

 

End Sub

VB.NET

 

Private Sub RenderStyle()

 

  Try

    Dim oDoc As PartDocument = m_inventorApp.ActiveDocument

 

    ‘get the render styles(colors) collection

    Dim oRenderStyles As RenderStyles = oDoc.RenderStyles

 

    ‘get the desired render style (color) from the collection

    Dim oMyPartStyle As RenderStyle = oRenderStyles("Yellow")

 

    ‘color the part with the style

    oDoc.ActiveRenderStyle = oMyPartStyle

 

    ‘you can also specify the material of the part

    ‘get the materials collection

    Dim oMaterials As Materials = oDoc.Materials

 

    ‘get the desired material from the collection

    Dim oMyMaterial As Material
= oMaterials(
"Copper")

 

    ‘the material has its own default color (i.e. render style)

    ‘you can override it as follows:

    ‘get the material’s default render style

    Dim oDefaultStyle As RenderStyle = oMyMaterial.RenderStyle

 

    ‘apply a new style to the material

    Dim oMyMaterialStyle As RenderStyle = oRenderStyles("Blue")

 

    Dim oLocalMaterial As Material

    ‘library one cannot be edited. so convert it to local

    If oMyMaterial.StyleLocation = StyleLocationEnum.kLibraryStyleLocation Then

      oLocalMaterial = oMyMaterial.ConvertToLocal()

    Else

      oLocalMaterial = oMyMaterialStyle

    End If

 

    oLocalMaterial.RenderStyle = oMyMaterialStyle

 

    ‘finally,set the material for the part

    oDoc.ComponentDefinition.Material = oLocalMaterial

 

    ‘ Save this to library

    ‘oLocalMaterial.SaveToGlobal()

 

    oDoc.Update()

 

  Catch ex As Exception

    MsgBox(ex.ToString())

  End Try

 

End Sub

C#

 

private void RunRenderStyle()

{

  try

  {

    PartDocument oDoc = (PartDocument)m_inventorApp.ActiveDocument;

 

    //get the render styles(colors) collection

    RenderStyles oRenderStyles = oDoc.RenderStyles;

 

    //get the desired render style (color) from the collection

    RenderStyle oMyPartStyle = oRenderStyles["Yellow"];

 

    //color the part with the style

    oDoc.ActiveRenderStyle = oMyPartStyle;

 

    //you can also specify the material of the part

    //get the materials collection

    Materials oMaterials = oDoc.Materials;

 

    //get the desired material from the collection

    Material oMyMaterial = oMaterials["Copper"];

 

    //the material has its own default color (i.e. render style)

    //you can override it as follows:

    //get the material’s default render style

    RenderStyle oDefaultStyle = oMyMaterial.RenderStyle;

 

    //apply a new style to the material

    RenderStyle oMyMaterialStyle = oRenderStyles["Blue"];

 

    Material oLocalMaterial = null;

    if (oMyMaterial.StyleLocation == eLocationEnum.kLibraryStyleLocation)

    {

      oLocalMaterial = oMyMaterial.ConvertToLocal();

    }

    else

    {

      oLocalMaterial = oMyMaterial;

    }

 

    oLocalMaterial.RenderStyle = oMyMaterialStyle;

 

    //finally,set the material for the part

    oDoc.ComponentDefinition.Material = oLocalMaterial;

 

    oDoc.Update();

 

  }

  catch (System.Exception ex)

  {

    MessageBox.Show(ex.ToString());

  }

}

 

C++

 

void MaterialStyle()

{

   //get the active part document, assuming one is active

   CComPtr pDoc;

   Result = pInvApp->get_ActiveDocument(&pDoc);

   CComQIPtr pPartDoc(pDoc);

 

 

   //get the render styles(colors) collection

   CComPtr pRenderStyles;

   Result = pPartDoc->get_RenderStyles(&pRenderStyles);

 

   //get the desired render style (color) from the collection

   CComPtr  pMyPartStyle;

   Result = pRenderStyles->get_Item(CComVariant("Yellow"),&pMyPartStyle);

 

   //color the part with the style

   Result = pPartDoc->put_ActiveRenderStyle(pMyPartStyle);

 

   //you can also specify the material of the part

   //get the materials collection

   CComPtr pMaterials;

   Result = pPartDoc->get_Materials(&pMaterials);

 

 

   //get the desired material from the collection

   CComPtr pMyMaterial;

   Result = pMaterials->get_Item(CComVariant("Copper"),&pMyMaterial);

 

   //the material has its own default color (i.e. render style)

   //you can override it as follows:

   //get the material’s default render style

   CComPtr pDefaultStyle;

   Result = pMyMaterial->get_RenderStyle(&pDefaultStyle);

 

   //apply a new style to the material

   CComPtr pMyMaterialStyle;

   Result = pRenderStyles->get_Item(CComVariant("Blue"),&pMyMaterialStyle);

 

 

   CComPtr oLocalMaterial = NULL;

 

   StyleLocationEnum  pStyleLocation;

 

   pMyMaterial->get_StyleLocation(&pStyleLocation);

 

   if(pStyleLocation == StyleLocationEnum::kLibraryStyleLocation  )

   {

     Result = pMyMaterial->ConvertToLocal(&oLocalMaterial);

   }

   else

   {

     oLocalMaterial = pMyMaterial;

   }

 

   oLocalMaterial->put_RenderStyle( pMyMaterialStyle);

 

   //finally,set the material for the part

   CComPtr pPartDef;

   Result = pPartDoc->get_ComponentDefinition(&pPartDef);

 

}

<

p style=”line-height: normal;margin: 0cm 0cm 0pt” class=”MsoNormal”> 


Comments

Leave a Reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading