// Obtain workspace and project beforehand by an arbitrary way.
HpdApi api = HpdApi.GetInstance();
api.Init();
Project proj = api.OpenProject($"{pathToMyProject}");
Variant variant = proj.GetVariants().First();
Workspace workspace = variant.GetWorkspaces().First();
workspace.Open(false, false);
// Choose a library part for cable by some method, e.g. GetLibraryPartsByPartNumber().
LibCable cableLib = (LibCable)proj.ActiveConnection.GetLibraryPartsByPartNumber("MyLibCable").FirstOrDefault();
// Get wire subentities of our cable.
var wires = cableLib?.GetCableSubentities().OfType<CableWireInfo>().ToList();
// Get pins and set up connections, similarly as in previous example.
List<IPinBase> allPins = workspace.GetAllOccurrences().OfType<IPinBase>().ToList();
IPinBase pin1 = allPins[0];
IPinBase pin2 = allPins[1];
IPinBase pin3 = allPins[2];
IPinBase pin4 = allPins[3];
Dictionary<CableWireInfo, Connection> connectionsDict = new Dictionary<CableWireInfo, Connection>();
// Assume cable has at least two child wires.
connectionsDict.Add(wires[0], new Connection(pin1, pin2));
connectionsDict.Add(wires[1], new Connection(pin3, pin4));
// Define positions of top insulator's control points.
TransformProperty<LengthProperty> topEntityEnd1 = new TransformProperty<LengthProperty>
(
new LengthProperty(30, Unit.Millimeter),
new LengthProperty(30, Unit.Millimeter),
new LengthProperty(30, Unit.Millimeter)
);
TransformProperty<LengthProperty> topEntityEnd2 = new TransformProperty<LengthProperty>
(
new LengthProperty(-30, Unit.Millimeter),
new LengthProperty(-30, Unit.Millimeter),
new LengthProperty(-30, Unit.Millimeter)
);
//Get all cable sub-entities of type shield.
IEnumerable<BaseCableEntityInfo> cableShields = cableLib?.GetCableSubentities()?.Where(c => c is CableShieldInfo);
Dictionary<BaseCablePlaceableEntityInfo, (List<PlacePosition>, List<PlacePosition>)> sublayersPositionData = new Dictionary<BaseCablePlaceableEntityInfo, (List<PlacePosition>, List<PlacePosition>)>();
// Define some coordinates for control points.
int oddEnd = -100;
int evenEnd = 100;
int distanceNewLayer = 0;
int distanceSameLayer = 30;
int offsetZ = 0;
// Cycle through cable shields and define positions of control points for each of them.
foreach (BaseCablePlaceableEntityInfo cableEntity in cableShields)
{
TransformProperty<LengthProperty> placePosition1 = new TransformProperty<LengthProperty>
(
new LengthProperty(oddEnd - distanceNewLayer, Unit.Millimeter),
new LengthProperty(oddEnd - distanceNewLayer, Unit.Millimeter),
new LengthProperty(oddEnd + offsetZ, Unit.Millimeter)
);
TransformProperty<LengthProperty> placePosition3 = new TransformProperty<LengthProperty>
(
new LengthProperty(oddEnd - distanceNewLayer + distanceSameLayer, Unit.Millimeter),
new LengthProperty(oddEnd - distanceNewLayer + distanceSameLayer, Unit.Millimeter),
new LengthProperty(oddEnd + offsetZ, Unit.Millimeter)
);
TransformProperty<LengthProperty> placePosition2 = new TransformProperty<LengthProperty>
(
new LengthProperty(evenEnd + distanceNewLayer, Unit.Millimeter),
new LengthProperty(evenEnd + distanceNewLayer, Unit.Millimeter),
new LengthProperty(evenEnd + offsetZ, Unit.Millimeter)
);
TransformProperty<LengthProperty> placePosition4 = new TransformProperty<LengthProperty>
(
new LengthProperty(evenEnd + distanceNewLayer - distanceSameLayer, Unit.Millimeter),
new LengthProperty(evenEnd + distanceNewLayer - distanceSameLayer, Unit.Millimeter),
new LengthProperty(evenEnd + offsetZ, Unit.Millimeter)
);
sublayersPositionData.Add(cableEntity, (new List<PlacePosition> { new PlacePosition(placePosition1), new PlacePosition(placePosition3) }, new List<PlacePosition> { new PlacePosition(placePosition2), new PlacePosition(placePosition4) }));
distanceNewLayer += 60;
offsetZ += 50;
}
// Aggregate position data to a wrapping class.
CableLayersPositionData clpd = new CableLayersPositionData(new PlacePosition(topEntityEnd1), new PlacePosition(topEntityEnd2), new List<TransformProperty<LengthProperty>>(), sublayersPositionData);
// And finally place the cable.
IOccCable cable = workspace.PlaceCable(cableLib, connectionsDict, clpd);
workspace.Save();
workspace.Close();
proj.Save();
proj.Close();
api.Close();