Hey guys,
SW2018 has this cool feature of creating a bounding box.
I would like to create a macro that will display the dimensions of the bounding box as well as the mass of the part/assembly.
Thanks.
Hey guys,
SW2018 has this cool feature of creating a bounding box.
I would like to create a macro that will display the dimensions of the bounding box as well as the mass of the part/assembly.
Thanks.
There have always been bounding box create function, is this not true?
How to Create a Bounding Box for Any Part in SOLIDWORKS
Macro to create Bounding Box - How about Cylinders?
Macro to create Bounding Box - How about Cylinders?Does anyone have Assembly Bounding box macro for SW 2017?
Hi Kenneth,
I'm still on SW2015 so I use this macro to create BB on models. It doesn't get the mass but you can easily get it from custom properties.
'--------------------------------------------
' Preconditions:
' 1. Open an assembly or part document.
' 2. Run the macro.
'
' Postconditions:
' 1. Adds a 3D sketch to the assembly or part showing the bounding box.
' 2. Shows results on a message box.
'
' NOTE: The bounding box is approximated and oriented
' with the model coordinate system.
' Remember to delete the bounding box 3D sketch if not needed
'----------------------------------------------
Option Explicit
Sub main()
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swAssy As SldWorks.AssemblyDoc
Dim swPart As SldWorks.PartDoc
Dim vBox As Variant
Dim X_max As Double
Dim X_min As Double
Dim Y_max As Double
Dim Y_min As Double
Dim Z_max As Double
Dim Z_min As Double
Dim D_X As Double
Dim D_Y As Double
Dim D_Z As Double
Dim Ret As Integer
Dim Delta_X As String
Dim Delta_Y As String
Dim Delta_Z As String
Dim Header As String
Dim swSketchMgr As SldWorks.SketchManager
Dim swSketchPt(8) As SldWorks.SketchPoint
Dim swSketchSeg(12) As SldWorks.SketchSegment
Set swApp = CreateObject("SldWorks.Application")
Set swModel = swApp.ActiveDoc
If swModel.GetType < 1 Or swModel.GetType > 2 Then
MsgBox ("Active document must be Assembly or Part")
Exit Sub
End If
If swModel.GetType = 2 Then
Set swAssy = swModel
vBox = swAssy.GetBox(0)
End If
If swModel.GetType = 1 Then
Set swPart = swModel
vBox = swPart.GetPartBox(True)
End If
' Initialize values
X_max = vBox(3)
X_min = vBox(0)
Y_max = vBox(4)
Y_min = vBox(1)
Z_max = vBox(5)
Z_min = vBox(2)
' Calculate Bounding Box sizes
D_X = X_max - X_min
D_Y = Y_max - Y_min
D_Z = Z_max - Z_min
Debug.Print "Assembly Bounding Box (" + swModel.GetPathName + ") = "
Debug.Print " (" + Str(X_min * 1000#) + "," + Str(Y_min * 1000#) + "," + Str(Z_min * 1000#) + ") mm"
Debug.Print " (" + Str(X_max * 1000#) + "," + Str(Y_max * 1000#) + "," + Str(Z_max * 1000#) + ") mm"
Set swSketchMgr = swModel.SketchManager
swSketchMgr.Insert3DSketch True
swSketchMgr.AddToDB = True
' Draw points at each corner of bounding box
Set swSketchPt(0) = swSketchMgr.CreatePoint(X_min, Y_min, Z_min)
Set swSketchPt(1) = swSketchMgr.CreatePoint(X_min, Y_min, Z_max)
Set swSketchPt(2) = swSketchMgr.CreatePoint(X_min, Y_max, Z_min)
Set swSketchPt(3) = swSketchMgr.CreatePoint(X_min, Y_max, Z_max)
Set swSketchPt(4) = swSketchMgr.CreatePoint(X_max, Y_min, Z_min)
Set swSketchPt(5) = swSketchMgr.CreatePoint(X_max, Y_min, Z_max)
Set swSketchPt(6) = swSketchMgr.CreatePoint(X_max, Y_max, Z_min)
Set swSketchPt(7) = swSketchMgr.CreatePoint(X_max, Y_max, Z_max)
' Draw bounding box
Set swSketchSeg(0) = swSketchMgr.CreateLine(X_min, Y_min, Z_min, X_max, Y_min, Z_min)
Set swSketchSeg(1) = swSketchMgr.CreateLine(X_max, Y_min, Z_min, X_max, Y_min, Z_max)
Set swSketchSeg(2) = swSketchMgr.CreateLine(X_max, Y_min, Z_max, X_min, Y_min, Z_max)
Set swSketchSeg(3) = swSketchMgr.CreateLine(X_min, Y_min, Z_max, X_min, Y_min, Z_min)
Set swSketchSeg(4) = swSketchMgr.CreateLine(X_min, Y_min, Z_min, X_min, Y_max, Z_min)
Set swSketchSeg(5) = swSketchMgr.CreateLine(X_min, Y_min, Z_max, X_min, Y_max, Z_max)
Set swSketchSeg(6) = swSketchMgr.CreateLine(X_max, Y_min, Z_min, X_max, Y_max, Z_min)
Set swSketchSeg(7) = swSketchMgr.CreateLine(X_max, Y_min, Z_max, X_max, Y_max, Z_max)
Set swSketchSeg(8) = swSketchMgr.CreateLine(X_min, Y_max, Z_min, X_max, Y_max, Z_min)
Set swSketchSeg(9) = swSketchMgr.CreateLine(X_max, Y_max, Z_min, X_max, Y_max, Z_max)
Set swSketchSeg(10) = swSketchMgr.CreateLine(X_max, Y_max, Z_max, X_min, Y_max, Z_max)
Set swSketchSeg(11) = swSketchMgr.CreateLine(X_min, Y_max, Z_max, X_min, Y_max, Z_min)
swSketchMgr.AddToDB = False
swSketchMgr.Insert3DSketch True
' Show box sizes
Header = "File Name: " + Chr(13) + swModel.GetPathName + Chr(13) + Chr(13) + "Bounding Box sizes (X, Y, Z) = "
Delta_X = Str(Int(Abs(D_X) * 1000))
Delta_Y = Str(Int(Abs(D_Y) * 1000))
Delta_Z = Str(Int(Abs(D_Z) * 1000))
Ret = MsgBox(Header + Delta_X + " x " + Delta_Y + " x " + Delta_Z + " mm" + Chr(13) + Chr(13) + "Please delete the bounding box 3D sketch from model if not needed.", vbOKOnly, "Model Bounding Box")
End Sub
Sergio,
Just a point from remarks about bounding box. You might want to use precise values instead: Create Precise Bounding Box
It is present for all versions. I have done some validation and the difference is pretty high. I have attached the sample model which demonstrates the differences.
You can alter the macro to output both bounding boxes to see the difference
DrawBox swPart, minX, minY, minZ, maxX, maxY, maxZ Debug.Print "Width: " & maxX - minX Debug.Print "Length: " & maxZ - minZ Debug.Print "Height: " & maxY - minY Dim vBBox As Variant vBBox = swPart.GetPartBox(True) DrawBox swPart, CDbl(vBBox(0)), CDbl(vBBox(1)), CDbl(vBBox(2)), CDbl(vBBox(3)), CDbl(vBBox(4)), CDbl(vBBox(5)) Debug.Print "Width (BBox): " & vBBox(3) - vBBox(0) Debug.Print "Length (BBox): " & vBBox(5) - vBBox(2) Debug.Print "Height (BBox): " & vBBox(4) - vBBox(1)
I have updated the article to compare both methods in regards to precision and performance. If you use bounding box for any calculations I would strongly recommend to use extreme points method as the tolerance might be more than 10%-15%
Hi Artem,
I was wondering if there was a way to rename to 3dsketch that is created in the macro? I'm new to writing macro's and luckily you have provided the core of what I was looking for with the precision bounding box! So thank you for that!
Thanks
Hi Kyle,
Sure, you need to make the following change in the DrawBox function:
model.SketchManager.AddToDB = False
model.SketchManager.Insert3DSketch True
Dim swSketchFeat As SldWorks.Feature
Set swSketchFeat = model.SketchManager.ActiveSketch
model.SketchManager.Insert3DSketch True
swSketchFeat.Name = "BoundingBox"
End Sub
Hi Artem, I have another question for you, not sure if this is the best place for it but I'm going to post anyways.
I added the following code to the program that insert the center points of the lines. I would like it to create another 3D feature named "Part Center Points". The goal is that with one click of the macro, I get two features. I want two feature so I can show and hide them independently. You'll the the CreatePoints below.
Thanks again!
model.ClearSelection2 True
model.SketchManager.Insert3DSketch True
model.SketchManager.AddToDB = True
model.SketchManager.CreateLine maxX, minY, minZ, maxX, minY, maxZ
model.SketchManager.CreateLine maxX, minY, maxZ, minX, minY, maxZ
model.SketchManager.CreateLine minX, minY, maxZ, minX, minY, minZ
model.SketchManager.CreateLine minX, minY, minZ, maxX, minY, minZ
model.SketchManager.CreateLine maxX, maxY, minZ, maxX, maxY, maxZ
model.SketchManager.CreateLine maxX, maxY, maxZ, minX, maxY, maxZ
model.SketchManager.CreateLine minX, maxY, maxZ, minX, maxY, minZ
model.SketchManager.CreateLine minX, maxY, minZ, maxX, maxY, minZ
model.SketchManager.CreateLine minX, minY, minZ, minX, maxY, minZ
model.SketchManager.CreateLine minX, minY, maxZ, minX, maxY, maxZ
model.SketchManager.CreateLine maxX, minY, minZ, maxX, maxY, minZ
model.SketchManager.CreateLine maxX, minY, maxZ, maxX, maxY, maxZ
model.SketchManager.CreatePoint 0, maxY, maxZ
model.SketchManager.CreatePoint maxX, 0, maxZ
model.SketchManager.CreatePoint minX, 0, maxZ
model.SketchManager.CreatePoint 0, minY, maxZ
model.SketchManager.CreatePoint 0, 0, maxZ
model.SketchManager.CreatePoint 0, maxY, minZ
model.SketchManager.CreatePoint maxX, 0, minZ
model.SketchManager.CreatePoint minX, 0, minZ
model.SketchManager.CreatePoint 0, minY, minZ
model.SketchManager.CreatePoint 0, 0, minZ
model.SketchManager.AddToDB = False
Dim swSketchFeat As SldWorks.Feature
Set swSketchFeat = model.SketchManager.ActiveSketch
model.SketchManager.Insert3DSketch True
swSketchFeat.Name = "BoundaryBox"
Sergio,
Just a point from remarks about bounding box. You might want to use precise values instead: Create Precise Bounding Box