Hello community.
In order to develop a macro within my company, I would like to automate the creation of virtual parts in assembly.
However, I can't seem to develop the code that allows this action.
Below is the beginning of my research.
Dim swApp As Object
Sub main()
Set swApp = Application.SldWorks
Set swmodel = swApp.ActiveDoc
Dim swModelTitle As SldWorks.ModelDoc2
'-------------------------------------------------------------------------------------------------------------
'Verifying that an asm file document is open
'-------------------------------------------------------------------------------------------------------------
If Not swmodel Is Nothing Then' if a SW file is opened
Debug.Print "a SW file is opened" 'then msg debug and continue
Else: MsgBox ("There is no SW file open, please open an assembly and restart the macro") 'if no SW file open => 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 "The open file is an assembly file"
Else: Debug.Print "the open file is not an assembly file, please open an assembly and restart the macro"
MsgBox ("the open file is not an assembly file, please open an assembly and restart the macro")
Exit Sub
End If
'----------------------------------------------------------------------------------------------------------
'Retrieving the file name
'----------------------------------------------------------------------------------------------------------
Dim Path As String
Dim name_asm As String
name_asm = swmodel. GetTitle
path = swmodel. GetPathName
Debug.Print "filename: " & name_asm
Debug.Print "path:" & path
Sun nom_asm
nom_asm = Left(name_asm, (InStrRev(name_asm, ".", -1, vbTextCompare) - 1))
Debug.Print nom_asm
'-----------------------------------------------------------------------------------------------------------
'Creation of imprint parts
'-----------------------------------------------------------------------------------------------------------
Dim nom_pe As String
nom_pe = "EMP_" & nom_asm
Debug.Print "part name imprint: " & nom_pe
Sun new_part
boolstatus = swmodel. Extension.SelectByID2("Face Plane", "PLANE", 0, 0, 0, False, 0, Nothing, 0) 'Selecting Face Plane
new_part = swmodel. InsertNewVirtualPart(boolstatus, nom_pe)
swmodel. ForceRebuild3 True
swmodel. ViewZoomtofit2
End Sub
The error code obtained on the line new_part = ....
Do you have any idea what I'm doing wrong???
I admit that the example of the API help leaves me perplexed...
sans_titre.png