Dim swApp As Object Sub main() Set swApp = Application.SldWorks Set swmodel = swApp.ActiveDoc Dim swModelTitle As SldWorks.ModelDoc2 '------------------------------------------------------------------------------------------------------------- 'Vérification qu 'un document fichier asm est ouvert '------------------------------------------------------------------------------------------------------------- If Not swmodel Is Nothing Then 'si un fichier SW est ouvert Debug.Print "un fichier SW est ouvert" 'alors msg debug et poursuite Else: MsgBox ("Il n'y a pas de fichier SW ouvert, merci d'ouvrir un assemblage et relancer la macro") 'si pas de fichier SW ouvert => msg Exit Sub End If Dim type_doc As String type_doc = swDocumentTypes_e.swDocPART Debug.Print type_doc If swmodel.GetType = swDocumentTypes_e.swDocASSEMBLY Then Debug.Print "Le fichier ouvert est un fichier assemblage" Else: Debug.Print "le fichier ouvert n'est pas un fichier assemblage, merci d'ouvrir un assemblage et relancer la macro" MsgBox ("le fichier ouvert n'est pas un fichier assemblage, merci d'ouvrir un assemblage et relancer la macro") Exit Sub End If '---------------------------------------------------------------------------------------------------------- 'Récupéartion du nom du fichier '---------------------------------------------------------------------------------------------------------- Dim chemin As String Dim name_asm As String name_asm = swmodel.GetTitle chemin = swmodel.GetPathName Debug.Print "nom du fichier : " & name_asm Debug.Print "chemin d'acces : " & chemin Dim nom_asm nom_asm = Left(name_asm, (InStrRev(name_asm, ".", -1, vbTextCompare) - 1)) Debug.Print nom_asm '----------------------------------------------------------------------------------------------------------- 'Création pièce empreinte '----------------------------------------------------------------------------------------------------------- Dim nom_pe As SldWorks.Component2 'nom_pe = "EMP_" & nom_asm 'Debug.Print "nom pièce empreinte : " & nom_pe Dim new_part boolstatus = swmodel.Extension.SelectByID2("Plan de face", "PLANE", 0, 0, 0, False, 0, Nothing, 0) 'Sélection du plan de face new_part = swmodel.InsertNewVirtualPart(boolstatus, nom_pe) nom_pe.Name2 = EMP_" & nom_asm swmodel.ForceRebuild3 True swmodel.ViewZoomtofit2 End Sub