Hide Table of Contents

Set New Route Paths for Wires Example (C#)

This example shows how to set new route paths for wires.
//----------------------------------------------------------------------------
// 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 install_dir\api\redist\CLR2).
// 3. Open public_documents\samples\tutorial\api\RoutingAssem1.sldasm.
// 4. Open the Immediate window.
// 5. Run the macro.
//
// Postconditions: Examine the Immediate window.
//
// NOTE: Because the assembly document is used elsewhere,
// do not save any changes when saving it.
//---------------------------------------------------------------------------
 
using SolidWorks.Interop.sldworks;
using SolidWorks.Interop.swconst;
using System.Runtime.InteropServices;
using SolidWorks.Interop.SWRoutingLib;
using System;
using System.Diagnostics;
 
namespace SetRoutePathForWireCSharp.csproj
{
 
    partial class SolidWorksMacro
    {
 
        public void Main()
        {
            ModelDoc2 swModel = default(ModelDoc2);
            ModelDocExtension swModelDoc = default(ModelDocExtension);
            AssemblyDoc swTopLevelAssembly = default(AssemblyDoc);
            RouteManager rtRouteManager = default(RouteManager);
            ElectricalRoute rtElectricalRoute = default(ElectricalRoute);
            Wire rtWire = default(Wire);
            bool status = false;
            int count = 0;
            object[] wires = null;
            string cableName = null;
            double cuttingLength = 0;
            int routePathStatus = 0;
            int[] routeSegmentIDs = null;
            int routeSegmentIDsCount = 0;
            int i = 0;
            int j = 0;
            int k = 0;
 
            // Get the active document
            swModel = (ModelDoc2)swApp.ActiveDoc;
            swModelDoc = (ModelDocExtension)swModel.Extension;
 
            // Downcast from model document to assembly document
            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;
            }
 
            // Select and edit a route
            status = swModelDoc.SelectByID2("Route1@Harness1^RoutingAssem1-1@RoutingAssem1""ROUTEFABRICATED", 0, 0, 0, false, 0, null, 0);
            swModel.EditRoute();
 
            // Get electrical route
            rtElectricalRoute = (ElectricalRoute)rtRouteManager.GetElectricalRoute();
            if (rtElectricalRoute == null)
            {
                Debug.Print("Electrical route not found");
                return;
            }
            Debug.Print("Electrical route found");
 
            // Get the number of wires and get the wires
            count = rtElectricalRoute.GetWiresCount();
            Debug.Print("Number of wires: " + count);
            wires = (object[])rtElectricalRoute.GetWires();
 
            // For each wire...
 
            for (i = 0; i <= count - 1; i++)
            {
                // Get wire
                rtWire = (Wire)wires[i];
                if (rtWire == null)
                {
                    return;
                }
                Debug.Print("");
                // Get wire's cutting length and name of the cable
                cuttingLength = rtWire.GetCuttingLength();
                Debug.Print("  Cutting length: " + cuttingLength);
                cableName = rtWire.UserName;
                Debug.Print("  Cable name: " + cableName);
 
                // Get and set wire's route segments and their IDs
                routeSegmentIDs = (int[])rtWire.GetRouteSegmentIDs();
                routeSegmentIDsCount = rtWire.GetRouteSegmentIDsCount();
                for (j = 0; j <= routeSegmentIDsCount - 1; j++)
                {
                    Debug.Print("    Original route segmentID: " + routeSegmentIDs[j]);
                    int ID = routeSegmentIDs[j];
                    routeSegmentIDs[j] = ID - 1;
                }
 
                // Set new IDs for the route segments in the wire, clear
                // the previous selected route path, and create a new
                // route path for the route segments in the wire
                routePathStatus = rtWire.SetRoutePathForWire(routeSegmentIDs);
                Debug.Print("    Status of creating new route path (0 = success): " + routePathStatus);
 
                // Get wire's route segments and their IDs
                routeSegmentIDs = (int[])rtWire.GetRouteSegmentIDs();
                routeSegmentIDsCount = rtWire.GetRouteSegmentIDsCount();
                for (k = 0; k <= routeSegmentIDsCount - 1; k++)
                {
                    Debug.Print("    New route segment ID: " + routeSegmentIDs[k]);
                }
 
            }
 
            // Clear selection
            swModel.ClearSelection2(true);
 
            // Exit edit mode
            swTopLevelAssembly.EditAssembly();
 
        }
 
 
        /// <summary>
        /// The SldWorks swApp variable is pre-assigned for you.
        /// </summary>
 
        public SldWorks swApp;
 
    }
 
}


Provide feedback on this topic

SOLIDWORKS welcomes your feedback concerning the presentation, accuracy, and thoroughness of the documentation. Use the form below to send your comments and suggestions about this topic directly to our documentation team. The documentation team cannot answer technical support questions. Click here for information about technical support.

* Required

 
*Email:  
Subject:   Feedback on Help Topics
Page:   Set New Route Paths for Wires Example (C#)
*Comment:  
*   I acknowledge I have read and I hereby accept the privacy policy under which my Personal Data will be used by Dassault Systèmes

Print Topic

Select the scope of content to print:

x

We have detected you are using a browser version older than Internet Explorer 7. For optimized display, we suggest upgrading your browser to Internet Explorer 7 or newer.

 Never show this message again
x

Web Help Content Version: API Help (English only) 2019 SP05

To disable Web help from within SOLIDWORKS and use local help instead, click Help > Use SOLIDWORKS Web Help.

To report problems encountered with the Web help interface and search, contact your local support representative. To provide feedback on individual help topics, use the “Feedback on this topic” link on the individual topic page.