By Xiaodong Liang
Question
I want to highlight the Assembly Interference result in Inventor Assembly. I get Assembly Interference Result but want the highlighting behavior same as Inventor 'Analyze Interference' result. I don't want to highlight complete component occurrences only their interference area.
Solution
You could either insert the interference body as an non-parametric body and highlight the body by HighlightSet, or add the interference body as the client graphics. The following code demo shows the workflows with the two ways.
You can even check interference yourself without 'Analyze Interference' result. The other blog tells the workflow.
http://adndevblog.typepad.com/manufacturing/2013/07/inventor-api-intersection-of-two-bodies.html
'main function
Public Sub Interference()
Dim oAssDoc As AssemblyDocument
Set oAssDoc = ThisApplication.ActiveDocument
Dim oAssDef As AssemblyComponentDefinition
Set oAssDef = oAssDoc.ComponentDefinition
Dim oResults As InterferenceResults
Dim oCheckSet As ObjectCollection
Set oCheckSet = _
ThisApplication.TransientObjects.CreateObjectCollection
' Add all occurrences to the object collection
Dim oOcc As ComponentOccurrence
For Each oOcc In oAssDef.Occurrences
oCheckSet.Add oOcc
Next
' Get the interference between everything.
Set oResults = _
oAssDef.AnalyzeInterference(oCheckSet)
If oResults.Count > 0 Then
MsgBox "There are " & _
oResults.Count & " interferences."
'Call way_HighlightSet(oResults, oAssDoc)
Call way_ClientGraphics(oResults, oAssDoc)
Else
MsgBox "There is no interference."
End If
End Sub
Sub way_HighlightSet(oResults As InterferenceResults, _
oAssDoc As AssemblyDocument)
'get the transparent appearance
Dim localAsset As Asset
On Error Resume Next
Set localAsset = _
oAssDoc.Assets.Item("Clear")
If Err Then
On Error GoTo 0
Dim assetLib As AssetLibrary
Set assetLib = _
ThisApplication.AssetLibraries. _
Item("Autodesk Appearance Library")
Dim libAsset As Asset
Set libAsset = _
assetLib.AppearanceAssets.Item("Clear")
Set localAsset = _
libAsset.CopyTo(oAssDoc)
End If
Dim oHS As HighlightSet
Set oHS = oAssDoc.HighlightSets.Add
Call oHS.Color.SetColor(255, 0, 0)
For i = 1 To oResults.Count
'create a new part file
Dim oPartDoc As PartDocument
Set oPartDoc = ThisApplication.Documents _
.Add(kPartDocumentObject, _
ThisApplication.FileManager _
.GetTemplateFile(kPartDocumentObject), _
False)
'insert the interfererence body to the new file
Call oPartDoc.ComponentDefinition.Features _
.NonParametricBaseFeatures. _
Add(oResults.Item(i).InterferenceBody)
'save the temp file
Call oPartDoc.SaveAs( _
"c:\temp\DifferencePart" & i & ".ipt", False)
'add the new part to the assembly
Dim oTempOcc As ComponentOccurrence
Set oTempOcc = oAssDoc.ComponentDefinition _
.Occurrences.AddByComponentDefinition( _
oPartDoc.ComponentDefinition, _
ThisApplication.TransientGeometry.CreateMatrix())
'record the old appearance of occurrence one and two
Dim oOccOne_Old_Appearance
Dim oOccTwo_Old_Appearance
Set oOccOne_Old_Appearance = _
oResults.Item(i).OccurrenceOne.Appearance
Set oOccTwo_Old_Appearance = _
oResults.Item(i).OccurrenceTwo.Appearance
'apply the transparent appearance to
'occurrence one and two
oResults.Item(i).OccurrenceOne.Appearance = _
localAsset
oResults.Item(i).OccurrenceTwo.Appearance = _
localAsset
' highlight the temp occurrence
oHS.Clear
oHS.AddItem oTempOcc
'pause to see the result
MsgBox "Occurrences are highlighted from interference " & i
'restore all status
oTempOcc.Delete
oPartDoc.Close
Set oOccOne_Old_Appearance = oOccOne_Old_Appearance
Set oOccTwo_Old_Appearance = oOccTwo_Old_Appearance
Next
End Sub
Public Sub way_ClientGraphics(oResults As InterferenceResults, _
oAssDoc As AssemblyDocument)
'get the transparent appearance
Dim localAsset As Asset
On Error Resume Next
Set localAsset = oAssDoc.Assets.Item("Clear")
If Err Then
On Error GoTo 0
Dim assetLib As AssetLibrary
Set assetLib = ThisApplication.AssetLibraries.Item("Autodesk Appearance Library")
Dim libAsset As Asset
Set libAsset = assetLib.AppearanceAssets.Item("Clear")
Set localAsset = libAsset.CopyTo(oAssDoc)
End If
For i = 1 To oResults.Count
Dim oClientGraphics As ClientGraphics
Set oClientGraphics = _
oAssDoc.ComponentDefinition.ClientGraphicsCollection.Add("InterferenceBody")
' Create a new graphics node within the client graphics objects.
Dim oSurfacesNode As GraphicsNode
Set oSurfacesNode = _
oClientGraphics.AddNode(1)
Dim oTransientBRep As TransientBRep
Set oTransientBRep = _
ThisApplication.TransientBRep
' Create a copy of the solid body in the part
Dim oBody As SurfaceBody
Set oBody = _
oTransientBRep.Copy(oResults.Item(i).InterferenceBody)
' Create client graphics based on the transient body
Dim oSurfaceGraphics As SurfaceGraphics
Set oSurfaceGraphics = _
oSurfacesNode.AddSurfaceGraphics(oBody)
' Color it red
oSurfacesNode.RenderStyle = _
oAssDoc.RenderStyles.Item("Red")
'record the old appearance of occurrence one and two
Dim oOccOne_Old_Appearance
Dim oOccTwo_Old_Appearance
Set oOccOne_Old_Appearance = _
oResults.Item(i).OccurrenceOne.Appearance
Set oOccTwo_Old_Appearance = _
oResults.Item(i).OccurrenceTwo.Appearance
'apply the transparent appearance to
'occurrence one and two
oResults.Item(i).OccurrenceOne.Appearance = _
localAsset
oResults.Item(i).OccurrenceTwo.Appearance = _
localAsset
' Update the view to see the result
ThisApplication.ActiveView.Update
'pause to see the result
MsgBox "Occurrences are highlighted from interference " & i
'restore all status
oClientGraphics.Delete
Set oOccOne_Old_Appearance = oOccOne_Old_Appearance
Set oOccTwo_Old_Appearance = oOccTwo_Old_Appearance
ThisApplication.ActiveView.Update
Next
End Sub