Untitled
Anonymous
Plain Text
09/26/2025 2:50 PM
13.3 KB
23
Indexable
Never
' VBA SolidWorks Macro
' Creates a simplified BMW-style chassis (parts + assembly)
' Save this as a new macro in SolidWorks and Run.
Option Explicit
Dim swApp As SldWorks.SldWorks
Dim Part As ModelDoc2
Dim boolstatus As Boolean
Dim longstatus As Long, longwarnings As Long
Sub main()
Set swApp = Application.SldWorks
Dim targetFolder As String
targetFolder = ""
targetFolder = swApp.GetUserPreferenceStringValue(swUserPreferenceStringValue_e.swDefaultDirectoryDocuments)
' Prompt user for folder (optional override)
Dim dlgResult As String
dlgResult = InputBox("Enter folder path where parts will be saved (full path). Leave blank to use Documents:", "Output Folder", targetFolder)
If dlgResult <> "" Then
targetFolder = dlgResult
End If
If Right(targetFolder, 1) <> "\" Then targetFolder = targetFolder & "\"
' Create parts
Create_Rail_Left targetFolder & "Rail_Left.sldprt"
Create_Rail_Right targetFolder & "Rail_Right.sldprt"
Create_Front_Crossmember targetFolder & "Front_Crossmember.sldprt"
Create_Rear_Crossmember targetFolder & "Rear_Crossmember.sldprt"
Create_Center_Tunnel targetFolder & "Center_Tunnel.sldprt"
Create_Floor_Panel targetFolder & "Floor_Panel.sldprt"
Create_Front_Subframe targetFolder & "Front_Subframe_L.sldprt", True
Create_Front_Subframe targetFolder & "Front_Subframe_R.sldprt", False
Create_Side_Sill targetFolder & "Side_Sill_L.sldprt", True
Create_Side_Sill targetFolder & "Side_Sill_R.sldprt", False
Create_Rear_Bumper targetFolder & "Rear_Bumper_Beam.sldprt"
' Create assembly
Create_Assembly targetFolder
MsgBox "Done. Parts and assembly saved in: " & targetFolder, vbInformation, "BMW Chassis Macro"
End Sub
' Helper to create a new part document
Function NewPart() As ModelDoc2
Dim partTemplate As String
partTemplate = swApp.GetUserPreferenceStringValue(swUserPreferenceStringValue_e.swDefaultTemplatePart)
Set NewPart = swApp.NewDocument(partTemplate, 0, 0, 0)
Set Part = NewPart
Part.ShowNamedView2 "*Isometric", -1
Part.ViewZoomtofit2
End Function
Sub SaveAndClosePart(ByVal filename As String)
If Part Is Nothing Then Exit Sub
Part.Extension.SaveAs filename, 0, 2, "", longstatus, longwarnings
Part = Nothing
swApp.CloseDoc swApp.ActiveDoc.GetTitle
End Sub
' ---------- PART CREATORS ----------
' All dimensions in mm; SolidWorks default units assumed mm
Sub Create_Rail_Left(ByVal filename As String)
' Rail: box section 120x60, length 4300 mm, thin wall 4 mm
Dim doc As ModelDoc2
Set doc = NewPart()
Dim featMgr As FeatureManager
Set featMgr = doc.FeatureManager
' Sketch on Front Plane
doc.Extension.SelectByID2 "Front Plane", "PLANE", 0, 0, 0, False, 0, Nothing, 0
doc.SketchManager.InsertSketch True
' Draw rectangle centered at origin but offset in Y (left)
Dim railLength As Double: railLength = 4300#
Dim outerW As Double: outerW = 120#
Dim outerH As Double: outerH = 60#
Dim wallThk As Double: wallThk = 4#
Dim offsetY As Double: offsetY = -450# ' left side offset
' Rectangle corners
doc.SketchManager.CreateCenterRectangle 0#, offsetY, 0#, outerW / 2#, outerH / 2# + offsetY, 0#
doc.SketchManager.InsertSketch True
' Extrude with thin feature
Dim extrudeFeat As Feature
Set extrudeFeat = featMgr.FeatureExtrusion2(True, False, False, 0, 0, railLength, 0, False, False, _
False, False, 0, 0, False, False, False, True, True, False, 0, 0, False)
' Use thin feature parameters: (note API simplification)
' If thin feature not applied through this signature, use the ThinFeatureAdd method:
' simpler approach: create solid and shell - for speed we will just extrude solid and shell
' Shell to create thin wall: convert units mm to meters for shell? Shell expects same units
boolstatus = doc.Extension.SelectByID2("", "SOLIDBODY", 0, 0, 0, False, 0, Nothing, 0)
' Try shell
On Error Resume Next
featMgr.FeatureShell2 wallThk, False, True
On Error GoTo 0
doc.ClearSelection2 True
' Save
doc.ShowNamedView2 "*Front", -1
doc.ViewZoomtofit2
filename = Replace(filename, "/", "\")
doc.Extension.SaveAs filename, 0, 2, "", longstatus, longwarnings
swApp.CloseDoc doc.GetTitle
End Sub
Sub Create_Rail_Right(ByVal filename As String)
' Mirror of left rail (Y positive offset)
Dim doc As ModelDoc2
Set doc = NewPart()
Dim featMgr As FeatureManager
Set featMgr = doc.FeatureManager
doc.Extension.SelectByID2 "Front Plane", "PLANE", 0, 0, 0, False, 0, Nothing, 0
doc.SketchManager.InsertSketch True
Dim railLength As Double: railLength = 4300#
Dim outerW As Double: outerW = 120#
Dim outerH As Double: outerH = 60#
Dim wallThk As Double: wallThk = 4#
Dim offsetY As Double: offsetY = 450# ' right side offset
doc.SketchManager.CreateCenterRectangle 0#, offsetY, 0#, outerW / 2#, outerH / 2# + offsetY, 0#
doc.SketchManager.InsertSketch True
Dim extrudeFeat As Feature
Set extrudeFeat = featMgr.FeatureExtrusion2(True, False, False, 0, 0, railLength, 0, False, False, _
False, False, 0, 0, False, False, False, True, True, False, 0, 0, False)
On Error Resume Next
featMgr.FeatureShell2 wallThk, False, True
On Error GoTo 0
doc.ShowNamedView2 "*Front", -1
doc.ViewZoomtofit2
filename = Replace(filename, "/", "\")
doc.Extension.SaveAs filename, 0, 2, "", longstatus, longwarnings
swApp.CloseDoc doc.GetTitle
End Sub
Sub Create_Front_Crossmember(ByVal filename As String)
Dim doc As ModelDoc2
Set doc = NewPart()
Dim featMgr As FeatureManager
Set featMgr = doc.FeatureManager
' Crossmember: 1200 x 120 x 120 (thin wall 4mm), extrude in Z
doc.Extension.SelectByID2 "Top Plane", "PLANE", 0, 0, 0, False, 0, Nothing, 0
doc.SketchManager.InsertSketch True
doc.SketchManager.CreateCenterRectangle 0#, 0#, 0#, 600#, 60#, 0#
doc.SketchManager.InsertSketch True
Dim length As Double: length = 120# ' depth
Dim wallThk As Double: wallThk = 4#
Dim feat As Feature
Set feat = featMgr.FeatureExtrusion2(True, False, False, 0, 0, length, 0, False, False, _
False, False, 0, 0, False, False, False, True, True, False, 0, 0, False)
On Error Resume Next
featMgr.FeatureShell2 wallThk, False, True
On Error GoTo 0
filename = Replace(filename, "/", "\")
doc.Extension.SaveAs filename, 0, 2, "", longstatus, longwarnings
swApp.CloseDoc doc.GetTitle
End Sub
Sub Create_Rear_Crossmember(ByVal filename As String)
' Slightly similar to front crossmember
Create_Front_Crossmember filename
End Sub
Sub Create_Center_Tunnel(ByVal filename As String)
Dim doc As ModelDoc2
Set doc = NewPart()
Dim featMgr As FeatureManager
Set featMgr = doc.FeatureManager
' Tunnel: trapezoidal approx as rectangle 300 x 120 extruded length 2600 mm
doc.Extension.SelectByID2 "Front Plane", "PLANE", 0, 0, 0, False, 0, Nothing, 0
doc.SketchManager.InsertSketch True
doc.SketchManager.CreateCenterRectangle 0#, 0#, 0#, 150#, 60#, 0#
doc.SketchManager.InsertSketch True
Dim length As Double: length = 2600#
Dim wallThk As Double: wallThk = 4#
Dim feat As Feature
Set feat = featMgr.FeatureExtrusion2(True, False, False, 0, 0, length, 0, False, False, _
False, False, 0, 0, False, False, False, True, True, False, 0, 0, False)
On Error Resume Next
featMgr.FeatureShell2 wallThk, False, True
On Error GoTo 0
filename = Replace(filename, "/", "\")
doc.Extension.SaveAs filename, 0, 2, "", longstatus, longwarnings
swApp.CloseDoc doc.GetTitle
End Sub
Sub Create_Floor_Panel(ByVal filename As String)
Dim doc As ModelDoc2
Set doc = NewPart()
Dim featMgr As FeatureManager
Set featMgr = doc.FeatureManager
' Floor panel: thin plate 3200 x 1150 x 2.5 mm
doc.Extension.SelectByID2 "Top Plane", "PLANE", 0, 0, 0, False, 0, Nothing, 0
doc.SketchManager.InsertSketch True
doc.SketchManager.CreateCenterRectangle 0#, 0#, 0#, 1600#, 575#, 0#
doc.SketchManager.InsertSketch True
Dim depth As Double: depth = 2.5#
Dim feat As Feature
Set feat = featMgr.FeatureExtrusion2(True, False, False, 0, 0, depth, 0, False, False, _
False, False, 0, 0, False, False, False, True, True, False, 0, 0, False)
filename = Replace(filename, "/", "\")
doc.Extension.SaveAs filename, 0, 2, "", longstatus, longwarnings
swApp.CloseDoc doc.GetTitle
End Sub
Sub Create_Front_Subframe(ByVal filename As String, ByVal isLeft As Boolean)
Dim doc As ModelDoc2
Set doc = NewPart()
Dim featMgr As FeatureManager
Set featMgr = doc.FeatureManager
' Plate 450 x 300 x 8 mm
doc.Extension.SelectByID2 "Top Plane", "PLANE", 0, 0, 0, False, 0, Nothing, 0
doc.SketchManager.InsertSketch True
doc.SketchManager.CreateCenterRectangle 0#, 0#, 0#, 225#, 150#, 0#
doc.SketchManager.InsertSketch True
Dim depth As Double: depth = 8#
Dim feat As Feature
Set feat = featMgr.FeatureExtrusion2(True, False, False, 0, 0, depth, 0, False, False, _
False, False, 0, 0, False, False, False, True, True, False, 0, 0, False)
filename = Replace(filename, "/", "\")
doc.Extension.SaveAs filename, 0, 2, "", longstatus, longwarnings
swApp.CloseDoc doc.GetTitle
End Sub
Sub Create_Side_Sill(ByVal filename As String, ByVal isLeft As Boolean)
Dim doc As ModelDoc2
Set doc = NewPart()
Dim featMgr As FeatureManager
Set featMgr = doc.FeatureManager
' Side Sill: 4000 mm length, 80x60 cross section, 3 mm wall - approximated as solid beam extrude
doc.Extension.SelectByID2 "Front Plane", "PLANE", 0, 0, 0, False, 0, Nothing, 0
doc.SketchManager.InsertSketch True
doc.SketchManager.CreateCenterRectangle 0#, 0#, 0#, 40#, 30#, 0#
doc.SketchManager.InsertSketch True
Dim length As Double: length = 4000#
Dim feat As Feature
Set feat = featMgr.FeatureExtrusion2(True, False, False, 0, 0, length, 0, False, False, _
False, False, 0, 0, False, False, False, True, True, False, 0, 0, False)
filename = Replace(filename, "/", "\")
doc.Extension.SaveAs filename, 0, 2, "", longstatus, longwarnings
swApp.CloseDoc doc.GetTitle
End Sub
Sub Create_Rear_Bumper(ByVal filename As String)
Dim doc As ModelDoc2
Set doc = NewPart()
Dim featMgr As FeatureManager
Set featMgr = doc.FeatureManager
' Beam 1400 x 100 x 4 mm
doc.Extension.SelectByID2 "Top Plane", "PLANE", 0, 0, 0, False, 0, Nothing, 0
doc.SketchManager.InsertSketch True
doc.SketchManager.CreateCenterRectangle 0#, 0#, 0#, 700#, 50#, 0#
doc.SketchManager.InsertSketch True
Dim depth As Double: depth = 4#
Dim feat As Feature
Set feat = featMgr.FeatureExtrusion2(True, False, False, 0, 0, depth, 0, False, False, _
False, False, 0, 0, False, False, False, True, True, False, 0, 0, False)
filename = Replace(filename, "/", "\")
doc.Extension.SaveAs filename, 0, 2, "", longstatus, longwarnings
swApp.CloseDoc doc.GetTitle
End Sub
' ---------- ASSEMBLY CREATION ----------
Sub Create_Assembly(ByVal folderPath As String)
Dim asmDoc As ModelDoc2
Dim swAssyDoc As AssemblyDoc
Dim status As Boolean
' New Assembly
Dim asmTemplate As String
asmTemplate = swApp.GetUserPreferenceStringValue(swUserPreferenceStringValue_e.swDefaultTemplateAssembly)
Set asmDoc = swApp.NewDocument(asmTemplate, 0, 0, 0)
Set swAssyDoc = asmDoc
asmDoc.ShowNamedView2 "*Isometric", -1
asmDoc.ViewZoomtofit2
' Insert components
Dim compPath As String
compPath = folderPath & "Rail_Left.sldprt"
swAssyDoc.AddComponent5 compPath, 0, "", False, "", 0, 0, 0
compPath = folderPath & "Rail_Right.sldprt"
swAssyDoc.AddComponent5 compPath, 0, "", False, "", 4300, 0, 0
compPath = folderPath & "Front_Crossmember.sldprt"
swAssyDoc.AddComponent5 compPath, 0, "", False, "", 600, 0, 0
compPath = folderPath & "Rear_Crossmember.sldprt"
swAssyDoc.AddComponent5 compPath, 0, "", False, "", 3900, 0, 0
compPath = folderPath & "Center_Tunnel.sldprt"
swAssyDoc.AddComponent5 compPath, 0, "", False, "", 1100, 0, 0
compPath = folderPath & "Floor_Panel.sldprt"
swAssyDoc.AddComponent5 compPath, 0, "", False, "", 1600, 0, 0
compPath = folderPath & "Front_Subframe_L.sldprt"
swAssyDoc.AddComponent5 compPath, 0, "", False, "", 900, -700, 0
compPath = folderPath & "Front_Subframe_R.sldprt"
swAssyDoc.AddComponent5 compPath, 0, "", False, "", 900, 700, 0
compPath = folderPath & "Side_Sill_L.sldprt"
swAssyDoc.AddComponent5 compPath, 0, "", False, "", 100, -900, 0
compPath = folderPath & "Side_Sill_R.sldprt"
swAssyDoc.AddComponent5 compPath, 0, "", False, "", 100, 900, 0
compPath = folderPath & "Rear_Bumper_Beam.sldprt"
swAssyDoc.AddComponent5 compPath, 0, "", False, "", 4500, 0, 0
' Save assembly
Dim asmName As String
asmName = folderPath & "BMW_Chassis_Assembly.sldasm"
asmDoc.Extension.SaveAs asmName, 0, 2, "", longstatus, longwarnings
swApp.CloseDoc asmDoc.GetTitle
End SubEditor is loading...
Leave a Comment