PB How "selectbyid2" works in a macro to auto-place components in an asm

Hi all

A colleague created a macro to automatically place guides on a conveyor layout. This macro works well on his workstation, while on mine which has the same config (HP Z4), the parts are added to the asm but no constraints are created and all the added parts place their center of visualization on the origin of the asm.

Here is an excerpt from the macro for the constraints of the new parts:

'----------------------------------
            'Inserting and constraining guides
            '----------------------------------
            
            If TRM_aval Or CInt(length) > 1044 Then
            
                'Insert and constrain right entry guide
                If tole_à_droite = False Then
                
                    Set comp = Assembly.AddComponent5(lien_guide, 0, "", False, "", 0, 0, 0) 'Insert guide in assembly
                    
                    If Not Comp Is Nothing Then
                        guide = comp. Name2
                        Call Assembly.Extension.SelectByID2("Entrée@" & Conveyor & "@" & AssemblyTitle, "COORDSYS", 0, 0, 0, False, 0, Nothing, 0) Select Marker 1
                        Call Assembly.Extension.SelectByID2("Right Input" & larg & "@" & guide & "@" & AssemblyTitle, "COORDSYS", 0, 0, 0, True, 1, Nothing, 0) 'Select marker 2
                        Call Assembly.AddMate5(20, -1, False, 0, 0, 0, 0, 0, 0, 0, False, False, 0, longstatus) 'Constrain mark 1 on mark 2
                        Assembly.ClearSelection2 True
                    End If
                    
                End If

 

Thank you for your answers

Hello 

Problem solved, with the Select2 function, this selection mode is more robust and works on the different PCs of our BE. Here's how we rewrote the program:

Public Function constrain(component1 As SldWorks.Component2, place2 As String, component2 As SldWorks.Component2, place2 As String)
'Function to constrain 2 components using their marks

Dim feat As SldWorks.Feature
                        
Set feat = component1. FirstFeature

While Not feat Is Nothing' Reviews all the elements in the Component 1 tree
    If feat. Name = place1 Then
        Call feat. Select2(False, 0) 'Select marker 1
    End If
    Set feat = feat. GetNextFeature
Wend
                        
                        
Set feat = component2. FirstFeature

While Not feat Is Nothing' Reviews all the elements in the Component 2 tree
    If feat. Name = place2 Then
        Call feat. Select2(True, 1) 'Select marker 2
    End If
    Set feat = feat. GetNextFeature
Wend

Call Application.SldWorks.ActiveDoc.AddMate5(20, -1, False, 0, 0, 0, 0, 0, 0, 0, 0, False, False, 0, longstatus) 'Constrain mark 1 on mark 2
Application.SldWorks.ActiveDoc.ClearSelection2 True
                    
End Function
 

1 Like

Hello

 

I have the same macro principle and it works very well on all PCs in the BE, just I put my clearselection first.

 

'--------------------------
'positionnement de la pièce
    'Variable
Dim CompInsert As String            'Pièce qui viens d'etre inserer dans l'assemblage
CompInsert = ActiveSheet.Range("B" & i) & "-1@" & ActiveSheet.Range("B" & a)
Dim FirstSelection As String        'Constante, position de l'origine du composant
FirstSelection = "Point1@Origine@" & CompInsert
Dim SecondSelection As String       'A définir, chaque composant a une place différente
SecondSelection = ActiveSheet.Range("C" & i)      'suivant une esquisse et un point potentiellement différent

    'suppression du "Fixe" sur la première pièce
    boolstatus = swModel.Extension.SelectByID2(CompInsert, "COMPONENT", 0, 0, 0, False, 0, Nothing, 0)
    swModel.UnfixComponent

    'Selection du composant et ajout contrainte coincident origine/origine
    swModel.ClearSelection2 True
    boolstatus = swModel.Extension.SelectByID2(FirstSelection, "EXTSKETCHPOINT", 0, 0, 0, False, 1, Nothing, 0)
    boolstatus = swModel.Extension.SelectByID2(SecondSelection, "COORDSYS", 0, 0, 0, True, 1, Nothing, 0)
    Set swMate = swModel.AddMate5(20, -1, False, 0, 0.001, 0.001, 0.001, 0.001, 0.5235987755983, 0.5235987755983, 0.5235987755983, False, False, 0, longstatus)

    'Remettre la pièce "Fixe"
    boolstatus = swModel.Extension.SelectByID2(CompInsert, "COMPONENT", 0, 0, 0, False, 0, Nothing, 0)
    swModel.FixComponent
    swModel.ClearSelection2 True

 

I made an automatic assembly of several levels of a 60th of a component. So I put them back 'fixed' after my positioning, it makes the assembly usable afterwards.

I start with an excel sheet where I compile all my data beforehand, it allows me to detect errors when there are any.