I'm currently creating macro buttons to insert blocks into the background of a drawing.
So far nothing complicated, I have a code that works very well:
Set swApp = _ Application.SldWorks
Set Part = swApp.ActiveDoc Part.EditTemplate Part.EditSketch Dim myBlockDefinition As Object Set myBlockDefinition = Part.SketchManager.MakeSketchBlockFromFile(Nothing, "D:\_MAIA\1_MODELES\4_BLOCS-BUFFERS\Tol ISO2768 - EN22768 - mK.SLDBLK", False, 1, 0) Part.EditSheet Part.EditSketch End Sub
My problem concerns the position of the block on the drawing, I would like to be able to predefine a position in my code (in X and Y).
Currently, when I launch the macro, the block fits into the bottom left corner of the drawing.
I tried a lot of things but without success.
If anyone knows anything about VBA I'm interested ;-)
So before inserting your block, you will have to create and name your insertion point that you will use instead of your "nothing", see this link to create the point:
Dim myBlockDefinition As Object Set myBlockDefinition = Part.SketchManager.MakeSketchBlockFromFile(Nothing, "D:\_MAIA\1_MODELES\4_BLOCS-BUFFERS\Tol ISO2768 - EN22768 - mK.SLDBLK", False, 1, 0) Part.EditSheet Part.EditSketch Part.ClearSelection2 True End Sub
Here is a Vba macro to insert a block by choosing its coordinate points:
Dim swApp As Object
Dim swModel As Object
Dim swDrawing As Object
Dim swSheet As Object
Dim swBlockDef As Object
Dim swBlockIns As Object
Sub InsertBlockAtPoint()
' Se connecter à l'application SolidWorks
Set swApp = Application.SldWorks
' Obtenir le document actif (supposé être un dessin)
Set swModel = swApp.ActiveDoc
If swModel Is Nothing Then
MsgBox "Veuillez ouvrir un document de dessin."
Exit Sub
End If
' Vérifier si le document actif est un dessin
If swModel.GetType <> swDocumentTypes_e.swDocDRAWING Then
MsgBox "Le document actif n'est pas un dessin."
Exit Sub
End If
Set swDrawing = swModel
' Obtenir la feuille active
Set swSheet = swDrawing.GetCurrentSheet
' Spécifier le chemin vers la définition du bloc
Dim blockPath As String
blockPath = "C:\Chemin\Vers\Votre\Bloc.sldblk" ' Changez ceci par le chemin de votre bloc
' Charger la définition du bloc
Set swBlockDef = swDrawing.LoadBlockDefinition(blockPath)
If swBlockDef Is Nothing Then
MsgBox "Échec du chargement de la définition du bloc."
Exit Sub
End If
' Définir les coordonnées du point d'insertion
Dim insertPoint(2) As Double
insertPoint(0) = 0.1 ' Coordonnée X en mètres
insertPoint(1) = 0.1 ' Coordonnée Y en mètres
insertPoint(2) = 0 ' Coordonnée Z (généralement 0 pour les dessins 2D)
' Insérer le bloc au point spécifié
Set swBlockIns = swSheet.InsertBlock(swBlockDef, insertPoint)
If swBlockIns Is Nothing Then
MsgBox "Échec de l'insertion du bloc."
Else
MsgBox "Bloc inséré avec succès."
End If
End Sub
Then, a few remarks for your next requests:
Avoid digging up 2016 Resolute jobs in the future... especially if one of the contributors has not published recently.