/
Aluminum Extrusion Table Macro

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:

Public Sub TableTop(InputWidth As Double, InputLength As Double)

Set swApp = Application.SldWorks

' New Document
    Dim swSheetWidth As Double
    swSheetWidth = 0
    Dim swSheetHeight As Double
    swSheetHeight = 0
    Set Part = swApp.NewDocument("C:\ProgramData\SolidWorks\SOLIDWORKS 2020\templates\Part.prtdot", 0, swSheetWidth, swSheetHeight)
    Dim swPart As PartDoc
    Set swPart = Part
    swApp.ActivateDoc2 "Part2", False, longstatus
    Set Part = swApp.ActiveDoc
    Dim myModelView As Object
    Set myModelView = Part.ActiveView
    myModelView.FrameState = swWindowState_e.swWindowMaximized
    
' Named View
    Part.ShowNamedView2 "*Trimetric", 8
    Part.ViewZoomtofit2
    
' Variables
    Dim width As Double
    Dim length As Double
    
    width = InputWidth
    length = InputLength
    
' Create Sketch
    Dim vSkLines As Variant
    vSkLines = Part.SketchManager.CreateCenterRectangle(0, 0, 0, width / 2, 0.0127, 0)
    
' Fully Define
    Dim hDatumObj As Object
    Dim vDatumObj As Object
    longstatus = Part.SketchManager.FullyDefineSketch(True, True, 1023, True, 1, hDatumObj, 1, vDatumObj, -1, -1)
    Part.SketchManager.InsertSketch True

' Named View
    Part.ShowNamedView2 "*Trimetric", 8
    Part.ViewZoomtofit2
    Dim myFeature As Object
    boolstatus = Part.Extension.SelectByID2("Sketch1", "SKETCH", 0, 0, 0, False, 0, Nothing, 0)
    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 InsertPlaneTT(length, width)
    
' Saving
    longstatus = Part.SaveAs3("C:\Users\jaydo\Documents\Waterloo\Coop F2020\Macros\Models\Legs\Table_Top.SLDPRT", 0, 2)

' Get Title

    Dim part_name As String
    part_name = Part.GetTitle()

' Close Doc
    swApp.CloseDoc part_name

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: 

Public Sub InsertPlaneAl(distance As Double)

Dim dist As Double
dist = distance

' Create Planes
    boolstatus = Part.Extension.SelectByID2("Right Plane", "PLANE", 0, 0, 0, True, 0, Nothing, 0)
    Dim myRefPlane As Object
    Set myRefPlane = Part.FeatureManager.InsertRefPlane(8, 0.01, 0, 0, 0, 0)
    Part.ClearSelection2 True
    boolstatus = Part.Extension.SelectByID2("Plane1", "PLANE", 0, 0, 0, False, 0, Nothing, 0)
    Part.ClearSelection2 True
    boolstatus = Part.Extension.SelectByID2("Plane1", "PLANE", 0, 0, 0, False, 0, Nothing, 0)
    boolstatus = Part.SelectedFeatureProperties(0, 0, 0, 0, 0, 0, 0, 1, 0, "Right_Side")
    Part.ClearSelection2 True
    
    boolstatus = Part.Extension.SelectByID2("Right Plane", "PLANE", 0, 0, 0, True, 0, Nothing, 0)
    Set myRefPlane = Part.FeatureManager.InsertRefPlane(264, 0.01, 0, 0, 0, 0)
    Part.ClearSelection2 True
    boolstatus = Part.Extension.SelectByID2("Plane2", "PLANE", 0, 0, 0, False, 0, Nothing, 0)
    Part.ClearSelection2 True
    boolstatus = Part.Extension.SelectByID2("Plane2", "PLANE", 0, 0, 0, False, 0, Nothing, 0)
    boolstatus = Part.SelectedFeatureProperties(0, 0, 0, 0, 0, 0, 0, 1, 0, "Left_Side")
    Part.ClearSelection2 True
    
    boolstatus = Part.Extension.SelectByID2("Top Plane", "PLANE", 0, 0, 0, True, 0, Nothing, 0)
    Set myRefPlane = Part.FeatureManager.InsertRefPlane(8, 0.01, 0, 0, 0, 0)
    Part.ClearSelection2 True
    boolstatus = Part.Extension.SelectByID2("Plane3", "PLANE", 0, 0, 0, False, 0, Nothing, 0)
    Part.ClearSelection2 True
    boolstatus = Part.Extension.SelectByID2("Plane3", "PLANE", 0, 0, 0, False, 0, Nothing, 0)
    boolstatus = Part.SelectedFeatureProperties(0, 0, 0, 0, 0, 0, 0, 1, 0, "Top_Side")
    Part.ClearSelection2 True
    
    boolstatus = Part.Extension.SelectByID2("Top Plane", "PLANE", 0, 0, 0, True, 0, Nothing, 0)
    Set myRefPlane = Part.FeatureManager.InsertRefPlane(264, 0.01, 0, 0, 0, 0)
    Part.ClearSelection2 True
    boolstatus = Part.Extension.SelectByID2("Front Plane", "PLANE", 0, 0, 0, True, 0, Nothing, 0)
    Set myRefPlane = Part.FeatureManager.InsertRefPlane(8, dist, 0, 0, 0, 0)
    Part.ClearSelection2 True
    boolstatus = Part.Extension.SelectByID2("Plane4", "PLANE", 0, 0, 0, False, 0, Nothing, 0)
    Part.ClearSelection2 True
    boolstatus = Part.Extension.SelectByID2("Plane4", "PLANE", 0, 0, 0, False, 0, Nothing, 0)
    Part.ClearSelection2 True
    boolstatus = Part.Extension.SelectByID2("Plane4", "PLANE", 0, 0, 0, False, 0, Nothing, 0)
    boolstatus = Part.SelectedFeatureProperties(0, 0, 0, 0, 0, 0, 0, 1, 0, "Bottom_Side")
    boolstatus = Part.Extension.SelectByID2("Plane5", "PLANE", 0, 0, 0, False, 0, Nothing, 0)
    boolstatus = Part.Extension.SelectByID2("Plane5", "PLANE", 0, 0, 0, False, 0, Nothing, 0)
    Part.ClearSelection2 True
    boolstatus = Part.Extension.SelectByID2("Plane5", "PLANE", 0, 0, 0, False, 0, Nothing, 0)
    boolstatus = Part.SelectedFeatureProperties(0, 0, 0, 0, 0, 0, 0, 1, 0, "Front_Ext")

'Hide planes
    boolstatus = Part.Extension.SelectByID2("Front_Ext", "PLANE", 0, 0, 0, False, 0, Nothing, 0)
    Part.BlankRefGeom
    boolstatus = Part.Extension.SelectByID2("Bottom_Side", "PLANE", 0, 0, 0, False, 0, Nothing, 0)
    Part.BlankRefGeom
    boolstatus = Part.Extension.SelectByID2("Top_Side", "PLANE", 0, 0, 0, False, 0, Nothing, 0)
    Part.BlankRefGeom
    boolstatus = Part.Extension.SelectByID2("Left_Side", "PLANE", 0, 0, 0, False, 0, Nothing, 0)
    Part.BlankRefGeom
    boolstatus = Part.Extension.SelectByID2("Right_Side", "PLANE", 0, 0, 0, False, 0, Nothing, 0)
    Part.BlankRefGeom
End Sub

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


  1.  The next for this macro is to have the model it creates get run through static simulations in order to determine weaknesses.
  2. 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. 
  3. 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**

  File Modified

File Aluminum Extrusion.SLDPRT

Dec 18, 2020 by Former user

File Bracket_Assm.SLDPRT

Dec 18, 2020 by Former user

File Bracket_Assm_Opp.SLDPRT

Dec 18, 2020 by Former user

File Table_Creation.swp

Dec 18, 2020 by Former user

Contributors:

Related content

Creating an Automated, Personalized Frame Generator Using SolidWorks VBA
Creating an Automated, Personalized Frame Generator Using SolidWorks VBA
More like this
Metal Extrusion Selection
Metal Extrusion Selection
More like this
SolidWorks Braiding Macro
SolidWorks Braiding Macro
More like this
Metal Extrusion
More like this
SolidWorks Macros with Visual Basic for Applications (VBA)
SolidWorks Macros with Visual Basic for Applications (VBA)
More like this