Quantcast
Channel: Manufacturing DevBlog
Viewing all articles
Browse latest Browse all 516

Inventor API: Detect a Content Center part is inserted as standard or custom

$
0
0

By Xiaodong Liang

When we place a part to the assembly, it could be a common part, or a part from content center yet as standard, or the content center part as custom. Sometimes we would need to know which type part is inserted. 

Screen Shot 2018-01-04 at 6.52.28 PM

To differentiate the UI >> [Place] or [Place from Content Center], the first I thought of is the event: UserInputEvents::OnActivateCommand. For UI>>[Place], it can tell a meaningful name, but for UI >> [Place from Content Center], it only tells a command "InteractionEvents"  which will also happen with some other UI operations. It cannot either tell as standard or as custom. In addition, the placing would be performed by an API code, the command name cannot over everything. 

So I turned to check the other event: AssemblyEvents::OnNewOccurrence. It can provide the final component definition after the part is inserted, by the definition, we could check if it is a content center part as standard by PartComponentDefinition::IsContentMember. As to the content center part as custom, it looks we have to check the properties of the document because a content center part will have some special property sets than the common part, even if it has been converted to as custom part. The other blog tells more. 

Based on the investigation, I produced a demo VBA code as below. Hope it helps.

Download TestProject

'module

Dim oClass As TestEvenClass
Sub startEvent()
    Set oClass = New TestEvenClass
    
    oClass.ini
    
    Do While True
        DoEvents
    Loop

End Sub

'event class
Public WithEvents oAssEvents As AssemblyEvents 

Public Sub ini()
  Set oAssEvents = ThisApplication.AssemblyEvents 
End Sub
  

Private Sub oAssEvents_OnNewOccurrence(ByVal DocumentObject As AssemblyDocument, ByVal Occurrence As ComponentOccurrence, ByVal BeforeOrAfter As EventTimingEnum, ByVal Context As NameValueMap, HandlingCode As HandlingCodeEnum)

If BeforeOrAfter = kAfter Then
    
    For Index = 1 To Context.Count

        Dim oContextName As String
        oContextName = Context.Name(Index)
            
        If oContextName = "ComponentDefinition" Then
            Dim oDef As ComponentDefinition
            Set oDef = Context.Value(Context.Name(Index))
            If TypeOf oDef Is PartComponentDefinition Then
                Dim oPartDef As PartComponentDefinition
                Set oPartDef = oDef
                If oPartDef.IsContentMember Then'this is a part from content center in standard way
                    Debug.Print "This is a part from Content Center "
                Else' check if it is common part of custom part of Content Center
                   Dim isCustomPart As Boolean
                   isCustomPart = False
                   Dim oPropertySets As PropertySets
                   Set oPropertySets = oPartDef.Document.PropertySets
                   Dim oEachProSet As PropertySet
                   Dim oEachP As Property

                    For i = 1 To oPropertySets.Count

                        ' use the 6th propertyset of [ContentCenter].' If it is a Custom part, there is only one property within this property set.' The name is “IsCustomPart”. While if it is a Standard CC part, there are some properties
                        ' to indicate the information of the CC such as family, member etc..'the internal name of this propertyset is not same across different content center part'so, it looks we have to use display name, which assumes no user defined property set with the same name.

                        Set oEachProSet = oPropertySets(i)
                        'If oEachProSet.InternalName = "{5E21919D-4082-492A-8A30-039424CD07B5}" Then
                        If oEachProSet.DisplayName = "ContentCenter" Then
                            For k = 1 To oEachProSet.Count
                                Set oEachP = oEachProSet(k)
                                If oEachP.Name = "IsCustomPart" Then
                                    Dim oValue As String
                                    oValue = oEachP.Value
                                    If oValue = "1" Then
                                        isCustomPart = True
                                        Exit For
                                    End If
                                End If
                            Next
                        End If
                        If isCustomPart Then
                            Exit For
                        End If
                    Next
                    If isCustomPart Then
                        Debug.Print "This is a custom part from content center"
                    Else
                        Debug.Print "This is a common part"
                    End If
                End If
            End If
        End If

    Next
    
End If

End Sub
 

Viewing all articles
Browse latest Browse all 516

Trending Articles