A question on the SOLIDWORKS User Forum asks whether there is any way to automatically create an offset rectangle from a drawing view’s bounding box. Kevin Chandler — one of the most prolific responders on that forum — gives the short answer: “Auto? No, unless via the API.”
That’s the entire opening for this post. The native UI does not support it. The API does.
The use cases are real and recurring: inspection templates with a fixed clearance zone around each view, nesting documentation where each part gets a border at a set offset, supplier drawings where the view boundary needs to be marked explicitly for DXF nesting software. All of them need the same thing: a rectangle drawn in the sheet sketch, offset from the view’s outline, assigned to a controlled layer.
What “bounding box” means in a drawing context
SOLIDWORKS drawings have two different things people call the bounding box:
The view outline (IView.GetOutline()) is the view’s full bounding rectangle on the sheet. It includes the visible geometry, view labels, and any annotation balloon overhangs. It is already in sheet coordinate space — meters from the sheet origin. This is what you get without any coordinate transformation.
The projected model bounding box is the tight geometric footprint of the referenced part or assembly, projected through the view’s orientation onto the sheet plane. This requires pulling the model’s 3D bounding box, applying the view’s ModelToViewTransform, and accounting for the sheet scale. It is tighter than GetOutline() and excludes labels.
For most use cases — clearance zones, inspection frames, nesting borders — GetOutline() is what you want. The offset already provides visual separation from the annotation. This post uses GetOutline(). The model-transform approach is covered in the section on tighter bounds at the end.
The sheet-context sketch requirement
This is the part that trips up most macros. SOLIDWORKS drawings have two sketch contexts:
- View sketch: geometry that lives inside a view and moves with it when you reposition the view
- Sheet sketch: geometry that lives on the sheet itself and does not move when views move
An offset rectangle around a view should be in the sheet sketch, so it stays put when someone drags the view. To create geometry in the sheet sketch, you must activate the sheet before calling SketchManager.CreateLine. The activation call is:
swDraw.ActivateView "" ' empty string = activate sheet, not a named view
If you skip this and the drawing has an active view focus, the lines go into the view sketch and will move with the view. The result looks right at first and breaks later.
Layer management before drawing
Drawing layers in SOLIDWORKS are controlled through ILayerMgr, accessed via IModelDoc2.GetLayerManager. The workflow:
- Get the
ILayerMgrinstance - Check whether the target layer exists with
GetLayer(name) - Create it with
AddLayerif it does not exist - Set it as the active layer with
IModelDoc2.SetCurrentLayer
All sketch entities created after SetCurrentLayer land on that layer until you change it again. This means you can batch-create offset rectangles for every view in a drawing, each on the same layer, in a single macro run.
The complete macro
This macro iterates every view on the active sheet, reads the outline with GetOutline, offsets by a configurable distance, creates a 4-line rectangle in the sheet sketch, and assigns all lines to a named layer. If the layer does not exist, it is created.
Option Explicit
' Configuration — change these before running
Const LAYER_NAME As String = "OFFSET_BOX"
Const LAYER_COLOR As Long = RGB(255, 0, 0) ' red
Const LAYER_STYLE As Integer = 0 ' 0=solid, 1=dashed, 2=phantom
Const LAYER_WIDTH As Integer = 0 ' 0=hairline, 1=thin, 2=normal, 3=thick
Const OFFSET_MM As Double = 5# ' offset distance in millimetres
Sub main()
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swDraw As SldWorks.DrawingDoc
Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
If swModel Is Nothing Then
MsgBox "No active document.", vbExclamation
Exit Sub
End If
If swModel.GetType() <> swDocDRAWING Then
MsgBox "Active document is not a drawing.", vbExclamation
Exit Sub
End If
Set swDraw = swModel
' Ensure the target layer exists
EnsureLayer swModel, LAYER_NAME, LAYER_COLOR, LAYER_STYLE, LAYER_WIDTH
' Convert offset to meters (SOLIDWORKS internal unit)
Dim offsetM As Double
offsetM = OFFSET_MM / 1000#
' Activate sheet context — mandatory before creating sheet sketch entities
swDraw.ActivateView ""
swDraw.ClearSelection2 True
' Set target layer
swModel.SetCurrentLayer LAYER_NAME
' Iterate all views on the active sheet
Dim swSheet As SldWorks.sheet
Set swSheet = swDraw.GetCurrentSheet()
Dim vViews As Variant
vViews = swSheet.GetViews()
If IsEmpty(vViews) Then
MsgBox "No views found on active sheet.", vbInformation
Exit Sub
End If
Dim i As Integer
For i = 0 To UBound(vViews)
Dim swView As SldWorks.view
Set swView = vViews(i)
Dim vOutline As Variant
vOutline = swView.GetOutline()
' vOutline: (0)=xMin, (1)=yMin, (2)=xMax, (3)=yMax — in meters, sheet space
Dim xMin As Double, yMin As Double, xMax As Double, yMax As Double
xMin = vOutline(0) - offsetM
yMin = vOutline(1) - offsetM
xMax = vOutline(2) + offsetM
yMax = vOutline(3) + offsetM
' Draw rectangle as 4 separate lines (sheet sketch context)
swModel.CreateLine2 xMin, yMin, 0, xMax, yMin, 0 ' bottom
swModel.CreateLine2 xMax, yMin, 0, xMax, yMax, 0 ' right
swModel.CreateLine2 xMax, yMax, 0, xMin, yMax, 0 ' top
swModel.CreateLine2 xMin, yMax, 0, xMin, yMin, 0 ' left
Next i
swModel.ClearSelection2 True
swModel.GraphicsRedraw2
MsgBox "Done. Offset boxes created on layer '" & LAYER_NAME & "'.", vbInformation
End Sub
Sub EnsureLayer(swModel As SldWorks.ModelDoc2, _
layerName As String, _
color As Long, _
style As Integer, _
width As Integer)
Dim swLayerMgr As SldWorks.LayerMgr
Set swLayerMgr = swModel.GetLayerManager()
If swLayerMgr Is Nothing Then Exit Sub
If swLayerMgr.GetLayer(layerName) Is Nothing Then
swLayerMgr.AddLayer layerName, "Auto-generated offset from view outline", _
color, style, width
End If
End Sub
What each section does
swDraw.ActivateView "" — this is not optional. Without it, geometry created in the SketchManager context goes into whatever view sketch is currently active. The empty string argument tells SOLIDWORKS to activate the drawing sheet, not a named view.
swSheet.GetViews() returns a Variant array of IView objects for the active sheet. The first element (i=0) is typically the sheet itself (a null or sheet-level view); the loop handles this safely because GetOutline() on a sheet view returns a near-zero box that produces a harmless tiny rectangle. If you want to skip the sheet view, add a check: If swView.Name = "" Then GoTo NextView.
IView.GetOutline() returns sheet-space coordinates in meters. The offset (offsetM) is also in meters — hence the conversion from the millimetre constant. SOLIDWORKS API coordinates are always in SI units internally.
swModel.CreateLine2 x1, y1, z, x2, y2, z — the Z coordinate is always zero in sheet space. This method creates a line in the currently active sketch context, on the currently active layer. It returns a Boolean (True = success) which this macro does not check; add error checking if you need reliability guarantees in batch production runs.
Filtering views by type or name
The IView.Type property returns a value from swDrawingViewTypes_e. Useful filters:
' Skip standard (named) model views only, process others
If swView.Type <> swDrawingViewTypes_e.swDrawingNamedView Then
' process this view
End If
Or filter by name prefix if you follow a naming convention:
If Left(swView.Name, 6) = "SHEET_" Then GoTo NextView
Using the model bounding box for a tighter fit
If GetOutline() includes too much whitespace (label area, balloon overhangs) and you need the tight geometry footprint, the alternative is to project the 3D model bounding box through the view transform. This is the approach documented in detail in the SOLIDWORKS API DXF export tutorial — the core transform chain (ModelToViewTransform combined with sheet scale) is identical.
The short version for a part view:
' Get the model's 3D bounding box (in model space, meters)
Dim swPart As SldWorks.PartDoc
Set swPart = swView.ReferencedDocument
Dim vBox As Variant
vBox = swPart.GetPartBox(True) ' (xMin,yMin,zMin, xMax,yMax,zMax)
' Apply view transform to project corners into sheet space
Dim swMathUtils As SldWorks.MathUtility
Set swMathUtils = swApp.GetMathUtility()
Dim swViewXform As SldWorks.MathTransform
Set swViewXform = swView.ModelToViewTransform
' Transform each corner, compute sheet-space bounding box from the 8 projected points
' ... (see codestack.net draw sheet context sketch example for full implementation)
The result is a tighter rectangle that matches the visible edge envelope rather than the label frame. The tradeoff: you need to project all 8 bounding-box corners through the view transform and compute the sheet-space min/max, which is about 30 more lines of code. For most use cases, GetOutline() at 5mm offset is indistinguishable in practice.
Running the macro on multiple sheets
To run across all sheets in the drawing, wrap the sheet logic in a loop:
Dim vSheetNames As Variant
vSheetNames = swDraw.GetSheetNames()
Dim s As Integer
For s = 0 To UBound(vSheetNames)
swDraw.ActivateSheet vSheetNames(s)
swDraw.ActivateView ""
swModel.SetCurrentLayer LAYER_NAME
' ... same view-iteration logic ...
Next s
ActivateSheet followed by ActivateView "" ensures each sheet is in the sheet sketch context before geometry is created.
Batch DXF output with the offset lines
If the goal is to export DXFs where the offset rectangle serves as a nesting border, CadShift handles the entire batch DXF export from assemblies in one click and exports each drawing view as a separate DXF. Running this macro first, then exporting, produces DXF files that already contain the offset frame on a dedicated layer — directly usable by laser or waterjet nesting software without post-processing.
For the full drawing automation pipeline (create views, add annotations, export), the drawing creation macro walkthrough covers the IDrawingDoc API in more depth, including how to control view scale, position, and named view types programmatically.