高亮装配中发生干涉的部分

又是好一阵子没冒泡了。总算结束了全年大部分活动。现在需要充电了! 还有博客, 不好意思尴尬 

刚接到一个问题,需要将装配中通过AnalyzeInterference得到的干涉部分高亮。在Inventor API中,有专门的高亮对象 (HighlightSet),能够将需要的实体用特定颜色进行突出显示。API帮助文档里有样例。但测试中发现,不能直接将干涉结果作为HighlightSet的内容,只能先把干涉的部分存为一个零件,插入到当前装配,然后将此部件作为HighlightSet的内容。另存的过程其实就是专为非参数化特征。

而灵活性更大的方式是用自定义图形,大部分情况下,这种高亮都是临时示意性的。所以可以根据干涉部分创建对应的自定义图形,然后显示出来,至于颜色,材质。轻松搞定。

以下是两种方法的样例,以及结果的截图


'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

image



image


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值