Convert Guidelines into a Route Example (C#)
This example shows how to convert selected guidelines into a route.
//---------------------------------------------------------------------------
//
// Preconditions:
//
// 1. Add SolidWorks Routing as an add-in
// (in SolidWorks select Tools
> Add-Ins > SolidWorks Routing).
// 2. Add Solidworks.interop.SwRoutingLib.dll as a reference
//
(in
the IDE right-click the project,
// select Add Reference, and browse to
<SldWorks_install_dir>\api\redist).
// 3. In Tools > Options > System Options > Routing > Routing File
Locations,
// add locations of your SolidWorks Routing files.
// 4. Open:
// <SolidWorks_install_dir>\samples\tutorial\api\top_assy_fromToList.sldasm
// 5. Select Harness_1-top_assy_fromToList, the assembly that contains the route,
//
in the FeatureManager
design tree.
// 6. Rename the namespace to match the name of your C# project.
// 7. Open an Immediate Window.
//
// Postconditions:
//
// The selected guidelines are converted into a route.
// NOTE: Because the assembly is used in a SolidWorks demo,
//
do not save any
changes when you close it.
//
//
//---------------------------------------------------------------------------
using
SolidWorks.Interop.sldworks;
using
SolidWorks.Interop.swconst;
using
SolidWorks.Interop.SWRoutingLib;
using
System;
using
System.Diagnostics;
namespace
ConvertGuidelines_CSharp.csproj
{
partial
class
SolidWorksMacro
{
public
void Main()
{
ModelDoc2
swModel = default(ModelDoc2);
AssemblyDoc
swTopLevelAssembly = default(AssemblyDoc);
RouteManager
rtRouteManager = default(RouteManager);
AutoRoute
autoRoute = default(AutoRoute);
int
resultCode = 0;
bool
boolstatus = false;
swModel = (ModelDoc2)swApp.ActiveDoc;
swTopLevelAssembly = (AssemblyDoc)swModel;
// Get the RouteManager from
the top-level assembly
rtRouteManager = (RouteManager)swTopLevelAssembly.GetRouteManager();
if
(rtRouteManager == null)
{
Debug.Print("No
RouteManager found in top-level document. ");
return;
}
rtRouteManager.EditRoute();
autoRoute = rtRouteManager.GetAutoRoute();
resultCode = autoRoute.ShowGuidelines();
// Select guidelines
boolstatus =
swModel.Extension.SelectByID2("Spline9",
"SKETCHSEGMENT",
-0.4838667682886, 0.003047999999978, -0.435722051299,
false, 0,
null, 0);
boolstatus = swModel.Extension.SelectByID2("Spline10",
"SKETCHSEGMENT",
-0.4396017186256, 0.003047999999978, -0.4107731953752,
true, 0,
null, 0);
// Convert guidelines to route
boolstatus = autoRoute.ConvertGuidelinesToRoute();
rtRouteManager.ExitRoute();
swTopLevelAssembly.EditAssembly();
}
public
SldWorks
swApp;
}
}