Macros can be used to help reduce repetitive tasks. The example below creates (4) ISO views for a part or assembly. It is a simple macro that helps you:
- Standardize provides a standard set of ISO views for parts and assemblies between different users. This macro can also be used on the common part and assembly templates.
- Automate the manual creation of these views would be time-consuming and non-value added. This simple macro takes a second to run while the views would take minutes to create manually. It would also be difficult to create them consistently.
Macros can be used to help reduce repetitive tasks. The example below creates (4) ISO views for a part or assembly. It is a simple macro that helps you:
- A common header can be included to document the name, author, version of SolidWorks® software used when the macro was created, and a description of the macro's function.
- The key at the top describes the meaning of color within the editor. By looking at the text, you can surmise the meaning of the code (comment, reserved word, or other code).
- The macro also traps errors that would cause the program to exit abnomally. An example is checking to see if a part or assembly is open, if not warn the user and exit the macro gracefully. Otherwise, an error message would appear. Trapping errors, verifying that the user entered an expected value, or performed a required task is a good programming practice. This way, the user is warned by the program with details of what they did wrong instead of just receiving an error.
EXAMPLE - a simple macro that was posted to the comp.cad.solidworks newsgroup
Key:
Text = Comments
Text = Reserved VB Words
Text = Non-reserved words
' ****************************************************************'
Name: ISOViews.swp
Rev: 1.1' SolidWorks
Ver: 2001 Plus'
Created By: Mike J. Wilson, modified by GJankowski'
Special thanks to Dennis Kelley and Dan Hanger for tips'
SolidWorks Ver: 2001 Plus'
Description: This macro will create a set of ISO view for a part or assy
' ****************************************************************
Dim swApp As Object 'SldWorks object
Dim Part As Object 'ModelDoc object
'Consistent with type names defined in swconst.bas
'This way the entire file does not need to be added to the application
Const swDocPART = 1
Const swDocASSEMBLY = 2
Sub main()
'Get the active session
Set swApp = CreateObject("SldWorks.Application")
'Get handle to the active SolidWorks part
Set Part = swApp.ActiveDoc
'Set the message box props
Dim Msg, Style, Title, Help, Ctxt, Response, MyString
Title = "Add ISO Views"
' Define the title'If the document is not a part or assy, warn the user and quit
If (Part.gettype = swDocPART) Or (Part.gettype = swDocASSEMBLY)
Then
'Do nothing it is part or assy
Else
'Define the message
Msg = "This application only works on a part or assy."
'Display the message box
Response = MsgBox(Msg, 0, Title)
Exit Sub
End If
'Delete existing views with these names if they exist
Part.DeleteNamedView ("ISO Front-Right")
Part.DeleteNamedView ("ISO Right-Rear")
Part.DeleteNamedView ("ISO Rear-Left")
Part.DeleteNamedView ("ISO Left-Front")
'Math for the view rotation
pi = 4 * Atn(1)
Z = Tan(30 * pi / 180)
X = Atn(Z / Sqr(-Z * Z + 1))
Y = -45 * pi / 180
Part.ShowNamedView2 "*Front", -1 'Base the new view on this
viewPart.ActiveView().RotateAboutCenter X, Y 'Rotate the part
Part.ViewZoomtofit2 'Zoom to fit
Part.NameView ("ISO Front-Right") 'Save the new view
Part.ShowNamedView2 "*Right", -1
Part.ActiveView().RotateAboutCenter X, Y
Part.ViewZoomtofit2Part.NameView ("ISO Right-Rear")
Part.ShowNamedView2 "*Back", -1
Part.ActiveView().RotateAboutCenter X, Y
Part.ViewZoomtofit2
Part.NameView ("ISO Rear-Left")
Part.ShowNamedView2 "*Left", -1
Part.ActiveView().RotateAboutCenter X, Y
Part.ViewZoomtofit2
Part.NameView ("ISO Left-Front")
'Release the SWX and ModelDoc objects
Set Part = Nothing
Set swApp = Nothing
End Sub
This macro is a good example of a simple, useful tool that can easily be created using VBA (Visual Basic for Applications) and the SolidWorks API.