JamesMcD
Automotive
- Dec 10, 2001
- 25
I'm new to VBA and this macro-writing business, but here is my attempt.
This macro:
-inserts a sheet, naming it with the correct number (it does not get called sheetcount + 1 because we want to disregard one of the sheets when numbering them)
-Deletes a particular template note on inserted pages (I could not get it to delete by selecting the note I.D., so I did a dirty workaround of using a selection box.)
-Retrieves an array of the current sheet names
-Sorts them (I think...Haven't seen the results of this yet.)
-Applies the sorted names back to the drawing file using the SolidWorks IReorderSheets function.(Supposedly. This is the part I can't get to work. I get a "byRef argument type mismatch" error on "value = instance.IReorderSheets(SheetCount2, vSheetNames")
Has anyone used the SolidWorks API IReorderSheets function before? It seems that I have to dim vSheetNames As Variant to get that array created, but then the IReorderSheets function doesn't like the Variant type? I tried to change vSheetNames to String throughout but that put the error on the "vSheetNames = swDoc.GetSheetNames" line.
Any help would be greatly appreciated.
Thanks,
James
------------------------
Dim instance As IDrawingDoc
Dim value As Boolean
Dim swApp As Object
Dim vSheetNames As Variant
Dim swCurrentSheet As Object
Dim Part As Object
Dim boolstatus As Boolean
Dim longstatus As Long, longwarnings As Long
Dim SheetCount2 As Integer
Dim SheetCount As Integer
Sub main()
Set swApp = _
Application.SldWorks
Set swDoc = swApp.ActiveDoc
If swDoc.GetType <> swDocDRAWING Then
MsgBox "This macro is to be used with drawings only."
Exit Sub
End If
' The above prevents the user from running this macro in part mode.
Set Part = swApp.ActiveDoc
SheetCount = swDoc.GetSheetCount 'counts the sheets
boolstatus = Part.NewSheet3("Sheet " & SheetCount, 12, 12, 1, 5, False, "u:\james mcdonald\template project\NEW SHEET.slddrt", 0.2794, 0.2159, "Default")
' New sheet is created and numbered to follow the counted sheets
Set swCurrentSheet = swDoc.GetCurrentSheet
' Current sheet ID is assigned to "swCurrentSheet" variable.
boolstatus = Part.Extension.SelectByID2("swCurrentSheet", "SHEET", 0.09845245225603, 0.1260896778594, 0, False, 0, Nothing, 0)
' Current sheet is selected
Part.EditTemplate
' Edit template mode selected
Part.ClearSelection2 True
boolstatus = Part.Extension.SketchBoxSelect("0.256344", "-0.018144", "0.000000", "0.612411", "0.061910", "0.000000")
' Selection box created to encompass part notes
Part.EditDelete
' Part notes deleted.
boolstatus = Part.Extension.SelectByID2("Sheet Format13", "SHEET", 0#, 0.125, 0, False, 0, Nothing, 0)
Part.EditSheet
' Return to edit sheet mode.
vSheetNames = swDoc.GetSheetNames
' Retrieves array of current sheet names
Call AlphaSort(vSheetNames)
' Routine to sort sheet names
SheetCount2 = swDoc.GetSheetCount
' Sheets counted a 2nd time, since one has been added.
value = instance.IReorderSheets(SheetCount2, vSheetNames)
' Call function to apply sorted sheet names to document.
End Sub
-----------------------------
Sub AlphaSort(vSheetNames As Variant)
Dim First As Integer
Dim Last As Integer
Dim i As Integer
Dim j As Integer
Dim Temp As String
Dim List As String
First = LBound(vSheetNames())
Last = UBound(vSheetNames())
For i = First To Last - 1
For j = i + 1 To Last
If vSheetNames(i) > vSheetNames(j) Then
Temp = vSheetNames(j)
vSheetNames(j) = vSheetNames(i)
vSheetNames(i) = Temp
End If
Next j
Next i
End Sub
---------------------------------
Function IReorderSheets( _
ByVal SheetCount2 As Integer, _
ByRef vSheetNames As Variant _
) As Boolean
End Function
This macro:
-inserts a sheet, naming it with the correct number (it does not get called sheetcount + 1 because we want to disregard one of the sheets when numbering them)
-Deletes a particular template note on inserted pages (I could not get it to delete by selecting the note I.D., so I did a dirty workaround of using a selection box.)
-Retrieves an array of the current sheet names
-Sorts them (I think...Haven't seen the results of this yet.)
-Applies the sorted names back to the drawing file using the SolidWorks IReorderSheets function.(Supposedly. This is the part I can't get to work. I get a "byRef argument type mismatch" error on "value = instance.IReorderSheets(SheetCount2, vSheetNames")
Has anyone used the SolidWorks API IReorderSheets function before? It seems that I have to dim vSheetNames As Variant to get that array created, but then the IReorderSheets function doesn't like the Variant type? I tried to change vSheetNames to String throughout but that put the error on the "vSheetNames = swDoc.GetSheetNames" line.
Any help would be greatly appreciated.
Thanks,
James
------------------------
Dim instance As IDrawingDoc
Dim value As Boolean
Dim swApp As Object
Dim vSheetNames As Variant
Dim swCurrentSheet As Object
Dim Part As Object
Dim boolstatus As Boolean
Dim longstatus As Long, longwarnings As Long
Dim SheetCount2 As Integer
Dim SheetCount As Integer
Sub main()
Set swApp = _
Application.SldWorks
Set swDoc = swApp.ActiveDoc
If swDoc.GetType <> swDocDRAWING Then
MsgBox "This macro is to be used with drawings only."
Exit Sub
End If
' The above prevents the user from running this macro in part mode.
Set Part = swApp.ActiveDoc
SheetCount = swDoc.GetSheetCount 'counts the sheets
boolstatus = Part.NewSheet3("Sheet " & SheetCount, 12, 12, 1, 5, False, "u:\james mcdonald\template project\NEW SHEET.slddrt", 0.2794, 0.2159, "Default")
' New sheet is created and numbered to follow the counted sheets
Set swCurrentSheet = swDoc.GetCurrentSheet
' Current sheet ID is assigned to "swCurrentSheet" variable.
boolstatus = Part.Extension.SelectByID2("swCurrentSheet", "SHEET", 0.09845245225603, 0.1260896778594, 0, False, 0, Nothing, 0)
' Current sheet is selected
Part.EditTemplate
' Edit template mode selected
Part.ClearSelection2 True
boolstatus = Part.Extension.SketchBoxSelect("0.256344", "-0.018144", "0.000000", "0.612411", "0.061910", "0.000000")
' Selection box created to encompass part notes
Part.EditDelete
' Part notes deleted.
boolstatus = Part.Extension.SelectByID2("Sheet Format13", "SHEET", 0#, 0.125, 0, False, 0, Nothing, 0)
Part.EditSheet
' Return to edit sheet mode.
vSheetNames = swDoc.GetSheetNames
' Retrieves array of current sheet names
Call AlphaSort(vSheetNames)
' Routine to sort sheet names
SheetCount2 = swDoc.GetSheetCount
' Sheets counted a 2nd time, since one has been added.
value = instance.IReorderSheets(SheetCount2, vSheetNames)
' Call function to apply sorted sheet names to document.
End Sub
-----------------------------
Sub AlphaSort(vSheetNames As Variant)
Dim First As Integer
Dim Last As Integer
Dim i As Integer
Dim j As Integer
Dim Temp As String
Dim List As String
First = LBound(vSheetNames())
Last = UBound(vSheetNames())
For i = First To Last - 1
For j = i + 1 To Last
If vSheetNames(i) > vSheetNames(j) Then
Temp = vSheetNames(j)
vSheetNames(j) = vSheetNames(i)
vSheetNames(i) = Temp
End If
Next j
Next i
End Sub
---------------------------------
Function IReorderSheets( _
ByVal SheetCount2 As Integer, _
ByRef vSheetNames As Variant _
) As Boolean
End Function