ds-blue-logo
Preview  |  SOLIDWORKS USER FORUM
Use your SOLIDWORKS ID or 3DEXPERIENCE ID to log in.
AAAyugma Acharya29/10/2020

I have no knowledge of programming and I found this macro on 3D Content Central that I wanted to use for a project. It creates drawings with the view you want for selected parts in an assembly. The code is below (authored by: Jake Carr, link: https://www.3dcontentcentral.com/Search/macro.aspx?id=807151 ):

'Variable Declarations
Dim swApp As SldWorks.SldWorks
Dim swModel As ModelDoc2
Dim docDepend As Variant
Dim swDraw As DrawingDoc
Dim swSheet As Sheet
Dim swView As View
Dim viewNames As Variant
Dim swBomAnnotations As BomTableAnnotation
Dim asmPath As String
Dim asmName As String
Dim docNames() As String
Dim docPaths() As String
Dim viewName As String
Dim i As Integer
Dim sourceCount As Integer
Dim label As MSForms.label
Dim listBox As MSForms.CheckBox
Dim result As Boolean
Dim swConfig As String


'********************************************************************************************************************************************************

' Set the parameters below for your specific use case. These include the drawing template, format template, and bom template.

'********************************************************************************************************************************************************


'Include components in subassemblies or not
Const subAssemblies As Boolean = False

'Full path to drawing template
Const drawingTemplatePath As String = "C:\ProgramData\SOLIDWORKS\SOLIDWORKS 2018\templates\Drawing.drwdot"

'Sheet format Template
Const drawingFormatPath As String = "C:\ProgramData\SOLIDWORKS\SOLIDWORKS 2018\lang\english\sheetformat\a4 - landscape.slddrt"

'BOM Template Path
Const bomTemplate As String = "C:\Timberock\Administration\Timberock Engineering Templates\BOM Templates\Timberock Assembly BOM Template.sldbomtbt"

'Page size
Const paperSize As Integer = swDwgPaperAsize

'X and Y position of view inserted into each sheet in meters. These will probably have to be tweaked for your template
Const x As Double = 0.06
Const y As Double = 0.075

'Set the view to insert into each document
Const namedView As String = "*Isometric"

'Set the view to shaded edges
Const displayMode As Integer = swSHADED_EDGES

'BOM Anchor Point
Const bomAnchor As Long = swBOMConfigurationAnchor_TopLeft

'Bom Type
Const bomType As Long = swBomType_TopLevelOnly

'Bom Numbering
Const bomNumbering As Integer = swNumberingType_None


'********************************************************************************************************************************************************

' This function initializes the form. It will return false if there are any problems and true otherwise. It loads all of the components into the listbox

'********************************************************************************************************************************************************


Public Function initialize() As Boolean

'Get Solidworks Apps
Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc

If IsNull(swModel) Then initialize = False

'Check for Assembly, get path, title, and dependencies. Get the active configuration name. Exit if not assembly type.
If swModel.GetType = swDocASSEMBLY Then
asmPath = swModel.GetPathName
asmName = swModel.GetTitle
docDepend = swApp.GetDocumentDependencies2(asmPath, subAssemblies, True, False)
swConfig = swApp.GetActiveConfigurationName(asmPath)
Else
MsgBox ("Error! Not an Assembly File")
Exit Function
End If

'Make sure document has dependencies
If IsEmpty(docDepend) Then Exit Function

'Loop through the document dependencies and built a list of the names and paths
i = 0
sourceCount = 1
While i <= UBound(docDepend)

ReDim Preserve docNames(sourceCount)
ReDim Preserve docPaths(sourceCount)

docNames(sourceCount) = docDepend(i)
docPaths(sourceCount) = docDepend(i + 1)

i = i + 2
sourceCount = sourceCount + 1
Wend

'Add all the names to the list box
drawingComponents.componentListBox.Clear
With drawingComponents.componentListBox

For i = 1 To UBound(docNames)

.AddItem docNames(i)

Next i

End With

initialize = True

End Function


Private Sub componentListBox_Click()

End Sub

'********************************************************************************************************************************************************

' This is where all of the code happens to create the drawing. Basic Process:

' 1. Create a new drawing with the template specified
' 2. Add the view specified to the first sheet
' 3. Add the BOM of the Assembly to the anchor point specified
' 4. Loop through components in listbox
' 5. Create a new sheet for each selected component and activate it
' 6. Place the view on the new sheet
' 7. Activate the first sheet with the assembly
' 8. Close the form

'********************************************************************************************************************************************************


Private Sub okButton_Click()

Dim sheetProperties As Variant
Dim scale1 As Double
Dim scale2 As Double

'Create a new drawing using the template path
Set swDraw = swApp.INewDocument2(drawingTemplatePath, paperSize, 0, 0)
Set swSheet = swDraw.GetCurrentSheet
swSheet.SetName (asmName)

'Insert the view for the Assembly
Set swView = insertView(asmPath, namedView, displayMode, x, y)

'Insert the BOM for the assembly
Set swBomAnnotations = swView.InsertBomTable4(True, 0, 0, bomAnchor, bomType, swConfig, bomTemplate, False, bomNumbering, False)

'Get the sheet properties to ensure all sheets have the same scale
sheetProperties = swSheet.GetProperties
scale1 = sheetProperties(2)
scale2 = sheetProperties(3)

'Loop through the components in the list box and add a sheet with a view if they were selected
For i = 1 To drawingComponents.componentListBox.ListCount
If drawingComponents.componentListBox.Selected(i - 1) Then

swDraw.NewSheet4 docNames(i), swDwgPapersUserDefined, swDwgTemplateCustom, scale1, scale2, False, drawingFormatPath, 0, 0, "", 0.5, 0.5, 0.5, 0.5, 1, 1
swDraw.ActivateSheet (docNames(i))

Set swView = insertView(docPaths(i), namedView, displayMode, x, y)

End If
Next i

'Activate the first page and zoom to fit the drawing
swDraw.ActivateSheet (asmName)
Set swModel = swApp.ActiveDoc
swModel.ViewZoomtofit2

'Close the form
Unload drawingComponents

End Sub


'********************************************************************************************************************************************************

' This function places a view on the current sheet of the part/assembly specified and returns the view.

' fullPath is the path to the part/assembly you want to add. e.g. C:\Temp\part.sldprt
' viewName is the view to insert. e.g. *Isometric, *Front, etc.
' displayMode is the mode in which to display the model. e.g. swSHADED_EDGES, swWIREFRAME, ect.
' xPos and yPos give the coordinate to place the view in meters from the bottom left corner of the drawing

' example: set swView = insertView(C:\Temp\part.sldprt, "*Isometric", swSHADED_EDGES, 0.05, 0.05)

'********************************************************************************************************************************************************

Public Function insertView(fullPath As String, viewName As String, displayMode As Integer, xPos As Double, yPos As Double) As View

Dim swApp As SldWorks.SldWorks
Dim swModel As ModelDoc2
Dim swDraw As DrawingDoc
Dim swSheet As Sheet
Dim swView As View
Dim viewNames As Variant
Dim i As Integer

'Get Solidworks Apps
Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc

'Check for drawing type
If swModel.GetType = swDocDRAWING Then
Set swDraw = swModel
Set swSheet = swDraw.GetCurrentSheet
Else
MsgBox ("Error! Not a Drawing File")
Exit Function
End If

'Generate the pallete views and get their names
swDraw.GenerateViewPaletteViews (fullPath)
viewNames = swDraw.GetDrawingPaletteViewNames()

'Loop through their names and insert the view
For i = 0 To UBound(viewNames)

If viewNames(i) = viewName Then

Set swView = swDraw.DropDrawingViewFromPalette2(viewName, xPos, yPos, 0)
swView.SetDisplayMode3 True, displayMode, False, True
Exit For
End If

Next i

Set insertView = swView

End Function


'********************************************************************************************************************************************************

' Cancel button closes the form

'********************************************************************************************************************************************************


'If cancel button is clicked close the form
Private Sub cancelButton_Click()
'close the form and don't do anything
Unload drawingComponents
End Sub


'********************************************************************************************************************************************************

' Check/uncheck all will select/unselect all itmes in the listbox

'********************************************************************************************************************************************************


Private Sub checkAll_Click()

Dim i As Integer

If checkAll.Value = True Then
For i = 0 To componentListBox.ListCount - 1
componentListBox.Selected(i) = True
Next i
End If

If checkAll.Value = False Then
For i = 0 To componentListBox.ListCount - 1
componentListBox.Selected(i) = False
Next i
End If

End Sub

Private Sub UserForm_Click()

End Sub

From what I can see, it pulls a specific path for a BOM Template for the parts but does not. I would like to know what adjustments I can make so that the macro pulls a specific path for the BOM Template of the assemblies as well. 

Any help would be greatly appreciated.