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:
<p>Public Enum swUserPreferenceToggle_e</p><p> swExtRefUpdateCompNames = 18</p><p>End Enum</p><p>Sub main()</p><p> Dim swApp As SldWorks.SldWorks</p><p> Dim swModel As SldWorks.ModelDoc2</p><p> Dim swConfigMgr As SldWorks.ConfigurationManager</p><p> Dim swConfig As SldWorks.Configuration</p><p> Dim swRootComp As SldWorks.Component2</p><p> Dim Children As Variant</p><p> Dim swChild As SldWorks.Component2</p><p> Dim ChildCount As Integer</p><p> Dim OldName As String</p><p> Dim NewName As String</p><p> Dim bOldSetting As Boolean</p><p> Dim bRet As Boolean</p><p> Dim i As Long</p><p> </p><p> Set swApp = Application.SldWorks</p><p> Set swModel = swApp.ActiveDoc</p><p> Set swConfigMgr = swModel.ConfigurationManager</p><p> Set swConfig = swConfigMgr.ActiveConfiguration</p><p> Set swRootComp = swConfig.GetRootComponent</p><p> </p><p> bOldSetting = swApp.GetUserPreferenceToggle(swExtRefUpdateCompNames)</p><p> swApp.SetUserPreferenceToggle swExtRefUpdateCompNames, False</p><p> </p><p> Children = swRootComp.GetChildren</p><p> ChildCount = UBound(Children)</p><p> </p><p></p><p> 'Projectnummer</p><p> j = 0</p><p> Debug.Print swModel.GetTitle</p><p> Do</p><p> j = j + 1</p><p> If Not InStr(Left(swModel.GetTitle, j), "-") = 0 Then</p><p> SearchStr = Left(swModel.GetTitle, j)</p><p> Bla = "Ja"</p><p> End If</p><p> Loop Until Bla = "Ja"</p><p> </p><p> 'Versienummer toekenning</p><p> Do</p><p> Do</p><p> versie = InputBox("Welke versienummer wilt u toekennen?" & vbCrLf & "Versienummers bestaan uit 3 getallen", "VERSIENUMMER", Mid(swModel.GetTitle, j + 1, 3))</p><p> Loop Until Len(versie) = 3</p><p> Loop Until IsNumeric(versie) = True</p><p> </p><p> 'Hernoemen</p><p> For i = 0 To ChildCount</p><p> Set swChild = Children(i)</p><p> </p><p> ' Changing component name requires component to be selected</p><p> bRet = swChild.Select2(False, 0)</p><p> Debug.Print swChild.Name2</p><p> If Left(swChild.Name2, j) = SearchStr Then</p><p> OldName = swChild.Name2</p><p> NewName = Left(SearchStr & versie & Right(OldName, Len(OldName) - Len(SearchStr) - 3), Len(versie) + 11)</p><p> swChild.Name2 = NewName</p><p> End If</p><p> </p><p> Next i</p><p> swApp.SetUserPreferenceToggle swExtRefUpdateCompNames, bOldSetting</p><p> </p><p>End Sub</p>SolidworksApi macros