Get RouteProperty from Selected Sketch Segment Example (VB.NET)
This example shows how to get RouteProperty information for a
sketch segment.
'----------------------------------------------------------------------------
' Preconditions:
' 1.
Ensure that the latest SolidWorks.Interop.SWRoutingLib is loaded
' (right-click
on the project in Project
' Explorer,
click Add Reference, and browse
' install_dir\api\redist\CLR2).
' 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.
'--------------------------------------------------------------------------
Imports SolidWorks.Interop.sldworks
Imports SolidWorks.Interop.swconst
Imports SolidWorks.Interop.SWRoutingLib
Imports System
Imports System.Diagnostics
Partial Class SolidWorksMacro
Sub
Main()
Dim
swModel As ModelDoc2
Dim
swModelDoc As ModelDocExtension
Dim
swTopLevelAssembly As AssemblyDoc
Dim
rtRouteManager As RouteManager
Dim
bRetVal As Boolean
'
Get the active document
swModel
= swApp.ActiveDoc
swModelDoc
= swModel.Extension
'
Downcast from model document to assembly document
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
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(ByVal swModelDoc As ModelDocExtension, ByVal rtRouteManager
As RouteManager, ByVal 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 RouteProperty
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.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
swRouteType_e.swRouteType_Electrical
Debug.Print("Type
=
Electrical")
End
Select
Debug.Print("")
End
If
End
If
End
Sub
Private
Function DecodeSetRouteFixedLengthResult(ByVal lResult As Long) As String
Select
Case lResult
Case
swSetRouteFixedLengthError_e.swSetRouteFixedLengthError_NoError
DecodeSetRouteFixedLengthResult
= "swSetRouteFixedLengthError_NoError"
Case
swSetRouteFixedLengthError_e.swSetRouteFixedLengthError_NoProperty
DecodeSetRouteFixedLengthResult
= "swSetRouteFixedLengthError_NoProperty"
Case
swSetRouteFixedLengthError_e.swSetRouteFixedLengthError_NotFixedLengthSegment
DecodeSetRouteFixedLengthResult
= "swSetRouteFixedLengthError_NotFixedLengthSegment"
Case
swSetRouteFixedLengthError_e.swSetRouteFixedLengthError_NotFlexible
DecodeSetRouteFixedLengthResult
= "swSetRouteFixedLengthError_NotFlexible"
Case
swSetRouteFixedLengthError_e.swSetRouteFixedLengthError_SelectionFailed
DecodeSetRouteFixedLengthResult
= "swSetRouteFixedLengthError_SelectionFailed"
Case
swSetRouteFixedLengthError_e.swSetRouteFixedLengthError_SetLengthFailed
DecodeSetRouteFixedLengthResult
= "swSetRouteFixedLengthError_SetLengthFailed"
Case
Else
DecodeSetRouteFixedLengthResult
= "UNEXPECTED RESULT"
End
Select
End
Function
Public
swApp As SldWorks
End Class