cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - Want the oppurtunity to discuss enhancements to PTC products? Join a working group! X

Mate part inside Assembly using VB.Net

mmoruzzi
4-Participant

Mate part inside Assembly using VB.Net

Hi all,

I'm trying to mate two different component inside an assembly using VB.Net API. I found the example inside the official documentation (vbapi_examples) and everything works fine.

Here is the code:

'Assembling Components
    '======================================================================
    'Function   :   assembleByDatums
    'Purpose    :   This function demonstrates how to assemble a component
    '               into an assembly, and how to constrain the component
    '               by aligning datum planes. If the complete set of datum
    '               planes is not found, the function will show the
    '               component constraint dialog to the user to allow them
    '               to adjust the placement.
    '======================================================================
    Public Sub assembleByDatums(ByRef session As IpfcBaseSession, _
                                ByVal componentFileName As String, _
                                ByVal assemblyDatums() As String, _
                                ByVal componentDatums() As String)

        Dim model As IpfcModel
        Dim assembly As IpfcAssembly                
        Dim modelDesc As IpfcModelDescriptor
        Dim componentModel As IpfcSolid
        Dim asmcomp As IpfcComponentFeat
        Dim constraints As IpfcComponentConstraints
        Dim i As Integer
        Dim asmItem As IpfcModelItem
        Dim compItem As IpfcModelItem
        Dim ids As Cintseq
        Dim path As IpfcComponentPath
        Dim asmSelect As IpfcSelection
        Dim compSelect As IpfcSelection
        Dim constraint As IpfcComponentConstraint
        Dim errorCount As Integer

        Try

            '======================================================================
            'Get the current assembly and new component
            '======================================================================
            model = session.CurrentModel
            If model Is Nothing Then
                Throw New Exception("Model not present")
            End If
            If (Not model.Type = EpfcModelType.EpfcMDL_ASSEMBLY) Then
                Throw New Exception("Model is not an assembly")
            End If
            assembly = CType(model, IpfcAssembly)

            modelDesc = (New CCpfcModelDescriptor).CreateFromFileName(componentFileName)
            componentModel = session.GetModelFromDescr(modelDesc)
            If componentModel Is Nothing Then
                componentModel = session.RetrieveModel(modelDesc)
            End If

            '======================================================================
            'Package the component initially
            '======================================================================                        
            asmcomp = assembly.AssembleComponent(componentModel, Nothing)

            '======================================================================
            'Prepare the constraints array
            '======================================================================
            errorCount = 0
            constraints = New CpfcComponentConstraints
            For i = 0 To 2

                '==================================================================
                'Find the assembly datum
                '==================================================================
                asmItem = assembly.GetItemByName(EpfcModelItemType.EpfcITEM_SURFACE, _
                                                 assemblyDatums(i))

                If asmItem Is Nothing Then                    
                    errorCount = errorCount + 1
                    Continue For
                End If

                '==================================================================
                'Find the component datum
                '==================================================================
                compItem = componentModel.GetItemByName(EpfcModelItemType.EpfcITEM_SURFACE, _
                                          componentDatums(i))

                If compItem Is Nothing Then                    
                    errorCount = errorCount + 1
                    Continue For
                End If

                '==================================================================
                'For the assembly reference, initialize a component path.
                'This is necessary even if the reference geometry is in the
                'assembly
                '==================================================================                
                ids = New Cintseq
                path = (New CMpfcAssembly).CreateComponentPath(assembly, ids)

                '==================================================================
                'Allocate the references
                '==================================================================
                asmSelect = (New CMpfcSelect).CreateModelItemSelection(asmItem, path)
                compSelect = (New CMpfcSelect).CreateModelItemSelection(compItem, Nothing)

                '==================================================================
                'Allocate and fill the constraint
                '==================================================================
                constraint = (New CCpfcComponentConstraint).Create _
                              (EpfcComponentConstraintType.EpfcASM_CONSTRAINT_ALIGN)
                constraint.AssemblyReference = asmSelect
                constraint.ComponentReference = compSelect
                constraints.Insert(constraints.Count, constraint)

            Next

            '======================================================================
            'Set the assembly component constraints and regenerate the assembly if
            'atleast one constraint has been defined properly
            '======================================================================
            If errorCount < 2 Then
                asmcomp.SetConstraints(constraints, Nothing)
                assembly.Regenerate(Nothing)
                session.GetModelWindow(assembly).Repaint()
            End If
            
            '======================================================================
            'If any of the expect datums was not found, prompt the user to constrain
            'the new component
            '======================================================================
            If errorCount > 0 Then
                MsgBox("Unable to locate all required datum references." + _
                        "New component is packaged")
                asmcomp.RedefineThroughUI()
            End If

        Catch ex As Exception
            MsgBox(ex.Message.ToString + Chr(13) + ex.StackTrace.ToString)
            Exit Sub
        End Try

    End Sub

In the example I set the constrain between Planes (TOP, SIDE, FRONT) of the main assembly and the same plane of the new inserted part.

I need to set constrain between the last part of the assembly and the new inserted one but I can't find the way to do that. Can somebody help me?

Have a good day !

Michele

0 REPLIES 0
Top Tags