How to scale drawing view and place it in center of sheet?

Hi.

I need to place a drawing view i center of sheet and scale to fit inside shee, can any body help me?

SolidworksApi macros
Answers 5
SF
SF 2009-09-30 Best Answer
There is an api call for finding the bounding box of a model.  component2.getbox.  That should allow you to determine the x, y or z extent of the model, then if you know you paper size, get a scale factor.
SF
SF 2009-09-30

Hi B.Lindahl

Thanks for your reply, will you make me a code examble?

SF
SF 2009-09-30

This gets a bit long, but I won't take full credit for the code; much is cut and pasted from the API help screens.  By the way, I'm programming in SW2008.  Some of the calls may be old, but should still work.  Here is the code.  Three things to update in the code, both are followed by comment lines saying to update the above information.  paperH, paperV, and the path and file name of your default template.

The code works with an assembly as the open document.  It finds the bounding box, then creates a new drawing document, inserts a view in the middle of the sheet, and updates the sheet scale based on the original active document's bounding box size.  I have it creating a front view based on fitting the x and y direction to the sheet.

Enjoy:

Option Explicit
Public Enum swComponentSuppressionState_e
    swComponentSuppressed = 0       '  Fully suppressed - nothing is loaded
    swComponentLightweight = 1      '  Featherweight - only graphics data is loaded
    swComponentFullyResolved = 2    '  Fully resolved - model is completely loaded
End Enum
'  The visibility information possible for components.
'  For use with auComponent_c::Visibility.
Public Enum swComponentVisibilityState_e
    swComponentHidden = 0
    swComponentVisible = 1
End Enum

Function GetMax _
( _
    ByVal Val1 As Double, _
    ByVal Val2 As Double, _
    ByVal Val3 As Double _
) As Double
' Finds maximum of 3 values
    GetMax = Val1
    If Val2 > GetMax Then
        GetMax = Val2
    End If
    If Val3 > GetMax Then
        GetMax = Val3
    End If
End Function

Function GetMin _
( _
    ByVal Val1 As Double, _
    ByVal Val2 As Double, _
    ByVal Val3 As Double _
) As Double
' Finds minimum of 3 values
    GetMin = Val1
    If Val2 < GetMin Then
        GetMin = Val2
    End If
    If Val3 < GetMin Then
        GetMin = Val3
    End If
End Function

Sub main()
    Const MaxDouble             As Double = 1.79769313486231E+308
    Const MinDouble             As Double = -1.79769313486231E+308
    Dim swApp                   As SldWorks.SldWorks
    Dim swModel                 As SldWorks.ModelDoc2
    Dim swAssy                  As SldWorks.AssemblyDoc
    Dim swConfig                As SldWorks.Configuration
    Dim swRootComp              As SldWorks.Component2
    Dim vChild                  As Variant
    Dim swChildComp             As SldWorks.Component2
    Dim vBox                    As Variant
    Dim Part                    As Object

    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 swSketchPt(8)           As SldWorks.SketchPoint
    Dim swSketchSeg(12)         As SldWorks.SketchSegment

    Dim i                       As Long
    Dim nStatus                 As Long
    Dim bRet                    As Boolean
    Dim sclDenom                As Double
    Dim xExtent                 As Double
    Dim yExtent                 As Double
    Dim paperH                  As Double
    Dim paperV                  As Double
    Dim fName                   As String

    Set swApp = CreateObject("SldWorks.Application")
    Set swModel = swApp.ActiveDoc
    Set swAssy = swModel
    Set swConfig = swAssy.GetActiveConfiguration
    Set swRootComp = swConfig.GetRootComponent
    fName = swModel.GetPathName
    ' Initialize values
    X_max = MinDouble
    X_min = MaxDouble
    Y_max = MinDouble
    Y_min = MaxDouble
    Z_max = MinDouble
    Z_min = MaxDouble

    paperH = 0.4318 'horizontal paper size in meters - replace with your paper size
    paperV = 0.2794 'vertical paper size in meters - replace with your paper size

    vChild = swRootComp.GetChildren
    For i = 0 To UBound(vChild)
        Set swChildComp = vChild(i)
        If swChildComp.Visible = swComponentVisible Then
            vBox = swChildComp.GetBox(False, False)
            X_max = GetMax(vBox(0), vBox(3), X_max)
            X_min = GetMin(vBox(0), vBox(3), X_min)
            Y_max = GetMax(vBox(1), vBox(4), Y_max)
            Y_min = GetMin(vBox(1), vBox(4), Y_min)
            Z_max = GetMax(vBox(2), vBox(5), Z_max)
            Z_min = GetMin(vBox(2), vBox(5), Z_min)
        End If
    Next i
    xExtent = (X_max - X_min) '/ 1000
    yExtent = (Y_max - Y_min) '/ 1000
   
    sclDenom = xExtent / paperH
    If yExtent / paperV > sclDenom Then sclDenom = yExtent / paperV
    sclDenom = Round(sclDenom + 1)

swApp.ActiveDoc.ActiveView.FrameLeft = 0
swApp.ActiveDoc.ActiveView.FrameTop = 18
swApp.ActiveDoc.ActiveView.FrameState = 1
Set Part = swApp.NewDocument("X:\Engineering\Applications\SolidWorks\Templates\xxx B-Size rJ.drwdot", 12, paperV, paperH)

'**
'replace path and file name above with your path and file name of default template!
'**

Set Part = swApp.ActiveDoc

Part.SetupSheet4 "Sheet1", 12, 13, 1, sclDenom, False, "*.drt", paperH, paperV, "Default"
Dim DrawView As Object
Set DrawView = Part.CreateDrawViewFromModelView2(fName, "*Front", paperH / 2, paperV / 2, 0)

End Sub

SF
SF 2009-10-01

Hi Mr.Lindahl

How can I do the same for a part?

SF
SF 2009-10-01
There is a similar call for a part document - partdoc.getpartbox.  There is some great sample code from your locally installed "API help".  These help screens are where much of the code above was cut and pasted into my post.