Example 12 - root.swb
This macro is used to solve one dimension by changing the other. Lets say you have an isoceles triangle (all side equal length) with the base horizontal and length one. Go ahead and put a dimension on the base (width) of 1.00. If you constrained the sketch correctly the height of the triangle will be 0.866 which you can show with a driven dimension. Now you run this macro. An input box will ask you to select a dimension (click on the 0.866 dimension) and enter a target value of 2.00 and press the OK button. Now it asks you for the driving dimension. Click on the 1.00 dimension and press the OK button (you don't have to enter anything in the box). The program will keep changing the width of the triangle until the height is 2.00 and the base is 2.309. And you didnt have to trig anything out!
This is a very simple application. Truthfully equations are much more accurate than this (faster too). If you find yourself at a loss to develop an equation or spending too much time doing so, this might be a quick brute force method to get where you want. Enjoy.
' ******************************************************************************
' ROOT.SWB
'
' macro written on 02/07/00 by
' Joe Jones joe@nhcad.com
' NEW HAMPSHIRE CAD www.nhcad.com
'
' This macro will ask for (3) things
' 1) a dimension
' 2) a value you want to drive that dimension to
' 3) a second dimension that will change until a solution is found
'
' ******************************************************************************
Dim swApp As Object
Dim modelDoc As Object
Dim retval as integer
Dim Dim1 As String
Dim Dim2 As String
Dim Dim1Target As Double
Dim Cnt As Integer
Dim MaxIt As Integer
Dim Units As Integer
Dim Tol As Double
Dim LastDim1 As Double
Dim Dim1Val As Double
Dim Dim2Val As Double
Dim retStr As String
Sub main()
MaxIt = 15 ' pop out of program if this limit is reached
Delta = 0.1 ' my initial guess
Tol = 0.000001 ' if Dim1 changes less than this since the last
' iteration then end the program
Cnt = 0
Set swApp = CreateObject ("SldWorks.Application")
Set modelDoc = swApp.ActiveDoc
Set SelMgr = modelDoc.SelectionManager()
'modelDoc.ClearSelection
retStr = InputBox("Select a dimension and type in its desired value here")
Dim1Target = Val(retStr)
Set selObj = SelMgr.GetSelectedObject2(1)
modelDoc.ClearSelection
Dim1 = selObj.FullName
retStr = InputBox("Select the driving dimension and press OK")
Set selObj = SelMgr.GetSelectedObject2(1)
modelDoc.ClearSelection
Dim2 = selObj.FullName
Units = modelDoc.LengthUnit
Select Case Units
Case 0 ' mm
conversion = 1000
Case 1 ' cm
conversion = 100
Case 2 ' m
conversion = 1
Case 3 ' in
conversion = 39.36996
Case 4 ' ft
conversion = 3.28083
Case 5 ' ft-in
conversion = 39.36996
End Select
LastDim1 = val( modelDoc.parameter( Dim1 ).systemvalue ) * conversion
Dim1Val = val( modelDoc.parameter( Dim1 ).systemvalue ) * conversion
Dim1Val = Dim1Val + 2*Tol
While Abs(Dim1Val-LastDim1) > Tol AND Cnt <= MaxIt
LastDim1 = val( modelDoc.parameter( Dim1 ).systemvalue )*conversion
Cnt = Cnt + 1
Dim2Val = val( modelDoc.parameter( Dim2 ).systemvalue )*conversion
Dim2Val = Dim2Val + Delta
retval = modelDoc.parameter(Dim2).SetValue2 ( Dim2Val, 1 )
modelDoc.ForceRebuild
Dim1Val = val( modelDoc.parameter( Dim1 ).systemvalue )*conversion
If Dim1Val - LastDim1 <> 0 Then Delta = _
Delta * ( Dim1Target - Dim1Val ) / ( Dim1Val - LastDim1 )
Wend
Set modelDoc = Nothing
Set swApp = Nothing
End Sub