Hello, I am trying to save an assembly and its drawing with a new assembly name and a new drawing name. I managed to do so using SaveAs3, but when I try to open the original assembly and drawing, they are updated to reference the new assembly and drawing.
Which option should I choose to avoid this?
PS: I’m closing and reopening the drawing because when I try to modify the file and export it to PDF directly, the graphics sometimes appear not distorted.
Sub CreateDrawing()
Dim swApp As SldWorks.SldWorks
Dim swModel As ModelDoc2
Dim swDrawModel As ModelDoc2
Dim assemblyPath As String
Dim assemblyFolder As String
Dim assemblyName As String
Dim newName As String
Dim newFilePath As String
Dim drawingPath As String
Dim newDrawingPath As String
Dim cDrawing As DrawingDoc
Dim swView As View
Dim suffix As String
Dim t As Double
Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
If swModel Is Nothing Then
MsgBox "No document is open.", vbExclamation
Exit Sub
End If
If swModel.GetType <> swDocASSEMBLY Then
MsgBox "Please open an assembly.", vbExclamation
Exit Sub
End If
' Get current assembly path
assemblyPath = swModel.GetPathName
If assemblyPath = "" Then
MsgBox "Assembly must be saved before renaming.", vbExclamation
Exit Sub
End If
' Extract folder and name
assemblyFolder = Left(assemblyPath, InStrRev(assemblyPath, "\"))
assemblyName = Mid(assemblyPath, InStrRev(assemblyPath, "\") + 1)
assemblyName = Left(assemblyName, InStrRev(assemblyName, ".") - 1)
' Create suffix (date)
suffix = "_" & Format(Date, "yyyymmdd")
newName = assemblyName & suffix
' File paths
newFilePath = assemblyFolder & newName & ".SLDASM"
drawingPath = assemblyFolder & assemblyName & ".SLDDRW"
newDrawingPath = assemblyFolder & newName & ".SLDDRW"
' Open and update drawing if it exists
If Dir(drawingPath) <> "" Then
Set swDrawModel = swApp.OpenDoc(drawingPath, swDocDRAWING)
If swDrawModel Is Nothing Then
MsgBox "Failed to open drawing.", vbExclamation
Exit Sub
End If
Set cDrawing = swDrawModel
cDrawing.ForceRebuild3 True
' Scale all views to fit sheet
Set swView = cDrawing.GetFirstView
If Not swView Is Nothing Then Set swView = swView.GetNextView
Do While Not swView Is Nothing
swView.UseSheetScale = True
Set swView = swView.GetNextView
Loop
' Save as new drawing file
swDrawModel.SaveAs3 newDrawingPath, 0, 2
' Close old drawing
swApp.CloseDoc drawingPath
' Reopen new drawing to keep it active
Set swDrawModel = swApp.OpenDoc(newDrawingPath, swDocDRAWING)
End If
' Save assembly with new name
swModel.SaveAs3 newFilePath, 0, 2
' Close old assembly
swApp.CloseDoc assemblyPath
MsgBox "New drawing and assembly saved as '" & newName & "' and drawing is now open.", vbInformation
End Sub