Can anyone explain me, why I can import a 3D model using the import wizard but I Can't do that with SW API?
I Try a lot to make this import with VBA line code but i have no sucess.
There are my VBA code:
Option Explicit
' Declare SOLIDWORKS variables
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swPart As SldWorks.PartDoc
Dim swFeat As SldWorks.Feature
Dim boolstatus As Boolean
Dim longstatus As Long
Dim longwarnings As Long
Sub main()
' Connect to SOLIDWORKS
Set swApp = Application.SldWorks
If swApp Is Nothing Then
MsgBox "Failed to connect to SOLIDWORKS. Ensure SOLIDWORKS is running.",vbCritical
Exit Sub
End If
' Specify the folder containing DXF files
Dim folderPath As String
folderPath = "C:\myarchive\folder"
If Right(folderPath, 1) <> "\" Then folderPath = folderPath & "\"
' Create FileSystemObject to iterate through files
Dim fso As Object
Set fso = CreateObject("Scripting.FileSystemObject")
' Verify folder exists
If Not fso.FolderExists(folderPath) Then
MsgBox "Folder does not exist: " & folderPath, vbCritical
Set fso = Nothing
Exit Sub
End If
' Get folder
Dim folder As Object
Set folder = fso.GetFolder(folderPath)
' Variables for file processing
Dim file As Object
Dim dxfFilePath As String
Dim sldprtFilePath As String
Dim fileCount As Long
fileCount = 0
' Loop through all files in the folder
For Each file In folder.Files
' Check if the file is a DXF
If UCase(fso.GetExtensionName(file.Name)) = "DXF" Then
dxfFilePath = folderPath & file.Name
Debug.Print "Processing file: " & dxfFilePath
' Import DXF as 3D model
Dim importData As SldWorks.ImportDxfDwgData
Set importData = swApp.GetImportFileData(dxfFilePath)
' Create a new part document
Set swModel = swApp.NewDocument("C:\mylocation\of\templates\Peça.prtdot", 0, 0, 0)
If swModel Is Nothing Then
Debug.Print "Failed to create new part document for: " & dxfFilePath
GoTo NextFile
End If
If Not importData Is Nothing Then
' Configure import settings for 3D model
importData.ImportMethod("") = swImportDxfDwg_ImportMethod_e.swImportDxfDwg_ImportToExistingPart
' Import the DXF file
'swModel = swApp.LoadFile4(dxfFilePath, "r", importData, longwarnings )
boolstatus = swApp.LoadFile3(dxfFilePath, "", importData)
If boolstatus Then
' Define the output SLDPRT file path
sldprtFilePath = Left(dxfFilePath, Len(dxfFilePath) - 4) & ".SLDPRT"
' Save as SLDPRT
longstatus = swModel.SaveAs2(sldprtFilePath, swSaveAsVersion_e.swSaveAsStandardDrawing, True, swSaveAsOptions_e.swSaveAsOptions_Silent)
If longstatus = 0 Then
Debug.Print "Successfully saved: " & sldprtFilePath
fileCount = fileCount + 1
Else
Debug.Print "Failed to save: " & sldprtFilePath
End If
Else
Debug.Print "Failed to import DXF as 3D model: " & dxfFilePath
End If
Else
Debug.Print "Failed to get import data for: " & dxfFilePath
End If
' Close the document
swApp.CloseDoc swModel.GetTitle
Set swModel = Nothing
End If
NextFile:
Next file
' Inform user of completion
MsgBox "Processed " & fileCount & " DXF files.", vbInformation
' Clean up
Set fso = Nothing
Set swApp = Nothing
End Sub