I have these two codes (harvested on internet) that separately are doing great. But i need them to be one. My main objective is to generate only one file, and not two, as this code is retrieving; and with the same as my part.
This one is working great, both is actualy, but this is creating two dxf files and I only need one of them. Beside, it create using the "view name" i rather it to be the file name.
https://www.codestack.net/solidworks-api/document/drawing/export-sheet-metal-views/
The next one doesnt works properly but it uses the file name, to name the new file. Macro to export Solidworks Part to dxf
Solution based on this code:
https://www.codestack.net/solidworks-api/document/drawing/export-sheet-metal-views/
Tested with one drawing that has already been saved and with only one view that is a flat-pattern view.
To solve the question of having two files being saved replace this code:
Sub ExportFlatPatternViews(draw As SldWorks.DrawingDoc, sheet As SldWorks.sheet)
Dim vViews As Variant
vViews = sheet.GetViews()
If Not IsEmpty(vViews) Then
Dim i As Integer
For i = 0 To UBound(vViews)
Dim swView As SldWorks.view
Set swView = vViews(i)
If swView.IsFlatPatternView() Then
ExportFlatPatternView draw, swView
End If
Next
End If
End Sub
With this one:
Sub ExportFlatPatternViews(draw As SldWorks.DrawingDoc, sheet As SldWorks.sheet)
Dim swView As SldWorks.view
Set swView = draw.GetFirstView
Set swView = swView.GetNextView
While Not swView Is Nothing
If swView.IsFlatPatternView Then
ExportFlatPatternView draw, swView
End If
Set swView = swView.GetNextView
Wend
End Sub
For some reason the original code is getting the same view twice, but with different names. Would need some more time to check why.
To save with filename change the code where filename
is being assigned.
'fileName = view.Name & OUT_EXT
Dim saveDir As String
saveDir = model.GetPathName()
If saveDir = "" Then
Err.Raise vbError, "", "Only saved drawings are supported"
End If
fileName = model.GetTitle & OUT_EXT
Since the ModelDoc2
is as drawing file the ModelDoc2.GetTitle
will return the file name and the sheet name.
Tested with one drawing that has already been saved and with only one view that is a flat-pattern view.
The following code will get the sheet name that contains the view
Dim sheetName As String
sheetName = view.sheet.GetName
Now we can replace the sheet name by an empty string in the fileName
Need to take in account that the GetTitle
concatenates the file name and the sheet name with -
, so we will replace that to.
fileName = Replace(fileName, " - " & sheetName, "")
So to make it work add code after the fileName
is assigned:
fileName = model.GetTitle & OUT_EXT
'Code to add
Dim sheetName As String
sheetName = view.sheet.GetName
fileName = Replace(fileName, " - " & sheetName, "")