I've been trying to change a parts color for three days.
I've seen in an example, that you can use "RenderStyle" of the "ComponentOccurrence" class.
But the "ComponentOccurence"-class doesn't have this property any longer (Inventor 2014 API).
Does anybody know, which property or class I can use instead of?
If you are trying to change part color in Assembly document try the below code (vb.net)
If m_inventorApplication.ActiveDocumentType <> DocumentTypeEnum.kAssemblyDocumentObject Then Exit Sub Try Dim oAssyDoc As Inventor.AssemblyDocument oAssyDoc = m_inventorApplication.ActiveDocument Dim oOccurrences As Inventor.ObjectCollection oOccurrences = m_inventorApplication.TransientObjects.CreateObjectCollection Dim oPartOcc As Inventor.ComponentOccurrence For Each oPartOcc In oAssyDoc.SelectSet oOccurrences.Add(oPartOcc) Next Dim oRsAssy As Inventor.RenderStyle oRsAssy = oAssyDoc.RenderStyles.Item("Red") For Each oPartOcc In oOccurrences oPartOcc.SetRenderStyle(StyleSourceTypeEnum.kOverrideRenderStyle, oRsAssy) Next Catch ex As Exception MsgBox(ex.Message) End Try
if inside part document itself try this
If m_inventorApplication.ActiveDocumentType <> DocumentTypeEnum.kPartDocumentObject Then Exit Sub Try Dim oPartDoc As Inventor.PartDocument oPartDoc = m_inventorApplication.ActiveDocument Dim oOccurrences As Inventor.ObjectCollection oOccurrences = m_inventorApplication.TransientObjects.CreateObjectCollection Dim oPartFeature As Object 'Inventor.PartFeature For Each oPartFeature In oPartDoc.SelectSet oOccurrences.Add(oPartFeature) Next Dim oRs As Inventor.RenderStyle oRs = oPartDoc.RenderStyles.Item("Red") For Each oPartFeature In oOccurrences oPartFeature.SetRenderStyle(StyleSourceTypeEnum.kOverrideRenderStyle, oRs) Next Catch ex As Exception MsgBox(ex.Message) End Try
you can convert the same to vba also.
Inventor 2014 API method is different than the above and see the below code from API Help
Public Sub SetOccurrenceAppearance() Dim asmDoc As AssemblyDocument Set asmDoc = ThisApplication.ActiveDocument ' Get an appearance from the document. To assign an appearance is must ' exist in the document. This looks for a local appearance and if that ' fails it copies the appearance from a library to the document. Dim localAsset As Asset On Error Resume Next Set localAsset = asmDoc.Assets.Item("Bamboo") If Err Then On Error GoTo 0 ' Failed to get the appearance in the document, so import it. ' Get an asset library by name. Either the displayed name (which ' can changed based on the current language) or the internal name ' (which is always the same) can be used. Dim assetLib As AssetLibrary Set assetLib = ThisApplication.AssetLibraries.Item("Autodesk Appearance Library") 'Set assetLib = ThisApplication.AssetLibraries.Item("314DE259-5443-4621-BFBD-1730C6CC9AE9") ' Get an asset in the library. Again, either the displayed name or the internal ' name can be used. Dim libAsset As Asset Set libAsset = assetLib.AppearanceAssets.Item("Bamboo") 'Set libAsset = assetLib.AppearanceAssets.Item("ACADGen-082") ' Copy the asset locally. Set localAsset = libAsset.CopyTo(asmDoc) End If On Error GoTo 0 ' Have an occurrence selected. Dim occ As ComponentOccurrence Set occ = ThisApplication.CommandManager.Pick(kAssemblyOccurrenceFilter, "Select an occurrence.") ' Assign the asset to the occurrence. occ.appearance = localAsset End Sub
Thank you for your help.
I've already tried this example in the API help.
Unfortunately, the code doesn't work.
I've no access to the "Item"-Property in asmDoc.Assets.Item("...");
I use C#.
the syntax of C# is not always the same as VB.NET. To get an item of an array, normally, in C#, it could be
asmDoc.Assets[name, e.g. "XXX"]
asmDoc.Assets[index. e.g. 1,2,3...]
or, sometimes the library of API may provides such as
Array.get_Item("XXX")
Array.get_Item(1)
in VB.NET, normally it could be:
asmDoc.Assets(name, e.g. "XXX")
asmDoc.Assets(index. e.g. 1,2,3...)
or, sometimes the library of API may provides such as
Array.Item("XXX")
Array.Item(1)
Thank you for your help.
Now I can use the "Items"-property.
Unforunately, I can't set the "Appearance"-property of the "ComponentOccurancy"-class.
Visual Studio says, that this property is read only.
(asmDoc is the active assembly document and compocc is the componentOccurence of the selected part)
Asset localAsset = asmDoc.Assets["Holz (Kirsche)"];
//the following line doesn't work
compOcc.Appearance = localAsset;
Can't find what you're looking for? Ask the community or share your knowledge.