Get RouteProperty from Selected Sketch Segment Example (C#)
This example shows how to get the routing properties 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.
//-------------------------------------------------
using SolidWorks.Interop.sldworks;
using SolidWorks.Interop.swconst;
using SolidWorks.Interop.SWRoutingLib;
using System;
using System.Diagnostics;
namespace GetRouteProperties_CSharp.csproj
{
partial
class SolidWorksMacro
{
public
void Main()
{
ModelDoc2
swModel = default(ModelDoc2);
ModelDocExtension
swModelDoc = default(ModelDocExtension);
AssemblyDoc
swTopLevelAssembly = default(AssemblyDoc);
RouteManager
rtRouteManager = default(RouteManager);
bool
bRetVal = false;
//
Get the active document
swModel
= (ModelDoc2)swApp.ActiveDoc;
swModelDoc
= swModel.Extension;
//
Downcast from model document to assembly document
swTopLevelAssembly
= (AssemblyDoc)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
= (RouteManager)swTopLevelAssembly.GetRouteManager();
if
(rtRouteManager == null)
{
Debug.Print("No
RouteManager found in top-level document: " + swModel.GetPathName());
return;
}
//
Select the route in the routing assembly
bRetVal
= swModelDoc.SelectByID2("Route1@Harness1-top_assy-1@top_assy",
"ROUTEFABRICATED", 0, 0, 0, false, 0, null, 0);
swModel.EditRoute();
TestRoute(swModelDoc,
rtRouteManager, "Spline1");
//
Not fixed length should fail to set
swTopLevelAssembly.EditAssembly();
}
private
void TestRoute(ModelDocExtension swModelDoc, RouteManager rtRouteManager,
string strSketchSegmentName)
{
bool
bRetVal = false;
bRetVal
= swModelDoc.SelectByID2(strSketchSegmentName, "SKETCHSEGMENT",
0, 0, 0, false, 0, null, 0);
if
((!(bRetVal == false)))
{
//
Get the RouteProperty for the selected sketch segment
RouteProperty
rtRouteProperty = default(RouteProperty);
rtRouteProperty
= rtRouteManager.GetRouteProperty();
if
((rtRouteProperty != null))
{
double
dOriginalLength = 0;
dOriginalLength
= rtRouteProperty.GetFixedLength();
double
dNewLength = 0;
dNewLength
= dOriginalLength * 1.1;
swSetRouteFixedLengthError_e
lResult = 0;
lResult
= (swSetRouteFixedLengthError_e)rtRouteProperty.SetFixedLength(dNewLength);
string
strSetResult = null;
strSetResult
= DecodeSetRouteFixedLengthResult(lResult);
double
dFinalLength = 0;
dFinalLength
= rtRouteProperty.GetFixedLength();
Debug.Print("Is
Fixed Length? = " + (dOriginalLength > 0.0));
Debug.Print("Fixed
Length (T0)= " + dOriginalLength);
Debug.Print("Set
result = " + lResult);
Debug.Print("Fixed
Length (T1)= " + dFinalLength);
Debug.Print("Bend
radius = " + rtRouteProperty.BendRadius);
Debug.Print("Covering
present = " + rtRouteProperty.HasCovering);
switch
(rtRouteProperty.RouteType)
{
case
(int)swRouteType_e.swRouteType_Electrical:
Debug.Print("Type
= Electrical");
break;
}
Debug.Print("");
}
}
}
private
string DecodeSetRouteFixedLengthResult(swSetRouteFixedLengthError_e
lResult)
{
string
functionReturnValue = null;
switch
(lResult)
{
case
swSetRouteFixedLengthError_e.swSetRouteFixedLengthError_NoError:
functionReturnValue
= "swSetRouteFixedLengthError_NoError";
break;
case
swSetRouteFixedLengthError_e.swSetRouteFixedLengthError_NoProperty:
functionReturnValue
= "swSetRouteFixedLengthError_NoProperty";
break;
case
swSetRouteFixedLengthError_e.swSetRouteFixedLengthError_NotFixedLengthSegment:
functionReturnValue
= "swSetRouteFixedLengthError_NotFixedLengthSegment";
break;
case
swSetRouteFixedLengthError_e.swSetRouteFixedLengthError_NotFlexible:
functionReturnValue
= "swSetRouteFixedLengthError_NotFlexible";
break;
case
swSetRouteFixedLengthError_e.swSetRouteFixedLengthError_SelectionFailed:
functionReturnValue
= "swSetRouteFixedLengthError_SelectionFailed";
break;
case
swSetRouteFixedLengthError_e.swSetRouteFixedLengthError_SetLengthFailed:
functionReturnValue
= "swSetRouteFixedLengthError_SetLengthFailed";
break;
default:
functionReturnValue
= "UNEXPECTED RESULT";
break;
}
return
functionReturnValue;
}
public
SldWorks swApp;
}
}