New electrical wires can be created using the Revit API.
The static Wire.Create() allows for new wires to be created in the document. The Create() method requires the id of the view in which the newly created wire will be visible. It must be the id of a floor plan or reflected ceiling plan view. The WiringType for the wire can be either Arc, for wiring that is concealed within walls, ceilings, or floors, or Chamfer, for wiring that is exposed.
The location of the wire is specified by a list of XYZ points defining the vertices of the wire, and optionally a start and/or end connector. The endpoint connectors can be null, however, if the start connector is specified, the connector's origin will be added to the wire's vertices as the start point. Likewise, if the end connector is specified, the connector's origin will be added to the wire's vertices as the end point. The static method Wire.AreVertexPointsValid() will check a list of XYZ points and start and end connectors to ensure they are suitable for a wire.
The following example creates a new straight-line wire in the active view with no connectors specified.
Code Region: Creating a new wire |
public Wire CreateWire(Document document) { Wire wire = null; FilteredElementCollector collector = new FilteredElementCollector(document); IList<Element> wireTypes = collector.OfCategory(BuiltInCategory.OST_Wire).WhereElementIsElementType().ToElements(); WireType wireType = wireTypes.First() as WireType; if (wireType != null) { IList<XYZ> wireVertices = new List<XYZ>(); wireVertices.Add(new XYZ(0, 0, 0)); wireVertices.Add(new XYZ(2, 0, 0)); wire = Wire.Create(document, wireType.Id, document.ActiveView.Id, WiringType.Arc, wireVertices, null, null); } return wire; } |
To connect a wire to elements after creation, call Wire.ConnectTo(), passing in a start and end connector. If the wire is already connected when this method is used, the old connection will be disconnected and the wire will be connected to the new target.
Once a wire is created, vertices can be retrieved using the Wire.GetVertex() method. This method takes an index of the requested vertex which should be between 0 and Wire.NumberOfVertices (which includes the start and end points of the wire).
Use Wire.AppendVertex() to add a vertex to the end of the list, or Wire.InsertVertex() to add a vertex at a specific point in the list. The Wire.IsVertexPointValid() method checks if the given vertex point can be added to this wire. IsVertexPointValid() will return false if the point cannot be added because there is already a vertex at this position on the view plane (within tolerance). Note that a vertex cannot be inserted before the start vertex if the start vertex already connects to an element. Similarly, a vertex cannot be appended to the end of the list if the end point is already connected to an element.
Wire.RemoveVertex() will remove a vertex from the list. If the wire vertex is already connected to an element, this method will fail to remove the vertex. In order to remove this vertex, it should be disconnected first, then removed, and then reconnected (if required).