Aluminum Extrusion Table Macro
Introduction
This macro was created to have an easy starting place to learn the basics of VBA. The goal was to create a table in SolidWorks with only giving the computer the length, width and, height of the table and then run it through a simulations to determine where the table has weak spots and can be improved. After the simulations the hope was to take the data learned from the simulation and using it in the next creation of the table.
Macro Breakdown
UserForm
The macro starts by opening a UserForm that lets the user input the desired table Length, width and, height.
UserForm in Table Macro |
Extrusion Creation
The next step in the macro is the making of the aluminum extrusions. This is done by opening up a premade sketch and then extruding the sketch to a specific length. After the sketch has been extruded the program adds reference planes for future mating, saves it with a name specific to the part and then closes the document. Below is an example of the code:
Public Sub ExtrusionLengthSide(InputLength As Double) ' Open Doc Set Part = swApp.OpenDoc6("C:\Users\jaydo\Documents\Waterloo\Coop F2020\Macros\Models\Legs\Aluminum Extrusion.SLDPRT", 1, 0, "", longstatus, longwarnings) swApp.ActivateDoc2 "Aluminum Extrusion", False, longstatus Set Part = swApp.ActiveDoc boolstatus = Part.Extension.SelectByID2("Sketch1", "SKETCH", 0, 0, 0, False, 0, Nothing, 0) ' Variables Dim length As Double length = InputLength - 0.04 ' Named View Part.ShowNamedView2 "*Trimetric", 8 Part.ViewZoomtofit2 Dim myFeature As Object Set myFeature = Part.FeatureManager.FeatureExtrusion2(True, False, False, 0, 0, length, 0.01, False, False, False, False, 1.74532925199433E-02, 1.74532925199433E-02, False, False, False, False, True, True, True, 0, 0, False) Part.SelectionManager.EnableContourSelection = False ' Create Planes Call InsertPlaneAl(length) ' Saving longstatus = Part.SaveAs3("C:\Users\jaydo\Documents\Waterloo\Coop F2020\Macros\Models\Legs\Side_Piece.SLDPRT", 0, 2) ' Close Doc swApp.CloseDoc "Aluminum Extrusion" End Sub
Table Top Creation
The next thing to be done is the creating the top. This is very similar to the extrusions accept since the table top has three variables length, width, and thickness a single sketch can't be opened for every table. This sub routine opens a new part file and then creates a rectangular sketch using the the width and a thickness of a 1/2". Then the sketch is extruded, reference planes are inserted and the document is saved and closed. Below is an example of this code:
Creating Planes
Adding reference planes is an easy way to create mates. All of the mates codes take in the dimensions of the part and create a planes on each surface of the parts. This is done because faces are much harder to reference in VBA. This code creates planes then hides them: Below is an example of the code:
Assembly
The last step in the macro is the assembly. Using a For loop the macro inserts the proper number extrusions and braces to a newly opened assembly file. Next the macro mates the parts together to make a table. To create the mates the best option is to use the record function to get the first bit of code and then copy and paste the code but change the mating parts and planes to suit the scenario. Below is an example of the mating code:
boolstatus = Part.Extension.SelectByID2("Top_Side@Leg-2@" + title, "PLANE", 0, 0, 0, True, 1, Nothing, 0) boolstatus = Part.Extension.SelectByID2("Front_Ext@Side_Piece-1@" + title, "PLANE", 0, 0, 0, True, 1, Nothing, 0) ' Create CoincidentMateFeatureData Dim MateData As CoincidentMateFeatureData Set MateData = Part.CreateMateData(0) ' Set the Entities To Mate Dim EntitiesToMate(1) As Object Set EntitiesToMate(0) = Part.SelectionManager.GetSelectedObject6(1, -1) Set EntitiesToMate(1) = Part.SelectionManager.GetSelectedObject6(2, -1) Dim EntitiesToMateVar As Variant EntitiesToMateVar = EntitiesToMate MateData.EntitiesToMate = (EntitiesToMateVar) ' Set the Mate Alignment MateData.MateAlignment = 1 ' Create the mate Dim MateFeature As Feature Set MateFeature = Part.CreateMate(MateData) Part.ClearSelection2 True Part.EditRebuild3
Full Video of Macro
Future Plans
- The next for this macro is to have the model it creates get run through static simulations in order to determine weaknesses.
- After the simulations, the next step would be write code to take in the data from the simulation and use it to improve the first model. For example, if there is a lot of deformation in one spot the macro will add extra leg for support.
- Repeating the first 2 steps will create an automated iterative design loop to come up with the best design possible.
Macro File
** TO BE ABLE TO USE THIS MACRO THE FILE PATHS NEED TO BE CHANGED**