Get RouteProperty from Selected Sketch Segment Example (VBA)
This example shows how to get RouteProperty information for a sketch segment.
'----------------------------------------------------------------------------
' Preconditions:
' 1.
Ensure that the latest SWRoutingLib is loaded (Tools
> Add Reference).
' 2. Open
' install_dir\samples\tutorial\routing\electrical\top_assy.sldasm.
' 3.
On the Electrical menu, click Start by Drag/Drop.
' 4.
From the Design library, drag and drop
' routing
> electrical > 90_richco_hurc-4-01-clip
' into a hole in the
housing wall.
' 5.
Click OK in the Select a Configuration dialog.
' 6.
Drag and drop routing > electrical
> plug-5pindin into another hole
' in the same housing wall.
' 7.
Click the green checkmark to accept the defaults.
' 8.
In the Auto Route dialog, select CPoint1 in the plug and Axis3 of the
clip.
' 9.
Click the green checkmark to create the route.
' 10.
Save to a different directory.
'
' NOTE:
Because this assembly
is used elsewhere,
' do not save changes to
the original path.
'
' 11. Run this macro by pressing the F5 key.
'
' Postconditions: Inspect the Immediate Window.
'---------------------------------------------------------------------------
Option Explicit
Dim swApp As SldWorks.SldWorks
Sub Main()
Dim
swModel As
SldWorks.ModelDoc2
Dim
swModelDoc As
SldWorks.ModelDocExtension
Dim
swTopLevelAssembly As
SldWorks.AssemblyDoc
Dim
swRoutingAssembly As
SldWorks.AssemblyDoc
Dim
rtRouteManager As
SWRoutingLib.RouteManager
Dim
swComponent As
SldWorks.Component2
Dim
rtElectricalRoute As
SWRoutingLib.ElectricalRoute
Dim
bRetVal As
Boolean
Dim
rtRouteProperty As
SWRoutingLib.RouteProperty
' Connect to SolidWorks
Set
swApp = Application.SldWorks
' Get the active document
Set
swModel = swApp.ActiveDoc
Set
swModelDoc = swModel.Extension
' Downcast from model document to assembly
document
Set
swTopLevelAssembly = swModel
' Get the RouteManager from the top-level
assembly
'
Use
selection tied to the current document, which is the
'
top-level
assembly, so get the RouteManager there instead of from the
'
route
subassembly
Set
rtRouteManager = swTopLevelAssembly.GetRouteManager
If
rtRouteManager Is Nothing Then
Debug.Print
"No RouteManager found in top-level document: " & swTopLevelAssembly.GetPathName
Exit
Sub
End
If
' Select the route in the routing assembly
bRetVal
= swModelDoc.SelectByID2("Route1@Harness1-top_assy-1@top_assy",
"ROUTEFABRICATED", 0, 0, 0, False, 0, Nothing, 0)
swModel.EditRoute
TestRoute
swModelDoc, rtRouteManager, "Spline1" ' Not fixed length should
fail to set
swTopLevelAssembly.EditAssembly
End Sub
Private Sub TestRoute(swModelDoc As ModelDocExtension,
rtRouteManager As RouteManager, strSketchSegmentName As String)
Dim
bRetVal As Boolean
bRetVal
= swModelDoc.SelectByID2(strSketchSegmentName, "SKETCHSEGMENT",
0, 0, 0, False, 0, Nothing, 0)
If
(Not (bRetVal = False)) Then
'
Get the RouteProperty for the selected sketch segment
Dim
rtRouteProperty As
SWRoutingLib.RouteProperty
Set
rtRouteProperty = rtRouteManager.GetRouteProperty
If
Not rtRouteProperty Is Nothing Then
Dim
dOriginalLength As Double
dOriginalLength
= rtRouteProperty.GetFixedLength
Dim
dNewLength As Double
dNewLength
= dOriginalLength * 1.1
Dim
lResult As Long
lResult
= rtRouteProperty.SetFixedLength(dNewLength)
Dim
strSetResult As String
strSetResult
= DecodeSetRouteFixedLengthResult(lResult)
Dim
dFinalLength As Double
dFinalLength
= rtRouteProperty.GetFixedLength
Debug.Print
"Is Fixed Length? = " & (dOriginalLength > 0#)
Debug.Print
"Fixed Length (T0)= " & dOriginalLength
Debug.Print
"Set result =
" & lResult & " (" & strSetResult & ")"
Debug.Print
"Fixed Length (T1)= " & dFinalLength
Debug.Print
"Bend radius =
" & rtRouteProperty.BendRadius
Debug.Print
"Covering present = " & rtRouteProperty.HasCovering
Select
Case rtRouteProperty.RouteType
Case
SWRoutingLib.swRouteType_e.swRouteType_Electrical
Debug.Print
"Type =
Electrical"
End
Select
Debug.Print
""
End
If
End
If
End Sub
' Obtain the routing fixed length error result (swconst.swSetRouteFixedLengthError_e)
Private Function DecodeSetRouteFixedLengthResult(ByVal
lResult As Long)
Select
Case lResult
Case
swSetRouteFixedLengthError_NoError
DecodeSetRouteFixedLengthResult
= "swSetRouteFixedLengthError_NoError"
Case
swSetRouteFixedLengthError_NoProperty
DecodeSetRouteFixedLengthResult
= "swSetRouteFixedLengthError_NoProperty"
Case
swSetRouteFixedLengthError_NotFixedLengthSegment
DecodeSetRouteFixedLengthResult
= "swSetRouteFixedLengthError_NotFixedLengthSegment"
Case
swSetRouteFixedLengthError_NotFlexible
DecodeSetRouteFixedLengthResult
= "swSetRouteFixedLengthError_NotFlexible"
Case
swSetRouteFixedLengthError_SelectionFailed
DecodeSetRouteFixedLengthResult
= "swSetRouteFixedLengthError_SelectionFailed"
Case
swSetRouteFixedLengthError_SetLengthFailed
DecodeSetRouteFixedLengthResult
= "swSetRouteFixedLengthError_SetLengthFailed"
Case
Else
DecodeSetRouteFixedLengthResult
= "UNEXPECTED RESULT"
End
Select
End Function