Hello, I'm looking to do automatic drawings but I'm stuck. The macro I made works but only for one part (the one I used to make the macro)
I'm looking for a way to make it work for any piece, but I don't know how to make it so that instead of giving the coordinates of the vertices of my piece or the face, it automatically detects it. So that no matter the size of the inserted part , the drawing can be done automatically.
Dim swApp As SldWorks.SldWorks
Dim swDraw As SldWorks.DrawingDoc
Dim boolstatus As Boolean
Dim Part As SldWorks.ModelDoc2
Dim pathName As String
Const MODELE As String = "CHEMIN MISE EN PLAN\.drwdot"
Const TABLE As String = "CHEMIN TABLE DE PERCAGE\.sldholtbt"
Const TOP_FACE_NAME As String = "TOP_FACE"
Const RIGHT_FACE_NAME As String = "RIGHT_FACE"
Sub main()
Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
If swModel Is Nothing Then
MsgBox "Cette Macro ne fonctionne que sur une pièce", vbCritical, "AVERTISSEMENT"
End
End If
If swModel.GetType <> swDocPART Then
MsgBox "Cette Macro ne fonctionne que sur une pièce", vbCritical, "AVERTISSEMENT"
End
End If
swModel.GetTitle
pathName = swModel.GetPathName
COMPOSANT = pathName
'Debug.Print COMPOSANT
Set spec = swApp.GetOpenDocSpec(COMPOSANT)
Set Part = swApp.OpenDoc7(spec)
Set Draw = swApp.NewDocument(MODELE, 12, 0.21, 0.297)
Set swDraw = swApp.ActiveDoc
Dim swCompModel As SldWorks.PartDoc
Set swCompModel = swApp.OpenDoc6(COMPOSANT, swDocumentTypes_e.swDocPART, swOpenDocOptions_e.swOpenDocOptions_Silent, "", 0, 0)
swApp.ActivateDoc3 COMPOSANT, False, 0, 0
Dim swFace As SldWorks.Entity
Set swFace = swCompModel.GetEntityByName(TOP_FACE_NAME, swSelectType_e.swSelFACES)
If Not swFace Is Nothing Then
swFace.SelectByMark False, 1
End If
Set swFace = swCompModel.GetEntityByName(RIGHT_FACE_NAME, swSelectType_e.swSelFACES)
If Not swFace Is Nothing Then
swFace.SelectByMark True, 2
End If
Dim swView As SldWorks.view
swApp.ActivateDoc3 swDraw.GetTitle, False, 0, 0
Set swView = swDraw.CreateRelativeView(COMPOSANT, 0.1, 0.13, swRelativeViewCreationDirection_e.swRelativeViewCreationDirection_FRONT, swRelativeViewCreationDirection_e.swRelativeViewCreationDirection_RIGHT)
Set Part = swDraw
Part.ClearSelection2 True
boolstatus = Part.Extension.SelectByID2("", "DRAWINGVIEW", 0.0995, 0.13, 0, True, 0, Nothing, 0)
Set myView = Part.CreateUnfoldedViewAt3(0.16, 0.13, 0, False)
boolstatus = Part.Extension.SelectByID2("", "DRAWINGVIEW", 0.0995, 0.13, 0, True, 0, Nothing, 0)
Set myView = Part.CreateUnfoldedViewAt3(0.04, 0.13, 0, False)
boolstatus = Part.Extension.SelectByID2("", "DRAWINGVIEW", 0.0995, 0.13, 0, True, 0, Nothing, 0)
Set myView = Part.CreateUnfoldedViewAt3(0.0995, 0.18, 0, False)
boolstatus = Part.Extension.SelectByID2("", "DRAWINGVIEW", 0.0995, 0.13, 0, True, 0, Nothing, 0)
Set myView = Part.CreateUnfoldedViewAt3(0.0995, 0.08, 0, False)
'Vue aux. Droite
boolstatus = Part.Extension.SelectByID2("", "DRAWINGVIEW", 0.16, 0.127, 0, True, 16, Nothing, 0)
boolstatus = Part.Extension.SelectByID2("", "VERTEX", 0.1609, 0.097, 0, True, 1, Nothing, 0)
boolstatus = Part.Extension.SelectByID2("", "FACE", 0.16, 0.127, 0, True, 2, Nothing, 0)
'Vue aux. Gauche
boolstatus = Part.Extension.SelectByID2("", "DRAWINGVIEW", 0.04, 0.127, 0, True, 16, Nothing, 0)
boolstatus = Part.Extension.SelectByID2("", "VERTEX", 0.039, 0.097, 0, True, 1, Nothing, 0)
boolstatus = Part.Extension.SelectByID2("", "FACE", 0.04, 0.127, 0, True, 2, Nothing, 0)
'Vue aux. Haut
boolstatus = Part.Extension.SelectByID2("", "DRAWINGVIEW", 0.102, 0.18, 0, True, 16, Nothing, 0)
boolstatus = Part.Extension.SelectByID2("", "VERTEX", 0.055, 0.18095, 0, True, 1, Nothing, 0)
boolstatus = Part.Extension.SelectByID2("", "FACE", 0.102, 0.18, 0, True, 2, Nothing, 0)
'Vue aux. Bas
boolstatus = Part.Extension.SelectByID2("", "DRAWINGVIEW", 0.102, 0.08, 0, True, 16, Nothing, 0)
boolstatus = Part.Extension.SelectByID2("", "VERTEX", 0.055, 0.079, 0, True, 1, Nothing, 0)
boolstatus = Part.Extension.SelectByID2("", "FACE", 0.102, 0.08, 0, True, 2, Nothing, 0)
'Vue principale Dessus
boolstatus = Part.Extension.SelectByID2("", "DRAWINGVIEW", 0.0995, 0.13, 0, True, 16, Nothing, 0)
boolstatus = Part.Extension.SelectByID2("", "VERTEX", 0.055, 0.097, 0, True, 1, Nothing, 0)
boolstatus = Part.Extension.SelectByID2("", "FACE", 0.0995, 0.13, 0, True, 2, Nothing, 0)
'Dim myView As Object
Set myView = Part.SelectionManager.GetSelectedObjectsDrawingView2(1, -1)
Dim myHoleTable As Object
Set myHoleTable = myView.InsertHoleTable2(False, 0.291, 0.205, 2, "A", TABLE)
Part.ClearSelection2 True
boolstatus = Part.ActivateSheet("Sheet1")
'Set swDraw = swModel
Set swView = Part.CreateDrawViewFromModelView3(ModelPath, "*Dimétrique", 0.053, 0.032, 0)
boolstatus = Part.Extension.SelectByID2("", "FACE", 0.053, 0.032, 0, True, 2, Nothing, 0)
swDraw.ViewDisplayShaded
swDraw.ViewModelEdges
End Sub