diff --git a/solidworks-api/document/drawing/open-referenced-model/index.md b/solidworks-api/document/drawing/open-referenced-model/index.md index 8448eca6..0337dafc 100644 --- a/solidworks-api/document/drawing/open-referenced-model/index.md +++ b/solidworks-api/document/drawing/open-referenced-model/index.md @@ -1,7 +1,7 @@ --- layout: sw-tool -title: Open Drawing View Referenced Document -caption: VBA macro to open referenced document of the drawing view +title: VBA macro to open referenced document of the drawing view +caption: Open Drawing View Referenced Document description: VBA macro opens the document referenced by the selected drawing view in the referenced configuration and display state image: ref-doc-display-state.svg labels: [drawing,reference,display state] diff --git a/solidworks-api/document/drawing/rename-views-after-sheets/Macro.vba b/solidworks-api/document/drawing/rename-views-after-sheets/Macro.vba new file mode 100644 index 00000000..5762ede2 --- /dev/null +++ b/solidworks-api/document/drawing/rename-views-after-sheets/Macro.vba @@ -0,0 +1,69 @@ +Dim swApp As SldWorks.SldWorks + +Sub main() + + Set swApp = Application.SldWorks + + Dim swModel As SldWorks.ModelDoc2 + + Set swModel = swApp.ActiveDoc + + If Not swModel Is Nothing Then + + If swModel.GetType() = swDocumentTypes_e.swDocDRAWING Then + + Dim swDraw As SldWorks.DrawingDoc + + Set swDraw = swModel + + Dim vSheets As Variant + vSheets = swDraw.GetViews + + Dim i As Integer + + For i = 0 To UBound(vSheets) + + Dim vViews As Variant + vViews = vSheets(i) + + Dim swSheetView As SldWorks.View + + Set swSheetView = vViews(0) + + Dim j As Integer + + Dim nextViewIndex As Integer + nextViewIndex = 0 + + For j = 1 To UBound(vViews) + + Dim swView As SldWorks.View + Set swView = vViews(j) + + Dim viewType As Integer + viewType = swView.Type + + If viewType <> swDrawingViewTypes_e.swDrawingDetailView And viewType <> swDrawingViewTypes_e.swDrawingSectionView Then + + nextViewIndex = nextViewIndex + 1 + + Dim newViewName As String + newViewName = swSheetView.Name & "(" & nextViewIndex & ")" + + If False = swView.SetName2(newViewName) Then + Err.Raise vbError, "", "Failed to rename " & swView.Name & " to " & "" + End If + End If + + Next + + Next + + Else + Err.Raise vbError, "", "Active document is not a drawing" + End If + Else + Err.Raise vbError, "", "Please open the drawing" + End If + +End Sub \ No newline at end of file diff --git a/solidworks-api/document/drawing/rename-views-after-sheets/index.md b/solidworks-api/document/drawing/rename-views-after-sheets/index.md new file mode 100644 index 00000000..a40b1268 --- /dev/null +++ b/solidworks-api/document/drawing/rename-views-after-sheets/index.md @@ -0,0 +1,10 @@ +--- +caption: Rename Drawings Views After Sheets +title: Macro to rename all drawing views after the sheet name +description: VBA macro renames all SOLIDWORKS drawings views after the sheet name the drawing view is on +--- +This VBA macro allows to rename all drawing views from all sheets in the active SOLIDWORKS drawing document after the sheet name followed by index. + +Detailing and section views will be excluded from the renaming process. + +{% code-snippet { file-name: Macro.vba } %} \ No newline at end of file diff --git a/solidworks-api/document/features-manager/batch-create-folders/Macro.vba b/solidworks-api/document/features-manager/batch-create-folders/Macro.vba new file mode 100644 index 00000000..b73efcec --- /dev/null +++ b/solidworks-api/document/features-manager/batch-create-folders/Macro.vba @@ -0,0 +1,58 @@ +Dim swApp As SldWorks.SldWorks + +Sub main() + + Set swApp = Application.SldWorks + + Dim swModel As SldWorks.ModelDoc2 + + Set swModel = swApp.ActiveDoc + + If Not swModel Is Nothing Then + + Dim foldersCount As Integer + Dim folderNamePrefix As String + + foldersCount = CInt(InputBox("Specify the number of folders to create", "Batch Folder Creator", "5")) + folderNamePrefix = InputBox("Specify the prefix name of the folder", "Batch Folder Creator", "MyFolder") + + Dim swAnchorFeat As SldWorks.Feature + Set swAnchorFeat = swModel.Extension.GetLastFeatureAdded + + Dim swFeatMgr As SldWorks.FeatureManager + Set swFeatMgr = swModel.FeatureManager + + Dim i As Integer + + Dim nextIndex As Integer + nextIndex = 0 + + For i = 1 To foldersCount + + swAnchorFeat.Select2 False, -1 + + Dim swFolderFeat As SldWorks.Feature + Set swFolderFeat = swFeatMgr.InsertFeatureTreeFolder2(swFeatureTreeFolderType_e.swFeatureTreeFolder_EmptyBefore) + + If swFolderFeat Is Nothing Then + Err.Raise vbError, "", "Failed to create a folder, make sure there there is at least one feature in the model" + End If + + Dim folderName As String + + Do + nextIndex = nextIndex + 1 + folderName = folderNamePrefix & nextIndex + Loop While False <> swFeatMgr.IsNameUsed(swNameType_e.swFeatureName, folderName) + + swFolderFeat.Name = folderName + + swModel.Extension.ReorderFeature swFolderFeat.Name, "", swMoveLocation_e.swMoveToEnd + + Next + + Else + Err.Raise vbError, "", "No model opened" + End If + +End Sub \ No newline at end of file diff --git a/solidworks-api/document/features-manager/batch-create-folders/index.md b/solidworks-api/document/features-manager/batch-create-folders/index.md new file mode 100644 index 00000000..2646807d --- /dev/null +++ b/solidworks-api/document/features-manager/batch-create-folders/index.md @@ -0,0 +1,14 @@ +--- +caption: Batch Create Folders +title: Batch create feature folders in the active SOLIDWORKS document +description: VBA macro creates specified number of the feature folders with the specified prefix name in the active SOLIDWORKS part or assembly +--- +This VBA macro allows to create feature folders in the batch mode in the active SOLIDWORKS assembly or part document. + +Macro will ask for the number of folders to be created and the folder prefix name. + +Macro will create the specified number of folder with the prefix name followed by the index. + +> If folder with the next index already exists, next index will be used for the naming + +{% code-snippet { file-name: Macro.vba } %} \ No newline at end of file diff --git a/solidworks-api/document/features-manager/split-folders-confgurations/Macro.vba b/solidworks-api/document/features-manager/split-folders-confgurations/Macro.vba new file mode 100644 index 00000000..1cfc8c30 --- /dev/null +++ b/solidworks-api/document/features-manager/split-folders-confgurations/Macro.vba @@ -0,0 +1,150 @@ +Const CREATE_DERIVED_CONFS As Boolean = True + +Const FOLDER_END_TAG As String = "___EndTag___" + +Dim swApp As SldWorks.SldWorks + +Sub main() + + Set swApp = Application.SldWorks + + Dim swModel As SldWorks.ModelDoc2 + + Set swModel = swApp.ActiveDoc + + If Not swModel Is Nothing Then + + Dim vFeatFolders As Variant + Dim vAllFeatFolders As Variant + + Dim swSelMgr As SldWorks.SelectionMgr + Set swSelMgr = swModel.SelectionManager + + vAllFeatFolders = GetAllFeatureFolders(swModel) + + If swSelMgr.GetSelectedObjectCount2(-1) = 0 Then + vFeatFolders = vAllFeatFolders + Else + vFeatFolders = GetSelectedFeatureFolders(swModel) + End If + + If Not IsEmpty(vFeatFolders) Then + + Dim activeConfName As String + activeConfName = swModel.ConfigurationManager.ActiveConfiguration.Name + + Dim i As Integer + + For i = 0 To UBound(vFeatFolders) + Dim swFeatFolder As SldWorks.Feature + Set swFeatFolder = vFeatFolders(i) + CreateConfigurationForFolder swModel, swFeatFolder, vAllFeatFolders, IIf(CREATE_DERIVED_CONFS, activeConfName, "") + Next + + End If + + Else + Err.Raise vbError, "", "No active document" + End If + +End Sub + +Function GetAllFeatureFolders(model As SldWorks.ModelDoc2) As Variant + + Dim swFeatFolders() As SldWorks.Feature + + Dim swFeat As SldWorks.Feature + Set swFeat = model.FirstFeature + + While Not swFeat Is Nothing + + If swFeat.GetTypeName2() = "FtrFolder" And InStr(LCase(swFeat.Name), LCase(FOLDER_END_TAG)) = 0 Then + + If (Not swFeatFolders) = -1 Then + ReDim swFeatFolders(0) + Else + ReDim Preserve swFeatFolders(UBound(swFeatFolders) + 1) + End If + + Set swFeatFolders(UBound(swFeatFolders)) = swFeat + + End If + + Set swFeat = swFeat.GetNextFeature + + Wend + + + If (Not swFeatFolders) = -1 Then + GetAllFeatureFolders = Empty + Else + GetAllFeatureFolders = swFeatFolders + End If + +End Function + +Function GetSelectedFeatureFolders(model As SldWorks.ModelDoc2) As Variant + + Dim swSelMgr As SldWorks.SelectionMgr + Set swSelMgr = model.SelectionManager + + Dim swFeatFolders() As SldWorks.Feature + + Dim i As Integer + + For i = 1 To swSelMgr.GetSelectedObjectCount2(-1) + + If swSelMgr.GetSelectedObjectType3(i, -1) = swSelectType_e.swSelFTRFOLDER Then + + Dim swFeat As SldWorks.Feature + Set swFeat = swSelMgr.GetSelectedObject6(i, -1) + + If (Not swFeatFolders) = -1 Then + ReDim swFeatFolders(0) + Else + ReDim Preserve swFeatFolders(UBound(swFeatFolders) + 1) + End If + + Set swFeatFolders(UBound(swFeatFolders)) = swFeat + End If + + Next + + If (Not swFeatFolders) = -1 Then + GetSelectedFeatureFolders = Empty + Else + GetSelectedFeatureFolders = swFeatFolders + End If + +End Function + +Sub CreateConfigurationForFolder(model As SldWorks.ModelDoc2, folderFeat As SldWorks.Feature, allFeatFolders As Variant, parentConfName As String) + + Dim swFolderConf As SldWorks.Configuration + Set swFolderConf = model.ConfigurationManager.AddConfiguration2(folderFeat.Name, "", "", swConfigurationOptions2_e.swConfigOption_DontActivate Or swConfigurationOptions2_e.swConfigOption_SuppressByDefault, parentConfName, "", False) + + If swFolderConf Is Nothing Then + Err.Raise vbError, "", "Failed to create configuration for " & folderFeat.Name + End If + + Dim i As Integer + + For i = 0 To UBound(allFeatFolders) + + Dim swOtherFeatFolder As SldWorks.Feature + Set swOtherFeatFolder = allFeatFolders(i) + + If swApp.IsSame(folderFeat, swOtherFeatFolder) <> swObjectEquality.swObjectSame Then + + Dim targetConf(0) As String + targetConf(0) = swFolderConf.Name + + If False = swOtherFeatFolder.SetSuppression2(swFeatureSuppressionAction_e.swSuppressFeature, swInConfigurationOpts_e.swSpecifyConfiguration, targetConf) Then + Err.Raise vbError, "", "Failed to configure the suppression of the folder feature for " & swOtherFeatFolder.Name & " in " & swFolderConf.Name + End If + + End If + + Next + +End Sub \ No newline at end of file diff --git a/solidworks-api/document/features-manager/split-folders-confgurations/index.md b/solidworks-api/document/features-manager/split-folders-confgurations/index.md new file mode 100644 index 00000000..22819615 --- /dev/null +++ b/solidworks-api/document/features-manager/split-folders-confgurations/index.md @@ -0,0 +1,20 @@ +--- +caption: Split Folders To Configurations +title: Split feature folders of the SOLIDWORKS file to individual configurations +description: VBA macro creates individual configurations for each feature folder in the active SOLIDWORKS part or assembly +--- +This VBA macro creates configuration for each top-level feature folder in the active SOLIDWORKS part or assembly. + +If no objects selected in the model then all folder features will be processed, otherwise only selected feature folders will be processed. + +Created configuration will be named after the feature folder. + +It is possible to specify if derived or top level configurations should be created for each feature folder. + +~~~ vb +Const CREATE_DERIVED_CONFS As Boolean = True 'True to create derived configuration, False to create top level configuration +~~~ + +All other folders will be suppressed for each configuration. Features outside of the folders will not be suppressed. + +{% code-snippet { file-name: Macro.vba } %} \ No newline at end of file diff --git a/solidworks-api/options/document/detailing-mode-save-model-data/Macro.vba b/solidworks-api/options/document/detailing-mode-save-model-data/Macro.vba new file mode 100644 index 00000000..30a4429c --- /dev/null +++ b/solidworks-api/options/document/detailing-mode-save-model-data/Macro.vba @@ -0,0 +1,38 @@ +Const ENABLE As Boolean = True + +Const swCommands_Save As Long = 2 + +Dim swApp As SldWorks.SldWorks + +Sub main() + + Set swApp = Application.SldWorks + + Dim swModel As SldWorks.ModelDoc2 + + Set swModel = swApp.ActiveDoc + + If Not swModel Is Nothing Then + + If swModel.GetType() = swDocumentTypes_e.swDocDRAWING Then + Dim saveModelDataOpt As Boolean + Dim includeStandardView As Boolean + + saveModelDataOpt = swModel.Extension.GetUserPreferenceToggle(swUserPreferenceToggle_e.swDetailingModeSaveModelData, swUserPreferenceOption_e.swDetailingNoOptionSpecified) + includeStandardView = swModel.Extension.GetUserPreferenceToggle(swUserPreferenceToggle_e.swDetailingModeIncludeStandardViewsInViewPalette, swUserPreferenceOption_e.swDetailingNoOptionSpecified) + + swModel.Extension.SetUserPreferenceToggle swUserPreferenceToggle_e.swDetailingModeSaveModelData, swUserPreferenceOption_e.swDetailingNoOptionSpecified, ENABLE + swModel.Extension.SetUserPreferenceToggle swUserPreferenceToggle_e.swDetailingModeIncludeStandardViewsInViewPalette, swUserPreferenceOption_e.swDetailingNoOptionSpecified, ENABLE + + swApp.RunCommand swCommands_Save, "" + + swModel.Extension.SetUserPreferenceToggle swUserPreferenceToggle_e.swDetailingModeSaveModelData, swUserPreferenceOption_e.swDetailingNoOptionSpecified, saveModelDataOpt + swModel.Extension.SetUserPreferenceToggle swUserPreferenceToggle_e.swDetailingModeIncludeStandardViewsInViewPalette, swUserPreferenceOption_e.swDetailingNoOptionSpecified, includeStandardView + Else + Err.Raise vbError, "", "Only drawing documents are supported" + End If + Else + Err.Raise vbError, "", "Open drawing document" + End If + +End Sub \ No newline at end of file diff --git a/solidworks-api/options/document/detailing-mode-save-model-data/index.md b/solidworks-api/options/document/detailing-mode-save-model-data/index.md new file mode 100644 index 00000000..3ca4bab6 --- /dev/null +++ b/solidworks-api/options/document/detailing-mode-save-model-data/index.md @@ -0,0 +1,20 @@ +--- +caption: Toggle Drawing Detailing Mode On Save +title: Save SOLIDWORKS drawing with detailing mode on and off +description: VBA Macro to toggle detailing mode on and off while saving +--- +When working with large drawings it may be beneficial to employ the detailing mode. In order to properly utilize the detailing mode it is required to save the data within the document itself. + +This process may decrease the saving performance. + +The toggle option to enable or disable saving of the detailing mode data is driven by document preferences. + +This macro allows to turn on or off the settings and perform the saving of the document. + +~~~ vb +Const ENABLE As Boolean = True 'True to save with detailing data, False to save without the detailing data +~~~ + +It is possible to create 2 macro buttons (one which saves with detailing data and one which saves without the detailing data). + +{% code-snippet { file-name: Macro.vba } %} \ No newline at end of file