public List <ElementId> SortX(ICollection <ElementId> toSort, Document doc) { List <ElementId> xSorted = new List <ElementId>(); int countElem = toSort.Count; foreach (int count in Enumerable.Range(0, countElem)) { xSorted.Add(null); } foreach (ElementId eid in toSort) { countElem = toSort.Count; foreach (ElementId compEid in toSort) { if (eid.IntegerValue != compEid.IntegerValue) { if (RackDim.GetRotatedToVert(eid, doc).GetEndPoint(0).X > RackDim.GetRotatedToVert(compEid, doc).GetEndPoint(0).X) { countElem -= 1; } } } xSorted[countElem - 1] = eid; } return(xSorted); }
public Result Execute( ExternalCommandData commandData, ref string message, ElementSet elements) { UIApplication uiapp = commandData.Application; UIDocument uidoc = uiapp.ActiveUIDocument; Application app = uiapp.Application; Document doc = uidoc.Document; Selection SelectedObjs = uidoc.Selection; ICollection <ElementId> ids = uidoc.Selection.GetElementIds(); ElementId defTextType = doc.GetDefaultElementTypeId(ElementTypeGroup.TextNoteType); List <double> cDiams = new List <double>(); Line LineDir = null; bool updateRack = false; List <string> Conduits = new List <string>(); TextNote toUpdate = null; RackDim.GetMenuValues(uiapp); Store.mod_split = 30; foreach (ElementId eid in ids) { Element elem = doc.GetElement(eid); if (elem.GetType() == typeof(TextNote)) { updateRack = true; toUpdate = elem as TextNote; } else { GeometryElement geom = elem.get_Geometry(Store.Dimop(doc.ActiveView)); cDiams.Add(elem.get_Parameter(BuiltInParameter.RBS_CONDUIT_DIAMETER_PARAM).AsDouble()); LineDir = RackDim.GetLineOfGeom(geom); } } if (toUpdate != null) { ids.Remove(toUpdate.Id); } double dMax = cDiams.Max(); foreach (string s in Conduits) { TaskDialog.Show("asd", s); } using (Transaction tx = new Transaction(doc)) { tx.Start("Create Text"); List <ElementId> Rows = new List <ElementId>(); List <List <ElementId> > allRows = SeparateRows(ids, doc, dMax); string diameters = ""; Double Distance = 0; List <double> leftXs = new List <double>(); List <ElementId> lefts = new List <ElementId>(); List <double> rightXs = new List <double>(); List <ElementId> rights = new List <ElementId>(); foreach (int i in SortRows(allRows, doc)) { string prev = ""; int typcount = 1; string diam; if (i == 0 && allRows.Count > 1) { diameters += "ABOVE: "; } else if (i == allRows.Count - 1 && allRows.Count != 1) { diameters += '\n' + "BELOW: "; } else if (allRows.Count > 1) { diameters += '\n' + "MIDDLE: "; } List <ElementId> SortedRows = SortX(allRows[i], doc); leftXs.Add(RackDim.GetRotatedToVert(SortedRows[0], doc).GetEndPoint(0).X); lefts.Add(SortedRows[0]); rightXs.Add(RackDim.GetRotatedToVert(SortedRows[SortedRows.Count - 1], doc).GetEndPoint(0).X); rights.Add(SortedRows[SortedRows.Count - 1]); foreach (ElementId eid in SortedRows) { diam = doc.GetElement(eid).get_Parameter(BuiltInParameter.RBS_CONDUIT_DIAMETER_PARAM).AsValueString(); if (diam == prev) { typcount += 1; } else { if (typcount != 1) { diameters = diameters + typcount + "-" + prev + "C, "; diameters = BreakLine(diameters, Store.mod_split); typcount = 1; } else if (prev != "") { diameters = diameters + prev + "C, "; diameters = BreakLine(diameters, Store.mod_split); } prev = diam; } } if (typcount != 1) { diameters = diameters + typcount + "-" + prev + "C"; } else { diameters = diameters + prev + "C"; } } ElementId firstEid = lefts[leftXs.IndexOf(leftXs.Max())]; ElementId lastEid = rights[rightXs.IndexOf(rightXs.Min())]; LocationCurve firstP = doc.GetElement(firstEid).Location as LocationCurve; LocationCurve lastP = doc.GetElement(lastEid).Location as LocationCurve; XYZ rotP = firstP.Curve.GetEndPoint(0); Distance = Math.Abs(RackDim.GetRotatedToVert(firstEid, doc, rotP).GetEndPoint(0).X - RackDim.GetRotatedToVert(lastEid, doc, rotP).GetEndPoint(0).X); Line firstL = firstP.Curve as Line; if (updateRack) { toUpdate.Text = diameters; } else { Line texRot = Line.CreateBound(firstP.Curve.GetEndPoint(0), firstP.Curve.GetEndPoint(1)); Double textAngle = texRot.Direction.AngleTo(new XYZ(0, -1, 0)); bool fix = false; if (textAngle >= Math.PI / 2) { textAngle = Math.PI - textAngle; fix = true; } TextNoteOptions textRotate = new TextNoteOptions { TypeId = defTextType, Rotation = textAngle }; TextNote text = TextNote.Create(doc, doc.ActiveView.Id, LineDir.Evaluate(Store.mod_place, true), diameters, textRotate); text.get_Parameter(BuiltInParameter.TEXT_ALIGN_HORZ).Set((Int32)TextAlignFlags.TEF_ALIGN_BOTTOM); if (fix) { text.Coord = firstP.Curve.Evaluate(Store.mod_place, true) .Add(RackDim.GetPerpendicular(firstL, Store.mod_place) .Direction.Multiply(Distance + (4 * Store.mod_firsty))); } else { text.Coord = firstP.Curve.Evaluate(Store.mod_place, true) .Subtract(RackDim.GetPerpendicular(firstL, Store.mod_place) .Direction.Multiply(Distance + (4 * Store.mod_firsty))); } text.Coord = new XYZ(text.Coord.X, text.Coord.Y + (1.4 * Store.mod_stepy), text.Coord.Z); text.AddLeader(TextNoteLeaderTypes.TNLT_STRAIGHT_R); text.AddLeader(TextNoteLeaderTypes.TNLT_STRAIGHT_R); IList <Leader> leaders = text.GetLeaders(); leaders[0].End = firstP.Curve.Evaluate(Store.mod_place, true); if (firstL.Direction.AngleTo(new XYZ(0, 1, 0)) >= Math.PI / 2) { leaders[1].End = leaders[0].End.Subtract(RackDim.GetPerpendicular(firstL, Store.mod_place) .Direction.Multiply(Distance)); } else { leaders[1].End = leaders[0].End.Add(RackDim.GetPerpendicular(firstL, Store.mod_place) .Direction.Multiply(Distance)); } if (fix) { leaders[0].Elbow = leaders[0].End.Subtract(RackDim.GetPerpendicular(firstL, Store.mod_place) .Direction.Multiply(1.4 * Store.mod_left)); leaders[1].Elbow = leaders[1].End.Add(RackDim.GetPerpendicular(firstL, Store.mod_place) .Direction.Multiply(1.4 * Store.mod_left)); } else { leaders[0].Elbow = leaders[0].End.Add(RackDim.GetPerpendicular(firstL, Store.mod_place) .Direction.Multiply(1.4 * Store.mod_left)); leaders[1].Elbow = leaders[1].End.Subtract(RackDim.GetPerpendicular(firstL, Store.mod_place) .Direction.Multiply(1.4 * Store.mod_left)); } } tx.Commit(); } return(Result.Succeeded); }