// initialize API and obtain workspace
var api = Studio.GetInstance();
api.Init();
var project = api.OpenProject("path to project");
var variant = project.GetVariants().FirstOrDefault();
var wsp = project.GetVariants().FirstOrDefault()?.GetWorkspaces().FirstOrDefault(w => w.Name.Contains("my workspace name"));
wsp.Open(false, false);
// obtain ribbon cable from library
var ribbonCableLib = project.ActiveConnection.GetLibraryPartsByType(new List<ApiPartType>{ApiPartType.RibbonCable}).FirstOrDefault();
// find connector occurrences by name
var conn1 = wsp.GetAllOccurrences().FirstOrDefault(o => o.Name == "cn_00001") as OccWsRibbonConnector;
var conn2 = wsp.GetAllOccurrences().FirstOrDefault(o => o.Name == "cn_00002") as OccWsRibbonConnector;
var conn3 = wsp.GetAllOccurrences().FirstOrDefault(o => o.Name == "cn_00003") as OccWsRibbonConnector;
var conn4 = wsp.GetAllOccurrences().FirstOrDefault(o => o.Name == "cn_00004") as OccWsRibbonConnector;
// find inline ribbon pin occurrence by name (it will be one of ribbon cable's control points)
var inline = wsp.GetAllOccurrences().FirstOrDefault(o => o.Name == "inline");
// define first branch info - this will be the main branch, method GetPostions1 returns two PlacePositions of cable's control points, it is declared below
var branchInfo = new RibbonCableInputBranchInfo(
conn1.Children.FirstOrDefault(ch => ch is IOccRibbonPin) as IOccRibbonPin,
null,
GetPositions1(),
8,
0);
// first subbranch of the cable, it is connected to "cn_00002" and has some middle control points (depends on definition of GetPositions2, which is analogical to GetPositions1)
var branchInfo2 = new RibbonCableInputBranchInfo(
null,
conn2.Children.FirstOrDefault(ch => ch is IOccRibbonPin) as IOccRibbonPin,
GetPositions2(),
4,
0);
// initialize a PlacePosition list with an occurrence - the inline ribbon pin and add some coordinate based positions returned by GetPositions5
var positionsList = new List<PlacePosition> {new PlacePosition(inline.Children.FirstOrDefault(ch => ch is IOccInlineRibbonPin) as IOccInlineRibbonPin)}.ToList();
positionsList.AddRange(GetPositions5());
// define second subbranch of the main branch, this branch will split in two new subbranches and contains an inline connector as a control point
var branchInfo3 = new RibbonCableInputBranchInfo(
null,
null,
positionsList,
4,
4);
// last two subbranches which are connected to a connector
var branchInfo4 = new RibbonCableInputBranchInfo(
null,
conn3.Children.FirstOrDefault(ch => ch is IOccRibbonPin) as IOccRibbonPin,
GetPositions3(),
1,
4);
var branchInfo5 = new RibbonCableInputBranchInfo(
null,
conn4.Children.FirstOrDefault(ch => ch is IOccRibbonPin) as IOccRibbonPin,
GetPositions4(),
3,
5);
// we keep connections empty in this example - the wires are connected to super pins
var connections = new List<Connection>();
var cable = wsp.PlaceRibbonCable(ribbonCableLib as LibRibbonCable, new List<RibbonCableInputBranchInfo>{ branchInfo, branchInfo2, branchInfo3, branchInfo4, branchInfo5 } , connections);
wsp.Save();
wsp.Close();
project.Save();
project.Close();
// local method which returns a list of two PlacePositions
List<PlacePosition> GetPositions1()
{
var x = new LengthProperty(2.000, Unit.Meter);
var y = new LengthProperty(-0.125, Unit.Meter);
var z = new LengthProperty(1.900, Unit.Meter);
var first = new TransformProperty<LengthProperty>(x, y, z);
x = new LengthProperty(1.850, Unit.Meter);
y = new LengthProperty(0.380, Unit.Meter);
z = new LengthProperty(0.160, Unit.Meter);
var second = new TransformProperty<LengthProperty>(x, y, z);
return new List<PlacePosition>{new PlacePosition(first), new PlacePosition(second)};
}