Hi,
I'm writing a macro for saving new versions of my solidworks files. What I want is to make a "snapshot" of the motherassembly and saves it with a new version number.
- The macro creates a new folder (named after the version number)
- The macro creates a subfolder (always named the same)
- The macro does a pack and go, targeting that subfolder;
- The macro renames every part, assembly, subparts and subassemblies;
* No toolbox components or parts from third parties.
- The macro does a forced update and saves the assembly
In the future I want to use the macro to:
- check if there are files that have a different version number, but are exactly the same.
If two or more are the same, the file with the oldest number will be referenced to the assembly. The rest will be deleted.
The problems
Subassemblies: Some of the vba codes can only target assembly's children, they can't target subassembly's children. I want to write the macro that it can target every single file.
Pack and go: I don't know how to exclude files from the Pack and Go feature.
Pack and go: I'm uncertain that Pack and Go is the way to go. I'm thinking a "Save As" might be easier to write?
Folderindex:
(Drive):\Projecten\ (Projectname) \ (Versionnumber) \ 04 - SOLIDWORKS\
Projectname is an abbreviation of the Project, usually containing 2 to 4 letters.
Versionnumber is the versioncontrol, containing 3 numbers.
The code:
Currently I have only code to rename children in an assembly:
SolidworksApi macrosPublic Enum swUserPreferenceToggle_e
swExtRefUpdateCompNames = 18
End Enum
Sub main()
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swConfigMgr As SldWorks.ConfigurationManager
Dim swConfig As SldWorks.Configuration
Dim swRootComp As SldWorks.Component2
Dim Children As Variant
Dim swChild As SldWorks.Component2
Dim ChildCount As Integer
Dim OldName As String
Dim NewName As String
Dim bOldSetting As Boolean
Dim bRet As Boolean
Dim i As Long
Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
Set swConfigMgr = swModel.ConfigurationManager
Set swConfig = swConfigMgr.ActiveConfiguration
Set swRootComp = swConfig.GetRootComponent
bOldSetting = swApp.GetUserPreferenceToggle(swExtRefUpdateCompNames)
swApp.SetUserPreferenceToggle swExtRefUpdateCompNames, False
Children = swRootComp.GetChildren
ChildCount = UBound(Children)
'Projectnummer
j = 0
Debug.Print swModel.GetTitle
Do
j = j + 1
If Not InStr(Left(swModel.GetTitle, j), "-") = 0 Then
SearchStr = Left(swModel.GetTitle, j)
Bla = "Ja"
End If
Loop Until Bla = "Ja"
'Versienummer toekenning
Do
Do
versie = InputBox("Welke versienummer wilt u toekennen?" & vbCrLf & "Versienummers bestaan uit 3 getallen", "VERSIENUMMER", Mid(swModel.GetTitle, j + 1, 3))
Loop Until Len(versie) = 3
Loop Until IsNumeric(versie) = True
'Hernoemen
For i = 0 To ChildCount
Set swChild = Children(i)
' Changing component name requires component to be selected
bRet = swChild.Select2(False, 0)
Debug.Print swChild.Name2
If Left(swChild.Name2, j) = SearchStr Then
OldName = swChild.Name2
NewName = Left(SearchStr & versie & Right(OldName, Len(OldName) - Len(SearchStr) - 3), Len(versie) + 11)
swChild.Name2 = NewName
End If
Next i
swApp.SetUserPreferenceToggle swExtRefUpdateCompNames, bOldSetting
End Sub